Skip to content

Commit bd12ee6

Browse files
committed
Have scriptSigFinalizer order sigs properly
1 parent 68361a1 commit bd12ee6

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

test/integration/taproot.spec.ts

+3-3
Original file line numberDiff line numberDiff 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

test/psbt.utils.ts

+14-1
Original file line numberDiff line numberDiff 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 pushes: Buffer[] = bitcoin.script
37+
.decompile(script)
38+
.filter(chunk => Buffer.isBuffer(chunk));
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
);

0 commit comments

Comments
 (0)