diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 4afc7fe69..bdffcd86a 100755 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2744,10 +2744,11 @@ bool CWallet::CreateTransaction(const std::vector& vecSend, CWalletT // TODO: pass in scriptChange instead of reservekey so // change transaction isn't always pay-to-bitcoin-address CScript scriptChange; - + bool coinControlDestChange=false; // coin control: send change to custom address if (!boost::get(&coin_control.destChange)) { scriptChange = GetScriptForDestination(coin_control.destChange); + coinControlDestChange=true; } else { // no coin control: send change to newly generated address // Note: We use a new key here to keep it from being obvious which side is the change. // The drawback is that by not reusing a previous key, the change may be lost if a @@ -2818,7 +2819,6 @@ bool CWallet::CreateTransaction(const std::vector& vecSend, CWalletT } txNew.vout.push_back(txout); } - // Choose coins to use if (pick_new_inputs) { nValueIn = 0; @@ -2829,6 +2829,23 @@ bool CWallet::CreateTransaction(const std::vector& vecSend, CWalletT return false; } } + // Send Change Back to Same (sending) Address + if(!coinControlDestChange && gArgs.GetBoolArg("-SendChangeToBack", false)){ + CTxDestination destChange; + CAmount maxVal=0; + bool avail = false; + for (const CInputCoin &coin : setCoins ){ + if(!avail || coin.txout.nValue > maxVal){ + avail = (ExtractDestination(coin.txout.scriptPubKey, destChange) || avail); + maxVal = coin.txout.nValue; + } + } + if(avail){ + scriptChange = GetScriptForDestination(destChange); + CTxOut change_prototype_txout(0, scriptChange); + size_t change_prototype_size = GetSerializeSize(change_prototype_txout, SER_DISK, 0); + } + } const CAmount nChange = nValueIn - nValueToSelect;