|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import { Descriptor, ast } from '@bitgo/wasm-miniscript'; |
| 4 | + |
| 5 | +import { getKey } from '../../../src/testutil'; |
| 6 | +import { toXOnlyPublicKey } from '../../../src'; |
| 7 | +import { PatternMatcher, Pattern } from '../../../src/descriptor/parse/PatternMatcher'; |
| 8 | + |
| 9 | +function key32(seed: string): Buffer { |
| 10 | + // return x-only public key from seed |
| 11 | + return toXOnlyPublicKey(getKey(seed).publicKey); |
| 12 | +} |
| 13 | + |
| 14 | +function getUnspendableKey(): string { |
| 15 | + return '50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0'; |
| 16 | +} |
| 17 | + |
| 18 | +function pk(b: Buffer): ast.MiniscriptNode { |
| 19 | + return { 'v:pk': b.toString('hex') }; |
| 20 | +} |
| 21 | + |
| 22 | +function sortedKeys(keys: Buffer[]): Buffer[] { |
| 23 | + return [...keys].sort((a, b) => a.compare(b)); |
| 24 | +} |
| 25 | + |
| 26 | +function multiArgs(threshold: number, keys: Buffer[]): [number, ...string[]] { |
| 27 | + return [threshold, ...sortedKeys(keys).map((k) => k.toString('hex'))]; |
| 28 | +} |
| 29 | + |
| 30 | +function taprootScriptOnlyFromAst(n: ast.TapTreeNode): Descriptor { |
| 31 | + return Descriptor.fromString(ast.formatNode({ tr: [getUnspendableKey(), n] }), 'definite'); |
| 32 | +} |
| 33 | + |
| 34 | +class StakingDescriptorBuilder { |
| 35 | + constructor( |
| 36 | + public userKey: Buffer, |
| 37 | + public providerKeys: Buffer[], |
| 38 | + public guardianKeys: Buffer[], |
| 39 | + public guardianThreshold: number, |
| 40 | + public stakingTimeLock: number |
| 41 | + ) {} |
| 42 | + |
| 43 | + getTimelockMiniscriptNode(): ast.MiniscriptNode { |
| 44 | + return { and_v: [pk(this.userKey), { older: this.stakingTimeLock }] }; |
| 45 | + } |
| 46 | + |
| 47 | + getWithdrawalMiniscriptNode(): ast.MiniscriptNode { |
| 48 | + return { and_v: [pk(this.userKey), { multi_a: multiArgs(this.guardianThreshold, this.guardianKeys) }] }; |
| 49 | + } |
| 50 | + |
| 51 | + getPenaltyMiniscriptNode(): ast.MiniscriptNode { |
| 52 | + return { |
| 53 | + and_v: [ |
| 54 | + { |
| 55 | + and_v: [ |
| 56 | + pk(this.userKey), |
| 57 | + this.providerKeys.length === 1 |
| 58 | + ? { 'v:pk': this.providerKeys[0].toString('hex') } |
| 59 | + : { 'v:multi_a': multiArgs(1, this.providerKeys) }, |
| 60 | + ], |
| 61 | + }, |
| 62 | + { multi_a: multiArgs(this.guardianThreshold, this.guardianKeys) }, |
| 63 | + ], |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + getStakingDescriptor(): Descriptor { |
| 68 | + return taprootScriptOnlyFromAst([ |
| 69 | + this.getPenaltyMiniscriptNode(), |
| 70 | + [this.getWithdrawalMiniscriptNode(), this.getTimelockMiniscriptNode()], |
| 71 | + ]); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// Inverse function to parse the descriptor |
| 76 | +function parseStakingDescriptor(descriptor: Descriptor): { |
| 77 | + penaltyNode: ast.MiniscriptNode; |
| 78 | + withdrawalNode: ast.MiniscriptNode; |
| 79 | + timelockNode: ast.MiniscriptNode; |
| 80 | +} | null { |
| 81 | + const pattern: Pattern = { |
| 82 | + tr: [getUnspendableKey(), [{ $var: 'penaltyNode' }, [{ $var: 'withdrawalNode' }, { $var: 'timelockNode' }]]], |
| 83 | + }; |
| 84 | + |
| 85 | + const matcher = new PatternMatcher(); |
| 86 | + const descriptorNode = ast.fromDescriptor(descriptor); |
| 87 | + const result = matcher.match(descriptorNode, pattern); |
| 88 | + |
| 89 | + if (!result) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + penaltyNode: result.penaltyNode as ast.MiniscriptNode, |
| 95 | + withdrawalNode: result.withdrawalNode as ast.MiniscriptNode, |
| 96 | + timelockNode: result.timelockNode as ast.MiniscriptNode, |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +describe('PatternMatcher', function () { |
| 101 | + it('should match basic object', function () { |
| 102 | + const pattern = { a: { $var: 'x' }, b: 'hello' }; |
| 103 | + const node = { a: 123, b: 'hello' }; |
| 104 | + const vars = new PatternMatcher().match(node, pattern); |
| 105 | + assert.deepStrictEqual(vars, { x: 123 }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should fail on non-matching object', function () { |
| 109 | + const pattern = { a: { $var: 'x' }, b: 'world' }; |
| 110 | + const node = { a: 123, b: 'hello' }; |
| 111 | + const vars = new PatternMatcher().match(node, pattern); |
| 112 | + assert.strictEqual(vars, null); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should match with repeated var', function () { |
| 116 | + const pattern = { a: { $var: 'x' }, b: { $var: 'x' } }; |
| 117 | + const node = { a: 123, b: 123 }; |
| 118 | + const vars = new PatternMatcher().match(node, pattern); |
| 119 | + assert.deepStrictEqual(vars, { x: 123 }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should fail with non-matching repeated var', function () { |
| 123 | + const pattern = { a: { $var: 'x' }, b: { $var: 'x' } }; |
| 124 | + const node = { a: 123, b: 456 }; |
| 125 | + const vars = new PatternMatcher().match(node, pattern); |
| 126 | + assert.strictEqual(vars, null); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should parse staking descriptor', function () { |
| 130 | + const builder = new StakingDescriptorBuilder( |
| 131 | + key32('user'), |
| 132 | + [key32('provider1')], |
| 133 | + [key32('guardian1'), key32('guardian2')], |
| 134 | + 2, |
| 135 | + 100 |
| 136 | + ); |
| 137 | + |
| 138 | + const descriptor = builder.getStakingDescriptor(); |
| 139 | + const parsed = parseStakingDescriptor(descriptor); |
| 140 | + |
| 141 | + assert.notStrictEqual(parsed, null); |
| 142 | + if (parsed) { |
| 143 | + assert.deepStrictEqual(parsed.penaltyNode, builder.getPenaltyMiniscriptNode()); |
| 144 | + assert.deepStrictEqual(parsed.withdrawalNode, builder.getWithdrawalMiniscriptNode()); |
| 145 | + assert.deepStrictEqual(parsed.timelockNode, builder.getTimelockMiniscriptNode()); |
| 146 | + } |
| 147 | + }); |
| 148 | +}); |
0 commit comments