Skip to content

create_tx accepts recipient addresses from the wrong network #360

In review

Description

@emma31-dev

Describe the bug

bdk-cli accepts recipient addresses and silent payment codes that belong to a different network than the wallet. The address network is discarded when it is converted to a ScriptBuf, so bdk-cli never compares it with the wallet network.

This is a silent-misdirection safety issue, not an unspendable-transaction bug: consensus validates the output scriptPubKey, not the address HRP, so a regtest node will accept a transaction paying to the witness program behind a tb1... address. The risk is sending funds to an address that was intended for another network.

Network compatibility is not 1:1:

  • testnet, testnet4, and signet share address prefixes and are treated as the same network by rust-bitcoin.
  • bitcoin is distinct.
  • regtest is distinct from the public test networks for bech32 addresses (legacy testnet base58 addresses may be accepted on regtest).
  • Silent payment codes use sp (mainnet), tsp (all test networks), and sprt (regtest); bdk_sp reports every tsp code as Network::Testnet.

Affected commands

All of these parse user-supplied addresses or codes without checking the wallet network:

  • create_tx --to (including create_tx --send_all --to)
  • create_sp_tx --to
  • create_sp_tx --to-sp (silent payment codes)
  • create_dns_tx --to
  • bump_fee --shrink
  • sign_message --address / verify_message --address (these parse with assume_checked, so they report “does not belong to this wallet” rather than a network mismatch)

To reproduce

The wallet needs a spendable UTXO; otherwise create_tx fails with Insufficient funds before the missing network check becomes visible.

1. Generate descriptors and save a regtest wallet config

DESC=$(bdk-cli --network regtest --datadir ./dd descriptor --type tr)
EXT=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["private_descriptors"]["external"])' <<<"$DESC")
INT=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["private_descriptors"]["internal"])' <<<"$DESC")

bdk-cli --network regtest --datadir ./dd wallet --wallet regtest_user1 config \
  --ext-descriptor "$EXT" \
  --int-descriptor "$INT" \
  --client-type rpc \
  --database-type sqlite \
  --url http://127.0.0.1:18443

2. Start a regtest node and fund the bdk-cli wallet

bitcoind -regtest -daemon -fallbackfee=0.0002 \
  -rpcuser=user -rpcpassword=password

bitcoin-cli -regtest -rpcuser=user -rpcpassword=password createwallet miner
MINER_ADDR=$(bitcoin-cli -regtest -rpcuser=user -rpcpassword=password \
  -rpcwallet=miner getnewaddress)
bitcoin-cli -regtest -rpcuser=user -rpcpassword=password \
  -rpcwallet=miner generatetoaddress 101 "$MINER_ADDR"

RECV=$(bdk-cli --network regtest --datadir ./dd wallet --wallet regtest_user1 \
  new_address | python3 -c 'import json,sys; print(json.load(sys.stdin)["address"])')
bitcoin-cli -regtest -rpcuser=user -rpcpassword=password \
  -rpcwallet=miner sendtoaddress "$RECV" 1
bitcoin-cli -regtest -rpcuser=user -rpcpassword=password \
  -rpcwallet=miner generatetoaddress 1 "$MINER_ADDR"

bdk-cli --network regtest --datadir ./dd wallet --wallet regtest_user1 full_scan

3. Try to pay a testnet address from the regtest wallet

bdk-cli --network regtest --datadir ./dd wallet --wallet regtest_user1 \
  create_tx --to tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx:1000

Actual on master: the command builds and prints a PSBT.
Expected: the command fails before building the PSBT:

ParseError: address tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx is not valid on regtest

4. Silent payment codes

Generate a testnet tsp code, then pass it to the regtest wallet:

SP_TESTNET=$(bdk-cli --network testnet --datadir ./dd silent_payment_code \
  --scan_key 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 \
  --spend_key 02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5 \
  | python3 -c 'import json,sys; print(json.load(sys.stdin)["message"])')

bdk-cli --network regtest --datadir ./dd wallet --wallet regtest_user1 \
  create_sp_tx --to-sp "$SP_TESTNET:1000"

Actual on master: the code is accepted and a PSBT is built.
Expected: the command fails with:

Silent payment code tsp1... is not valid for regtest

A tsp code must still be accepted by testnet, testnet4, and signet wallets.

5. bump_fee --shrink

Create and broadcast an RBF transaction, then bump it while shrinking to a testnet address:

bdk-cli --network regtest --datadir ./dd wallet --wallet regtest_user1 \
  bump_fee --txid <txid> --fee_rate 10.0 \
  --shrink tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx

Actual on master: a PSBT is built that pays the testnet address.
Expected: the command is rejected with the same is not valid on regtest error.

