Skip to content

Commit 3f9ab49

Browse files
OttoAllmendingerllm-git
andcommitted
feat(utxo-core): improve fromFixedScriptWallet descriptor handling
Add support for handling all script types by returning null for unsupported types like p2tr and p2trMusig2. Include type guard function to properly verify supported script types. This makes it more explicit which script types are supported and which ones are not. Issue: BTC-2170 Co-authored-by: llm-git <[email protected]>
1 parent a32b05a commit 3f9ab49

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-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/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)