Repository navigation
Reject recipient addresses from the wrong network - #370
emma31-dev wants to merge 1 commit into
Conversation
9c0cef4 to
eecb1d4
Compare
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
7aee766 to
1cc97ae
Compare
|
@vadim-anfv @tvpeter This pr is ready for your review when you have time |
4192823 to
903b805
Compare
vadim-anfv
left a comment
There was a problem hiding this comment.
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.
903b805 to
d123082
Compare
vadim-anfv
left a comment
There was a problem hiding this comment.
ACK d123082; successfully ran local tests
yanfsb
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Thank you for working on this.
I have left some comments.
| /// 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)?) | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Will remove. Thanks!
| return Err(Error::Generic(format!( | ||
| "Address {} is not valid for {network}", | ||
| address.assume_checked_ref() | ||
| ))); |
There was a problem hiding this comment.
Instead of returning Generic error variant, you can use the ParseError variant which is accurate here.
| /// 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)] |
There was a problem hiding this comment.
| #[arg(env = "SHRINK_ADDRESS", long = "shrink", value_parser = parse_unchecked_address)] | |
| #[arg(env = "SHRINK_ADDRESS", long = "shrink")] |
| } | ||
|
|
||
| /// Parse an address string into `Address<NetworkChecked>`. | ||
| #[cfg(feature = "message_signer")] |
There was a problem hiding this comment.
Since the rest of the codebase will be validating addresses against the network, I think the message_signer feature should also be doing same.
| #[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>>, |
There was a problem hiding this comment.
You can also add a test case for bump_fee too. I think there is a test gap
There was a problem hiding this comment.
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.
d123082 to
caa67e4
Compare
Description
Fixes #360.
Recipient addresses are kept as
Address<NetworkUnchecked>until the wallet network is known, then validated withAddress::require_networkbefore 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 --toand--to-spcreate_dns_tx --tobump_fee --shrink--to-spcodes are matched by network family, sotspcodes are accepted on testnet, testnet4 and signet.sign_message/verify_messageaddresses are also validated against the wallet network.Notes to the reviewers
Address matching. Validation uses
Address::require_networkinstead 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 reportedtestnetand could never return testnet4/signet.Silent payment codes.
bdk_spparses everytspcode asNetwork::Testnetbecause the prefix is shared by testnet, testnet4 and signet. Comparingsp_code.network == wallet.network()therefore rejected valid codes on signet and testnet4.sp_code_valid_for_networkmatches by family instead:Bitcoin<->spTestnet | Testnet4 | Signet<->tspRegtest<->sprtOnly the HRP distinguishes these codes;
bdk_sp'sderive_spdoes not read thenetworkfield, so the keys and payment derivation are identical across the test networks.bump_fee --shrink. This path still parsed withparse_addressandassume_checked, so it could reachdrain_tounchecked and build the same unspendable PSBT ascreate_tx --send_all. It now holds anAddress<NetworkUnchecked>and goes throughrecipient_script. Clap parses the type directly, so the bespokeparse_unchecked_addressvalue parser was dropped.Message signing.
parse_addressnow takes the wallet network and usesrequire_network, sosign_message/verify_messagereject addresses from another network instead of silently callingassume_checked. It remains gated to themessage_signerfeature, its only consumer.Errors. Address mismatches now use
BDKCliError::ParseError, which wrapsbitcoin::address::ParseError.rust-bitcoin'sDisplayfor that error only saysvalidation errorand keeps the address/network detail in its source, soBDKCliError'sParseErrordisplay now includes the source. Users see, for example,ParseError: address tb1... is not valid on regtestrather than a genericvalidation error.Tests. Added unit coverage for address and silent payment network matching, and integration coverage for
--send_all(drain_to), signettspcodes, wrong-network addresses acrosscreate_tx,create_sp_tx,create_dns_tx,bump_fee --shrinkandsign_message. Thebump_feecase extends the existing RBF test with a wrong-network--shrinkthat 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:
cargo fmtcargo clippysuccessfully before committingBugfixes: