Skip to content

Commit

Permalink
added decodeInvoice util (#98)
Browse files Browse the repository at this point in the history
* added decodeInvoice util
  • Loading branch information
Egge21M authored Jan 18, 2024
1 parent 49c376f commit 5b6d14a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { CashuMint } from './CashuMint.js';
import { CashuWallet } from './CashuWallet.js';
import { setGlobalRequestOptions } from './request.js';
import { getEncodedToken, getDecodedToken, deriveKeysetId } from './utils.js';
import { decode as getDecodedLnInvoice } from '@gandlaf21/bolt11-decode';
import { generateNewMnemonic, deriveSeedFromMnemonic } from './secrets.js';
import { getEncodedToken, getDecodedToken, deriveKeysetId, decodeInvoice } from './utils.js';
import { decode} from '@gandlaf21/bolt11-decode';

export * from './model/types/index.js';

/**
* @deprecated use decodeInvoice instead
*/
const getDecodedLnInvoice = decode;

export {
CashuMint,
CashuWallet,
getDecodedToken,
getEncodedToken,
deriveKeysetId,
getDecodedLnInvoice,
setGlobalRequestOptions,
generateNewMnemonic,
deriveSeedFromMnemonic
deriveSeedFromMnemonic,
decodeInvoice,
setGlobalRequestOptions
};
10 changes: 10 additions & 0 deletions src/model/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,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 5b6d14a

Please sign in to comment.