Skip to content

Commit 0c5b22d

Browse files
authored
Implement tests for address generation functionality
Add tests for address generation helpers in libcrypto.
1 parent 337ca11 commit 0c5b22d

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tests/test_addresses.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Tests for address generation helpers."""
2+
from __future__ import annotations
3+
4+
import pytest
5+
6+
from libcrypto.addresses import AddressError, AddressGenerator
7+
from libcrypto.keys import PrivateKey
8+
9+
10+
def test_bitcoin_address_variants_known_values(deterministic_private_key_hex: str) -> None:
11+
"""P2PKH, P2SH-P2WPKH, and P2WPKH should match published vectors for key = 1."""
12+
private_key = PrivateKey(deterministic_private_key_hex)
13+
public_key = private_key.get_public_key(compressed=True)
14+
15+
p2pkh = AddressGenerator.from_public_key(public_key.bytes, "p2pkh", "bitcoin")
16+
p2sh = AddressGenerator.from_public_key(public_key.bytes, "p2sh-p2wpkh", "bitcoin")
17+
p2wpkh = AddressGenerator.from_public_key(public_key.bytes, "p2wpkh", "bitcoin")
18+
19+
assert p2pkh == "1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH"
20+
assert p2sh == "3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN"
21+
assert p2wpkh == "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"
22+
23+
24+
def test_account_based_addresses_known_values(deterministic_private_key_hex: str) -> None:
25+
"""Ethereum and Tron address derivation should match deterministic constants."""
26+
private_key = PrivateKey(deterministic_private_key_hex)
27+
uncompressed_public = private_key.get_public_key(compressed=False)
28+
29+
ethereum = AddressGenerator.from_public_key(uncompressed_public.bytes, "default", "ethereum")
30+
tron = AddressGenerator.from_public_key(uncompressed_public.bytes, "default", "tron")
31+
32+
assert ethereum == "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf"
33+
assert tron == "TMVQGm1qAQYVdetCeGRRkTWYYrLXuHK2HC"
34+
35+
36+
def test_ripple_address_requires_compressed_key(deterministic_private_key_hex: str) -> None:
37+
"""Ripple address generation should work with compressed key and match known value."""
38+
private_key = PrivateKey(deterministic_private_key_hex)
39+
compressed = private_key.get_public_key(compressed=True)
40+
41+
ripple = AddressGenerator.from_public_key(compressed.bytes, "default", "ripple")
42+
43+
assert ripple == "rBgGZ9tc4him9KBzD8fKFiQz3fSZpaSwMH"
44+
45+
46+
def test_address_generator_rejects_unknown_type(deterministic_private_key_hex: str) -> None:
47+
"""Unsupported address types must raise AddressError."""
48+
private_key = PrivateKey(deterministic_private_key_hex)
49+
public_key = private_key.get_public_key(compressed=True)
50+
51+
with pytest.raises(AddressError):
52+
AddressGenerator.from_public_key(public_key.bytes, "p2wsh", "bitcoin")
53+
54+
55+
def test_address_generator_not_implemented_curve(deterministic_private_key_hex: str) -> None:
56+
"""Networks using Ed25519 should raise NotImplementedError for secp256k1 keys."""
57+
private_key = PrivateKey(deterministic_private_key_hex)
58+
public_key = private_key.get_public_key(compressed=True)
59+
60+
with pytest.raises(NotImplementedError):
61+
AddressGenerator.from_public_key(public_key.bytes, "default", "solana")

0 commit comments

Comments
 (0)