File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -232,6 +232,13 @@ def from_cons_bech32(cls, bech: str) -> "Address":
232
232
"""Create an address instance from a bech32-encoded consensus address"""
233
233
return cls ._from_bech32 (bech , BECH32_ADDR_CONS_PREFIX )
234
234
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
+
235
242
def _to_bech32 (self , prefix : str ) -> str :
236
243
five_bit_r = convertbits (self .addr , 8 , 5 )
237
244
assert five_bit_r is not None , "Unsuccessful bech32.convertbits call"
Original file line number Diff line number Diff line change @@ -28,3 +28,19 @@ def test_convert_public_key_to_address(self):
28
28
expected_address = Address (hashed_value [12 :])
29
29
30
30
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"
You can’t perform that action at this time.
0 commit comments