Skip to content

Commit 56bd7c3

Browse files
Merge pull request #5906 from BitGo/BTC-1963-list-invoice
chore: adding list-invoice example for lightning
2 parents ac236eb + 1d55a91 commit 56bd7c3

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* List Lightning invoices for a wallet with optional filtering.
3+
*
4+
* IMPORTANT: Your BitGo account must have the "custodyLightningWallet" license
5+
* enabled to use this functionality. Contact BitGo support if you receive a
6+
* license-related error.
7+
*
8+
* Copyright 2025, BitGo, Inc. All Rights Reserved.
9+
*/
10+
11+
import { BitGoAPI } from '@bitgo/sdk-api';
12+
import { Tlnbtc } from '@bitgo/sdk-coin-lnbtc';
13+
import { getLightningWallet } from 'modules/abstract-lightning/src';
14+
15+
// TODO: set access token for testnet
16+
// Get this from your BitGo account
17+
const accessToken = process.env.TESTNET_ACCESS_TOKEN || '';
18+
19+
// TODO: set your Lightning wallet ID here
20+
const walletId = process.env.LIGHTNING_WALLET_ID || '';
21+
22+
// Optional filter parameters
23+
const status = process.env.INVOICE_STATUS as 'open' | 'settled' | 'canceled' | undefined; // Can be 'open', 'settled', or 'canceled'
24+
const limit = process.env.LIMIT ? parseInt(process.env.LIMIT, 10) : 20; // Number of results to return
25+
26+
// Use tlnbtc for testnet, lnbtc for mainnet
27+
const coin = 'tlnbtc';
28+
29+
/**
30+
* List Lightning invoices with optional filtering
31+
* @returns {Promise<any>} Array of Lightning invoices
32+
*/
33+
async function main(): Promise<any> {
34+
try {
35+
const bitgo = new BitGoAPI({
36+
accessToken,
37+
env: 'test',
38+
});
39+
40+
// Register Lightning Bitcoin coin
41+
bitgo.register(coin, Tlnbtc.createInstance);
42+
43+
console.log(`Listing Lightning invoices for wallet: ${walletId}`);
44+
console.log(`Filters - Status: ${status || 'Any'}, Limit: ${limit}`);
45+
46+
if (!walletId) {
47+
throw new Error('Wallet ID is required - please set LIGHTNING_WALLET_ID environment variable');
48+
}
49+
50+
// Get the wallet
51+
const wallet = await bitgo.coin(coin).wallets().get({ id: walletId });
52+
53+
// Get the invoice from Lightning wallet
54+
const lightning = getLightningWallet(wallet);
55+
56+
// List invoices with the provided filters
57+
const query = {
58+
status,
59+
limit: limit ? BigInt(limit) : undefined,
60+
};
61+
const invoices = await lightning.listInvoices(query);
62+
63+
// Display invoice summary
64+
console.log(`\nFound ${invoices.length} invoices:`);
65+
66+
// Display detailed information for each invoice
67+
invoices.forEach((invoice, index) => {
68+
console.log(`\n--- Invoice ${index + 1} ---`);
69+
console.log(`Payment Hash: ${invoice.paymentHash}`);
70+
console.log(`Amount (msat): ${invoice.valueMsat}`);
71+
console.log(`Status: ${invoice.status}`);
72+
console.log(`Created At: ${invoice.createdAt}`);
73+
if (invoice.invoice) {
74+
console.log(`Invoice: ${invoice.invoice}`);
75+
}
76+
});
77+
78+
return invoices;
79+
} catch (e) {
80+
console.error('Error listing Lightning invoices:', e.message);
81+
throw e;
82+
}
83+
}
84+
85+
// Run the example
86+
main()
87+
.then(() => {
88+
console.log('\nExample completed successfully.');
89+
process.exit(0);
90+
})
91+
.catch((e) => {
92+
console.error('Example failed with error:', e.message);
93+
process.exit(-1);
94+
});

0 commit comments

Comments
 (0)