-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathcreateDescriptorWallet.ts
More file actions
73 lines (69 loc) · 2.12 KB
/
createDescriptorWallet.ts
File metadata and controls
73 lines (69 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { BitGoAPI } from '@bitgo/sdk-api';
import { BIP32 } from '@bitgo/wasm-utxo';
import { Wallet } from '@bitgo/sdk-core';
import { AbstractUtxoCoin } from '../../abstractUtxoCoin.js';
import { IDescriptorWallet } from '../descriptorWallet.js';
import { NamedDescriptor } from '../NamedDescriptor.js';
import { DescriptorFromKeys } from './createDescriptors.js';
export async function createDescriptorWallet(
bitgo: BitGoAPI,
coin: AbstractUtxoCoin,
{
descriptors,
...params
}: {
type: 'hot';
label: string;
enterprise: string;
keys: string[];
descriptors: NamedDescriptor[];
}
): Promise<IDescriptorWallet> {
// We don't use `coin.wallets().add` here because it does a bunch of validation that does not make sense
// for descriptor wallets.
const newWallet = await bitgo
.post(coin.url('/wallet/add'))
.send({
...params,
coinSpecific: { descriptors },
})
.result();
return new Wallet(bitgo, coin, newWallet) as IDescriptorWallet;
}
export async function createDescriptorWalletWithWalletPassphrase(
bitgo: BitGoAPI,
coin: AbstractUtxoCoin,
{
enterprise,
walletPassphrase,
descriptorsFromKeys,
...params
}: {
label: string;
enterprise: string;
walletPassphrase: string;
descriptorsFromKeys: DescriptorFromKeys;
[key: string]: unknown;
}
): Promise<IDescriptorWallet> {
const userKeychain = await coin.keychains().createUserKeychain(walletPassphrase);
const backupKeychain = await coin.keychains().createBackup();
const bitgoKeychain = await coin.keychains().createBitGo({ enterprise });
if (!userKeychain.prv) {
throw new Error('Missing private key');
}
const userKey = BIP32.fromBase58(userKeychain.prv);
const cosigners = [backupKeychain, bitgoKeychain].map((keychain) => {
if (!keychain.pub) {
throw new Error('Missing public key');
}
return BIP32.fromBase58(keychain.pub);
});
return createDescriptorWallet(bitgo, coin, {
...params,
type: 'hot',
enterprise,
keys: [userKeychain.id, backupKeychain.id, bitgoKeychain.id],
descriptors: descriptorsFromKeys(userKey, cosigners),
});
}