File tree Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -224,8 +224,8 @@ describe('bitcoinjs-lib (transaction with taproot)', () => {
224
224
} ) ;
225
225
psbt . addOutput ( { value : sendAmount , address : address ! } ) ;
226
226
227
- psbt . signInput ( 0 , leafKeys [ 1 ] ) ;
228
227
psbt . signInput ( 0 , leafKeys [ 0 ] ) ;
228
+ psbt . signInput ( 0 , leafKeys [ 1 ] ) ;
229
229
230
230
const tapscriptFinalizer = buildTapscriptFinalizer (
231
231
internalKey . publicKey ,
@@ -344,9 +344,9 @@ describe('bitcoinjs-lib (transaction with taproot)', () => {
344
344
leafPubkeys . push ( toXOnly ( leafKey . publicKey ) . toString ( 'hex' ) ) ;
345
345
}
346
346
347
- const leafScriptAsm = `${ leafPubkeys [ 2 ] } OP_CHECKSIG ${
347
+ const leafScriptAsm = `${ leafPubkeys [ 0 ] } OP_CHECKSIG ${
348
348
leafPubkeys [ 1 ]
349
- } OP_CHECKSIGADD ${ leafPubkeys [ 0 ] } OP_CHECKSIGADD OP_3 OP_NUMEQUAL`;
349
+ } OP_CHECKSIGADD ${ leafPubkeys [ 2 ] } OP_CHECKSIGADD OP_3 OP_NUMEQUAL`;
350
350
351
351
const leafScript = bitcoin . script . fromASM ( leafScriptAsm ) ;
352
352
Original file line number Diff line number Diff line change @@ -33,7 +33,20 @@ const buildTapscriptFinalizer = (
33
33
redeem : { output : script } ,
34
34
network,
35
35
} ) ;
36
- const sigs = ( input . partialSig || [ ] ) . map ( ps => ps . signature ) as Buffer [ ] ;
36
+ const stack = bitcoin . script . decompile ( script ) ;
37
+ if ( ! stack ) throw new Error ( 'Invalid script' ) ;
38
+ const pushes = stack . filter ( chunk => Buffer . isBuffer ( chunk ) ) as Buffer [ ] ;
39
+ const pkIdx = ( pk : Buffer ) => pushes . findIndex ( chunk => pk . equals ( chunk ) ) ;
40
+ let sigs : Buffer [ ] = [ ] ;
41
+ if ( input . partialSig ) {
42
+ const pkIdxSigs = input . partialSig . map ( ps => ( {
43
+ idx : pkIdx ( ps . pubkey ) ,
44
+ sig : ps . signature ,
45
+ } ) ) ;
46
+ // Sigs need to be reverse script order
47
+ pkIdxSigs . sort ( ( a , b ) => a . idx - b . idx ) ;
48
+ sigs = pkIdxSigs . map ( ( { sig } ) => sig ) ;
49
+ }
37
50
const finalScriptWitness = sigs . concat (
38
51
tapscriptSpend . witness as Buffer [ ] ,
39
52
) ;
You can’t perform that action at this time.
0 commit comments