Skip to content

Commit 40465df

Browse files
feat: add example for createDescriptorWalletWithWalletPassphrase
Issue: BTC-0
1 parent 8c7e4dc commit 40465df

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Create a multi-sig wallet at BitGo.
3+
* This makes use of the convenience function generateWallet
4+
*
5+
* This tool will help you see how to use the BitGo API to easily create a wallet.
6+
* In this form, it creates 2 keys on the host which runs this example.
7+
* It is HIGHLY RECOMMENDED that you GENERATE THE KEYS ON SEPARATE MACHINES for real money wallets!
8+
*
9+
* To perform more advanced features, such as encrypting keys yourself, please look at createWalletAdvanced.js
10+
*
11+
* Copyright 2022, BitGo, Inc. All Rights Reserved.
12+
*/
13+
14+
/**
15+
* Add Low Fee webhook to a wallet.
16+
*
17+
* Copyright 2022 BitGo, Inc. All Rights Reserved.
18+
*/
19+
20+
import { BitGoAPI } from '@bitgo/sdk-api';
21+
import { Tbtc } from '@bitgo/sdk-coin-btc'; // Replace with your given coin (e.g. Ltc, Tltc)
22+
import { AbstractUtxoCoin, descriptor } from '@bitgo/abstract-utxo';
23+
require('dotenv').config({ path: '../../.env' });
24+
25+
const bitgo = new BitGoAPI({
26+
accessToken: process.env.TESTNET_ACCESS_TOKEN,
27+
env: 'test', // Change this to env: 'production' when you are ready for production
28+
});
29+
30+
// Set the coin name to match the blockchain and network
31+
// btc = bitcoin, tbtc = testnet bitcoin
32+
const coin = 'tbtc';
33+
bitgo.register(coin, Tbtc.createInstance);
34+
35+
// TODO: set a label for your new wallet here
36+
const label = 'Example Descriptor Wallet';
37+
38+
// TODO: set your passphrase for your new wallet here
39+
const passphrase = 'test_wallet_passphrase';
40+
41+
// TODO: set your enterprise ID for your new wallet here
42+
const enterprise = 'your_enterprise_id';
43+
44+
async function main() {
45+
console.log(
46+
// this wrapper creates three keys: userKey, backupKey, and bitgoKey
47+
// at the moment, this is a somewhat artificial requirement from wallet platform
48+
// in the future, we will allow for more flexible key generation
49+
await descriptor.createWallet.createDescriptorWalletWithWalletPassphrase(
50+
bitgo,
51+
bitgo.coin(coin) as AbstractUtxoCoin,
52+
{
53+
label,
54+
walletPassphrase: passphrase,
55+
enterprise,
56+
descriptorsFromKeys(userKey, cosigners) {
57+
// userKey is backed up at BitGo with the wallet passphrase
58+
// cosigners are backup key and BitGo key
59+
const xpubs = [userKey, ...cosigners].map((key) => key.neutered().toBase58());
60+
61+
return [
62+
// here is a single-sig descriptor for the user key
63+
descriptor.createNamedDescriptorWithSignature('SingleSigWpkh', `wpkh(${xpubs[0]}/*)`, userKey),
64+
// here is a 2of3 multisig descriptor for the backup key and BitGo key
65+
descriptor.createNamedDescriptorWithSignature(
66+
'MultiSigWsh',
67+
`wsh(multi(2,${xpubs.map((xpub) => `${xpub}/*`).join(',')})`,
68+
userKey
69+
),
70+
// equivalent to the above, but returns two descriptors (external and internal)
71+
...descriptor.createWallet.DefaultWsh2Of3(userKey, cosigners),
72+
];
73+
},
74+
}
75+
)
76+
);
77+
}
78+
79+
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 } 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)