test(encoding): use real addresses in test_address and cover the script-type edges

Open Beginner friendly
#104 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli, testing

Research direction

Read tests/test_address.py and tests/test_encoding.py, then inspect the referenced functions in btc_toolkit/encoding.py to understand the current behavior. Replace the synthetic address cases, add the specified edge-case assertions, and check that each test fails when its covered behavior is removed or changed. Done when the tests cover real decoding, script endings, and the Base58 length guard without changing files under btc_toolkit/.

Written by the indexing model from the issue text.

Description

good first issue

What

A few tests pass for the wrong reason, and a few guards have no test at all:

  • test_p2wsh and test_p2tr in tests/test_address.py use "bc1q" + "a" * 58 and "bc1p" + "a" * 58, which have invalid checksums. They pass through the prefix-and-length guess of detect_address_type, not through real decoding.
  • script_type (encoding.py L240-L246) recognizes P2PKH by its 88ac ending and P2SH by its 87 ending; nothing fails if either ending check is removed, although an address would then be derived from a script that is not P2PKH or P2SH.
  • The length guard in b58check_decode (L127-L128), which stops a huge input from being decoded, is never really checked: test_base58_edge_cases passes "1" * 120 but only asserts a ValueError, and without the guard the checksum check raises one anyway.

What to do

  1. In tests/test_address.py, replace the two synthetic addresses with real ones: a P2WSH and a P2TR address (e.g. bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3 from BIP 173 and bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0 from BIP 350), and keep a separate test for the fallback guess on a checksum-invalid string. The guess gives a 62-character bc1q/bc1p address the same type, so these tests cannot catch broken decoding on their own: also add a real address the guess gets wrong, such as bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs (witness_v2; the guess says unknown) or bc1pfeessrawgf (P2A).
  2. In tests/test_encoding.py, test that a 25-byte script with the P2PKH prefix but another ending, and a 23-byte script with the P2SH prefix but another last byte, are not P2PKH / P2SH.
  3. Test that a Base58 string longer than 100 characters (e.g. "1" * 101) is rejected with the message "too long" (match the message: any ValueError is not enough).

Acceptance

  • Each new or changed test fails if the code it covers is removed or changed (please say in the PR that you checked).
  • No change under btc_toolkit/.

See CONTRIBUTING.md for the test, lint and type-check commands. Comment below before starting, so two people don't take the same issue.

Dominant language
Python
Stars
2
Forks
1
Avg merge
1h 45m
Merged PRs (30d)
23

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from devdavidejesus/btc-toolkit

All issues in devdavidejesus/btc-toolkit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.