Skip to content

Commit baa9290

Browse files
feat(abstract-utxo): permit BIP32Interface in descriptor utils
Allow passing private keys as BIP32Interface objects to descriptor utils. Useful in tests. Issue: BTC-1450
1 parent ffbff4e commit baa9290

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

modules/abstract-utxo/test/core/descriptor/descriptor.utils.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Descriptor } from '@bitgo/wasm-miniscript';
22

33
import { DescriptorMap, PsbtParams } from '../../../src/core/descriptor';
4-
import { getKeyTriple } from '../key.utils';
4+
import { getKeyTriple, KeyTriple } from '../key.utils';
5+
import { BIP32Interface } from '@bitgo/utxo-lib';
56

67
export function getDefaultXPubs(seed?: string): string[] {
78
return getKeyTriple(seed).map((k) => k.neutered().toBase58());
@@ -21,15 +22,22 @@ export type DescriptorTemplate =
2122
*/
2223
| 'ShWsh2Of3CltvDrop';
2324

24-
function multi(m: number, n: number, keys: string[], path: string): string {
25+
function toXPub(k: BIP32Interface | string): string {
26+
if (typeof k === 'string') {
27+
return k;
28+
}
29+
return k.neutered().toBase58();
30+
}
31+
32+
function multi(m: number, n: number, keys: BIP32Interface[] | string[], path: string): string {
2533
if (n < m) {
2634
throw new Error(`Cannot create ${m} of ${n} multisig`);
2735
}
2836
if (keys.length < n) {
2937
throw new Error(`Not enough keys for ${m} of ${n} multisig: keys.length=${keys.length}`);
3038
}
3139
keys = keys.slice(0, n);
32-
return `multi(${m},${keys.map((k) => `${k}/${path}`).join(',')})`;
40+
return `multi(${m},${keys.map((k) => `${toXPub(k)}/${path}`).join(',')})`;
3341
}
3442

3543
export function getPsbtParams(t: DescriptorTemplate): Partial<PsbtParams> {
@@ -44,7 +52,7 @@ export function getPsbtParams(t: DescriptorTemplate): Partial<PsbtParams> {
4452

4553
export function getDescriptorString(
4654
template: DescriptorTemplate,
47-
keys: string[] = getDefaultXPubs(),
55+
keys: KeyTriple | string[] = getDefaultXPubs(),
4856
path = '0/*'
4957
): string {
5058
switch (template) {
@@ -62,13 +70,16 @@ export function getDescriptorString(
6270

6371
export function getDescriptor(
6472
template: DescriptorTemplate,
65-
keys: string[] = getDefaultXPubs(),
73+
keys: KeyTriple | string[] = getDefaultXPubs(),
6674
path = '0/*'
6775
): Descriptor {
6876
return Descriptor.fromString(getDescriptorString(template, keys, path), 'derivable');
6977
}
7078

71-
export function getDescriptorMap(template: DescriptorTemplate, keys: string[] = getDefaultXPubs()): DescriptorMap {
79+
export function getDescriptorMap(
80+
template: DescriptorTemplate,
81+
keys: KeyTriple | string[] = getDefaultXPubs()
82+
): DescriptorMap {
7283
return toDescriptorMap({
7384
external: getDescriptor(template, keys, '0/*').toString(),
7485
internal: getDescriptor(template, keys, '1/*').toString(),

0 commit comments

Comments
 (0)