From e471c897c4b57dd3d4d72ece7d8809a2d3d74b9c Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Sun, 8 Dec 2024 13:16:28 +0700 Subject: [PATCH 1/2] fix: decode 0-amount invoices --- src/invoice.test.ts | 8 ++++++++ src/utils/invoice.ts | 9 +++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/invoice.test.ts b/src/invoice.test.ts index 9496dfa..931b016 100644 --- a/src/invoice.test.ts +++ b/src/invoice.test.ts @@ -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 }); @@ -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); diff --git a/src/utils/invoice.ts b/src/utils/invoice.ts index a5ca6b8..58e39c0 100644 --- a/src/utils/invoice.ts +++ b/src/utils/invoice.ts @@ -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", From 7b5b9fef2d10265a2d4d31760f72ace0a441417f Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Tue, 10 Dec 2024 14:59:26 +0700 Subject: [PATCH 2/2] chore: bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f89dcfe..c993efa 100644 --- a/package.json +++ b/package.json @@ -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",