Skip to content

Reject recipient addresses from the wrong network - #370

Open
emma31-dev wants to merge 1 commit into
bitcoindevkit:masterfrom
emma31-dev:fix/360-create-tx-network-validation
Open

emma31-dev wants to merge 1 commit into
bitcoindevkit:masterfrom
emma31-dev:fix/360-create-tx-network-validation

Conversation

@emma31-dev

@emma31-dev emma31-dev commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #360.

Recipient addresses are kept as Address<NetworkUnchecked> until the wallet network is known, then validated with Address::require_network before being converted to scripts. This now covers every path that builds an output from a user-supplied address or silent payment code:

  • create_tx --to (including --send_all)
  • create_sp_tx --to and --to-sp
  • create_dns_tx --to
  • bump_fee --shrink

--to-sp codes are matched by network family, so tsp codes are accepted on testnet, testnet4 and signet. sign_message / verify_message addresses are also validated against the wallet network.

Notes to the reviewers

Address matching. Validation uses Address::require_network instead of trying to name "the" network an address belongs to. An address can be valid for several networks (tb1... is valid on testnet, testnet4 and signet, and base58 testnet addresses are valid on regtest), so a first-match lookup always reported testnet and could never return testnet4/signet.

Silent payment codes. bdk_sp parses every tsp code as Network::Testnet because the prefix is shared by testnet, testnet4 and signet. Comparing sp_code.network == wallet.network() therefore rejected valid codes on signet and testnet4. sp_code_valid_for_network matches by family instead:

  • Bitcoin <-> sp
  • Testnet | Testnet4 | Signet <-> tsp
  • Regtest <-> sprt

Only the HRP distinguishes these codes; bdk_sp's derive_sp does not read the network field, so the keys and payment derivation are identical across the test networks.

bump_fee --shrink. This path still parsed with parse_address and assume_checked, so it could reach drain_to unchecked and build the same unspendable PSBT as create_tx --send_all. It now holds an Address<NetworkUnchecked> and goes through recipient_script. Clap parses the type directly, so the bespoke parse_unchecked_address value parser was dropped.

Message signing. parse_address now takes the wallet network and uses require_network, so sign_message / verify_message reject addresses from another network instead of silently calling assume_checked. It remains gated to the message_signer feature, its only consumer.

Errors. Address mismatches now use BDKCliError::ParseError, which wraps bitcoin::address::ParseError. rust-bitcoin's Display for that error only says validation error and keeps the address/network detail in its source, so BDKCliError's ParseError display now includes the source. Users see, for example, ParseError: address tb1... is not valid on regtest rather than a generic validation error.

Tests. Added unit coverage for address and silent payment network matching, and integration coverage for --send_all (drain_to), signet tsp codes, wrong-network addresses across create_tx, create_sp_tx, create_dns_tx, bump_fee --shrink and sign_message. The bump_fee case extends the existing RBF test with a wrong-network --shrink that must fail before the successful bump. Existing fixtures use regtest addresses, and the testnet SP-code fixture now uses the sandbox datadir instead of creating ~/.bdk-bitcoin.

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt
  • I ran cargo clippy successfully before committing
  • I ran the unit, offline and relevant online integration tests

Bugfixes:

@github-project-automation github-project-automation Bot moved this to Todo in BDK-CLI Oct 30, 2026
@emma31-dev
emma31-dev force-pushed the fix/360-create-tx-network-validation branch from 9c0cef4 to eecb1d4 Compare November 9, 2026 21:53
@emma31-dev
emma31-dev marked this pull request as draft November 13, 2026 12:23
@emma31-dev
emma31-dev marked this pull request as ready for review November 13, 2026 15:04
@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.90164% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.38%. Comparing base (537fbb9) to head (caa67e4).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/handlers/offline.rs 86.66% 4 Missing ⚠️
src/error.rs 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #370      +/-   ##
==========================================
+ Coverage   59.42%   60.38%   +0.96%     
==========================================
  Files          24       24              
  Lines        4498     4602     +104     
