Skip to content

Commit

Permalink
Merge pull request #134 from getAlby/fix/invoice-expiry
Browse files Browse the repository at this point in the history
fix: invoice expiry
  • Loading branch information
rolznz authored Jan 8, 2024
2 parents 1960773 + 4887c9c commit c157322
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
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.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",
Expand Down
22 changes: 10 additions & 12 deletions src/invoice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import Invoice from "./invoice";
const paymentRequestWithoutMemo =
"lnbc10n1pj4xmazpp5ns890al37jpreen4rlpl6fsw2hlp9n9hm0ts4dvwvcxq8atf4v6qhp50kncf9zk35xg4lxewt4974ry6mudygsztsz8qn3ar8pn3mtpe50scqzzsxqyz5vqsp5k508kdmvfpuac6lvn9wumr9x4mcpnh2t6jyp5kkxcjhueq4xjxqq9qyyssq0m88mwgknhkqfsa9u8e9dp8v93xlm0lqggslzj8mpsnx3mdzm8z5k9ns7g299pfm9zwm4crs00a364cmpraxr54jw5cf2qx9vycucggqz2ggul";

const paymentRequestWithoutExpiry =
"lnbc1u1pjc65cpsp5s0ug8ef4ftz7shgcrg9u32p26yfnss2jvn8lf5ef3dnfs3whj04qpp5u4rd3pf5nuj683ycqs95yxhhxtf0ydt36prvq9ntq54mhqvxax8qdqdg9kxy7fq2ph4xcqzysrzjqtypret4hcklglvtfrdt85l3exc0dctdp4qttmtcy5es3lpt6utsmlnye9rpnzdxcgqqqqqqqqqqqqqqyg9qxpqysgqafjchml7d6zfp7u7mjtcasxzp5pglvpejelazshdfgnzdqw030upmtul2luhqdjvkdcf483u5l5ratu8dk0ffr38ypx9aqk57d7vwfcq3xutqa";

const paymentRequestWithMemo =
"lnbc10u1pj4t6w0pp54wm83znxp8xly6qzuff2z7u6585rnlcw9uduf2haa42qcz09f5wqdq023jhxapqd4jk6mccqzzsxqyz5vqsp5mlvjs8nktpz98s5dcrhsuelrz94kl2vjukvu789yzkewast6m00q9qyyssqupynqdv7e5y8nlul0trva5t97g7v3gwx7akhu2dvu4pn66eu2pr5zkcnegp8myz3wrpj9ht06pwyfn4dvpmnr96ejq6ygex43ymaffqq3gud4d";

Expand All @@ -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);
});
});
8 changes: 5 additions & 3 deletions src/invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/utils/invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type DecodedInvoice = {
paymentHash: string;
satoshi: number;
timestamp: number;
expiry: number;
expiry: number | undefined;
description: string | undefined;
};

Expand Down Expand Up @@ -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",
);
Expand All @@ -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",
Expand Down

0 comments on commit c157322

Please sign in to comment.