Skip to content

Commit

Permalink
Match recipients with txouts by scriptPubKey in reassignAmounts() (#2548
Browse files Browse the repository at this point in the history
)

* Match recipients with txouts by scriptPubKey in reassignAmounts()

* Drop no longer used nChangePosRet from reassignAmounts() params
  • Loading branch information
UdjinM6 authored and codablock committed Dec 13, 2018
1 parent 378dadd commit ca0aec2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
fCreated = wallet->CreateTransaction(vecSend, *newTx, *keyChange, nFeeRequired, nChangePosRet, strFailReason, coinControl, true, recipients[0].inputType, recipients[0].fUseInstantSend);
transaction.setTransactionFee(nFeeRequired);
if (fSubtractFeeFromAmount && fCreated)
transaction.reassignAmounts(nChangePosRet);
transaction.reassignAmounts();

nValueOut = newTx->tx->GetValueOut();
nVinSize = newTx->tx->vin.size();
Expand Down
27 changes: 17 additions & 10 deletions src/qt/walletmodeltransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void WalletModelTransaction::setTransactionFee(const CAmount& newFee)
fee = newFee;
}

void WalletModelTransaction::reassignAmounts(int nChangePosRet)
void WalletModelTransaction::reassignAmounts()
{
int i = 0;
// For each recipient look for a matching CTxOut in walletTransaction and reassign amounts
for (QList<SendCoinsRecipient>::iterator it = recipients.begin(); it != recipients.end(); ++it)
{
SendCoinsRecipient& rcp = (*it);
Expand All @@ -61,19 +61,26 @@ void WalletModelTransaction::reassignAmounts(int nChangePosRet)
{
const payments::Output& out = details.outputs(j);
if (out.amount() <= 0) continue;
if (i == nChangePosRet)
i++;
subtotal += walletTransaction->tx->vout[i].nValue;
i++;
const unsigned char* scriptStr = (const unsigned char*)out.script().data();
CScript scriptPubKey(scriptStr, scriptStr+out.script().size());
for (const auto& txout : walletTransaction->tx->vout) {
if (txout.scriptPubKey == scriptPubKey) {
subtotal += txout.nValue;
break;
}
}
}
rcp.amount = subtotal;
}
else // normal recipient (no payment request)
{
if (i == nChangePosRet)
i++;
rcp.amount = walletTransaction->tx->vout[i].nValue;
i++;
for (const auto& txout : walletTransaction->tx->vout) {
CScript scriptPubKey = GetScriptForDestination(CBitcoinAddress(rcp.address.toStdString()).Get());
if (txout.scriptPubKey == scriptPubKey) {
rcp.amount = txout.nValue;
break;
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletmodeltransaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class WalletModelTransaction
void newPossibleKeyChange(CWallet *wallet);
CReserveKey *getPossibleKeyChange();

void reassignAmounts(int nChangePosRet); // needed for the subtract-fee-from-amount feature
void reassignAmounts(); // needed for the subtract-fee-from-amount feature

private:
QList<SendCoinsRecipient> recipients;
Expand Down

0 comments on commit ca0aec2

Please sign in to comment.