Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 586e53b

Browse files
author
Jan Xie
committed
cool address checksum EIP55
1 parent 8d69d80 commit 586e53b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ethereum/tests/test_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
from ethereum import utils
3+
4+
def test_add_cool_checksum():
5+
addresses = [
6+
# All caps
7+
'0x52908400098527886E0F7030069857D2E4169EE7',
8+
'0x8617E340B3D01FA5F11F306F4090FD50E238070D',
9+
# All Lower
10+
'0xde709f2102306220921060314715629080e2fb77',
11+
'0x27b1fdb04752bbc536007a920d24acb045561c26',
12+
# Normal
13+
'0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
14+
'0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
15+
'0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
16+
'0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
17+
]
18+
19+
for addr in addresses:
20+
assert utils.add_cool_checksum(addr.lower()) == addr
21+
assert utils.check_and_strip_cool_checksum(addr) == utils.normalize_address(addr)

ethereum/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,29 @@ def add_checksum(x):
136136
return x
137137
return x + sha3(x)[:4]
138138

139+
def add_cool_checksum(addr):
140+
addr = normalize_address(addr)
141+
addr_hex = encode_hex(addr)
142+
143+
o = ''
144+
h = encode_hex(sha3(addr_hex))
145+
for i, c in enumerate(addr_hex):
146+
if c in '0123456789':
147+
o += c
148+
else:
149+
o += c.lower() if h[i] in '01234567' else c.upper()
150+
return '0x'+o
139151

140152
def check_and_strip_checksum(x):
141153
if len(x) in (40, 48):
142154
x = decode_hex(x)
143155
assert len(x) == 24 and sha3(x[:20])[:4] == x[-4:]
144156
return x[:20]
145157

158+
def check_and_strip_cool_checksum(addr):
159+
assert add_cool_checksum(addr.lower()) == addr
160+
return normalize_address(addr)
161+
146162

147163
def normalize_address(x, allow_blank=False):
148164
if is_numeric(x):

0 commit comments

Comments
 (0)