From ea0fb08fed603a431fefb1a6d5ddb789b9f3e4a7 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 11 Aug 2021 13:07:21 +0100 Subject: [PATCH] feat(core): add witness version to encode_bech32_address() --- core/src/apps/bitcoin/addresses.py | 4 ++-- core/src/apps/bitcoin/common.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/core/src/apps/bitcoin/addresses.py b/core/src/apps/bitcoin/addresses.py index c03e4eb271..b3f09f719c 100644 --- a/core/src/apps/bitcoin/addresses.py +++ b/core/src/apps/bitcoin/addresses.py @@ -123,11 +123,11 @@ def address_p2wsh_in_p2sh(witness_script_hash: bytes, coin: CoinInfo) -> str: def address_p2wpkh(pubkey: bytes, coin: CoinInfo) -> str: assert coin.bech32_prefix is not None pubkeyhash = ecdsa_hash_pubkey(pubkey, coin) - return encode_bech32_address(coin.bech32_prefix, pubkeyhash) + return encode_bech32_address(coin.bech32_prefix, 0, pubkeyhash) def address_p2wsh(witness_script_hash: bytes, hrp: str) -> str: - return encode_bech32_address(hrp, witness_script_hash) + return encode_bech32_address(hrp, 0, witness_script_hash) def address_to_cashaddr(address: str, coin: CoinInfo) -> str: diff --git a/core/src/apps/bitcoin/common.py b/core/src/apps/bitcoin/common.py index 8a8a529497..d0a934e0dc 100644 --- a/core/src/apps/bitcoin/common.py +++ b/core/src/apps/bitcoin/common.py @@ -19,8 +19,8 @@ SIGHASH_ALL = const(0x01) # The number of bip32 levels used in a wallet (chain and address) BIP32_WALLET_DEPTH = const(2) -# supported witness version for bech32 addresses -_BECH32_WITVER = const(0x00) +# supported witness versions for bech32 addresses +_BECH32_WITVERS = (0, 1) MULTISIG_INPUT_SCRIPT_TYPES = ( InputScriptType.SPENDMULTISIG, @@ -76,8 +76,9 @@ def ecdsa_hash_pubkey(pubkey: bytes, coin: CoinInfo) -> bytes: return coin.script_hash(pubkey).digest() -def encode_bech32_address(prefix: str, script: bytes) -> str: - address = bech32.encode(prefix, _BECH32_WITVER, script) +def encode_bech32_address(prefix: str, witver: int, script: bytes) -> str: + assert witver in _BECH32_WITVERS + address = bech32.encode(prefix, witver, script) if address is None: raise wire.ProcessError("Invalid address") return address @@ -85,7 +86,7 @@ def encode_bech32_address(prefix: str, script: bytes) -> str: def decode_bech32_address(prefix: str, address: str) -> bytes: witver, raw = bech32.decode(prefix, address) - if witver != _BECH32_WITVER: + if witver not in _BECH32_WITVERS: raise wire.ProcessError("Invalid address witness program") assert raw is not None return bytes(raw)