Skip to content

Commit

Permalink
refactor: improve invoice fetching (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Oct 20, 2024
1 parent 6bcfad2 commit 7703f36
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 35 deletions.
26 changes: 26 additions & 0 deletions e2e/submarineSwap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,30 @@ test.describe("Submarine swap", () => {
await generateBitcoinBlock();
// TODO: verify amounts
});

test("Create with LNURL", async ({ page }) => {
await page.goto("/");

await page
.locator(
"div:nth-child(3) > .asset-wrap > .asset > .asset-selection",
)
.click();
await page.getByTestId("select-LN").click();
await page.locator(".asset-wrap").first().click();
await page.getByTestId("select-L-BTC").click();

await page.getByTestId("invoice").click();
await page
.getByTestId("invoice")
.fill(
"LNURL1DP68GUP69UHNZV3H9CCZUVPWXYARXVPSXQHKZURF9AKXUATJD3CQQKE2EU",
);

await page.getByTestId("sendAmount").fill("50 0000");

await page.getByTestId("create-swap-button").click();
// When we can click that button, the swap was created
await page.getByRole("button", { name: "Skip download" }).click();
});
});
29 changes: 18 additions & 11 deletions src/components/CreateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,30 @@ export const CreateButton = () => {
resolve(res);
} catch (e) {
log.warn(
"Fetching invoice from LNURL failed",
"Fetching invoice for LNURL failed:",
e,
);
throw e;
reject(e);
}
});
})(),
(() => {
try {
return fetchBip353(
lnurl(),
Number(receiveAmount()),
);
} catch (e) {
log.warn("Fetching invoice from BIP-353 failed", e);
throw e;
}
return new Promise<string>(async (resolve, reject) => {
try {
resolve(
await fetchBip353(
lnurl(),
Number(receiveAmount()),
),
);
} catch (e) {
log.warn(
"Fetching invoice from BIP-353 failed:",
e,
);
reject(e);
}
});
})(),
]);

Expand Down
52 changes: 28 additions & 24 deletions src/utils/invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,29 @@ export const decodeInvoice = (
}
};

export const fetchLnurl = (
export const fetchLnurl = async (
lnurl: string,
amount_sat: number,
): Promise<string> => {
return new Promise<string>((resolve, reject) => {
let url: string;
const amount = Math.round(amount_sat * 1000);

if (lnurl.includes("@")) {
// Lightning address
const urlsplit = lnurl.split("@");
url = `https://${urlsplit[1]}/.well-known/lnurlp/${urlsplit[0]}`;
} else {
// LNURL
const { bytes } = bech32.decodeToBytes(lnurl);
url = utf8.encode(bytes);
}
let url: string;
if (lnurl.includes("@")) {
// Lightning address
const urlsplit = lnurl.split("@");
url = `https://${urlsplit[1]}/.well-known/lnurlp/${urlsplit[0]}`;
} else {
// LNURL
const { bytes } = bech32.decodeToBytes(lnurl);
url = utf8.encode(bytes);
}

log.debug("fetching lnurl:", url);
fetch(url)
.then(checkResponse<LnurlResponse>)
.then((data) => checkLnurlResponse(amount, data))
.then((data) => fetchLnurlInvoice(amount, data))
.then(resolve)
.catch(reject);
});
const amount = Math.round(amount_sat * 1000);

log.debug("Fetching LNURL:", url);

const res = await checkResponse<LnurlResponse>(await fetch(url));
checkLnurlResponse(amount, res);

return await fetchLnurlInvoice(amount, res);
};

export const fetchBip353 = async (
Expand Down Expand Up @@ -132,10 +129,17 @@ export const fetchBip353 = async (
},
});
const resBody = await res.json();
if (resBody.Answer === undefined || resBody.Answer.length === 0) {
throw "no TXT record";
}

const paymentRequest = resBody.Answer[0].data;
const offer = new URLSearchParams(paymentRequest.split("?")[1]).get("lno");
return (await fetchBolt12Invoice(offer.replaceAll('"', ""), amountSat))
.invoice;
const invoice = (
await fetchBolt12Invoice(offer.replaceAll('"', ""), amountSat)
).invoice;
log.debug(`Resolved invoice for BIP-353:`, invoice);
return invoice;
};

const checkLnurlResponse = (amount: number, data: LnurlResponse) => {
Expand Down

0 comments on commit 7703f36

Please sign in to comment.