Skip to content

Commit

Permalink
Merge pull request #46 from getAlby/fix/38
Browse files Browse the repository at this point in the history
fix: prioritize parsing of lightning addresses over lnurls
  • Loading branch information
rolznz authored Aug 27, 2024
2 parents b3570f5 + 004651c commit fd98b35
Showing 1 changed file with 16 additions and 16 deletions.
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

0 comments on commit fd98b35

Please sign in to comment.