address: reject v2-v16 segwit addresses encoded with bech32 (BIP-350) - #2544
Merged
Merged
Conversation
Contributor
Author
|
Friendly ping — this is ready for review whenever a maintainer has bandwidth. The change enforces BIP-350 strictly (reject v2-v16 segwit addresses encoded with legacy bech32). Thanks! |
Contributor
Author
|
Friendly follow-up — this is still ready for review whenever a maintainer has bandwidth. It strictly enforces BIP-350 (rejecting v2–v16 segwit addresses encoded with bech32 instead of bech32m). Small, self-contained change, no conflicts. Thanks! |
Member
|
Ci run approved! |
BIP-350 requires that segregated witness outputs of version 1 through 16 use the bech32m checksum, while only version 0 uses bech32. decodeSegWitAddress only special-cased versions 0 and 1, so a witness program with version 2-16 encoded using the legacy bech32 checksum decoded successfully, in violation of the spec and contrary to the BIP-350 reference decoder (which rejects any non-zero witness version that is not bech32m). Generalize the version 1 check to cover all versions >= 1, matching the reference decode() function. Add the relevant BIP-350 INVALID_ADDRESS vectors (v2 and v16 encoded with bech32) as a regression test; they decoded successfully before this change and are now rejected. Signed-off-by: Lrifton92 <Lrifton92@users.noreply.github.com>
Lrifton92
force-pushed
the
fix/bech32-investigation
branch
from
July 21, 2026 00:25
a0ed479 to
27bbee9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@
Problem
decodeSegWitAddressis written as a full BIP-350 segwit decoder: it accepts witness versions 0 through 16 (rejectingversion > 16). BIP-350 requires that witness version 0 uses bech32, and versions 1 through 16 use bech32m. The reference decoder in the BIP rejects any non-zero witness version that is not bech32m:The current code only special-cased versions 0 and 1:
So a witness program with version 2-16 encoded using the legacy bech32 checksum decoded successfully, in violation of the spec and contrary to the BIP-350 reference decoder.
Reproduction
These are official
INVALID_ADDRESSvectors from BIP-350. Both decode successfully on current master and are rejected after this change:tb1z0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqglt7rf— witness v2 with bech32 (should require bech32m)BC1S0XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ54WELL— witness v16 with bech32Fix
Generalize the
version == 1check toversion >= 1, matching the BIP-350 referencedecode(). Added a regression test (address/bip350_diff_test.go) covering the v2 and v16 invalid vectors; it fails before this change and passes after.This aligns the internal reference decoder with BIP-350 for the full v0-16 range the function already claims to handle. Versions 2-16 are not yet surfaced through
DecodeAddresstoday, so this is primarily a spec-conformance / forward-compatibility fix rather than a behavior change for callers of the public API.Verification
go build ./...— cleango vet ./...— cleango test ./...—okfor theaddress,address/base58,address/bech32packagesgofmt -lon touched files — clean@