From af07c526d3ee9adf2d56f9c1d943a9d79a6a4fd5 Mon Sep 17 00:00:00 2001 From: Sai Raj <39055732+sairajzero@users.noreply.github.com> Date: Sat, 11 Aug 2018 04:19:50 +0530 Subject: [PATCH] added SendChangeToBack feature SendChangeToBack feature sends the change back to the address of the sender's coin. To enable SendChangeToBack feature, add SendChangeToBack=1 in flo.conf To enable FIFO CoinControl feature, add CoinControlFIFO=1 in flo.conf --- src/wallet/wallet.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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;