Skip to content

[feature]: add WithChangeAddress TxCreateOption for custom change output destination#1235

Open
murraystewart96 wants to merge 6 commits into
btcsuite:masterfrom
murraystewart96:custom-change-addr-support
Open

[feature]: add WithChangeAddress TxCreateOption for custom change output destination#1235
murraystewart96 wants to merge 6 commits into
btcsuite:masterfrom
murraystewart96:custom-change-addr-support

Conversation

@murraystewart96

@murraystewart96 murraystewart96 commented May 17, 2026

Copy link
Copy Markdown

Change Description

Description:

This change extends txToOutputs and addrMgrWithChangeSource to accept an optional changeAddr btcutil.Address parameter. When non-nil, the change source is built as a static script paying to that address rather than deriving a fresh HD wallet address. When nil, the existing derivation path is taken unchanged — there is no behaviour change for existing callers.

Steps to Test

added new test in createtx_test.go - TestTxToOutputsCustomChangeAddr

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.

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

LGTM 🏷️ (deepseek-v4-pro)

Comment thread wallet/createtx.go
Comment thread wallet/createtx.go
Comment thread wallet/createtx.go
Comment thread wallet/psbt.go
Comment thread wallet/wallet.go

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

Thanks for the PR. I've started the agents to perform an initial review, meanwhile, I would suggest putting this PR on hold, as we are in the process of a major refactor/upgrade, the milesteon can be tracked here.

FundPsbt calls ScriptForOutput on the change output to attach BIP32
derivation metadata, which requires the address to be wallet-owned. An
external change address has no such record and would cause the call to
fail. Return a clear error if WithChangeAddress is set rather than
letting it fail deep inside the PSBT funding path.
…fication

Two fixes for external custom change addresses:

1. Validate that the change address belongs to the wallet's network
   before building the pkScript.

2. Skip the NotifyReceived call for external change addresses.
   Registering a non-wallet address with the chain backend causes future
   payments to that address to be delivered as wallet notifications.

Update TestTxToOutputsCustomChangeAddr to use a testnet address, which
now correctly exercises the network validation.
@murraystewart96

Copy link
Copy Markdown
Author

@yyforyongyu Thanks for reviewing. I have addressed the issues highlighted by the agents.
I'll keep an eye on the progress of the refactor and address any conflicts that emerge from it.

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.

2 participants