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)', () => {
224224 } ) ;
225225 psbt . addOutput ( { value : sendAmount , address : address ! } ) ;
226226
227- psbt . signInput ( 0 , leafKeys [ 1 ] ) ;
228227 psbt . signInput ( 0 , leafKeys [ 0 ] ) ;
228+ psbt . signInput ( 0 , leafKeys [ 1 ] ) ;
229229
230230 const tapscriptFinalizer = buildTapscriptFinalizer (
231231 internalKey . publicKey ,
@@ -344,9 +344,9 @@ describe('bitcoinjs-lib (transaction with taproot)', () => {
344344 leafPubkeys . push ( toXOnly ( leafKey . publicKey ) . toString ( 'hex' ) ) ;
345345 }
346346
347- const leafScriptAsm = `${ leafPubkeys [ 2 ] } OP_CHECKSIG ${
347+ const leafScriptAsm = `${ leafPubkeys [ 0 ] } OP_CHECKSIG ${
348348 leafPubkeys [ 1 ]
349- } OP_CHECKSIGADD ${ leafPubkeys [ 0 ] } OP_CHECKSIGADD OP_3 OP_NUMEQUAL`;
349+ } OP_CHECKSIGADD ${ leafPubkeys [ 2 ] } OP_CHECKSIGADD OP_3 OP_NUMEQUAL`;
350350
351351 const leafScript = bitcoin . script . fromASM ( leafScriptAsm ) ;
352352
Original file line number Diff line number Diff line change @@ -33,7 +33,20 @@ const buildTapscriptFinalizer = (
3333 redeem : { output : script } ,
3434 network,
3535 } ) ;
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 ) => b . idx - a . idx ) ;
48+ sigs = pkIdxSigs . map ( ( { sig } ) => sig ) ;
49+ }
3750 const finalScriptWitness = sigs . concat (
3851 tapscriptSpend . witness as Buffer [ ] ,
3952 ) ;
You can’t perform that action at this time.
0 commit comments