From a72fcfcb130865d9e24e2aac7bbb23d4ee3d6b8d Mon Sep 17 00:00:00 2001 From: Alex Barnsley <8069294+alexbarnsley@users.noreply.github.com> Date: Fri, 11 Apr 2025 01:14:07 +0100 Subject: [PATCH 1/2] refactor: validate address method --- crypto/identity/address.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crypto/identity/address.py b/crypto/identity/address.py index 5f4429f..ca0b739 100644 --- a/crypto/identity/address.py +++ b/crypto/identity/address.py @@ -1,5 +1,6 @@ import hashlib from binascii import unhexlify +import re from crypto.identity.private_key import PrivateKey @@ -82,3 +83,8 @@ def get_checksum_address(cls, address: str) -> str: chars[i + 1] = chars[i + 1].upper() return "0x" + ''.join(chars) + + @classmethod + def validate(cls, address: str) -> bool: + # Simple validation to check if the address starts with 0x and is 42 characters long + return re.search(r'^0x[a-fA-F0-9]{40}$', address) is not None From 09bbcd935b9794589d49c4ea59341c551310ce23 Mon Sep 17 00:00:00 2001 From: Alex Barnsley <8069294+alexbarnsley@users.noreply.github.com> Date: Fri, 11 Apr 2025 01:14:21 +0100 Subject: [PATCH 2/2] test --- tests/identity/test_address.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/identity/test_address.py b/tests/identity/test_address.py index 7e70678..e825198 100644 --- a/tests/identity/test_address.py +++ b/tests/identity/test_address.py @@ -13,3 +13,7 @@ def test_address_from_private_key(identity): def test_address_from_passphrase(identity): address = Address.from_passphrase(identity['passphrase']) assert address == identity['data']['address'] + +def test_it_should_validate_the_address(identity): + assert Address.validate(identity['data']['address']) is True + assert Address.validate(identity['data']['address'][2:]) is False