From 918e33fee103c6f44e99cc2378b8a9d44af5a53a Mon Sep 17 00:00:00 2001 From: Perry Kundert Date: Tue, 8 Nov 2022 06:13:12 -0800 Subject: [PATCH] Add test of checksum_encode, using Keccak for ETH addr. upper/lower case --- GNUmakefile | 11 ++++++++++- hdwallet/libs/base58.py | 2 +- tests/test_base58.py | 9 ++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index adac4f8..73be7d4 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -3,12 +3,21 @@ PY3 ?= $(shell python3 --version >/dev/null 2>&1 && echo python3 || echo python VERSION = $(shell $(PY3) -c 'from hdwallet import __version__; print( __version__.strip("v"))') WHEEL = dist/hdwallet-$(VERSION)-py3-none-any.whl +PY3TEST = $(PY3) -m pytest + .PHONY: all test build build-check wheel install-dev install clean FORCE all: build test: - $(PY3) -m pytest + $(PY3TEST) + +# Run only tests with a prefix containing the target string, eg test-blah +test-%: + $(PY3TEST) *$*_test.py + +unit-%: + $(PY3TEST) -k $* build: clean wheel diff --git a/hdwallet/libs/base58.py b/hdwallet/libs/base58.py index 43006ec..cc94f13 100644 --- a/hdwallet/libs/base58.py +++ b/hdwallet/libs/base58.py @@ -13,7 +13,7 @@ def checksum_encode(address, crypto="eth"): out = "" - keccak_256 = keccak_pycryptodome.new(digest_bits=256) + keccak_256 = keccak.new(digest_bits=256) addr = address.lower().replace("0x", "") if crypto == "eth" else address.lower().replace("xdc", "") keccak_256.update(addr.encode("ascii")) hash_addr = keccak_256.hexdigest() diff --git a/tests/test_base58.py b/tests/test_base58.py index 6728319..4c696b5 100644 --- a/tests/test_base58.py +++ b/tests/test_base58.py @@ -7,7 +7,7 @@ import pytest from hdwallet.libs.base58 import ( - check_encode, check_decode, decode, encode, string_to_int + checksum_encode, check_encode, check_decode, decode, encode, string_to_int ) @@ -36,6 +36,13 @@ def test_base58(): assert encode(decode("111233QC4")) == "111233QC4" + # Ensure ETH address checksums are correct; these are Keccak hash of the lower-case hex address, + # with hash results mapped onto the upper/lower case bits of the address. + eth = "0xfc2077CA7F403cBECA41B1B0F62D91B5EA631B5E" + eth_lower = eth.lower() + eth_check = checksum_encode( eth_lower ) + assert eth_check == eth + def test_keccak(): """Keccak 256 hash is required by several crypto algorithms. Ensure our hash implementations