Skip to content

Commit fb298b9

Browse files
chore: adding pay invoice example for lightning
TICKET: BTC-1963
1 parent e8c3e36 commit fb298b9

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Pay a Lightning invoice from a BitGo Lightning wallet.
3+
*
4+
* IMPORTANT: Your BitGo account must have the "custodyLightningWallet" license
5+
* enabled to use this functionality.
6+
*
7+
* Copyright 2025, BitGo, Inc. All Rights Reserved.
8+
*/
9+
10+
import { BitGoAPI } from '@bitgo/sdk-api';
11+
import { Tlnbtc } from '@bitgo/sdk-coin-lnbtc';
12+
import { getLightningWallet, SubmitPaymentParams } from '@bitgo/abstract-lightning';
13+
14+
// Set access token for testnet
15+
const accessToken = process.env.TESTNET_ACCESS_TOKEN || '';
16+
17+
// Set your lightning wallet ID
18+
const walletId = process.env.LIGHTNING_WALLET_ID || '';
19+
20+
// Set your wallet passphrase - needed for signing
21+
const passphrase = process.env.WALLET_PASSPHRASE || '';
22+
23+
// Use tlnbtc for testnet, lnbtc for mainnet
24+
const coin = '';
25+
26+
// The invoice you want to pay
27+
const invoiceToPay = ''; // BOLT11 encoded invoice string
28+
29+
/**
30+
* Pay a Lightning invoice
31+
* @returns {Promise<any>} Payment result
32+
*/
33+
async function main(): Promise<void> {
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+
//get the wallet
44+
console.log(`Getting Lightning wallet with ID: ${walletId}`);
45+
46+
const wallet = await bitgo.coin(coin).wallets().get({ id: walletId });
47+
const lightning = getLightningWallet(wallet);
48+
49+
// Set up payment parameters
50+
const paymentParams: SubmitPaymentParams = {
51+
invoice: invoiceToPay,
52+
// If the invoice has an amount, this is optional
53+
amountMsat: BigInt(50000), // 50,000 millisatoshis = 50 satoshis
54+
passphrase: passphrase,
55+
// Optional parameters
56+
feeLimitMsat: BigInt(5000), // 5,000 millisatoshis = 5 satoshis maximum fee
57+
comment: 'Payment for services',
58+
sequenceId: `payment-${Date.now()}`, // Optional unique identifier
59+
};
60+
61+
console.log('Paying Lightning invoice...');
62+
const paymentResult = await lightning.payInvoice(paymentParams);
63+
64+
// Display payment information
65+
console.log('\nPayment completed:');
66+
console.log(`Transaction Request ID: ${paymentResult.txRequestId}`);
67+
console.log(`Transaction Request State: ${paymentResult.txRequestState}`);
68+
69+
if (paymentResult.paymentStatus) {
70+
console.log('\nPayment Status:');
71+
console.log(`Status: ${paymentResult.paymentStatus.status}`);
72+
console.log(`Payment Hash: ${paymentResult.paymentStatus.paymentHash}`);
73+
74+
if (paymentResult.paymentStatus.amountMsat) {
75+
console.log(`Amount Paid: ${paymentResult.paymentStatus.amountMsat} msat`);
76+
}
77+
78+
if (paymentResult.paymentStatus.feeMsat) {
79+
console.log(`Fee Paid: ${paymentResult.paymentStatus.feeMsat} msat`);
80+
}
81+
}
82+
83+
// If payment requires approval
84+
if (paymentResult.pendingApproval) {
85+
console.log('\nPayment requires approval:');
86+
console.log(`Pending Approval ID: ${paymentResult.pendingApproval.id}`);
87+
console.log('Please approve the payment in the BitGo dashboard or via the API');
88+
}
89+
} catch (e) {
90+
// Handle expected errors specifically
91+
if (e.message && e.message.includes('invalid invoice')) {
92+
console.error('\nINVALID INVOICE: The provided invoice string is not valid.');
93+
} else if (e.message && e.message.includes('unauthorized')) {
94+
console.error('\nAUTHENTICATION ERROR: Your access token is invalid or expired.');
95+
}
96+
97+
throw e;
98+
}
99+
}
100+
101+
// Run the example
102+
main()
103+
.then((result) => {
104+
console.log('\nExample completed successfully.');
105+
process.exit(0);
106+
})
107+
.catch((e) => {
108+
console.error('Example failed with error:', e.message);
109+
process.exit(-1);
110+
});

0 commit comments

Comments
 (0)