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

Commit e7f995a

Browse files
authored
Merge pull request #409 from ethereum/py3_cool_checksum
Py3 cool checksum
2 parents 586e53b + 94da783 commit e7f995a

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

ethereum/utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ def bytearray_to_bytestr(value):
6868
def mk_contract_address(sender, nonce):
6969
return sha3(rlp.encode([normalize_address(sender), nonce]))[12:]
7070

71+
7172
def mk_metropolis_contract_address(sender, initcode):
7273
return sha3(normalize_address(sender) + initcode)[12:]
7374

75+
7476
def safe_ord(value):
7577
if isinstance(value, int):
7678
return value
@@ -115,6 +117,7 @@ def int_to_32bytearray(i):
115117

116118
sha3_count = [0]
117119

120+
118121
def sha3(seed):
119122
sha3_count[0] += 1
120123
return sha3_256(to_string(seed))
@@ -136,25 +139,33 @@ def add_checksum(x):
136139
return x
137140
return x + sha3(x)[:4]
138141

142+
139143
def add_cool_checksum(addr):
140144
addr = normalize_address(addr)
141145
addr_hex = encode_hex(addr)
142146

143147
o = ''
144148
h = encode_hex(sha3(addr_hex))
149+
if not isinstance(addr_hex, str):
150+
# py3 bytes sequence
151+
addr_hex = list(chr(c) for c in addr_hex)
152+
h = list(chr(c) for c in h)
153+
145154
for i, c in enumerate(addr_hex):
146155
if c in '0123456789':
147156
o += c
148157
else:
149158
o += c.lower() if h[i] in '01234567' else c.upper()
150-
return '0x'+o
159+
return '0x' + o
160+
151161

152162
def check_and_strip_checksum(x):
153163
if len(x) in (40, 48):
154164
x = decode_hex(x)
155165
assert len(x) == 24 and sha3(x[:20])[:4] == x[-4:]
156166
return x[:20]
157167

168+
158169
def check_and_strip_cool_checksum(addr):
159170
assert add_cool_checksum(addr.lower()) == addr
160171
return normalize_address(addr)

0 commit comments

Comments
 (0)