Skip to content

Commit 5a1eb0b

Browse files
authored
Add BIP32 HD wallet regression tests
Added regression tests for BIP32 HD wallet functionality, including master serialization, child derivation, and error handling for invalid segments.
1 parent 0c5b22d commit 5a1eb0b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/test_bip32.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""BIP32 HD wallet regression tests."""
2+
from __future__ import annotations
3+
4+
import pytest
5+
6+
from libcrypto.bip32 import BIP32Error, HDWallet
7+
8+
9+
SEED = bytes.fromhex("000102030405060708090a0b0c0d0e0f")
10+
MASTER_XPRV = (
11+
"xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVv"
12+
"vNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"
13+
)
14+
CHILD_XPRV = (
15+
"xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53K"
16+
"w1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs"
17+
)
18+
19+
20+
def test_master_serialization_matches_reference() -> None:
21+
"""Master node serialization should match the official BIP32 test vector."""
22+
wallet = HDWallet(SEED)
23+
assert wallet.master_node.serialize_private() == MASTER_XPRV
24+
25+
26+
def test_child_derivation_matches_reference() -> None:
27+
"""Deriving m/0'/1 should match the corresponding reference xprv."""
28+
wallet = HDWallet(SEED)
29+
child = wallet.derive_from_path("m/0'/1")
30+
31+
assert child.serialize_private() == CHILD_XPRV
32+
33+
34+
def test_derivation_rejects_invalid_segment() -> None:
35+
"""Invalid derivation strings must raise BIP32Error."""
36+
wallet = HDWallet(SEED)
37+
38+
with pytest.raises(BIP32Error):
39+
wallet.derive_from_path("m/44'/0'/abc")

0 commit comments

Comments
 (0)