Skip to content

Commit

Permalink
feat: sort p2sh signatures for multisig (#369)
Browse files Browse the repository at this point in the history
* feat: sort signatures for p2sh txs
  • Loading branch information
r4mmer committed May 30, 2022
1 parent 5402641 commit d3ea911
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/new/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,12 @@ class HathorWallet extends EventEmitter {
const multisigData = accessData.multisig;
const xpub = HDPublicKey(accessData.xpubkey);

// deserialize P2SHSignature for all signatures
const p2shSignatures = signatures.map(sig => P2SHSignature.deserialize(sig));
// Deserialize P2SHSignature for all signatures
// XXX: the .sort here is very important since the fullnode requires the signatures
// in the same order as the pubkeys in the redeemScript and the order chosen for the
// pubkeys is the order of the sorted account path pubkey (hex encoded). This sort
// only works because the serialized signature starts with the account path pubkey.
const p2shSignatures = signatures.sort().map(sig => P2SHSignature.deserialize(sig));

for (const {index, input} of tx.inputs.map((input, index) => ({index, input}))) {
if (!(input.hash in historyTransactions)) {
Expand Down
7 changes: 7 additions & 0 deletions src/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,15 @@ const wallet = {
throw new Error('Signatures are incompatible with redeemScript');
}
const arr: Buffer[] = [];
let sigCount = 0;
for (const sig of signatures) {
helpers.pushDataToStack(arr, sig);
if (++sigCount >= minSignatures) {
// XXX: This conditional makes it so signatures after minSignatures are ignored
// This is because the OP_CHECKMULTISIG will only acknowledge and verify the minimum
// number and treat any excess as extra data on stack
break;
}
}
helpers.pushDataToStack(arr, redeemScript);
return util.buffer.concat(arr);
Expand Down

0 comments on commit d3ea911

Please sign in to comment.