From 622eb001a68f4eb6c8c1150c1aeb3dbdaea7d992 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 4 Jul 2018 17:54:13 +0200 Subject: [PATCH] src/apps/wallet/sign_tx: correct processing of block_height in bip115 --- src/apps/wallet/sign_tx/scripts.py | 15 ++++++--------- src/apps/wallet/sign_tx/writers.py | 24 +++++++++++++++++++++++- src/trezor/messages/TxInputType.py | 4 ++-- src/trezor/messages/TxOutputType.py | 4 ++-- vendor/trezor-common | 2 +- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/apps/wallet/sign_tx/scripts.py b/src/apps/wallet/sign_tx/scripts.py index 6e8a97da17..d2bf850272 100644 --- a/src/apps/wallet/sign_tx/scripts.py +++ b/src/apps/wallet/sign_tx/scripts.py @@ -42,21 +42,18 @@ def output_script_p2sh(scripthash: bytes) -> bytearray: return s -def script_replay_protection_bip115(block_hash: bytes, block_height: bytes) -> bytearray: +def script_replay_protection_bip115(block_hash: bytes, block_height: int) -> bytearray: if block_hash is None or block_height is None: return bytearray() - block_height_size = len(block_height) # size in bytes of block_height (should be 3) - s = bytearray(38) + assert len(block_hash) == 32 + s = bytearray(33) s[0] = 0x20 # 32 bytes for block hash s[1:33] = block_hash # block hash - s[33] = 0x03 # 3 bytes for block height - if block_height_size == 3: - s[34:37] = block_height - else: - s[34:37] = block_height[:3 - block_height_size] # must be only 3 bytes - s[37] = 0xB4 # OP_CHECKBLOCKHIGHT + write_scriptnum(s, block_height) + s.append(0xB4) # OP_CHECKBLOCKATHEIGHT return s + # SegWit: Native P2WPKH or P2WSH # === # https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh diff --git a/src/apps/wallet/sign_tx/writers.py b/src/apps/wallet/sign_tx/writers.py index fae6724cfd..634cbb1873 100644 --- a/src/apps/wallet/sign_tx/writers.py +++ b/src/apps/wallet/sign_tx/writers.py @@ -59,7 +59,7 @@ def write_varint(w, n: int): assert n >= 0 and n <= 0xFFFFFFFF if n < 253: w.append(n & 0xFF) - elif n < 65536: + elif n < 0x10000: w.append(253) w.append(n & 0xFF) w.append((n >> 8) & 0xFF) @@ -71,6 +71,28 @@ def write_varint(w, n: int): w.append((n >> 24) & 0xFF) +def write_scriptnum(w, n: int): + assert n >= 0 and n <= 0xFFFFFFFF + if n < 0x100: + w.append(1) + w.append(n & 0xFF) + elif n < 0x10000: + w.append(2) + w.append(n & 0xFF) + w.append((n >> 8) & 0xFF) + elif n < 0x1000000: + w.append(3) + w.append(n & 0xFF) + w.append((n >> 8) & 0xFF) + w.append((n >> 16) & 0xFF) + elif: + w.append(4) + w.append(n & 0xFF) + w.append((n >> 8) & 0xFF) + w.append((n >> 16) & 0xFF) + w.append((n >> 24) & 0xFF) + + def write_uint32(w, n: int): assert n >= 0 and n <= 0xFFFFFFFF w.append(n & 0xFF) diff --git a/src/trezor/messages/TxInputType.py b/src/trezor/messages/TxInputType.py index 88243a49fd..130d1be328 100644 --- a/src/trezor/messages/TxInputType.py +++ b/src/trezor/messages/TxInputType.py @@ -22,7 +22,7 @@ class TxInputType(p.MessageType): 9: ('decred_tree', p.UVarintType, 0), 10: ('decred_script_version', p.UVarintType, 0), 11: ('prev_block_hash_bip115', p.BytesType, 0), - 12: ('prev_block_height_bip115', p.BytesType, 0), + 12: ('prev_block_height_bip115', p.UVarintType, 0), } def __init__( @@ -38,7 +38,7 @@ class TxInputType(p.MessageType): decred_tree: int = None, decred_script_version: int = None, prev_block_hash_bip115: bytes = None, - prev_block_height_bip115: bytes = None, + prev_block_height_bip115: int = None, ) -> None: self.address_n = address_n if address_n is not None else [] self.prev_hash = prev_hash diff --git a/src/trezor/messages/TxOutputType.py b/src/trezor/messages/TxOutputType.py index 1c5d7abe45..cc3bd9d37b 100644 --- a/src/trezor/messages/TxOutputType.py +++ b/src/trezor/messages/TxOutputType.py @@ -19,7 +19,7 @@ class TxOutputType(p.MessageType): 6: ('op_return_data', p.BytesType, 0), 7: ('decred_script_version', p.UVarintType, 0), 8: ('block_hash_bip115', p.BytesType, 0), - 9: ('block_height_bip115', p.BytesType, 0), + 9: ('block_height_bip115', p.UVarintType, 0), } def __init__( @@ -32,7 +32,7 @@ class TxOutputType(p.MessageType): op_return_data: bytes = None, decred_script_version: int = None, block_hash_bip115: bytes = None, - block_height_bip115: bytes = None, + block_height_bip115: int = None, ) -> None: self.address = address self.address_n = address_n if address_n is not None else [] diff --git a/vendor/trezor-common b/vendor/trezor-common index 1b9b7d6c8e..5eae03131c 160000 --- a/vendor/trezor-common +++ b/vendor/trezor-common @@ -1 +1 @@ -Subproject commit 1b9b7d6c8ed44b24a7f105a83dc10ea7cc796ed9 +Subproject commit 5eae03131c89dabaa4caa8c5cb96742707206541