Bitcoin Core 28.99.0
P2P Digital Currency
psbtoperationsdialog.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
6
7#include <common/messages.h>
8#include <core_io.h>
9#include <interfaces/node.h>
10#include <key_io.h>
11#include <node/psbt.h>
12#include <node/types.h>
13#include <policy/policy.h>
14#include <qt/bitcoinunits.h>
15#include <qt/forms/ui_psbtoperationsdialog.h>
16#include <qt/guiutil.h>
17#include <qt/optionsmodel.h>
18#include <util/fs.h>
19#include <util/strencodings.h>
20
21#include <fstream>
22#include <iostream>
23#include <string>
24
30
32 QWidget* parent, WalletModel* wallet_model, ClientModel* client_model) : QDialog(parent, GUIUtil::dialog_flags),
33 m_ui(new Ui::PSBTOperationsDialog),
34 m_wallet_model(wallet_model),
35 m_client_model(client_model)
36{
37 m_ui->setupUi(this);
38
39 connect(m_ui->signTransactionButton, &QPushButton::clicked, this, &PSBTOperationsDialog::signTransaction);
40 connect(m_ui->broadcastTransactionButton, &QPushButton::clicked, this, &PSBTOperationsDialog::broadcastTransaction);
41 connect(m_ui->copyToClipboardButton, &QPushButton::clicked, this, &PSBTOperationsDialog::copyToClipboard);
42 connect(m_ui->saveButton, &QPushButton::clicked, this, &PSBTOperationsDialog::saveTransaction);
43
44 connect(m_ui->closeButton, &QPushButton::clicked, this, &PSBTOperationsDialog::close);
45
46 m_ui->signTransactionButton->setEnabled(false);
47 m_ui->broadcastTransactionButton->setEnabled(false);
48}
49
51{
52 delete m_ui;
53}
54
56{
57 m_transaction_data = psbtx;
58
59 bool complete = FinalizePSBT(psbtx); // Make sure all existing signatures are fully combined before checking for completeness.
60 if (m_wallet_model) {
61 size_t n_could_sign;
62 const auto err{m_wallet_model->wallet().fillPSBT(SIGHASH_ALL, /*sign=*/false, /*bip32derivs=*/true, &n_could_sign, m_transaction_data, complete)};
63 if (err) {
64 showStatus(tr("Failed to load transaction: %1")
65 .arg(QString::fromStdString(PSBTErrorString(*err).translated)),
67 return;
68 }
69 m_ui->signTransactionButton->setEnabled(!complete && !m_wallet_model->wallet().privateKeysDisabled() && n_could_sign > 0);
70 } else {
71 m_ui->signTransactionButton->setEnabled(false);
72 }
73
74 m_ui->broadcastTransactionButton->setEnabled(complete);
75
77}
78
80{
81 bool complete;
82 size_t n_signed;
83
85
86 const auto err{m_wallet_model->wallet().fillPSBT(SIGHASH_ALL, /*sign=*/true, /*bip32derivs=*/true, &n_signed, m_transaction_data, complete)};
87
88 if (err) {
89 showStatus(tr("Failed to sign transaction: %1")
90 .arg(QString::fromStdString(PSBTErrorString(*err).translated)), StatusLevel::ERR);
91 return;
92 }
93
95
96 if (!complete && !ctx.isValid()) {
97 showStatus(tr("Cannot sign inputs while wallet is locked."), StatusLevel::WARN);
98 } else if (!complete && n_signed < 1) {
99 showStatus(tr("Could not sign any more inputs."), StatusLevel::WARN);
100 } else if (!complete) {
101 showStatus(tr("Signed %1 inputs, but more signatures are still required.").arg(n_signed),
103 } else {
104 showStatus(tr("Signed transaction successfully. Transaction is ready to broadcast."),
106 m_ui->broadcastTransactionButton->setEnabled(true);
107 }
108}
109
111{
114 // This is never expected to fail unless we were given a malformed PSBT
115 // (e.g. with an invalid signature.)
116 showStatus(tr("Unknown error processing transaction."), StatusLevel::ERR);
117 return;
118 }
119
121 std::string err_string;
122 TransactionError error =
124
125 if (error == TransactionError::OK) {
126 showStatus(tr("Transaction broadcast successfully! Transaction ID: %1")
127 .arg(QString::fromStdString(tx->GetHash().GetHex())), StatusLevel::INFO);
128 } else {
129 showStatus(tr("Transaction broadcast failed: %1")
130 .arg(QString::fromStdString(TransactionErrorString(error).translated)), StatusLevel::ERR);
131 }
132}
133
135 DataStream ssTx{};
136 ssTx << m_transaction_data;
137 GUIUtil::setClipboard(EncodeBase64(ssTx.str()).c_str());
138 showStatus(tr("PSBT copied to clipboard."), StatusLevel::INFO);
139}
140
142 DataStream ssTx{};
143 ssTx << m_transaction_data;
144
145 QString selected_filter;
146 QString filename_suggestion = "";
147 bool first = true;
148 for (const CTxOut& out : m_transaction_data.tx->vout) {
149 if (!first) {
150 filename_suggestion.append("-");
151 }
152 CTxDestination address;
153 ExtractDestination(out.scriptPubKey, address);
155 QString address_str = QString::fromStdString(EncodeDestination(address));
156 filename_suggestion.append(address_str + "-" + amount);
157 first = false;
158 }
159 filename_suggestion.append(".psbt");
160 QString filename = GUIUtil::getSaveFileName(this,
161 tr("Save Transaction Data"), filename_suggestion,
162 //: Expanded name of the binary PSBT file format. See: BIP 174.
163 tr("Partially Signed Transaction (Binary)") + QLatin1String(" (*.psbt)"), &selected_filter);
164 if (filename.isEmpty()) {
165 return;
166 }
167 std::ofstream out{filename.toLocal8Bit().data(), std::ofstream::out | std::ofstream::binary};
168 out << ssTx.str();
169 out.close();
170 showStatus(tr("PSBT saved to disk."), StatusLevel::INFO);
171}
172
174 m_ui->transactionDescription->setText(renderTransaction(m_transaction_data));
176}
177
179{
180 QString tx_description;
181 QLatin1String bullet_point(" * ");
182 CAmount totalAmount = 0;
183 for (const CTxOut& out : psbtx.tx->vout) {
184 CTxDestination address;
185 ExtractDestination(out.scriptPubKey, address);
186 totalAmount += out.nValue;
187 tx_description.append(bullet_point).append(tr("Sends %1 to %2")
189 .arg(QString::fromStdString(EncodeDestination(address))));
190 // Check if the address is one of ours
191 if (m_wallet_model != nullptr && m_wallet_model->wallet().txoutIsMine(out)) tx_description.append(" (" + tr("own address") + ")");
192 tx_description.append("<br>");
193 }
194
195 PSBTAnalysis analysis = AnalyzePSBT(psbtx);
196 tx_description.append(bullet_point);
197 if (!*analysis.fee) {
198 // This happens if the transaction is missing input UTXO information.
199 tx_description.append(tr("Unable to calculate transaction fee or total transaction amount."));
200 } else {
201 tx_description.append(tr("Pays transaction fee: "));
202 tx_description.append(BitcoinUnits::formatWithUnit(BitcoinUnit::BTC, *analysis.fee));
203
204 // add total amount in all subdivision units
205 tx_description.append("<hr />");
206 QStringList alternativeUnits;
208 {
210 alternativeUnits.append(BitcoinUnits::formatHtmlWithUnit(u, totalAmount));
211 }
212 }
213 tx_description.append(QString("<b>%1</b>: <b>%2</b>").arg(tr("Total Amount"))
215 tx_description.append(QString("<br /><span style='font-size:10pt; font-weight:normal;'>(=%1)</span>")
216 .arg(alternativeUnits.join(" " + tr("or") + " ")));
217 }
218
219 size_t num_unsigned = CountPSBTUnsignedInputs(psbtx);
220 if (num_unsigned > 0) {
221 tx_description.append("<br><br>");
222 tx_description.append(tr("Transaction has %1 unsigned inputs.").arg(QString::number(num_unsigned)));
223 }
224
225 return tx_description;
226}
227
229 m_ui->statusBar->setText(msg);
230 switch (level) {
231 case StatusLevel::INFO: {
232 m_ui->statusBar->setStyleSheet("QLabel { background-color : lightgreen }");
233 break;
234 }
235 case StatusLevel::WARN: {
236 m_ui->statusBar->setStyleSheet("QLabel { background-color : orange }");
237 break;
238 }
239 case StatusLevel::ERR: {
240 m_ui->statusBar->setStyleSheet("QLabel { background-color : red }");
241 break;
242 }
243 }
244 m_ui->statusBar->show();
245}
246
248 if (!m_wallet_model) {
249 return 0;
250 }
251
252 size_t n_signed;
253 bool complete;
254 const auto err{m_wallet_model->wallet().fillPSBT(SIGHASH_ALL, /*sign=*/false, /*bip32derivs=*/false, &n_signed, m_transaction_data, complete)};
255
256 if (err) {
257 return 0;
258 }
259 return n_signed;
260}
261
263 PSBTAnalysis analysis = AnalyzePSBT(psbtx);
264 size_t n_could_sign = couldSignInputs(psbtx);
265
266 switch (analysis.next) {
267 case PSBTRole::UPDATER: {
268 showStatus(tr("Transaction is missing some information about inputs."), StatusLevel::WARN);
269 break;
270 }
271 case PSBTRole::SIGNER: {
272 QString need_sig_text = tr("Transaction still needs signature(s).");
274 if (!m_wallet_model) {
275 need_sig_text += " " + tr("(But no wallet is loaded.)");
276 level = StatusLevel::WARN;
277 } else if (m_wallet_model->wallet().privateKeysDisabled()) {
278 need_sig_text += " " + tr("(But this wallet cannot sign transactions.)");
279 level = StatusLevel::WARN;
280 } else if (n_could_sign < 1) {
281 need_sig_text += " " + tr("(But this wallet does not have the right keys.)"); // XXX wording
282 level = StatusLevel::WARN;
283 }
284 showStatus(need_sig_text, level);
285 break;
286 }
288 case PSBTRole::EXTRACTOR: {
289 showStatus(tr("Transaction is fully signed and ready for broadcast."), StatusLevel::INFO);
290 break;
291 }
292 default: {
293 showStatus(tr("Transaction status is unknown."), StatusLevel::ERR);
294 break;
295 }
296 }
297}
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
Definition: addresstype.cpp:49
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:140
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static QString format(Unit unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
static QString formatHtmlWithUnit(Unit unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as HTML string (with unit)
static QList< Unit > availableUnits()
Get list of units, for drop-down box.
static QString formatWithUnit(Unit unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as string (with unit)
Unit
Bitcoin units.
Definition: bitcoinunits.h:42
CAmount GetFeePerK() const
Return the fee in satoshis for a vsize of 1000 vbytes.
Definition: feerate.h:63
An output of a transaction.
Definition: transaction.h:150
Model for Bitcoin network client.
Definition: clientmodel.h:57
OptionsModel * getOptionsModel()
interfaces::Node & node() const
Definition: clientmodel.h:66
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
BitcoinUnit getDisplayUnit() const
Definition: optionsmodel.h:103
Dialog showing transaction details.
PartiallySignedTransaction m_transaction_data
PSBTOperationsDialog(QWidget *parent, WalletModel *walletModel, ClientModel *clientModel)
Ui::PSBTOperationsDialog * m_ui
void openWithPSBT(PartiallySignedTransaction psbtx)
size_t couldSignInputs(const PartiallySignedTransaction &psbtx)
void showTransactionStatus(const PartiallySignedTransaction &psbtx)
void showStatus(const QString &msg, StatusLevel level)
QString renderTransaction(const PartiallySignedTransaction &psbtx)
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:48
interfaces::Wallet & wallet() const
Definition: walletmodel.h:138
UnlockContext requestUnlock()
virtual node::TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string &err_string)=0
Broadcast transaction.
virtual wallet::isminetype txoutIsMine(const CTxOut &txout)=0
Return whether transaction output belongs to wallet.
virtual std::optional< common::PSBTError > fillPSBT(int sighash_type, bool sign, bool bip32derivs, size_t *n_signed, PartiallySignedTransaction &psbtx, bool &complete)=0
Fill PSBT.
virtual bool privateKeysDisabled()=0
@ SIGHASH_ALL
Definition: interpreter.h:30
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:294
is a home for simple string functions returning descriptive messages that are used in RPC and GUI int...
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:58
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut)
Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix when ...
Definition: guiutil.cpp:313
constexpr auto dialog_flags
Definition: guiutil.h:60
void setClipboard(const QString &str)
Definition: guiutil.cpp:668
bilingual_str PSBTErrorString(PSBTError err)
Definition: messages.cpp:106
bilingual_str TransactionErrorString(const TransactionError err)
Definition: messages.cpp:124
PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx)
Provides helpful miscellaneous information about where a PSBT is in the signing workflow.
Definition: psbt.cpp:16
TransactionError
Definition: types.h:20
static const CFeeRate DEFAULT_MAX_RAW_TX_FEE_RATE
Maximum fee rate for sendrawtransaction and testmempoolaccept RPC calls.
Definition: transaction.h:27
is a home for public enum and struct type definitions that are used by internally by node code,...
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction &psbt)
Counts the unsigned inputs of a PSBT.
Definition: psbt.cpp:327
bool FinalizeAndExtractPSBT(PartiallySignedTransaction &psbtx, CMutableTransaction &result)
Finalizes a PSBT if possible, and extracts it to a CMutableTransaction if it could be finalized.
Definition: psbt.cpp:495
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition: psbt.cpp:480
A mutable version of CTransaction.
Definition: transaction.h:378
A version of CTransaction with the PSBT format.
Definition: psbt.h:951
std::optional< CMutableTransaction > tx
Definition: psbt.h:952
std::string translated
Definition: translation.h:23
Holds the results of AnalyzePSBT (miscellaneous information about a PSBT)
Definition: psbt.h:30
std::optional< CAmount > fee
Amount of fee being paid by the transaction.
Definition: psbt.h:33
PSBTRole next
Which of the BIP 174 roles needs to handle the transaction next.
Definition: psbt.h:35
std::string EncodeBase64(Span< const unsigned char > input)