Skip to content

Commit 32a8475

Browse files
committed
Add test of checksum_encode, using Keccak for ETH addr. upper/lower case
1 parent fa874fd commit 32a8475

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

GNUmakefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ PY3 ?= $(shell python3 --version >/dev/null 2>&1 && echo python3 || echo python
33
VERSION = $(shell $(PY3) -c 'from hdwallet import __version__; print( __version__.strip("v"))')
44
WHEEL = dist/hdwallet-$(VERSION)-py3-none-any.whl
55

6+
PY3TEST = $(PY3) -m pytest
7+
68
.PHONY: all test build build-check wheel install-dev install clean FORCE
79

810
all: build
911

1012
test:
11-
$(PY3) -m pytest
13+
$(PY3TEST)
14+
15+
# Run only tests with a prefix containing the target string, eg test-blah
16+
test-%:
17+
$(PY3TEST) *$*_test.py
18+
19+
unit-%:
20+
$(PY3TEST) -k $*
1221

1322
build: clean wheel
1423

hdwallet/libs/base58.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
def checksum_encode(address, crypto="eth"):
1515
out = ""
16-
keccak_256 = keccak_pycryptodome.new(digest_bits=256)
16+
keccak_256 = keccak.new(digest_bits=256)
1717
addr = address.lower().replace("0x", "") if crypto == "eth" else address.lower().replace("xdc", "")
1818
keccak_256.update(addr.encode("ascii"))
1919
hash_addr = keccak_256.hexdigest()

tests/test_base58.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88

99
from hdwallet.libs.base58 import (
10-
check_encode, check_decode, decode, encode, string_to_int
10+
checksum_encode, check_encode, check_decode, decode, encode, string_to_int
1111
)
1212

1313

@@ -36,6 +36,13 @@ def test_base58():
3636

3737
assert encode(decode("111233QC4")) == "111233QC4"
3838

39+
# Ensure ETH address checksums are correct; these are Keccak hash of the lower-case hex address,
40+
# with hash results mapped onto the upper/lower case bits of the address.
41+
eth = "0xfc2077CA7F403cBECA41B1B0F62D91B5EA631B5E"
42+
eth_lower = eth.lower()
43+
eth_check = checksum_encode( eth_lower )
44+
assert eth_check == eth
45+
3946

4047
def test_keccak():
4148
"""Keccak 256 hash is required by several crypto algorithms. Ensure our hash implementations

0 commit comments

Comments
 (0)