-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathcreateDescriptors.ts
More file actions
49 lines (45 loc) · 1.62 KB
/
createDescriptors.ts
File metadata and controls
49 lines (45 loc) · 1.62 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
import { bip32 } from '@bitgo/wasm-utxo';
import { createNamedDescriptorWithSignature, NamedDescriptor } from '../NamedDescriptor.js';
import { getDescriptorFromBuilder, DescriptorBuilder } from '../builder/index.js';
export type DescriptorFromKeys = (
userKey: bip32.BIP32Interface,
cosigners: bip32.BIP32Interface[]
) => NamedDescriptor[];
/**
* Create a pair of external and internal descriptors for a 2-of-3 multisig wallet.
* Overrides the path of the builder to use the external and internal derivation paths (0/* and 1/*).
*
* @param builder
* @param userKey
*/
function createExternalInternalPair(
builder: DescriptorBuilder,
userKey: bip32.BIP32Interface
): [NamedDescriptor, NamedDescriptor] {
if (userKey.isNeutered()) {
throw new Error('User key must be private');
}
const external = createNamedDescriptorWithSignature(
builder.name + '/external',
getDescriptorFromBuilder({ ...builder, path: '0/*' }),
userKey
);
const internal = createNamedDescriptorWithSignature(
builder.name + '/internal',
getDescriptorFromBuilder({ ...builder, path: '1/*' }),
userKey
);
if (external.value === internal.value) {
throw new Error('External and internal descriptors must be different. Make to use the path in descriptor.');
}
return [external, internal];
}
/**
* Create a pair of external and internal descriptors for a 2-of-3 multisig wallet.
*
* @param userKey
* @param cosigners
* @constructor
*/
export const DefaultWsh2Of3: DescriptorFromKeys = (userKey, cosigners) =>
createExternalInternalPair({ name: 'Wsh2Of3', keys: [userKey.neutered(), ...cosigners], path: '' }, userKey);