Skip to content

Commit

Permalink
Add test of checksum_encode, using Keccak for ETH addr. upper/lower case
Browse files Browse the repository at this point in the history
  • Loading branch information
pjkundert committed Nov 8, 2022
1 parent 15e32d4 commit 918e33f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
11 changes: 10 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion hdwallet/libs/base58.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion tests/test_base58.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 918e33f

Please sign in to comment.