Skip to content

Commit 9aa4e6a

Browse files
committed
[Wallet] Add an option to keep the change address key, true by default
1 parent 9c9af5a commit 9aa4e6a

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,7 @@ UniValue fundrawtransaction(const JSONRPCRequest& request)
24962496
" \"changePosition\" (numeric, optional, default random) The index of the change output\n"
24972497
" \"includeWatching\" (boolean, optional, default false) Also select inputs which are watch only\n"
24982498
" \"lockUnspents\" (boolean, optional, default false) Lock selected unspent outputs\n"
2499+
" \"reserveChangeKey\" (boolean, optional, default true) Reserves the change output key from the keypool\n"
24992500
" \"feeRate\" (numeric, optional, default not set: makes wallet determine the fee) Set a specific feerate (" + CURRENCY_UNIT + " per KB)\n"
25002501
" \"subtractFeeFromOutputs\" (array, optional) A json array of integers.\n"
25012502
" The fee will be equally deducted from the amount of each specified output.\n"
@@ -2528,6 +2529,7 @@ UniValue fundrawtransaction(const JSONRPCRequest& request)
25282529
int changePosition = -1;
25292530
bool includeWatching = false;
25302531
bool lockUnspents = false;
2532+
bool reserveChangeKey = true;
25312533
CFeeRate feeRate = CFeeRate(0);
25322534
bool overrideEstimatedFeerate = false;
25332535
UniValue subtractFeeFromOutputs;
@@ -2549,6 +2551,7 @@ UniValue fundrawtransaction(const JSONRPCRequest& request)
25492551
{"changePosition", UniValueType(UniValue::VNUM)},
25502552
{"includeWatching", UniValueType(UniValue::VBOOL)},
25512553
{"lockUnspents", UniValueType(UniValue::VBOOL)},
2554+
{"reserveChangeKey", UniValueType(UniValue::VBOOL)},
25522555
{"feeRate", UniValueType()}, // will be checked below
25532556
{"subtractFeeFromOutputs", UniValueType(UniValue::VARR)},
25542557
},
@@ -2572,6 +2575,9 @@ UniValue fundrawtransaction(const JSONRPCRequest& request)
25722575
if (options.exists("lockUnspents"))
25732576
lockUnspents = options["lockUnspents"].get_bool();
25742577

2578+
if (options.exists("reserveChangeKey"))
2579+
reserveChangeKey = options["reserveChangeKey"].get_bool();
2580+
25752581
if (options.exists("feeRate"))
25762582
{
25772583
feeRate = CFeeRate(AmountFromValue(options["feeRate"]));
@@ -2608,7 +2614,7 @@ UniValue fundrawtransaction(const JSONRPCRequest& request)
26082614
CAmount nFeeOut;
26092615
string strFailReason;
26102616

2611-
if(!pwalletMain->FundTransaction(tx, nFeeOut, overrideEstimatedFeerate, feeRate, changePosition, strFailReason, includeWatching, lockUnspents, setSubtractFeeFromOutputs, changeAddress))
2617+
if(!pwalletMain->FundTransaction(tx, nFeeOut, overrideEstimatedFeerate, feeRate, changePosition, strFailReason, includeWatching, lockUnspents, setSubtractFeeFromOutputs, reserveChangeKey, changeAddress))
26122618
throw JSONRPCError(RPC_INTERNAL_ERROR, strFailReason);
26132619

26142620
UniValue result(UniValue::VOBJ);

src/wallet/wallet.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,7 @@ bool CWallet::SelectCoins(const vector<COutput>& vAvailableCoins, const CAmount&
21922192
return res;
21932193
}
21942194

2195-
bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool overrideEstimatedFeeRate, const CFeeRate& specificFeeRate, int& nChangePosInOut, std::string& strFailReason, bool includeWatching, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, const CTxDestination& destChange)
2195+
bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool overrideEstimatedFeeRate, const CFeeRate& specificFeeRate, int& nChangePosInOut, std::string& strFailReason, bool includeWatching, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, bool keepReserveKey, const CTxDestination& destChange)
21962196
{
21972197
vector<CRecipient> vecSend;
21982198

@@ -2241,6 +2241,10 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool ov
22412241
}
22422242
}
22432243

2244+
// optionally keep the change output key
2245+
if (keepReserveKey)
2246+
reservekey.KeepKey();
2247+
22442248
return true;
22452249
}
22462250

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
780780
* Insert additional inputs into the transaction by
781781
* calling CreateTransaction();
782782
*/
783-
bool FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool overrideEstimatedFeeRate, const CFeeRate& specificFeeRate, int& nChangePosInOut, std::string& strFailReason, bool includeWatching, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, const CTxDestination& destChange = CNoDestination());
783+
bool FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool overrideEstimatedFeeRate, const CFeeRate& specificFeeRate, int& nChangePosInOut, std::string& strFailReason, bool includeWatching, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, bool keepReserveKey = true, const CTxDestination& destChange = CNoDestination());
784784

785785
/**
786786
* Create a new transaction paying the recipients with a set of coins

0 commit comments

Comments
 (0)