Implementation of EIP-55 checksum address. Adaptation of https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification for python 3.7+. Refined after: https://github.com/ethereum/eips/issues/55#issuecomment-261521584 Note: As of today (eth-utils==1.8.1), this is ~4-5 t
(address: AddressTypes)
| 42 | |
| 43 | @lru_cache(maxsize=5000) |
| 44 | def to_checksum_address(address: AddressTypes) -> ChecksumAddress: |
| 45 | """Implementation of EIP-55 checksum address. |
| 46 | |
| 47 | Adaptation of https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification for |
| 48 | python 3.7+. |
| 49 | |
| 50 | Refined after: https://github.com/ethereum/eips/issues/55#issuecomment-261521584 |
| 51 | |
| 52 | Note: As of today (eth-utils==1.8.1), this is ~4-5 times faster than |
| 53 | `eth_utils.to_checksum_address`. |
| 54 | """ |
| 55 | out = "" |
| 56 | v = int.from_bytes(keccak(bytes(address.hex(), "ascii")), byteorder="big") |
| 57 | for i, char in enumerate(address.hex()): |
| 58 | if char in "0123456789": |
| 59 | out += char |
| 60 | else: |
| 61 | out += char.upper() if (v & (2 ** (255 - 4 * i))) else char.lower() |
| 62 | return ChecksumAddress(AddressHex(HexStr("0x" + out))) |
| 63 | |
| 64 | |
| 65 | def pex(data: bytes) -> str: |