Expected behavior

  • create_tx, create_sp_tx, create_dns_tx, and bump_fee --shrink validate the recipient against the wallet network before building the PSBT.
  • Silent payment codes are matched by network family, not strict Network equality, because tsp is shared by testnet, testnet4, and signet.
  • sign_message and verify_message reject addresses from another network with a network-mismatch error.
  • Valid addresses on testnet/testnet4/signet remain valid across those networks.

Fix

Implemented in PR #370 (fix/360-create-tx-network-validation): recipients are carried as Address<NetworkUnchecked> and validated with require_network(wallet.network()) immediately before conversion to ScriptBuf, and silent payment codes are validated by network family.

Activity

  1. added
    bug
    on Oct 18, 2026
  2. moved this from Todo to In Progress in BDK-CLIon Oct 30, 2026
  3. tvpeter commented on Dec 3, 2026

    @tvpeter
    Collaborator

    Describe the bug

    create_tx accepts a recipient address belonging to a different network than the wallet. The transaction is built successfully, but the resulting PSBT is unspendable on the target network and only fails later at broadcast time with a node-side rejection. The cause is in parse_recipient: it calls Address::from_str (which yields Address<NetworkUnchecked>) and immediately converts to ScriptBuf via .script_pubkey(), discarding the network information before the wallet's network is known. bdk-cli therefore never compares the two.

    To Reproduce

    1. Create a regtest wallet:
      bdk-cli --network regtest wallet -w regtest_user1 --descriptor "..." --change_descriptor "..." create
    2. Attempt to send to a testnet address:
      bdk-cli --network regtest wallet -w regtest_user1 create_tx --to tb1qdgxnu...:1000
    3. Observe: the command succeeds and returns a PSBT.
    4. Broadcasting that PSBT against the regtest node fails with an address/script error, because tb1... does not belong to the regtest network.

    Expected behavior

    create_tx should reject the recipient at construction time with a specific error naming the mismatch, rather than producing a PSBT that can never be spent. BDKCliError::InvalidNetwork { requested, found } should be created for this purpose. The recipient should be carried through as Address<NetworkUnchecked> and validated with require_network(ctx.state.wallet.network()) inside execute, immediately before conversion to ScriptBuf.

    Build environment

    • BDK-CLI tag/commit: master
    • OS+version: macOS 12.7.6 (Monterey)
    • Rust/Cargo version: rustc 1.97.1
    • Rust/Cargo target: x86_64-apple-darwin

    Additional context

    The fix is confined to CreateTxCommand and parse_recipient; no changes to the wallet API are needed.

    Thank you for reporting this. While I can confirm that the bug exist, majority of the description and reproduction steps are wrong:

    • There is no create subcommand (error: unrecognized subcommand 'create'). Wallets are set up with wallet config.
    • --descriptor and --change_descriptor don’t exist. The flags are --ext-descriptor / -e and --int-descriptor / -i.
    • Broadcast would not fail, and the PSBT is not unspendable. Nodes validate output scripts, not addresses. A regtest node would accept a transaction paying to the witness program behind a tb1 address. So steps 3–4 and the “only fails later at broadcast” claim are wrong. The real problem is that funds go silently to a script the user meant for a different network, with no warning.
    • rust-bitcoin treats testnet, testnet4 and signet as one network for address checks.
  4. emma31-dev commented on Dec 3, 2026

    @emma31-dev
    ContributorAuthor

    @tvpeter Thanks for the detailed review!

    you're right on all counts, and I want to own the inaccuracies. I wrote the repro from memory and let AI fill in the template, which is how the wrong subcommand/flags and the broadcast claim slipped in. And i forgot to update it as scope of PR changed.

    • CLI / config: wallet -w ... --descriptor ... create doesn't exist. Setup is wallet --wallet <name> config --ext-descriptor <desc> --int-descriptor <desc>, and I misremembered --descriptor / --change_descriptor for --ext-descriptor / --int-descriptor. I should have verified against --help instead of trusting recall and then repeated the same mistake a few more times.
    • Validation & spendability: I just confirmed this. consensus validates the scriptPubKey, not the HRP, so the output is spendable and a regtest node accepts it. I re-ran it end-to-end against regtest: create_tx to tb1q… succeeds and the transaction broadcasts, since it pays the same witness program as the bcrt1… form. The real defect is the silent cross-network misdirection with no warning.
    • Network grouping: I agree. rust-bitcoin treats testnet, testnet4, and signet as one address network, so a tb1 recipient is "valid" on any of them; only mainnet/regtest boundaries are actually caught.
    • Also worth noting: the fix is broader than CreateTxCommand alone. The same unchecked pattern exists in create_sp_tx, create_dns_tx, and bump_fee --shrink, so the PR covers those too.

    I'll update the issue description and reproduction steps to reflect the exact CLI flags and actual behavior. Thanks again for the detailed review!

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

  • Status
    In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions