|
1 | 1 | import assert from 'assert'; |
2 | 2 |
|
3 | | -import { buildToSpendTransaction, hashMessageWithTag } from '../../src/bip322'; |
| 3 | +import { testutil, bitgo } from '@bitgo/utxo-lib'; |
| 4 | + |
| 5 | +import { |
| 6 | + buildToSpendTransaction, |
| 7 | + hashMessageWithTag, |
| 8 | + buildToSpendTransactionFromChainAndIndex, |
| 9 | +} from '../../src/bip322'; |
4 | 10 |
|
5 | 11 | import { BIP322_PAYMENT_P2WPKH_FIXTURE } from './bip322.utils'; |
6 | 12 |
|
@@ -53,4 +59,46 @@ describe('to_spend', function () { |
53 | 59 | }); |
54 | 60 | }); |
55 | 61 | }); |
| 62 | + |
| 63 | + describe('buildToSpendTransactionFromChainAndIndex', function () { |
| 64 | + it('should throw an error for Taproot chains', function () { |
| 65 | + const taprootChains = [...bitgo.chainCodesP2tr, ...bitgo.chainCodesP2trMusig2]; |
| 66 | + taprootChains.forEach((chain) => { |
| 67 | + assert.throws(() => { |
| 68 | + buildToSpendTransactionFromChainAndIndex( |
| 69 | + testutil.getDefaultWalletKeys(), |
| 70 | + chain, |
| 71 | + 0, |
| 72 | + Buffer.from('Hello World') |
| 73 | + ); |
| 74 | + }, /BIP322 is not supported for Taproot script types/); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('should build a to_spend transaction for a non-Taproot chain', function () { |
| 79 | + function run(chain: bitgo.ChainCode) { |
| 80 | + it(`scriptType: ${bitgo.scriptTypeForChain(chain)}, chain ${chain}`, function () { |
| 81 | + const tx = buildToSpendTransactionFromChainAndIndex( |
| 82 | + testutil.getDefaultWalletKeys(), |
| 83 | + 20, |
| 84 | + 0, |
| 85 | + Buffer.from('Hello World') |
| 86 | + ); |
| 87 | + const expectedScriptPubKey = bitgo.outputScripts |
| 88 | + .createOutputScript2of3(testutil.getDefaultWalletKeys().deriveForChainAndIndex(20, 0).publicKeys, 'p2wsh') |
| 89 | + .scriptPubKey.toString(); |
| 90 | + const scriptPubKeyFromTx = tx.outs[0].script.toString(); |
| 91 | + assert.deepStrictEqual( |
| 92 | + scriptPubKeyFromTx, |
| 93 | + expectedScriptPubKey, |
| 94 | + 'ScriptPubKey does not match expected value' |
| 95 | + ); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + ([0, 1, 10, 11, 20, 21] as bitgo.ChainCode[]).forEach((chain) => { |
| 100 | + run(chain); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
56 | 104 | }); |
0 commit comments