Skip to content

Commit 384ed92

Browse files
feat: add example for createDescriptorWalletWithWalletPassphrase
Issue: BTC-0
1 parent 12ce976 commit 384ed92

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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));

modules/abstract-utxo/src/descriptor/NamedDescriptor.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ export type NamedDescriptor<T = string> = {
2424

2525
export function createNamedDescriptorWithSignature(
2626
name: string,
27-
descriptor: Descriptor,
27+
descriptor: string | Descriptor,
2828
signingKey: BIP32Interface
2929
): NamedDescriptor {
30+
if (typeof descriptor === 'string') {
31+
descriptor = Descriptor.fromString(descriptor, 'derivable');
32+
}
3033
const value = descriptor.toString();
3134
const signature = signMessage(value, signingKey, networks.bitcoin).toString('hex');
3235
return { name, value, signatures: [signature] };
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { Miniscript, Descriptor } from '@bitgo/wasm-miniscript';
22
export { assertDescriptorWalletAddress } from './assertDescriptorWalletAddress';
3-
export { NamedDescriptor, createNamedDescriptorWithSignature } from './NamedDescriptor';
3+
export { NamedDescriptor, createNamedDescriptorWithSignature, hasValidSignature } from './NamedDescriptor';
44
export { isDescriptorWallet, getDescriptorMapFromWallet } from './descriptorWallet';
55
export { getPolicyForEnv } from './validatePolicy';
66
export * as createWallet from './createWallet';

0 commit comments

Comments
 (0)