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: check for ln address and bolt11 before navigating to send #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
35 changes: 24 additions & 11 deletions hooks/__tests__/useHandleLinking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,51 @@ jest.mock("../../lib/lnurl", () => {
const testVectors: Record<string, { url: string; path: string }> = {
// Lightning Addresses
"lightning:[email protected]": {
url: "lightning:[email protected]",
url: "[email protected]",
path: "/send",
},
"lightning://[email protected]": {
url: "lightning:[email protected]",
url: "[email protected]",
path: "/send",
},
"LIGHTNING://[email protected]": {
url: "lightning:[email protected]",
url: "[email protected]",
path: "/send",
},
"LIGHTNING:[email protected]": {
url: "lightning:[email protected]",
url: "[email protected]",
path: "/send",
},

// Lightning invoices
"lightning:lnbc1": { url: "lightning:lnbc1", path: "/send" },
"lightning://lnbc1": { url: "lightning:lnbc1", path: "/send" },
"lightning:lnbc123": {
url: "lnbc123",
path: "/send",
},
"lightning://lnbc123": {
url: "lnbc123",
path: "/send",
},

// BIP21
"bitcoin:bitcoinaddress?lightning=invoice": {
url: "bitcoin:bitcoinaddress?lightning=invoice",
"bitcoin:bitcoinaddress?lightning=lnbc123": {
url: "lnbc123",
path: "/send",
},
"BITCOIN:bitcoinaddress?lightning=invoice": {
url: "bitcoin:bitcoinaddress?lightning=invoice",
"BITCOIN:bitcoinaddress?lightning=lnbc123": {
url: "lnbc123",
path: "/send",
},

// LNURL-withdraw
"lightning:lnurlw123": { url: "lightning:lnurlw123", path: "/withdraw" },
"lightning:lnurlw123": {
url: "lnurlw123",
path: "/withdraw",
},
"lightning://lnurlw123": {
url: "lnurlw123",
path: "/withdraw",
},
};

describe("handleLink", () => {
Expand Down
2 changes: 2 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ export const REQUIRED_CAPABILITIES: Nip47Capability[] = [
export const SATS_REGEX = /^\d*$/;

export const FIAT_REGEX = /^\d*(\.\d{0,2})?$/;

export const BOLT11_REGEX = /.*?((lnbcrt|lntb|lnbc)([0-9]{1,}[a-z0-9]+){1})/;
44 changes: 33 additions & 11 deletions lib/link.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { router } from "expo-router";
import { lnurl } from "./lnurl";
import { BOLT11_REGEX } from "./constants";
import { lnurl as lnurlLib } from "./lnurl";

const SUPPORTED_SCHEMES = ["lightning:", "bitcoin:", "alby:"];

Expand Down Expand Up @@ -44,27 +45,48 @@ export const handleLink = async (url: string) => {

console.info("Navigating to", fullUrl);

const lnurlValue = lnurl.findLnurl(fullUrl);
if (lnurlValue) {
const lnurlDetails = await lnurl.getDetails(lnurlValue);
const schemePattern = new RegExp(
`^(${SUPPORTED_SCHEMES.map((s) => s.replace(":", "")).join("|")}):`,
);
const trimmedUrl = fullUrl.replace(schemePattern, "");

// Check for LNURLs
const lnurl = lnurlLib.findLnurl(trimmedUrl);
if (lnurl) {
const lnurlDetails = await lnurlLib.getDetails(lnurl);

if (lnurlDetails.tag === "withdrawRequest") {
router.push({
pathname: "/withdraw",
params: {
url: fullUrl,
url: lnurl,
},
});
return;
}

if (lnurlDetails.tag === "payRequest") {
router.push({
pathname: "/send",
params: {
url: lnurl,
},
});
return;
}
}

router.push({
pathname: "/send",
params: {
url: fullUrl,
},
});
// Check for BOLT-11 invoices (including BIP-21 unified QRs)
const bolt11Match = trimmedUrl.match(BOLT11_REGEX);
if (bolt11Match) {
const bolt11 = bolt11Match[1];
router.push({
pathname: "/send",
params: {
url: bolt11,
},
});
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we redirect to Home if nothing matches with a flag to trigger an error here?

} else {
// Redirect the user to the home screen
// if no match was found
Expand Down
Loading