|
| 1 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 2 | +import { Tbtc } from '@bitgo/sdk-coin-btc'; // Replace with your given coin (e.g. Ltc, Tltc) |
| 3 | +import { AbstractUtxoCoin, descriptor } from '@bitgo/abstract-utxo'; |
| 4 | +require('dotenv').config({ path: '../../.env' }); |
| 5 | + |
| 6 | +const bitgo = new BitGoAPI({ |
| 7 | + accessToken: process.env.TESTNET_ACCESS_TOKEN, |
| 8 | + env: 'test', // Change this to env: 'production' when you are ready for production |
| 9 | +}); |
| 10 | + |
| 11 | +// Set the coin name to match the blockchain and network |
| 12 | +// btc = bitcoin, tbtc = testnet bitcoin |
| 13 | +const coin = 'tbtc'; |
| 14 | +bitgo.register(coin, Tbtc.createInstance); |
| 15 | + |
| 16 | +// TODO: set a label for your new wallet here |
| 17 | +const label = 'Example Descriptor Wallet'; |
| 18 | + |
| 19 | +// TODO: set your passphrase for your new wallet here |
| 20 | +const passphrase = 'test_wallet_passphrase'; |
| 21 | + |
| 22 | +// TODO: set your enterprise ID for your new wallet here |
| 23 | +const enterprise = 'your_enterprise_id'; |
| 24 | + |
| 25 | +async function main() { |
| 26 | + console.log( |
| 27 | + // this wrapper creates three keys: userKey, backupKey, and bitgoKey |
| 28 | + // at the moment, this is a somewhat artificial requirement from wallet platform |
| 29 | + // in the future, we will allow for more flexible key generation |
| 30 | + await descriptor.createWallet.createDescriptorWalletWithWalletPassphrase( |
| 31 | + bitgo, |
| 32 | + bitgo.coin(coin) as AbstractUtxoCoin, |
| 33 | + { |
| 34 | + label, |
| 35 | + walletPassphrase: passphrase, |
| 36 | + enterprise, |
| 37 | + descriptorsFromKeys(userKey, cosigners) { |
| 38 | + // userKey is backed up at BitGo with the wallet passphrase |
| 39 | + // cosigners are backup key and BitGo key |
| 40 | + const xpubs = [userKey, ...cosigners].map((key) => key.neutered().toBase58()); |
| 41 | + |
| 42 | + return [ |
| 43 | + // here is a single-sig descriptor for the user key |
| 44 | + descriptor.createNamedDescriptorWithSignature('SingleSigWpkh', `wpkh(${xpubs[0]}/*)`, userKey), |
| 45 | + // here is a 2of3 multisig descriptor for the backup key and BitGo key |
| 46 | + descriptor.createNamedDescriptorWithSignature( |
| 47 | + 'MultiSigWsh', |
| 48 | + `wsh(multi(2,${xpubs.map((xpub) => `${xpub}/*`).join(',')})`, |
| 49 | + userKey |
| 50 | + ), |
| 51 | + // equivalent to the above, but returns two descriptors (external and internal) |
| 52 | + ...descriptor.createWallet.DefaultWsh2Of3(userKey, cosigners), |
| 53 | + ]; |
| 54 | + }, |
| 55 | + } |
| 56 | + ) |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +main().catch((e) => console.log(e)); |
0 commit comments