|
1 | 1 | import * as utxolib from '@bitgo/utxo-lib'; |
2 | 2 | import { Descriptor, ast } from '@bitgo/wasm-miniscript'; |
3 | 3 |
|
4 | | -import { DescriptorMap } from './DescriptorMap'; |
5 | | - |
6 | 4 | /** Expand a template with the given root wallet keys and chain code */ |
7 | 5 | function expand(rootWalletKeys: utxolib.bitgo.RootWalletKeys, keyIndex: number, chainCode: number): string { |
8 | 6 | if (keyIndex !== 0 && keyIndex !== 1 && keyIndex !== 2) { |
@@ -42,12 +40,28 @@ export function getDescriptorForScriptType( |
42 | 40 | } |
43 | 41 | } |
44 | 42 |
|
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> { |
47 | 58 | const scopes = ['external', 'internal'] as const; |
48 | 59 | 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 | + ]) |
51 | 65 | ) |
52 | 66 | ); |
53 | 67 | } |
0 commit comments