From 2849c1f5518b9d6149cf4de46af654006222e0b8 Mon Sep 17 00:00:00 2001 From: fametrano Date: Fri, 3 Feb 2023 13:13:06 +0100 Subject: [PATCH 1/3] speeded up BIP32 public derivation by caching --- btclib/bip32/bip32.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/btclib/bip32/bip32.py b/btclib/bip32/bip32.py index 560c350a..4dfefb5e 100644 --- a/btclib/bip32/bip32.py +++ b/btclib/bip32/bip32.py @@ -34,6 +34,7 @@ from __future__ import annotations import copy +import functools import hmac from dataclasses import dataclass from typing import Union @@ -308,6 +309,9 @@ def __init__( if check_validity: self.assert_valid() + def __tuple(self) -> tuple[bytes, int, bytes, bytes]: + return (self.parent_fingerprint, self.index, self.chain_code, self.key) + def __prv_key_derivation(xkey: _BIP32KeyData, index: int) -> None: xkey.index = index From 0f1f09a2a1dacf132153853d93d6e87b8ee2e850 Mon Sep 17 00:00:00 2001 From: fametrano Date: Sun, 5 Feb 2023 21:08:43 +0100 Subject: [PATCH 2/3] removed useless code --- btclib/bip32/bip32.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/btclib/bip32/bip32.py b/btclib/bip32/bip32.py index 4dfefb5e..560c350a 100644 --- a/btclib/bip32/bip32.py +++ b/btclib/bip32/bip32.py @@ -34,7 +34,6 @@ from __future__ import annotations import copy -import functools import hmac from dataclasses import dataclass from typing import Union @@ -309,9 +308,6 @@ def __init__( if check_validity: self.assert_valid() - def __tuple(self) -> tuple[bytes, int, bytes, bytes]: - return (self.parent_fingerprint, self.index, self.chain_code, self.key) - def __prv_key_derivation(xkey: _BIP32KeyData, index: int) -> None: xkey.index = index From 8f6c5703e94040cb0d1a683c52120c1046f0f7e5 Mon Sep 17 00:00:00 2001 From: fametrano Date: Sun, 5 Feb 2023 21:22:05 +0100 Subject: [PATCH 3/3] improved test --- btclib/bip32/bip32.py | 8 +++++--- tests/bip32/test_bip32.py | 10 +++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/btclib/bip32/bip32.py b/btclib/bip32/bip32.py index 560c350a..7fb0544a 100644 --- a/btclib/bip32/bip32.py +++ b/btclib/bip32/bip32.py @@ -429,14 +429,16 @@ def _derive_from_account( if branch >= 0x80000000: raise BTClibValueError("invalid private derivation at branch level") if branch > max_index: - raise BTClibValueError(f"too high branch: {branch}") + err_msg = f"invalid branch number: {branch} is higher than {max_index}." + raise BTClibValueError(err_msg) if branches_0_1_only and branch not in (0, 1): - raise BTClibValueError(f"invalid branch: {branch} not in (0, 1)") + raise BTClibValueError(f"invalid branch number: {branch} not in (0, 1)") if address_index >= 0x80000000: raise BTClibValueError("invalid private derivation at address index level") if address_index > max_index: - raise BTClibValueError(f"too high address index: {address_index}") + err_msg = f"invalid address index: {address_index} is higher than {max_index}." + raise BTClibValueError(err_msg) return _derive(mxkey, f"m/{branch}/{address_index}") diff --git a/tests/bip32/test_bip32.py b/tests/bip32/test_bip32.py index 02263637..99754c2a 100644 --- a/tests/bip32/test_bip32.py +++ b/tests/bip32/test_bip32.py @@ -296,19 +296,19 @@ def test_derive_from_account() -> None: with pytest.raises(BTClibValueError, match=err_msg): derive_from_account(mxpub, 0x80000000, 0, True) - err_msg = "too high branch: " + err_msg = "invalid branch number: 65536" with pytest.raises(BTClibValueError, match=err_msg): - derive_from_account(mxpub, 0xFFFF + 1, 0) + derive_from_account(mxpub, 0xFFFF + 1, 0, branches_0_1_only=False) - err_msg = "invalid branch: 2" + err_msg = "invalid branch number: 2" with pytest.raises(BTClibValueError, match=err_msg): derive_from_account(mxpub, 2, 0) err_msg = "invalid private derivation at address index level" with pytest.raises(BTClibValueError, match=err_msg): - derive_from_account(mxpub, 0, 0x80000000) + derive_from_account(mxpub, 0, 0x80000000, max_index=0xFFFFFFFF) - err_msg = "too high address index: 65536" + err_msg = "invalid address index: 65536" with pytest.raises(BTClibValueError, match=err_msg): derive_from_account(mxpub, 0, 0xFFFF + 1)