This repository was archived by the owner on May 23, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change @@ -136,13 +136,29 @@ def add_checksum(x):
136
136
return x
137
137
return x + sha3 (x )[:4 ]
138
138
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
139
151
140
152
def check_and_strip_checksum (x ):
141
153
if len (x ) in (40 , 48 ):
142
154
x = decode_hex (x )
143
155
assert len (x ) == 24 and sha3 (x [:20 ])[:4 ] == x [- 4 :]
144
156
return x [:20 ]
145
157
158
+ def check_and_strip_cool_checksum (addr ):
159
+ assert add_cool_checksum (addr .lower ()) == addr
160
+ return normalize_address (addr )
161
+
146
162
147
163
def normalize_address (x , allow_blank = False ):
148
164
if is_numeric (x ):
You can’t perform that action at this time.
0 commit comments