Skip to content

Commit 553eaf8

Browse files
authored
Merge pull request #389 from InjectiveLabs/cp-508/add_address_creation_from_eth
[CP-508] add address creation from eth
2 parents e9538ac + e8dd491 commit 553eaf8

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pyinjective/wallet.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ def from_cons_bech32(cls, bech: str) -> "Address":
232232
"""Create an address instance from a bech32-encoded consensus address"""
233233
return cls._from_bech32(bech, BECH32_ADDR_CONS_PREFIX)
234234

235+
@classmethod
236+
def from_eth_address(cls, eth_address: str) -> "Address":
237+
"""Create an address instance from a hex-encoded Ethereum address"""
238+
eth_address_without_prefix = eth_address[2:] if eth_address.startswith("0x") else eth_address
239+
bytes_representation = bytes.fromhex(eth_address_without_prefix)
240+
return cls(bytes_representation)
241+
235242
def _to_bech32(self, prefix: str) -> str:
236243
five_bit_r = convertbits(self.addr, 8, 5)
237244
assert five_bit_r is not None, "Unsuccessful bech32.convertbits call"

tests/test_wallet.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,19 @@ def test_convert_public_key_to_address(self):
2828
expected_address = Address(hashed_value[12:])
2929

3030
assert expected_address == address
31+
32+
33+
class TestAddress:
34+
def test_from_acc_bech32(self):
35+
address = Address.from_acc_bech32("inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r")
36+
assert address.to_acc_bech32() == "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
37+
38+
eth_address = address.get_ethereum_address()
39+
assert eth_address == "0xbdaedec95d563fb05240d6e01821008454c24c36"
40+
41+
def test_from_eth(self):
42+
address = Address.from_eth_address("0xbdaedec95d563fb05240d6e01821008454c24c36")
43+
assert address.to_acc_bech32() == "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
44+
45+
eth_address = address.get_ethereum_address()
46+
assert eth_address == "0xbdaedec95d563fb05240d6e01821008454c24c36"

0 commit comments

Comments
 (0)