Skip to content

Commit

Permalink
#125 Update Aptos address encoding
Browse files Browse the repository at this point in the history
Add parameter to choose whether to trim zeroes from Aptos addresses (by default zeroes are not trimmed)
  • Loading branch information
ebellocchia committed Apr 2, 2024
1 parent 822b2d2 commit 1e0e1dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 7 additions & 3 deletions bip_utils/addr/aptos_addr.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ def EncodeKey(pub_key: Union[bytes, IPublicKey],
Args:
pub_key (bytes or IPublicKey): Public key bytes or object
**kwargs : Not used
Other Parameters:
trim_zeroes (bool, optional): True to trim left zeroes from the address string, false otherwise (default)
Returns:
str: Address string
Expand All @@ -100,12 +102,14 @@ def EncodeKey(pub_key: Union[bytes, IPublicKey],
ValueError: If the public key is not valid
TypeError: If the public key is not ed25519
"""
trim_zeroes = kwargs.get("trim_zeroes", False)

pub_key_obj = AddrKeyValidator.ValidateAndGetEd25519Key(pub_key)

payload_bytes = pub_key_obj.RawCompressed().ToBytes()[1:] + AptosAddrConst.SINGLE_SIG_SUFFIX_BYTE
key_hash_bytes = Sha3_256.QuickDigest(payload_bytes)
key_hash_str = BytesUtils.ToHexString(Sha3_256.QuickDigest(payload_bytes))

return CoinsConf.Aptos.ParamByKey("addr_prefix") + BytesUtils.ToHexString(key_hash_bytes).lstrip("0")
return CoinsConf.Aptos.ParamByKey("addr_prefix") + (key_hash_str.lstrip("0") if trim_zeroes else key_hash_str)


# Deprecated: only for compatibility, Encoder class shall be used instead
Expand Down
12 changes: 10 additions & 2 deletions tests/addr/test_aptos_addr.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"pub_key": b"0065091c6b34cf557caa07ed9d65887e59a07fd75f4670dd9baf7920c520da3804",
"address_dec": b"03aa1ceed625af8407d25e5894a165272b5d196dcdb8dc66456d2ac039bcf198",
"address_params": {},
"address": "0x3aa1ceed625af8407d25e5894a165272b5d196dcdb8dc66456d2ac039bcf198",
"address": "0x03aa1ceed625af8407d25e5894a165272b5d196dcdb8dc66456d2ac039bcf198",
},
{
"pub_key": b"0018c3ffaf12ab774572e875b24316991b673c20297d52951970ed7b853e48ce44",
Expand All @@ -49,12 +49,20 @@
"pub_key": b"1d86e698067245ec022bc3405bfeeeb9a0444388bc1a325486ff2ae0a39df61b",
"address_dec": b"0e8d7c29f28f4d72da95d1d8784d53c4240768ec916cc7ce60ef3c80d6509f07",
"address_params": {},
"address": "0xe8d7c29f28f4d72da95d1d8784d53c4240768ec916cc7ce60ef3c80d6509f07",
"address": "0x0e8d7c29f28f4d72da95d1d8784d53c4240768ec916cc7ce60ef3c80d6509f07",
},
{
"pub_key": b"e2db08dab9943141b6456d39c4506be647de9eedd912192dfd60bc9c9277cffe",
"address_dec": b"071e1d7330597e99001aad667d6deea321e95771d6975dd66bfb185f5cfd7d58",
"address_params": {},
"address": "0x071e1d7330597e99001aad667d6deea321e95771d6975dd66bfb185f5cfd7d58",
},
{
"pub_key": b"e2db08dab9943141b6456d39c4506be647de9eedd912192dfd60bc9c9277cffe",
"address_dec": b"071e1d7330597e99001aad667d6deea321e95771d6975dd66bfb185f5cfd7d58",
"address_params": {
"trim_zeroes": True,
},
"address": "0x71e1d7330597e99001aad667d6deea321e95771d6975dd66bfb185f5cfd7d58",
},
]
Expand Down
2 changes: 1 addition & 1 deletion tests/bip/bip44/test_bip44.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"0x1e3357e64ae4dfb82e5cac3ee77aee1648e526a2ab84476bda8bf9e1c4f10231",
"0x87428b0462b8174f951c4e11cdeb780656dab808b989cb0ee62c9bac423dabbb",
"0xb49e3dd5212cfed1762c09001e2a8b4aab6c4cda2956e60e13b826deddb1e4a4",
"0x598979bade27730c0a3de3ee38d644c7a85040209929c40fcf72527a50c2d9",
"0x00598979bade27730c0a3de3ee38d644c7a85040209929c40fcf72527a50c2d9",
],
},
# Arbitrum
Expand Down

0 comments on commit 1e0e1dc

Please sign in to comment.