Created
December 19, 2020 14:04
-
-
Save volonbolon/11d13af2a5d168bc12cd4bc0ad0066d0 to your computer and use it in GitHub Desktop.
EIP-55 implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
else: | |
to_check = int(hashed[idx], 16) | |
if to_check > 7: | |
checksumed += character.upper() | |
else: | |
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) | |
self.assertFalse(valid) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment