From 78c7e80319d6febe7837b8e9a318be45e515142b Mon Sep 17 00:00:00 2001 From: Tomas Susanka Date: Tue, 13 Feb 2018 12:10:31 +0100 Subject: [PATCH] signing/multisig: correct ordering of signatures --- src/apps/wallet/sign_tx/scripts.py | 20 ++++++++++++-------- src/apps/wallet/sign_tx/signing.py | 4 +++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/apps/wallet/sign_tx/scripts.py b/src/apps/wallet/sign_tx/scripts.py index 129f78e9e5..310435b373 100644 --- a/src/apps/wallet/sign_tx/scripts.py +++ b/src/apps/wallet/sign_tx/scripts.py @@ -137,22 +137,26 @@ def get_p2wpkh_witness(signature: bytes, pubkey: bytes, sighash: int): return w -def get_p2wsh_witness(multisig: MultisigRedeemScriptType, current_signature: bytes, other_signatures, sighash: int): - # filter empty - other_signatures = [s for s in other_signatures if len(s) > 0] +def get_p2wsh_witness(multisig: MultisigRedeemScriptType, signature: bytes, signature_index: int, sighash: int): - # witness program + other signatures + current signature + redeem script - num_of_witness_items = 1 + len(other_signatures) + 1 + 1 + signatures = multisig.signatures # other signatures + if len(signatures[signature_index]) > 0: + raise ScriptsError('One of the multisig signatures occupies the current signature\'s spot') + signatures[signature_index] = signature # our signature + + # filter empty + signatures = [s for s in multisig.signatures if len(s) > 0] + + # witness program + signatures + redeem script + num_of_witness_items = 1 + len(signatures) + 1 w = bytearray() write_varint(w, num_of_witness_items) write_varint(w, 0) # version 0 witness program - for s in other_signatures: + for s in signatures: append_signature(w, s, sighash) # size of the witness included - append_signature(w, current_signature, sighash) - redeem_script = script_multisig(multisig_get_pubkeys(multisig), multisig.m) write_varint(w, len(redeem_script)) diff --git a/src/apps/wallet/sign_tx/signing.py b/src/apps/wallet/sign_tx/signing.py index 5854916f0b..24b8a1768f 100644 --- a/src/apps/wallet/sign_tx/signing.py +++ b/src/apps/wallet/sign_tx/signing.py @@ -346,7 +346,9 @@ async def sign_tx(tx: SignTx, root): signature = ecdsa_sign(key_sign, bip143_hash) if txi.multisig: - witness = get_p2wsh_witness(txi.multisig, signature, txi.multisig.signatures, get_hash_type(coin)) + # place of our signature based on the pubkey + signature_index = multisig_pubkey_index(txi_sign.multisig, key_sign_pub) + witness = get_p2wsh_witness(txi.multisig, signature, signature_index, get_hash_type(coin)) else: witness = get_p2wpkh_witness(signature, key_sign_pub, get_hash_type(coin))