From 4887c9c66239fa4c86df22bcb3ed280c3892c4e1 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Fri, 5 Jan 2024 12:57:27 +0700 Subject: [PATCH] fix: invoice expiry --- package.json | 2 +- src/invoice.test.ts | 22 ++++++++++------------ src/invoice.ts | 8 +++++--- src/utils/invoice.ts | 12 ++++++------ 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 363fd88..64d2b12 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@getalby/lightning-tools", - "version": "5.0.0", + "version": "5.0.1", "description": "Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps", "type": "module", "source": "src/index.ts", diff --git a/src/invoice.test.ts b/src/invoice.test.ts index adbc249..049a192 100644 --- a/src/invoice.test.ts +++ b/src/invoice.test.ts @@ -3,6 +3,9 @@ import Invoice from "./invoice"; const paymentRequestWithoutMemo = "lnbc10n1pj4xmazpp5ns890al37jpreen4rlpl6fsw2hlp9n9hm0ts4dvwvcxq8atf4v6qhp50kncf9zk35xg4lxewt4974ry6mudygsztsz8qn3ar8pn3mtpe50scqzzsxqyz5vqsp5k508kdmvfpuac6lvn9wumr9x4mcpnh2t6jyp5kkxcjhueq4xjxqq9qyyssq0m88mwgknhkqfsa9u8e9dp8v93xlm0lqggslzj8mpsnx3mdzm8z5k9ns7g299pfm9zwm4crs00a364cmpraxr54jw5cf2qx9vycucggqz2ggul"; +const paymentRequestWithoutExpiry = + "lnbc1u1pjc65cpsp5s0ug8ef4ftz7shgcrg9u32p26yfnss2jvn8lf5ef3dnfs3whj04qpp5u4rd3pf5nuj683ycqs95yxhhxtf0ydt36prvq9ntq54mhqvxax8qdqdg9kxy7fq2ph4xcqzysrzjqtypret4hcklglvtfrdt85l3exc0dctdp4qttmtcy5es3lpt6utsmlnye9rpnzdxcgqqqqqqqqqqqqqqyg9qxpqysgqafjchml7d6zfp7u7mjtcasxzp5pglvpejelazshdfgnzdqw030upmtul2luhqdjvkdcf483u5l5ratu8dk0ffr38ypx9aqk57d7vwfcq3xutqa"; + const paymentRequestWithMemo = "lnbc10u1pj4t6w0pp54wm83znxp8xly6qzuff2z7u6585rnlcw9uduf2haa42qcz09f5wqdq023jhxapqd4jk6mccqzzsxqyz5vqsp5mlvjs8nktpz98s5dcrhsuelrz94kl2vjukvu789yzkewast6m00q9qyyssqupynqdv7e5y8nlul0trva5t97g7v3gwx7akhu2dvu4pn66eu2pr5zkcnegp8myz3wrpj9ht06pwyfn4dvpmnr96ejq6ygex43ymaffqq3gud4d"; @@ -18,24 +21,19 @@ describe("Invoice", () => { expect(decodedInvoice.createdDate.toISOString()).toBe( "2023-11-14T13:01:22.000Z", ); - expect(decodedInvoice.expiryDate.toISOString()).toBe( + expect(decodedInvoice.expiryDate!.toISOString()).toBe( "2023-11-15T13:01:22.000Z", ); expect(decodedInvoice.description).toBeNull(); }); + + test("decode invoice without expiry", () => { + const decodedInvoice = new Invoice({ pr: paymentRequestWithoutExpiry }); + expect(decodedInvoice.expiryDate).toBeUndefined(); + }); + test("decode invoice with description", () => { const decodedInvoice = new Invoice({ pr: paymentRequestWithMemo }); expect(decodedInvoice.description).toBe("Test memo"); }); - - test("validate preimage", async () => { - const decodedInvoice = new Invoice({ - pr: "lnbc120n1p3ecwp5pp5z8n0tzytydn57x6q0kqgfearewkx6kdh90svrkrc64azwy9jpnfqdq4f35kw6r5wdshgueqw35hqcqzpgxqyz5vqsp535pwwk083jvpnf87nl3mr4ext8q5f576s57cds72nvu7fpr037nq9qyyssqtq40wszjzs0vpaka2uckjf4xs2fu24f4vp9eev8r230m6epcp2kxdg8xztlw89p2kzkdpadujuflv6f8avgw3jhnvcxjkegdtydd95sp8hwns5", - }); - expect( - await decodedInvoice.validatePreimage( - "dedbef581d83342848d99c02519053f01856add237f94437bc9bbec7bd6f6e55", - ), - ).toBe(true); - }); }); diff --git a/src/invoice.ts b/src/invoice.ts index ae8e4dc..06f315f 100644 --- a/src/invoice.ts +++ b/src/invoice.ts @@ -9,10 +9,10 @@ export default class Invoice { preimage: string | null; verify: string | null; satoshi: number; - expiry: number; // expiry in seconds (not a timestamp) + expiry: number | undefined; // expiry in seconds (not a timestamp) timestamp: number; // created date in seconds createdDate: Date; - expiryDate: Date; + expiryDate: Date | undefined; description: string | null; constructor(args: InvoiceArgs) { @@ -29,7 +29,9 @@ export default class Invoice { this.timestamp = decodedInvoice.timestamp; this.expiry = decodedInvoice.expiry; this.createdDate = new Date(this.timestamp * 1000); - this.expiryDate = new Date((this.timestamp + this.expiry) * 1000); + this.expiryDate = this.expiry + ? new Date((this.timestamp + this.expiry) * 1000) + : undefined; this.description = decodedInvoice.description ?? null; this.verify = args.verify ?? null; this.preimage = args.preimage ?? null; diff --git a/src/utils/invoice.ts b/src/utils/invoice.ts index 25a9523..a5ca6b8 100644 --- a/src/utils/invoice.ts +++ b/src/utils/invoice.ts @@ -4,7 +4,7 @@ type DecodedInvoice = { paymentHash: string; satoshi: number; timestamp: number; - expiry: number; + expiry: number | undefined; description: string | undefined; }; @@ -32,8 +32,6 @@ export const decodeInvoice = ( const satoshi = parseInt(amountTag.value) / 1000; // millisats - const expiryTag = decoded.sections.find((value) => value.name === "expiry"); - const timestampTag = decoded.sections.find( (value) => value.name === "timestamp", ); @@ -42,10 +40,12 @@ export const decodeInvoice = ( const timestamp = timestampTag.value; - if (expiryTag?.name !== "expiry" || expiryTag.value === undefined) - return null; + let expiry: number | undefined; + const expiryTag = decoded.sections.find((value) => value.name === "expiry"); - const expiry = expiryTag.value; + if (expiryTag?.name === "expiry") { + expiry = expiryTag.value; + } const descriptionTag = decoded.sections.find( (value) => value.name === "description",