|
4 | 4 |
|
5 | 5 | #include <qt/walletview.h> |
6 | 6 |
|
| 7 | +#include <node/psbt.h> |
| 8 | +#include <node/transaction.h> |
| 9 | +#include <policy/policy.h> |
7 | 10 | #include <qt/addressbookpage.h> |
8 | 11 | #include <qt/askpassphrasedialog.h> |
9 | 12 | #include <qt/clientmodel.h> |
|
20 | 23 |
|
21 | 24 | #include <interfaces/node.h> |
22 | 25 | #include <ui_interface.h> |
| 26 | +#include <util/strencodings.h> |
23 | 27 |
|
24 | 28 | #include <QAction> |
25 | 29 | #include <QActionGroup> |
@@ -197,6 +201,80 @@ void WalletView::gotoVerifyMessageTab(QString addr) |
197 | 201 | signVerifyMessageDialog->setAddress_VM(addr); |
198 | 202 | } |
199 | 203 |
|
| 204 | +void WalletView::gotoLoadPSBT() |
| 205 | +{ |
| 206 | + QString filename = GUIUtil::getOpenFileName(this, |
| 207 | + tr("Load Transaction Data"), QString(), |
| 208 | + tr("Partially Signed Transaction (*.psbt)"), nullptr); |
| 209 | + if (filename.isEmpty()) return; |
| 210 | + if (GetFileSize(filename.toLocal8Bit().data(), MAX_FILE_SIZE_PSBT) == MAX_FILE_SIZE_PSBT) { |
| 211 | + Q_EMIT message(tr("Error"), tr("PSBT file must be smaller than 100 MiB"), CClientUIInterface::MSG_ERROR); |
| 212 | + return; |
| 213 | + } |
| 214 | + std::ifstream in(filename.toLocal8Bit().data(), std::ios::binary); |
| 215 | + std::string data(std::istreambuf_iterator<char>{in}, {}); |
| 216 | + |
| 217 | + std::string error; |
| 218 | + PartiallySignedTransaction psbtx; |
| 219 | + if (!DecodeRawPSBT(psbtx, data, error)) { |
| 220 | + Q_EMIT message(tr("Error"), tr("Unable to decode PSBT file") + "\n" + QString::fromStdString(error), CClientUIInterface::MSG_ERROR); |
| 221 | + return; |
| 222 | + } |
| 223 | + |
| 224 | + CMutableTransaction mtx; |
| 225 | + bool complete = false; |
| 226 | + PSBTAnalysis analysis = AnalyzePSBT(psbtx); |
| 227 | + QMessageBox msgBox; |
| 228 | + msgBox.setText("PSBT"); |
| 229 | + switch (analysis.next) { |
| 230 | + case PSBTRole::CREATOR: |
| 231 | + case PSBTRole::UPDATER: |
| 232 | + msgBox.setInformativeText("PSBT is incomplete. Copy to clipboard for manual inspection?"); |
| 233 | + break; |
| 234 | + case PSBTRole::SIGNER: |
| 235 | + msgBox.setInformativeText("Transaction needs more signatures. Copy to clipboard?"); |
| 236 | + break; |
| 237 | + case PSBTRole::FINALIZER: |
| 238 | + case PSBTRole::EXTRACTOR: |
| 239 | + complete = FinalizeAndExtractPSBT(psbtx, mtx); |
| 240 | + if (complete) { |
| 241 | + msgBox.setInformativeText(tr("Would you like to send this transaction?")); |
| 242 | + } else { |
| 243 | + // The analyzer missed something, e.g. if there are final_scriptSig/final_scriptWitness |
| 244 | + // but with invalid signatures. |
| 245 | + msgBox.setInformativeText(tr("There was an unexpected problem processing the PSBT. Copy to clipboard for manual inspection?")); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel); |
| 250 | + switch (msgBox.exec()) { |
| 251 | + case QMessageBox::Yes: { |
| 252 | + if (complete) { |
| 253 | + std::string err_string; |
| 254 | + CTransactionRef tx = MakeTransactionRef(mtx); |
| 255 | + |
| 256 | + TransactionError result = BroadcastTransaction(*clientModel->node().context(), tx, err_string, DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK(), /* relay */ true, /* wait_callback */ false); |
| 257 | + if (result == TransactionError::OK) { |
| 258 | + Q_EMIT message(tr("Success"), tr("Broadcasted transaction sucessfully."), CClientUIInterface::MSG_INFORMATION | CClientUIInterface::MODAL); |
| 259 | + } else { |
| 260 | + Q_EMIT message(tr("Error"), QString::fromStdString(err_string), CClientUIInterface::MSG_ERROR); |
| 261 | + } |
| 262 | + } else { |
| 263 | + // Serialize the PSBT |
| 264 | + CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); |
| 265 | + ssTx << psbtx; |
| 266 | + GUIUtil::setClipboard(EncodeBase64(ssTx.str()).c_str()); |
| 267 | + Q_EMIT message(tr("PSBT copied"), "Copied to clipboard", CClientUIInterface::MSG_INFORMATION); |
| 268 | + return; |
| 269 | + } |
| 270 | + } |
| 271 | + case QMessageBox::Cancel: |
| 272 | + break; |
| 273 | + default: |
| 274 | + assert(false); |
| 275 | + } |
| 276 | +} |
| 277 | + |
200 | 278 | bool WalletView::handlePaymentRequest(const SendCoinsRecipient& recipient) |
201 | 279 | { |
202 | 280 | return sendCoinsPage->handlePaymentRequest(recipient); |
|
0 commit comments