Skip to content

Commit

Permalink
added decodeInvoice util
Browse files Browse the repository at this point in the history
  • Loading branch information
Egge21M committed Jan 17, 2024
1 parent 808d1d3 commit 9dbc1ff
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/model/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,13 @@ export type AmountPreference = {
amount: number;
count: number;
};

export type InvoiceData = {
paymentRequest: string;
amountInSats: number;
amountInMSats: number;
timestamp: number;
paymentHash: string;
memo: string;
expiry: number;
};
29 changes: 28 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { decode } from '@gandlaf21/bolt11-decode';
import { encodeBase64ToJson, encodeJsonToBase64 } from './base64.js';
import {
AmountPreference,
InvoiceData,
MintKeys,
Proof,
Token,
Expand Down Expand Up @@ -184,10 +186,35 @@ export function checkResponse(data: { error?: string; detail?: string }) {
}
}

export function joinUrls(...parts: string[]): string {
export function joinUrls(...parts: Array<string>): string {
return parts.map((part) => part.replace(/(^\/+|\/+$)/g, '')).join('/');
}

export function decodeInvoice(bolt11Invoice: string): InvoiceData {
const invoiceData: InvoiceData = {} as InvoiceData;
const decodeResult = decode(bolt11Invoice);
for (let i = 0; i < decodeResult.sections.length; i++) {
const decodedSection = decodeResult.sections[i];
if (decodedSection.name === 'amount') {
invoiceData.amountInSats = Number(decodedSection.value) / 1000;
invoiceData.amountInMSats = Number(decodedSection.value);
}
if (decodedSection.name === 'timestamp') {
invoiceData.timestamp = decodedSection.value;
}
if (decodedSection.name === 'description') {
invoiceData.memo = decodedSection.value;
}
if (decodedSection.name === 'expiry') {
invoiceData.expiry = decodedSection.value;
}
if (decodedSection.name === 'payment_hash') {
invoiceData.paymentHash = decodedSection.value.toString('hex');
}
}
return invoiceData;
}

export {
bigIntStringify,
bytesToNumber,
Expand Down
16 changes: 16 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,19 @@ describe('test cleanToken', () => {
expect(mint_info_url).toBe('https://8333.space:3338/info');
});
});

describe('test decodeInvoice', () => {
test('decoding a lightning invoice', async () => {
const invoice =
'lnbc15u1p3xnhl2pp5jptserfk3zk4qy42tlucycrfwxhydvlemu9pqr93tuzlv9cc7g3sdqsvfhkcap3xyhx7un8cqzpgxqzjcsp5f8c52y2stc300gl6s4xswtjpc37hrnnr3c9wvtgjfuvqmpm35evq9qyyssqy4lgd8tj637qcjp05rdpxxykjenthxftej7a2zzmwrmrl70fyj9hvj0rewhzj7jfyuwkwcg9g2jpwtk3wkjtwnkdks84hsnu8xps5vsq4gj5hs';
const invoiceData = utils.decodeInvoice(invoice);
expect(invoiceData.timestamp).toStrictEqual(1651105770);
expect(invoiceData.amountInMSats).toStrictEqual(1500000);
expect(invoiceData.amountInSats).toStrictEqual(1500);
expect(invoiceData.paymentHash).toStrictEqual(
'90570c8d3688ad5012aa5ff982606971ae46b3f9df0a100cb15f05f61718f223'
);
expect(invoiceData.expiry).toStrictEqual(600);
expect(invoiceData.memo).toStrictEqual('bolt11.org');
});
});

0 comments on commit 9dbc1ff

Please sign in to comment.