Skip to content

Commit 9406946

Browse files
Sjorshebasto
authored andcommitted
refactor: helper function signWithExternalSigner()
Does not change behavior. Review hint: git show --color-moved --color-moved-ws=allow-indentation-change Github-Pull: bitcoin-core/gui#555 Rebased-From: 4b5a6cd
1 parent fc421d4 commit 9406946

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

src/qt/sendcoinsdialog.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,34 @@ void SendCoinsDialog::presentPSBT(PartiallySignedTransaction& psbtx)
447447
} // msgBox.exec()
448448
}
449449

450+
bool SendCoinsDialog::signWithExternalSigner(PartiallySignedTransaction& psbtx, CMutableTransaction& mtx, bool& complete) {
451+
TransactionError err;
452+
try {
453+
err = model->wallet().fillPSBT(SIGHASH_ALL, /*sign=*/true, /*bip32derivs=*/true, /*n_signed=*/nullptr, psbtx, complete);
454+
} catch (const std::runtime_error& e) {
455+
QMessageBox::critical(nullptr, tr("Sign failed"), e.what());
456+
return false;
457+
}
458+
if (err == TransactionError::EXTERNAL_SIGNER_NOT_FOUND) {
459+
//: "External signer" means using devices such as hardware wallets.
460+
QMessageBox::critical(nullptr, tr("External signer not found"), "External signer not found");
461+
return false;
462+
}
463+
if (err == TransactionError::EXTERNAL_SIGNER_FAILED) {
464+
//: "External signer" means using devices such as hardware wallets.
465+
QMessageBox::critical(nullptr, tr("External signer failure"), "External signer failure");
466+
return false;
467+
}
468+
if (err != TransactionError::OK) {
469+
tfm::format(std::cerr, "Failed to sign PSBT");
470+
processSendCoinsReturn(WalletModel::TransactionCreationFailed);
471+
return false;
472+
}
473+
// fillPSBT does not always properly finalize
474+
complete = FinalizeAndExtractPSBT(psbtx, mtx);
475+
return true;
476+
}
477+
450478
void SendCoinsDialog::sendButtonClicked([[maybe_unused]] bool checked)
451479
{
452480
if(!model || !model->getOptionsModel())
@@ -479,33 +507,7 @@ void SendCoinsDialog::sendButtonClicked([[maybe_unused]] bool checked)
479507
assert(!complete);
480508
assert(err == TransactionError::OK);
481509
if (model->wallet().hasExternalSigner()) {
482-
try {
483-
err = model->wallet().fillPSBT(SIGHASH_ALL, true /* sign */, true /* bip32derivs */, nullptr, psbtx, complete);
484-
} catch (const std::runtime_error& e) {
485-
QMessageBox::critical(nullptr, tr("Sign failed"), e.what());
486-
send_failure = true;
487-
return;
488-
}
489-
if (err == TransactionError::EXTERNAL_SIGNER_NOT_FOUND) {
490-
//: "External signer" means using devices such as hardware wallets.
491-
QMessageBox::critical(nullptr, tr("External signer not found"), "External signer not found");
492-
send_failure = true;
493-
return;
494-
}
495-
if (err == TransactionError::EXTERNAL_SIGNER_FAILED) {
496-
//: "External signer" means using devices such as hardware wallets.
497-
QMessageBox::critical(nullptr, tr("External signer failure"), "External signer failure");
498-
send_failure = true;
499-
return;
500-
}
501-
if (err != TransactionError::OK) {
502-
tfm::format(std::cerr, "Failed to sign PSBT");
503-
processSendCoinsReturn(WalletModel::TransactionCreationFailed);
504-
send_failure = true;
505-
return;
506-
}
507-
// fillPSBT does not always properly finalize
508-
complete = FinalizeAndExtractPSBT(psbtx, mtx);
510+
send_failure = !signWithExternalSigner(psbtx, mtx, complete);
509511
}
510512

511513
// Broadcast transaction if complete (even with an external signer this

src/qt/sendcoinsdialog.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ public Q_SLOTS:
7979
void minimizeFeeSection(bool fMinimize);
8080
// Format confirmation message
8181
bool PrepareSendText(QString& question_string, QString& informative_text, QString& detailed_text);
82+
/* Sign PSBT using external signer.
83+
*
84+
* @param[in,out] psbtx the PSBT to sign
85+
* @param[in,out] mtx needed to attempt to finalize
86+
* @param[in,out] complete whether the PSBT is complete (a successfully signed multisig transaction may not be complete)
87+
*
88+
* @returns false if any failure occurred, which may include the user rejection of a transaction on the device.
89+
*/
90+
bool signWithExternalSigner(PartiallySignedTransaction& psbt, CMutableTransaction& mtx, bool& complete);
8291
void updateFeeMinimizedLabel();
8392
void updateCoinControlState();
8493

0 commit comments

Comments
 (0)