From e7f230d66ed4c78edff73bef8d2b0d6508f440d7 Mon Sep 17 00:00:00 2001 From: Andrew Kozlik Date: Mon, 25 May 2020 15:39:10 +0200 Subject: [PATCH] core/sign_tx: Use varint length encoding for witness stack items. --- core/src/apps/bitcoin/scripts.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/core/src/apps/bitcoin/scripts.py b/core/src/apps/bitcoin/scripts.py index 0fbe736075..022a72a283 100644 --- a/core/src/apps/bitcoin/scripts.py +++ b/core/src/apps/bitcoin/scripts.py @@ -16,7 +16,12 @@ from .multisig import ( multisig_get_pubkeys, multisig_pubkey_index, ) -from .writers import write_bytes_fixed, write_bytes_unchecked, write_op_push +from .writers import ( + write_bytes_fixed, + write_bytes_prefixed, + write_bytes_unchecked, + write_op_push, +) if False: from typing import List, Optional @@ -245,8 +250,8 @@ def input_script_p2wsh_in_p2sh(script_hash: bytes) -> bytearray: def witness_p2wpkh(signature: bytes, pubkey: bytes, sighash: int) -> bytearray: w = empty_bytearray(1 + 5 + len(signature) + 1 + 5 + len(pubkey)) write_bitcoin_varint(w, 0x02) # num of segwit items, in P2WPKH it's always 2 - append_signature(w, signature, sighash) - append_pubkey(w, pubkey) + write_signature_prefixed(w, signature, sighash) + write_bytes_prefixed(w, pubkey) return w @@ -290,7 +295,7 @@ def witness_p2wsh( write_bitcoin_varint(w, 0) for s in signatures: - append_signature(w, s, sighash) # size of the witness included + write_signature_prefixed(w, s, sighash) # size of the witness included # redeem script write_bitcoin_varint(w, redeem_script_length) @@ -389,6 +394,12 @@ def output_script_paytoopreturn(data: bytes) -> bytearray: # === +def write_signature_prefixed(w: Writer, signature: bytes, sighash: int) -> None: + write_bitcoin_varint(w, len(signature) + 1) + write_bytes_unchecked(w, signature) + w.append(sighash) + + def append_signature(w: Writer, signature: bytes, sighash: int) -> None: write_op_push(w, len(signature) + 1) write_bytes_unchecked(w, signature)