Skip to content

Reject altered leading zero bytes in BTC addresses - #540

Open
Plastic-time wants to merge 1 commit into
python-validators:masterfrom
Plastic-time:codex/btc-base58-leading-zeroes
Open

Plastic-time wants to merge 1 commit into
python-validators:masterfrom
Plastic-time:codex/btc-base58-leading-zeroes

Conversation

@Plastic-time

Copy link
Copy Markdown

Removing the initial 1 from the valid address 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2 currently leaves a string that btc_address() still accepts. Adding or removing a leading 1 from 1111111111111111111114oLvT2 is also accepted.

The Base58 decoder currently returns an integer, discarding leading zero bytes, and the caller then pads it to 25 bytes. Different input strings therefore become the same bytes before checksum verification. Each leading 1 must represent a zero byte, as in Bitcoin Core's Base58 decoder.

This change returns the decoded bytes with those leading zeroes preserved and requires exactly 25 bytes before checking the checksum. Tests reject all three altered addresses and retain valid P2PKH, P2SH, testnet and zero-payload cases.

Validation on Python 3.12.14:

  • Before the fix: 3 regression failures, 12 passing BTC tests.
  • After the fix: all 15 BTC tests pass; full suite: 1,521 passed.
  • Ruff lint and formatting, Pyright, Bandit and git diff --check: passed.

The Python 3.11, 3.13 and 3.14 test matrix was not run locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant