|
| 1 | +import { BIP32Interface } from '@bitgo/utxo-lib'; |
| 2 | +import * as utxolib from '@bitgo/utxo-lib'; |
| 3 | +import { Descriptor } from '@bitgo/wasm-miniscript'; |
| 4 | +import { DescriptorBuilder, getDescriptorFromBuilder } from './builder'; |
| 5 | + |
| 6 | +type NodeUnary<Key extends string> = { [k in Key]: unknown }; |
| 7 | + |
| 8 | +function isUnaryNode<TKey extends string>(node: unknown, key: TKey): node is NodeUnary<TKey> { |
| 9 | + if (typeof node !== 'object' || node === null) { |
| 10 | + return false; |
| 11 | + } |
| 12 | + const keys = Object.keys(node); |
| 13 | + return keys.length === 1 && keys[0] === key; |
| 14 | +} |
| 15 | + |
| 16 | +function unwrapNode(node: unknown, path: string[]): unknown { |
| 17 | + let current = node; |
| 18 | + for (const key of path) { |
| 19 | + if (!isUnaryNode(current, key)) { |
| 20 | + return undefined; |
| 21 | + } |
| 22 | + current = current[key]; |
| 23 | + } |
| 24 | + return current; |
| 25 | +} |
| 26 | + |
| 27 | +function parseMulti(node: unknown): { |
| 28 | + threshold: number; |
| 29 | + keys: BIP32Interface[]; |
| 30 | + path: string; |
| 31 | +} { |
| 32 | + if (!Array.isArray(node)) { |
| 33 | + throw new Error('Unexpected node'); |
| 34 | + } |
| 35 | + const [threshold, ...keyNodes] = node; |
| 36 | + if (typeof threshold !== 'number') { |
| 37 | + throw new Error('Expected threshold number'); |
| 38 | + } |
| 39 | + const keyWithPath = keyNodes.map((keyNode) => { |
| 40 | + if (!isUnaryNode(keyNode, 'XPub')) { |
| 41 | + throw new Error('Expected XPub node'); |
| 42 | + } |
| 43 | + if (typeof keyNode.XPub !== 'string') { |
| 44 | + throw new Error('Expected XPub string'); |
| 45 | + } |
| 46 | + const parts = keyNode.XPub.split('/'); |
| 47 | + return { xpub: parts[0], path: parts.slice(1).join('/') }; |
| 48 | + }); |
| 49 | + const paths = keyWithPath.map((k) => k.path); |
| 50 | + paths.forEach((path, i) => { |
| 51 | + if (path !== paths[0]) { |
| 52 | + throw new Error(`Expected all paths to be the same: ${path} !== ${paths[0]}`); |
| 53 | + } |
| 54 | + }); |
| 55 | + return { |
| 56 | + threshold, |
| 57 | + keys: keyWithPath.map((k) => utxolib.bip32.fromBase58(k.xpub)), |
| 58 | + path: paths[0], |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +export function parseDescriptorNode(node: unknown): DescriptorBuilder { |
| 63 | + const wshMsMulti = unwrapNode(node, ['Wsh', 'Ms', 'Multi']); |
| 64 | + if (wshMsMulti) { |
| 65 | + const { threshold, keys, path } = parseMulti(wshMsMulti); |
| 66 | + let name; |
| 67 | + if (threshold === 2 && keys.length === 2) { |
| 68 | + name = 'Wsh2Of2'; |
| 69 | + } else if (threshold === 2 && keys.length === 3) { |
| 70 | + name = 'Wsh2Of3'; |
| 71 | + } else { |
| 72 | + throw new Error('Unexpected multisig'); |
| 73 | + } |
| 74 | + return { |
| 75 | + name, |
| 76 | + keys, |
| 77 | + path, |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + const shWshMsAndV = unwrapNode(node, ['Sh', 'Wsh', 'Ms', 'AndV']); |
| 82 | + if (shWshMsAndV) { |
| 83 | + if (Array.isArray(shWshMsAndV) && shWshMsAndV.length === 2) { |
| 84 | + const [a, b] = shWshMsAndV; |
| 85 | + const dropAfterAbsLocktime = unwrapNode(a, ['Drop', 'After', 'absLockTime']); |
| 86 | + if (typeof dropAfterAbsLocktime !== 'number') { |
| 87 | + throw new Error('Expected absLockTime number'); |
| 88 | + } |
| 89 | + if (!isUnaryNode(b, 'Multi')) { |
| 90 | + throw new Error('Expected Multi node'); |
| 91 | + } |
| 92 | + const multi = parseMulti(b.Multi); |
| 93 | + if (multi.threshold === 2 && multi.keys.length === 3) { |
| 94 | + return { |
| 95 | + name: 'ShWsh2Of3CltvDrop', |
| 96 | + locktime: dropAfterAbsLocktime, |
| 97 | + keys: multi.keys, |
| 98 | + path: multi.path, |
| 99 | + }; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + throw new Error('Not implemented'); |
| 105 | +} |
| 106 | + |
| 107 | +export function parseDescriptor(descriptor: Descriptor): DescriptorBuilder { |
| 108 | + const builder = parseDescriptorNode(descriptor.node()); |
| 109 | + if (getDescriptorFromBuilder(builder).toString() !== descriptor.toString()) { |
| 110 | + throw new Error('Failed to parse descriptor'); |
| 111 | + } |
| 112 | + return builder; |
| 113 | +} |
0 commit comments