Merge pull request #44 from zcash-hackworks/43-ua-hrp-padding

Include HRP inside Unified Address padding bytes
This commit is contained in:
Daira Hopwood 2021-07-30 20:22:32 +01:00 committed by GitHub
commit ae90208ebb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 4 deletions

View File

@ -16,6 +16,10 @@ import orchard_key_components
def tlv(typecode, value):
return b"".join([bytes([typecode, len(value)]), value])
def padding(hrp):
assert(len(hrp) <= 16)
return bytes(hrp, "utf8") + bytes(16 - len(hrp))
def encode_unified(receivers):
orchard_receiver = b""
if receivers[0]:
@ -33,9 +37,11 @@ def encode_unified(receivers):
typecode = 0x01
t_receiver = tlv(typecode, receivers[2][1])
r_bytes = b"".join([orchard_receiver, sapling_receiver, t_receiver, bytes(16)])
hrp = "u"
r_bytes = b"".join([orchard_receiver, sapling_receiver, t_receiver, padding(hrp)])
converted = convertbits(f4jumble(r_bytes), 8, 5)
return bech32_encode("u", converted, Encoding.BECH32M)
return bech32_encode(hrp, converted, Encoding.BECH32M)
def decode_unified(addr_str):
(hrp, data, encoding) = bech32_decode(addr_str)
@ -43,8 +49,8 @@ def decode_unified(addr_str):
decoded = f4jumble_inv(bytes(convertbits(data, 5, 8, False)))
suffix = decoded[-16:]
# check trailing zero bytes
assert suffix == bytes(16)
# check trailing padding bytes
assert suffix == padding(hrp)
decoded = decoded[:-16]
s = 0