diff --git a/bip44/utils.py b/bip44/utils.py index 684472c..3ebbb51 100644 --- a/bip44/utils.py +++ b/bip44/utils.py @@ -1,3 +1,5 @@ +from typing import Union + from coincurve import PublicKey from sha3 import keccak_256 as _keccak_256 @@ -11,8 +13,12 @@ def keccak_256(b: bytes) -> bytes: return h.digest() -def get_eth_addr(pk: bytes) -> str: +def get_eth_addr(pk: Union[str, bytes]) -> str: """Get ETH address from a public key.""" - if len(pk) != 64: - pk = PublicKey(pk).format(False)[1:] - return f"0x{keccak_256(pk)[-20:].hex()}" + + pk_bytes = bytes.fromhex(pk) if isinstance(pk, str) else pk + + if len(pk_bytes) != 64: + pk_bytes = PublicKey(pk_bytes).format(False)[1:] + + return f"0x{keccak_256(pk_bytes)[-20:].hex()}" diff --git a/poetry.lock b/poetry.lock index adbdce5..b90aaf3 100644 --- a/poetry.lock +++ b/poetry.lock @@ -541,11 +541,11 @@ version = "3.7.4.2" [[package]] category = "dev" -description = "Measures number of Terminal column cells of wide-character codes" +description = "Measures the displayed width of unicode strings in a terminal" name = "wcwidth" optional = false python-versions = "*" -version = "0.1.9" +version = "0.2.2" [[package]] category = "dev" @@ -908,8 +908,8 @@ typing-extensions = [ {file = "typing_extensions-3.7.4.2.tar.gz", hash = "sha256:79ee589a3caca649a9bfd2a8de4709837400dfa00b6cc81962a1e6a1815969ae"}, ] wcwidth = [ - {file = "wcwidth-0.1.9-py2.py3-none-any.whl", hash = "sha256:cafe2186b3c009a04067022ce1dcd79cb38d8d65ee4f4791b8888d6599d1bbe1"}, - {file = "wcwidth-0.1.9.tar.gz", hash = "sha256:ee73862862a156bf77ff92b09034fc4825dd3af9cf81bc5b360668d425f3c5f1"}, + {file = "wcwidth-0.2.2-py2.py3-none-any.whl", hash = "sha256:b651b6b081476420e4e9ae61239ac4c1b49d0c5ace42b2e81dc2ff49ed50c566"}, + {file = "wcwidth-0.2.2.tar.gz", hash = "sha256:3de2e41158cb650b91f9654cbf9a3e053cee0719c9df4ddc11e4b568669e9829"}, ] zipp = [ {file = "zipp-3.1.0-py3-none-any.whl", hash = "sha256:aa36550ff0c0b7ef7fa639055d797116ee891440eac1a56f378e2d3179e0320b"}, diff --git a/tests/test_bip44.py b/tests/test_bip44.py index 2562126..ff5b0a6 100644 --- a/tests/test_bip44.py +++ b/tests/test_bip44.py @@ -7,6 +7,12 @@ MNEMONIC_JA = "なみだ むろん しひょう こうつう はかい たいうん さほう ことり げんき おでかけ ひこく ざんしょ" +def test_utils(): + pk = "02d9ed78008e7b6c4bdc2beea13230fb3ccb8072728c0986894a3d544485e9b727" + assert get_eth_addr(pk) == get_eth_addr(bytes.fromhex(pk)) + assert get_eth_addr(pk) == "0x7aD23D6eD9a1D98E240988BED0d78e8C81Ec296C".lower() + + def test_eth_wallet(): w = Wallet(MNEMONIC)