mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-21 21:00:58 +00:00
feat(core): add witness version to encode_bech32_address()
This commit is contained in:
parent
dbc464750b
commit
ea0fb08fed
@ -123,11 +123,11 @@ def address_p2wsh_in_p2sh(witness_script_hash: bytes, coin: CoinInfo) -> str:
|
|||||||
def address_p2wpkh(pubkey: bytes, coin: CoinInfo) -> str:
|
def address_p2wpkh(pubkey: bytes, coin: CoinInfo) -> str:
|
||||||
assert coin.bech32_prefix is not None
|
assert coin.bech32_prefix is not None
|
||||||
pubkeyhash = ecdsa_hash_pubkey(pubkey, coin)
|
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:
|
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:
|
def address_to_cashaddr(address: str, coin: CoinInfo) -> str:
|
||||||
|
@ -19,8 +19,8 @@ SIGHASH_ALL = const(0x01)
|
|||||||
# The number of bip32 levels used in a wallet (chain and address)
|
# The number of bip32 levels used in a wallet (chain and address)
|
||||||
BIP32_WALLET_DEPTH = const(2)
|
BIP32_WALLET_DEPTH = const(2)
|
||||||
|
|
||||||
# supported witness version for bech32 addresses
|
# supported witness versions for bech32 addresses
|
||||||
_BECH32_WITVER = const(0x00)
|
_BECH32_WITVERS = (0, 1)
|
||||||
|
|
||||||
MULTISIG_INPUT_SCRIPT_TYPES = (
|
MULTISIG_INPUT_SCRIPT_TYPES = (
|
||||||
InputScriptType.SPENDMULTISIG,
|
InputScriptType.SPENDMULTISIG,
|
||||||
@ -76,8 +76,9 @@ def ecdsa_hash_pubkey(pubkey: bytes, coin: CoinInfo) -> bytes:
|
|||||||
return coin.script_hash(pubkey).digest()
|
return coin.script_hash(pubkey).digest()
|
||||||
|
|
||||||
|
|
||||||
def encode_bech32_address(prefix: str, script: bytes) -> str:
|
def encode_bech32_address(prefix: str, witver: int, script: bytes) -> str:
|
||||||
address = bech32.encode(prefix, _BECH32_WITVER, script)
|
assert witver in _BECH32_WITVERS
|
||||||
|
address = bech32.encode(prefix, witver, script)
|
||||||
if address is None:
|
if address is None:
|
||||||
raise wire.ProcessError("Invalid address")
|
raise wire.ProcessError("Invalid address")
|
||||||
return 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:
|
def decode_bech32_address(prefix: str, address: str) -> bytes:
|
||||||
witver, raw = bech32.decode(prefix, address)
|
witver, raw = bech32.decode(prefix, address)
|
||||||
if witver != _BECH32_WITVER:
|
if witver not in _BECH32_WITVERS:
|
||||||
raise wire.ProcessError("Invalid address witness program")
|
raise wire.ProcessError("Invalid address witness program")
|
||||||
assert raw is not None
|
assert raw is not None
|
||||||
return bytes(raw)
|
return bytes(raw)
|
||||||
|
Loading…
Reference in New Issue
Block a user