Skip to content

Instantly share code, notes, and snippets.

Created December 19, 2020 14:04
What would you like to do?
EIP-55 implementation
#!/usr/bin/env python
import unittest
from sha3 import keccak_256
def checksum_encoded(addr):
checksumed = ''
hashed = keccak_256(addr.encode('utf-8')).digest().hex()[:40]
for idx, character in enumerate(addr):
if character in "0123456789": # Decimal digits should be passed as they are
checksumed += character
to_check = int(hashed[idx], 16)
if to_check > 7:
checksumed += character.upper()
checksumed += character
return checksumed
def validate(addr, valid):
lowercased = addr.lower()
checksum = checksum_encoded(lowercased)
return checksum == valid
class TestChecksum(unittest.TestCase):
def test_checksum(self):
checksum = checksum_encoded('8da82fcc73cbf2fb1a8c627ec2f0e3fcc3f06748')
self.assertEqual('8DA82fcc73CBF2fb1a8c627EC2F0E3fCc3F06748', checksum)
def test_validate(self):
checksum = checksum_encoded('8da82fcc73cbf2fb1a8c627ec2f0e3fcc3f06748')
misread = '8DA82fcc73CBF2fb1a8c627EC2F0E3fCc3E06748'
valid = validate(misread, checksum)
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment