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: decode 0-amount invoices #181

Merged
merged 3 commits into from
Dec 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@getalby/lightning-tools",
"version": "5.1.1",
"version": "5.1.2",
"description": "Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps",
"type": "module",
"source": "src/index.ts",
Expand Down
8 changes: 8 additions & 0 deletions src/invoice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const paymentRequestWithMemo =
const signetPaymentRequest =
"lntbs758310n1pnryklfpp59hmrqxpmanfm4sh4afnqs80yas294hvscr2lv0scp4hza7gpyf5sdyzgd5xzmnwv4kzqvpwxqcnqvpsxqcrqgr5dusryvpjxsknqdedxvc9gv338g6nyw35xyhrzd3ntgszscfnxdjrgvryvsukzd3n893njvf5x5mnvctzx9nrsv3hv9jrgvty9ycqzzsxqrrsssp5pq5nl5xw9hf4k7xl8d635kd60kgdm0jnwe3tvu7dp8zrfedcyzes9qyyssq8qcl3h6ptahwtc8k7q9qrz8v3r0fhp779wuhykxkmn0x6qegl4x4jga2ykcwf5vu89slhzka0w4n7a9n26qcxgzhg4mdymky8smdvvqpw9t93a";

const zeroAmountPaymentRequest =
"lnbc1pn42dukpp5wzqdjf8cv7pxa3rpa5vur8804ud0ckt24jctkq6qlr7w25kuc2fsdp82pshjgr5dusyymrfde4jq4mpd3kx2apq24ek2uscqzpuxqr8pqsp5nkrvgqj37ztv2luy6sfg0fgsr4p4rrqw3s3z5g63f8fsxh86u0hq9p4gqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpqysgqu68rgn4k22zlgzuylamdv6zaczf4rwwrrzlhvw672m8cphctk8shsamruj5ymh04jssy6x09fx99ahrsm7z4w840psu2u3nhtfjw50qpf8qku8";

describe("Invoice", () => {
test("decode invoice without description", () => {
const decodedInvoice = new Invoice({ pr: paymentRequestWithoutMemo });
Expand Down Expand Up @@ -43,6 +46,11 @@ describe("Invoice", () => {
expect(decodedInvoice.description).toBe("Test memo");
});

test("decode invoice with zero amount", () => {
const decodedInvoice = new Invoice({ pr: zeroAmountPaymentRequest });
expect(decodedInvoice.satoshi).toBe(0);
});

test("decode signet invoice", () => {
const decodedInvoice = new Invoice({ pr: signetPaymentRequest });
expect(decodedInvoice.satoshi).toBe(75831);
Expand Down
9 changes: 5 additions & 4 deletions src/utils/invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ export const decodeInvoice = (

const paymentHash = hashTag.value;

const amountTag = decoded.sections.find((value) => value.name === "amount");
let satoshi = 0;

if (amountTag?.name !== "amount" || amountTag.value === undefined)
return null;
const amountTag = decoded.sections.find((value) => value.name === "amount");

const satoshi = parseInt(amountTag.value) / 1000; // millisats
if (amountTag?.name === "amount" && amountTag.value) {
satoshi = parseInt(amountTag.value) / 1000; // millisats
}

const timestampTag = decoded.sections.find(
(value) => value.name === "timestamp",
Expand Down
Loading