Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/qt/sendcoinsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ bool SendCoinsDialog::PrepareSendText(QString& question_string, QString& informa
}

// prepare transaction for getting txFee earlier
// Create unsigned transaction to support creating unsigned PSBTs.
// Signing is deferred until the user clicks "Send".
m_current_transaction = std::make_unique<WalletModelTransaction>(recipients);
WalletModel::SendCoinsReturn prepareStatus;

Expand Down Expand Up @@ -500,11 +502,13 @@ void SendCoinsDialog::sendButtonClicked([[maybe_unused]] bool checked)
return;
}

// Create PSBT from the prepared transaction
CMutableTransaction mtx = CMutableTransaction{*(m_current_transaction->getWtx())};
PartiallySignedTransaction psbtx(mtx);

bool send_failure = false;
if (retval == QMessageBox::Save) {
// "Create Unsigned" clicked
CMutableTransaction mtx = CMutableTransaction{*(m_current_transaction->getWtx())};
PartiallySignedTransaction psbtx(mtx);
bool complete = false;
// Fill without signing
const auto err{model->wallet().fillPSBT(std::nullopt, /*sign=*/false, /*bip32derivs=*/true, /*n_signed=*/nullptr, psbtx, complete)};
Expand All @@ -518,8 +522,6 @@ void SendCoinsDialog::sendButtonClicked([[maybe_unused]] bool checked)
assert(!model->wallet().privateKeysDisabled() || model->wallet().hasExternalSigner());
bool broadcast = true;
if (model->wallet().hasExternalSigner()) {
CMutableTransaction mtx = CMutableTransaction{*(m_current_transaction->getWtx())};
PartiallySignedTransaction psbtx(mtx);
bool complete = false;
// Always fill without signing first. This prevents an external signer
// from being called prematurely and is not expensive.
Expand All @@ -540,6 +542,22 @@ void SendCoinsDialog::sendButtonClicked([[maybe_unused]] bool checked)
presentPSBT(psbtx);
}
}
} else {
// Sign the transaction now that the user has confirmed they want to send.
bool complete = false;
// Fill and sign the PSBT
const auto err{model->wallet().fillPSBT(std::nullopt, /*sign=*/true, /*bip32derivs=*/false, /*n_signed=*/nullptr, psbtx, complete)};
if (err || !complete) {
Q_EMIT message(tr("Send Coins"), tr("Failed to sign transaction."),
CClientUIInterface::MSG_ERROR);
send_failure = true;
broadcast = false;
} else {
// Extract the signed transaction
CHECK_NONFATAL(FinalizeAndExtractPSBT(psbtx, mtx));
const CTransactionRef tx = MakeTransactionRef(mtx);
m_current_transaction->setWtx(tx);
}
}

// Broadcast the transaction, unless an external signer was used and it
Expand Down
4 changes: 3 additions & 1 deletion src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
int nChangePosRet = -1;

auto& newTx = transaction.getWtx();
const auto& res = m_wallet->createTransaction(vecSend, coinControl, /*sign=*/!wallet().privateKeysDisabled(), nChangePosRet, nFeeRequired);
// Transaction is not signed during preparation.
const bool should_sign = false;
const auto& res = m_wallet->createTransaction(vecSend, coinControl, should_sign, nChangePosRet, nFeeRequired);
newTx = res ? *res : nullptr;
transaction.setTransactionFee(nFeeRequired);
if (fSubtractFeeFromAmount && newTx)
Expand Down
Loading