==========================================
+ Hits         2673     2779     +106     
+ Misses       1825     1823       -2     
Flag Coverage Δ
rust 60.38% <95.90%> (+0.96%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@emma31-dev
emma31-dev force-pushed the fix/360-create-tx-network-validation branch from 7aee766 to 1cc97ae Compare November 14, 2026 15:41
@emma31-dev

Copy link
Copy Markdown
Contributor Author

@vadim-anfv @tvpeter This pr is ready for your review when you have time

@tvpeter tvpeter added this to the CLI 4.1.0 milestone Nov 18, 2026
@tvpeter tvpeter moved this from Todo to Ready to Review in BDK-CLI Nov 18, 2026
@emma31-dev
emma31-dev force-pushed the fix/360-create-tx-network-validation branch 3 times, most recently from 4192823 to 903b805 Compare November 25, 2026 23:02

@vadim-anfv vadim-anfv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approach ACK, and thanks for covering all three commands rather than only create_tx.

Two things look wrong to me. The silent payment check compares networks for equality, which I think rejects valid codes on signet and testnet4. And bump_fee --shrink still reaches drain_to unchecked, so the same unspendable PSBT is still possible. The other comments are inline.

Comment thread CHANGELOG.md Resolved
Comment thread src/utils/common.rs Resolved
Comment thread tests/integration/offline.rs Resolved
Comment thread src/handlers/offline.rs Resolved
Comment thread src/handlers/offline.rs Resolved
Comment thread tests/integration/offline.rs Resolved
Comment thread tests/integration/offline.rs Resolved
@emma31-dev
emma31-dev force-pushed the fix/360-create-tx-network-validation branch from 903b805 to d123082 Compare November 29, 2026 13:10

@vadim-anfv vadim-anfv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ACK d123082; successfully ran local tests

@vadim-anfv
vadim-anfv requested a review from tvpeter November 30, 2026 11:45

@yanfsb yanfsb 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.

tACK d123082

Tested on regtest with Bitcoin Core 32.1.0. Normal bcrt1 spend confirmed, wrong-network recipients were rejected across create_tx (including --send_all), create_dns_tx, create_sp_tx, and bump_fee --shrink, and a tsp code was accepted on signet and testnet4 wallets.

Small changelog nit: I'd drop "instead of building an unspendable PSBT". On base (5ea0eea), testmempoolaccept returned allowed: true for signed regtest txs built from tb1 and bc1 addresses since the HRP isn't part of the scriptPubKey.

@tvpeter tvpeter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for working on this.

I have left some comments.

Comment thread src/utils/common.rs Outdated
Comment on lines +84 to +89
/// Parse an address without checking it against a network.
pub(crate) fn parse_unchecked_address(
address_str: &str,
) -> Result<Address<NetworkUnchecked>, Error> {
Ok(Address::from_str(address_str)?)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This helper function is not needed. Clap automatically parses the value through FromStr once value_parser is not provided. So this function adds no unique value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will remove. Thanks!

Comment thread src/utils/common.rs Outdated
Comment on lines +97 to +100
return Err(Error::Generic(format!(
"Address {} is not valid for {network}",
address.assume_checked_ref()
)));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Instead of returning Generic error variant, you can use the ParseError variant which is accurate here.

Comment thread src/handlers/offline.rs Outdated
/// Allows the wallet to reduce the amount to the specified address in order to increase fees.
#[arg(env = "SHRINK_ADDRESS", long = "shrink", value_parser = parse_address)]
pub shrink_address: Option<Address>,
#[arg(env = "SHRINK_ADDRESS", long = "shrink", value_parser = parse_unchecked_address)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
#[arg(env = "SHRINK_ADDRESS", long = "shrink", value_parser = parse_unchecked_address)]
#[arg(env = "SHRINK_ADDRESS", long = "shrink")]

Comment thread src/utils/common.rs
}

/// Parse an address string into `Address<NetworkChecked>`.
#[cfg(feature = "message_signer")]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since the rest of the codebase will be validating addresses against the network, I think the message_signer feature should also be doing same.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

True. Will add.

Comment thread src/handlers/offline.rs
#[arg(env = "SHRINK_ADDRESS", long = "shrink", value_parser = parse_address)]
pub shrink_address: Option<Address>,
#[arg(env = "SHRINK_ADDRESS", long = "shrink", value_parser = parse_unchecked_address)]
pub shrink_address: Option<Address<NetworkUnchecked>>,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You can also add a test case for bump_fee too. I think there is a test gap

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will include too. thanks!

Validate recipient addresses and silent payment codes against the wallet's network before building a transaction.

- Carry recipients as Address<NetworkUnchecked> and convert them with
  recipient_script, which requires the wallet's network.
- Apply the same check to create_sp_tx, create_dns_tx and bump_fee --shrink.
- Validate sign_message/verify_message addresses against the wallet network.
- Include the underlying cause in address parse errors so network mismatches
  name the offending address and network.
@emma31-dev
emma31-dev force-pushed the fix/360-create-tx-network-validation branch from d123082 to caa67e4 Compare December 3, 2026 20:29

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

Status: Ready to Review

Development

Successfully merging this pull request may close these issues.

create_tx accepts recipient addresses from the wrong network

4 participants