1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-21 12:51:03 +00:00

feat(core): add witness version to encode_bech32_address()

This commit is contained in:
Pavol Rusnak 2021-08-11 13:07:21 +01:00 committed by Andrew Kozlik
parent dbc464750b
commit ea0fb08fed
2 changed files with 8 additions and 7 deletions

View File

@ -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:

View File

@ -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)