Skip to content

Commit eb46a79

Browse files
Merge pull request #6284 from BitGo/BTC-2170.tweak-fromfixedscript
feat(utxo-core): improve descriptor handling for fromFixedScriptWallet
2 parents a08f67e + 2af49d9 commit eb46a79

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

modules/utxo-core/src/descriptor/fromFixedScriptWallet.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as utxolib from '@bitgo/utxo-lib';
22
import { Descriptor, ast } from '@bitgo/wasm-miniscript';
33

4-
import { DescriptorMap } from './DescriptorMap';
5-
64
/** Expand a template with the given root wallet keys and chain code */
75
function expand(rootWalletKeys: utxolib.bitgo.RootWalletKeys, keyIndex: number, chainCode: number): string {
86
if (keyIndex !== 0 && keyIndex !== 1 && keyIndex !== 2) {
@@ -42,12 +40,28 @@ export function getDescriptorForScriptType(
4240
}
4341
}
4442

45-
export function getNamedDescriptorsForRootWalletKeys(rootWalletKeys: utxolib.bitgo.RootWalletKeys): DescriptorMap {
46-
const scriptTypes = ['p2sh', 'p2shP2wsh', 'p2wsh'] as const;
43+
function isSupportedScriptType(
44+
scriptType: utxolib.bitgo.outputScripts.ScriptType2Of3
45+
): scriptType is 'p2sh' | 'p2shP2wsh' | 'p2wsh' {
46+
return ['p2sh', 'p2shP2wsh', 'p2wsh'].includes(scriptType);
47+
}
48+
49+
/**
50+
* Get a map of named descriptors for the given root wallet keys.
51+
* Unsupported script types will have a value of null.
52+
* Currently supports p2sh, p2shP2wsh, and p2wsh script types.
53+
* @param rootWalletKeys
54+
*/
55+
export function getNamedDescriptorsForRootWalletKeys(
56+
rootWalletKeys: utxolib.bitgo.RootWalletKeys
57+
): Map<string, Descriptor | null> {
4758
const scopes = ['external', 'internal'] as const;
4859
return new Map(
49-
scriptTypes.flatMap((scriptType) =>
50-
scopes.map((scope) => [`${scriptType}/${scope}`, getDescriptorForScriptType(rootWalletKeys, scriptType, scope)])
60+
utxolib.bitgo.outputScripts.scriptTypes2Of3.flatMap((scriptType) =>
61+
scopes.map((scope) => [
62+
`${scriptType}/${scope}`,
63+
isSupportedScriptType(scriptType) ? getDescriptorForScriptType(rootWalletKeys, scriptType, scope) : null,
64+
])
5165
)
5266
);
5367
}

modules/utxo-core/src/descriptor/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './DescriptorMap';
44
export * from './derive';
55
export * from './Output';
66
export * from './VirtualSize';
7+
export * from './fromFixedScriptWallet';
78

89
/** @deprecated - import from @bitgo/utxo-core directly instead */
910
export * from '../Output';

modules/utxo-core/test/descriptor/fromFixedScriptWallet.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const customPrefixes: (utxolib.bitgo.Triple<string> | undefined)[] = [
1919
['1/2', '1/2', '1/2'], // Custom prefixes for testing
2020
['1/2', '3/4', '5/6'], // Different custom prefixes
2121
];
22-
const scriptTypes = ['p2sh', 'p2shP2wsh', 'p2wsh'] as const;
22+
const scriptTypes = utxolib.bitgo.outputScripts.scriptTypes2Of3;
2323
const scope = ['external', 'internal'] as const;
2424
const index = [0, 1, 2];
2525

@@ -111,6 +111,10 @@ function runTestGetDescriptorMap(customPrefix: utxolib.bitgo.Triple<string> | un
111111
const key = `${scriptType}/${s}`;
112112
it(`should have a correct descriptor for ${key}`, function () {
113113
const descriptorFromMap = descriptorMap.get(key);
114+
if (scriptType === 'p2tr' || scriptType === 'p2trMusig2') {
115+
assert.equal(descriptorFromMap, null, `Descriptor for ${key} should be null`);
116+
return;
117+
}
114118
assert.ok(descriptorFromMap, `Descriptor for ${key} should exist`);
115119
const expectedDescriptor = getDescriptorForScriptType(rootWalletKeys, scriptType, s);
116120
assert.equal(descriptorFromMap.toString(), expectedDescriptor.toString());
@@ -124,7 +128,9 @@ customPrefixes.forEach((customPrefix) => {
124128
scriptTypes.forEach((scriptType) => {
125129
index.forEach((index) => {
126130
scope.forEach((scope) => {
127-
runTestGetDescriptorWithScriptType(customPrefix, scriptType, index, scope);
131+
if (scriptType !== 'p2tr' && scriptType !== 'p2trMusig2') {
132+
runTestGetDescriptorWithScriptType(customPrefix, scriptType, index, scope);
133+
}
128134
});
129135
});
130136
});

0 commit comments

Comments
 (0)