from ubinascii import unhexlify from trezor import wire 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 """ from ubinascii import hexlify from trezor.crypto.hashlib import sha3_256 rskip60 = network is not None and network.rskip60 hx = hexlify(address_bytes).decode() 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 def bytes_from_address(address: str) -> bytes: 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:]) elif len(address) == 0: return bytes() raise wire.ProcessError("Ethereum: Invalid address length")