Skip to content

address: reject witness v1 20-byte programs in DecodeAddress - #2685

Open
acxxcxx wants to merge 1 commit into
btcsuite:masterfrom
acxxcxx:fix/address-v1-20byte
Open

acxxcxx wants to merge 1 commit into
btcsuite:masterfrom
acxxcxx:fix/address-v1-20byte

Conversation

@acxxcxx

@acxxcxx acxxcxx commented Nov 17, 2026 •

Copy link
Copy Markdown

Change Description

In DecodeAddress, case 20 previously returned an AddressWitnessPubKeyHash
without checking witnessVer == 0. Because decodeSegWitAddress accepts
Bech32m witness v1+ programs and DecodeAddress allows witnessVer == 0 || 1,
a Bech32m-encoded witness v1 20-byte program silently decoded as a witness v0
P2WPKH address.

This PR:

  • Restricts case 20 in DecodeAddress to witnessVer == 0 and returns
    UnsupportedWitnessProgLenError(len(witnessProg)) for non-v0 20-byte
    witness programs (aligned with BIP-341 / BIP-350).
  • Adds TestDecodeAddressRejectsWitnessV1TwentyByteProgram in
    address/regression_test.go → address/address_test.go covering both the positive v0 control and the
    rejected v1 20-byte regression case.

Steps to Test

cd address && go test -v -count=1 ./...

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

@AmanBoronbaev AmanBoronbaev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version check in case 20 fixes the loss of the witness version. Could the regression test assert require.ErrorIs(t, err, address.UnsupportedWitnessProgLenError(20))? With require.Error, a regression that rejects every v1 address during checksum decoding would also pass this case.

The comment above the v1 case could say “this library has no address type for a v1 20-byte program.” A 20-byte program is within the 2–40 byte range for v1 in the BIP 350 reference decoder, even though it does not represent a Taproot output. That distinction matters if DecodeAddress gains support for unknown witness programs later.

@acxxcxx
acxxcxx force-pushed the fix/address-v1-20byte branch from 245869d to 38c7fc6 Compare November 18, 2026 01:46
@acxxcxx

acxxcxx commented Nov 18, 2026

Copy link
Copy Markdown
Author

Thank you for the review, @AmanBoronbaev!

Updated address/regression_test.go in commit 38c7fc6f:

  • Replaced require.Error with require.ErrorIs(t, err, address.UnsupportedWitnessProgLenError(20)) to assert the specific expected error.
  • Clarified the comment above the v1 test to explain that this library does not currently define an address type for a v1 20-byte witness program.

I also reran the address package tests locally, and all tests passed.

@AmanBoronbaev AmanBoronbaev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed on 38c7fc6. The v0 control still returns an AddressWitnessPubKeyHash, and the v1 case now checks UnsupportedWitnessProgLenError(20) and a nil address. I ran go test -race -count=1 ./... and go vet ./... in the address module. Both passed.

@allocz allocz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, as we should not encode p2wpkh programs with bech32m.

Comment thread address/regression_test.go Resolved
In DecodeAddress, case 20 previously returned an AddressWitnessPubKeyHash
without checking witnessVer == 0, causing a Bech32m-encoded witness v1
20-byte program to silently downgrade to a v0 P2WPKH address.

Reject non-v0 20-byte witness programs with UnsupportedWitnessProgLenError
and add a regression test.
@acxxcxx
acxxcxx force-pushed the fix/address-v1-20byte branch from 38c7fc6 to f56a445 Compare November 19, 2026 01:34
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.

3 participants