Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prioritize parsing of lightning addresses over lnurls #46

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions lib/lnurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const fromInternetIdentifier = (address: string) => {
// modified to allow _ in subdomains
if (
address.match(
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-_0-9]+\.)+[a-zA-Z]{2,}))$/
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-_0-9]+\.)+[a-zA-Z]{2,}))$/,
)
) {
let [name, host] = address.split("@");
Expand All @@ -59,6 +59,12 @@ const fromInternetIdentifier = (address: string) => {
};

const normalizeLnurl = (lnurlString: string) => {
// maybe it's a lightning address?
const urlFromAddress = fromInternetIdentifier(lnurlString);
if (urlFromAddress) {
return new URL(urlFromAddress);
}

// maybe it's bech32 encoded?
try {
const url = bech32Decode(lnurlString);
Expand All @@ -67,12 +73,6 @@ const normalizeLnurl = (lnurlString: string) => {
console.info("ignoring bech32 parsing error", e);
}

// maybe it's a lightning address?
const urlFromAddress = fromInternetIdentifier(lnurlString);
if (urlFromAddress) {
return new URL(urlFromAddress);
}

//maybe it's already a URL?
return new URL(`https://${lnurlString.replace(/^lnurl[pwc]/i, "")}`);
};
Expand All @@ -82,14 +82,18 @@ export const lnurl = {
return Boolean(fromInternetIdentifier(address));
},
isLNURLDetailsError(
res: LNURLError | LNURLDetails | LNURLPaymentInfo
res: LNURLError | LNURLDetails | LNURLPaymentInfo,
): res is LNURLError {
return "status" in res && res.status.toUpperCase() === "ERROR";
},
findLnurl(text: string) {
const stringToText = text.trim().toLowerCase();
let match;

if (this.isLightningAddress(stringToText)) {
return stringToText;
}

// look for a LNURL with protocol scheme
if ((match = stringToText.match(/lnurl[pwc]:(\S+)/i))) {
return match[1];
Expand All @@ -100,10 +104,6 @@ export const lnurl = {
return match[1];
}

if (this.isLightningAddress(stringToText)) {
return stringToText;
}

return null;
},

Expand All @@ -115,7 +115,7 @@ export const lnurl = {

if (!response.ok) {
throw new Error(
"Non-OK response from request to " + url + ": " + response.status
"Non-OK response from request to " + url + ": " + response.status,
);
}

Expand All @@ -137,7 +137,7 @@ export const lnurl = {
throw new Error(
`Connection problem or invalid lnurl / lightning address: ${
e instanceof Error ? e.message : ""
}`
}`,
);
}
},
Expand All @@ -148,7 +148,7 @@ export const lnurl = {

if (!response.ok) {
throw new Error(
"Non-OK response from request to " + url + ": " + response.status
"Non-OK response from request to " + url + ": " + response.status,
);
}

Expand All @@ -173,7 +173,7 @@ export const lnurl = {
throw new Error(
`Connection problem or invalid lnurl / lightning address: ${
e instanceof Error ? e.message : ""
}`
}`,
);
}
},
Expand Down