Skip to content

Commit d67bcad

Browse files
Merge pull request #5877 from BitGo/BTC-1963-create-invoice
chore: adding create-invoice example for custodial lightning
2 parents 3c7d9d2 + d8ca16b commit d67bcad

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Create a Lightning invoice in an existing BitGo wallet.
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 { CreateInvoiceBody, getLightningWallet, Invoice } from '@bitgo/abstract-lightning';
14+
require('dotenv').config({ path: '../../.env' });
15+
16+
// TODO: set access token for testnet
17+
// Get this from your BitGo account
18+
const accessToken = process.env.TESTNET_ACCESS_TOKEN || '';
19+
20+
// TODO: set your lightning wallet ID
21+
const walletId = process.env.LIGHTNING_WALLET_ID || '';
22+
23+
// Use tlnbtc for testnet, lnbtc for mainnet
24+
const coin = 'tlnbtc';
25+
26+
/**
27+
* Create a Lightning invoice
28+
* This function creates an invoice in an existing Lightning wallet
29+
* @returns {Promise<Invoice>} Invoice object
30+
*/
31+
async function main(): Promise<Invoice> {
32+
try {
33+
const bitgo = new BitGoAPI({
34+
accessToken,
35+
env: 'test',
36+
});
37+
38+
// Register Lightning Bitcoin coin
39+
bitgo.register(coin, Tlnbtc.createInstance);
40+
41+
// Validate input
42+
if (!walletId) {
43+
throw new Error('Lightning wallet ID is required');
44+
}
45+
46+
console.log(`Getting Lightning wallet with ID: ${walletId}`);
47+
const wallet = await bitgo.coin(coin).wallets().get({ id: walletId });
48+
const lightning = getLightningWallet(wallet);
49+
50+
// Set up invoice parameters - note that amounts need to be provided as BigInt
51+
const invoiceParams: CreateInvoiceBody = {
52+
valueMsat: BigInt(50000), // 50,000 millisatoshis = 50 satoshis
53+
memo: `Test invoice created at ${new Date().toISOString()}`,
54+
expiry: 3600, // 1 hour expiry
55+
};
56+
57+
console.log('Creating Lightning invoice...');
58+
const invoice = await lightning.createInvoice(invoiceParams);
59+
60+
// Display invoice information
61+
console.log('\nInvoice created successfully:');
62+
console.log(`Payment Hash: ${invoice.paymentHash}`);
63+
console.log(`Invoice Amount: ${invoice.valueMsat.toString()} msat`);
64+
console.log(`Status: ${invoice.status}`);
65+
console.log(`Expires At: ${invoice.expiresAt.toISOString()}`);
66+
67+
// Display invoice string (this is what recipients need to pay)
68+
console.log('\nFull Invoice String (share with payer):');
69+
console.log(invoice.invoice);
70+
71+
console.log('\nTo check invoice status later, use the payment hash.');
72+
73+
return invoice;
74+
} catch (e) {
75+
throw e;
76+
}
77+
}
78+
79+
// Run the example
80+
main()
81+
.then((invoice) => {
82+
console.log('Example completed successfully.');
83+
process.exit(0);
84+
})
85+
.catch((e) => {
86+
console.error('Example failed with error:', e.message);
87+
process.exit(-1);
88+
});

0 commit comments

Comments
 (0)