Skip to content

Commit

Permalink
improved test
Browse files Browse the repository at this point in the history
  • Loading branch information
fametrano committed Feb 5, 2023
1 parent 0f1f09a commit 8f6c570
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 5 additions & 3 deletions btclib/bip32/bip32.py
Expand Up @@ -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}")

Expand Down
10 changes: 5 additions & 5 deletions tests/bip32/test_bip32.py
Expand Up @@ -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)

Expand Down

0 comments on commit 8f6c570

Please sign in to comment.