2019-01-28 14:44:18 +00:00
|
|
|
from ubinascii import unhexlify
|
|
|
|
|
|
|
|
from trezor import wire
|
|
|
|
|
2018-08-30 11:45:09 +00:00
|
|
|
|
2019-01-28 14:44:18 +00:00
|
|
|
def address_from_bytes(address_bytes: bytes, network=None) -> str:
|
|
|
|
"""
|
|
|
|
Converts address in bytes to a checksummed string as defined
|
|
|
|
in https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
|
|
|
|
"""
|
2018-08-30 11:45:09 +00:00
|
|
|
from ubinascii import hexlify
|
|
|
|
from trezor.crypto.hashlib import sha3_256
|
|
|
|
|
|
|
|
rskip60 = network is not None and network.rskip60
|
|
|
|
|
2019-01-28 14:44:18 +00:00
|
|
|
hx = hexlify(address_bytes).decode()
|
2018-08-30 11:45:09 +00:00
|
|
|
|
|
|
|
prefix = str(network.chain_id) + "0x" if rskip60 else ""
|
|
|
|
hs = sha3_256(prefix + hx, keccak=True).digest()
|
|
|
|
h = ""
|
|
|
|
|
|
|
|
for i in range(20):
|
|
|
|
l = hx[i * 2]
|
|
|
|
if hs[i] & 0x80 and l >= "a" and l <= "f":
|
|
|
|
l = l.upper()
|
|
|
|
h += l
|
|
|
|
l = hx[i * 2 + 1]
|
|
|
|
if hs[i] & 0x08 and l >= "a" and l <= "f":
|
|
|
|
l = l.upper()
|
|
|
|
h += l
|
|
|
|
|
|
|
|
return "0x" + h
|
2019-01-28 14:44:18 +00:00
|
|
|
|
|
|
|
|
2019-01-29 14:35:09 +00:00
|
|
|
def bytes_from_address(address: str) -> bytes:
|
2019-01-28 14:44:18 +00:00
|
|
|
if len(address) == 40:
|
|
|
|
return unhexlify(address)
|
|
|
|
|
|
|
|
elif len(address) == 42:
|
|
|
|
if address[0:2] not in ("0x", "0X"):
|
|
|
|
raise wire.ProcessError("Ethereum: invalid beginning of an address")
|
|
|
|
return unhexlify(address[2:])
|
|
|
|
|
2019-01-29 14:35:09 +00:00
|
|
|
elif len(address) == 0:
|
|
|
|
return bytes()
|
|
|
|
|
2019-01-28 14:44:18 +00:00
|
|
|
raise wire.ProcessError("Ethereum: Invalid address length")
|