Merge pull request #59 from daira/ua-item-order

Encode UAs/UVKs in ascending typecode order
This commit is contained in:
Daira Hopwood 2022-01-04 16:54:23 +00:00 committed by GitHub
commit 61894e7793
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -23,7 +23,7 @@ def encode_unified(rng, items, hrp):
has_p2pkh = False
has_p2sh = False
for item in items:
for item in sorted(items):
if item[1]:
if item[0] == P2PKH_ITEM:
has_p2pkh = True
@ -32,10 +32,9 @@ def encode_unified(rng, items, hrp):
assert (not (has_p2pkh and has_p2sh))
encoded_items.append(tlv(item[0], item[1]))
items_bytes = rng.sample(encoded_items, k=len(encoded_items))
items_bytes.append(padding(hrp))
encoded_items.append(padding(hrp))
r_bytes = b"".join(items_bytes)
r_bytes = b"".join(encoded_items)
converted = convertbits(f4jumble(r_bytes), 8, 5)
return bech32_encode(hrp, converted, Encoding.BECH32M)
@ -51,6 +50,7 @@ def decode_unified(encoded, expected_hrp, expected_lengths):
rest = decoded[:-16]
result = {}
prev_type = -1
while len(rest) > 0:
(item_type, rest) = parse_compact_size(rest)
(item_len, rest) = parse_compact_size(rest)
@ -78,5 +78,8 @@ def decode_unified(encoded, expected_hrp, expected_lengths):
assert not ('unknown' in result), "duplicate unknown item detected"
result['unknown'] = (item_type, item)
assert item_type > prev_type, "items out of order: typecodes %r and %r" % (prev_type, item_type)
prev_type = item_type
return result

View File

@ -87,8 +87,8 @@ def main():
ufvk = encode_unified(rng, receivers, "uview")
expected_lengths = {
P2PKH_ITEM: 65,
SAPLING_ITEM: 128,
P2PKH_ITEM: 65,
SAPLING_ITEM: 128,
ORCHARD_ITEM: 96,
unknown_tc: unknown_len
}