|
| 1 | +/** |
| 2 | + * Create a Lightning self-custodial wallet at BitGo. |
| 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 * as crypto from 'crypto'; |
| 14 | + |
| 15 | +// TODO: set access token for testnet |
| 16 | +// Get this from your BitGo account |
| 17 | +const accessToken = ''; |
| 18 | + |
| 19 | +// TODO: set passphrase to create a wallet |
| 20 | +const passphrase = ''; |
| 21 | + |
| 22 | +// TODO: set your enterprise ID |
| 23 | +const enterprise = ''; |
| 24 | + |
| 25 | +// Generate a passcode encryption code (required for Lightning wallets) |
| 26 | +// IMPORTANT: Store this information securely. You will need it to recover your wallet if you lose wallet password. |
| 27 | +const passcodeEncryptionCode = process.env.PASSCODE_ENCRYPTION_CODE || crypto.randomBytes(32).toString('hex'); |
| 28 | + |
| 29 | +// Use tlnbtc for testnet, lnbtc for mainnet |
| 30 | +const coin = 'tlnbtc'; |
| 31 | + |
| 32 | +/** |
| 33 | + * Create a Lightning self-custodial wallet |
| 34 | + * This function creates a self-custodial Lightning wallet on the BitGo platform |
| 35 | + * @returns {Promise<void>} Wallet object |
| 36 | + */ |
| 37 | +async function main(): Promise<void> { |
| 38 | + try { |
| 39 | + const bitgo = new BitGoAPI({ |
| 40 | + accessToken, |
| 41 | + env: 'test', |
| 42 | + }); |
| 43 | + |
| 44 | + // Register Lightning Bitcoin coin |
| 45 | + bitgo.register(coin, Tlnbtc.createInstance); |
| 46 | + |
| 47 | + // Create unique label for the wallet |
| 48 | + const label = `Lightning Self-Custodial Wallet ${new Date().toISOString()}`; |
| 49 | + |
| 50 | + // Configure wallet creation options |
| 51 | + const walletOptions = { |
| 52 | + label, |
| 53 | + passphrase, |
| 54 | + enterprise, |
| 55 | + passcodeEncryptionCode, |
| 56 | + subType: 'lightningSelfCustody' as const, |
| 57 | + }; |
| 58 | + |
| 59 | + console.log('Creating Lightning self-custodial wallet...'); |
| 60 | + console.log('Note: This requires the custodyLightningWallet license on your BitGo account.'); |
| 61 | + |
| 62 | + const wallet = await bitgo.coin(coin).wallets().generateWallet(walletOptions); |
| 63 | + const walletInstance = wallet.wallet; |
| 64 | + |
| 65 | + // Display wallet information |
| 66 | + console.log('\nWallet created successfully:'); |
| 67 | + console.log(`Wallet ID: ${walletInstance.id()}`); |
| 68 | + console.log(`Wallet label: ${walletInstance.label()}`); |
| 69 | + console.log(`Wallet type: ${walletInstance.type()}`); |
| 70 | + console.log(`Wallet subType: ${walletInstance.subType()}`); |
| 71 | + |
| 72 | + // Display backup information |
| 73 | + console.log('\nIMPORTANT - BACKUP THIS INFORMATION:'); |
| 74 | + console.log(`User keychain encrypted xPrv: ${wallet.userKeychain.encryptedPrv}`); |
| 75 | + |
| 76 | + // CRITICAL: Node Auth keychain is required for self-custodial Lightning wallets |
| 77 | + console.log(`Passcode Encryption Code: ${passcodeEncryptionCode}`); |
| 78 | + console.log('\nStore this information securely. You will need it to recover your wallet.'); |
| 79 | + } catch (e) { |
| 80 | + throw e; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Run the example |
| 85 | +main() |
| 86 | + .then(() => { |
| 87 | + console.log('Example completed successfully.'); |
| 88 | + process.exit(0); |
| 89 | + }) |
| 90 | + .catch((e) => { |
| 91 | + console.error('Example failed with error:', e.message); |
| 92 | + process.exit(-1); |
| 93 | + }); |
0 commit comments