From 1e943b6208f3045a55a35d0a8d57f903f7a8b008 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Fri, 2 Jul 2021 19:35:21 +0200 Subject: [PATCH] Trezor: get tx key async --- src/dialog/TxInfoDialog.cpp | 20 ++++++++------- src/dialog/TxInfoDialog.h | 1 - src/dialog/TxProofDialog.cpp | 10 +++++++- src/dialog/TxProofDialog.h | 1 + src/libwalletqt/Wallet.cpp | 14 ++++++----- src/libwalletqt/Wallet.h | 3 ++- src/utils/scheduler.cpp | 47 ++++++++++++++++-------------------- src/utils/scheduler.h | 4 ++- 8 files changed, 55 insertions(+), 45 deletions(-) diff --git a/src/dialog/TxInfoDialog.cpp b/src/dialog/TxInfoDialog.cpp index a056d11..80ba5af 100644 --- a/src/dialog/TxInfoDialog.cpp +++ b/src/dialog/TxInfoDialog.cpp @@ -20,18 +20,13 @@ TxInfoDialog::TxInfoDialog(QSharedPointer ctx, TransactionInfo *txIn , ui(new Ui::TxInfoDialog) , m_ctx(std::move(ctx)) , m_txInfo(txInfo) + , m_txProofDialog(new TxProofDialog(this, m_ctx, txInfo)) { ui->setupUi(this); m_txid = txInfo->hash(); ui->label_txid->setText(m_txid); - m_txKey = m_ctx->wallet->getTxKey(txInfo->hash()); - if (m_txKey.isEmpty()) { - ui->btn_CopyTxKey->setEnabled(false); - ui->btn_CopyTxKey->setToolTip("Transaction key unknown"); - } - connect(ui->btn_CopyTxKey, &QPushButton::pressed, this, &TxInfoDialog::copyTxKey); connect(ui->btn_createTxProof, &QPushButton::pressed, this, &TxInfoDialog::createTxProof); @@ -61,8 +56,6 @@ TxInfoDialog::TxInfoDialog(QSharedPointer ctx, TransactionInfo *txIn ui->frameDestinations->hide(); } - m_txProofDialog = new TxProofDialog(this, m_ctx, txInfo); - QCoreApplication::processEvents(); qreal lineHeight = QFontMetrics(ui->destinations->document()->defaultFont()).height(); @@ -125,11 +118,20 @@ void TxInfoDialog::updateData() { } void TxInfoDialog::copyTxKey() { - Utils::copyToClipboard(m_txKey); + m_ctx->wallet->getTxKeyAsync(m_txid, [this](QVariantMap map){ + QString txKey = map.value("tx_key").toString(); + if (txKey.isEmpty()) { + QMessageBox::warning(this, "Unable to copy transaction key", "Transaction key unknown"); + } else { + Utils::copyToClipboard(txKey); + QMessageBox::information(this, "Transaction key copied", "Transaction key copied to clipboard."); + } + }); } void TxInfoDialog::createTxProof() { m_txProofDialog->show(); + m_txProofDialog->getTxKey(); } TxInfoDialog::~TxInfoDialog() = default; \ No newline at end of file diff --git a/src/dialog/TxInfoDialog.h b/src/dialog/TxInfoDialog.h index 9a5bea6..a1ad1b9 100644 --- a/src/dialog/TxInfoDialog.h +++ b/src/dialog/TxInfoDialog.h @@ -36,7 +36,6 @@ private: QSharedPointer m_ctx; TransactionInfo *m_txInfo; TxProofDialog *m_txProofDialog; - QString m_txKey; QString m_txid; }; diff --git a/src/dialog/TxProofDialog.cpp b/src/dialog/TxProofDialog.cpp index 2a601df..96c81ea 100644 --- a/src/dialog/TxProofDialog.cpp +++ b/src/dialog/TxProofDialog.cpp @@ -18,7 +18,7 @@ TxProofDialog::TxProofDialog(QWidget *parent, QSharedPointer ctx, Tr ui->setupUi(this); m_txid = txInfo->hash(); - m_txKey = m_ctx->wallet->getTxKey(m_txid); + m_direction = txInfo->direction(); for (auto const &t: txInfo->transfers()) { @@ -54,6 +54,14 @@ TxProofDialog::TxProofDialog(QWidget *parent, QSharedPointer ctx, Tr this->adjustSize(); } +void TxProofDialog::getTxKey() { + if (!m_txKey.isEmpty()) return; + + m_ctx->wallet->getTxKeyAsync(m_txid, [this](QVariantMap map){ + m_txKey = map.value("tx_key").toString(); + }); +} + void TxProofDialog::setTxId(const QString &txid) { ui->label_txid->setText(txid); } diff --git a/src/dialog/TxProofDialog.h b/src/dialog/TxProofDialog.h index 9de0ec3..ba47a57 100644 --- a/src/dialog/TxProofDialog.h +++ b/src/dialog/TxProofDialog.h @@ -21,6 +21,7 @@ public: explicit TxProofDialog(QWidget *parent, QSharedPointer ctx, TransactionInfo *txid); ~TxProofDialog() override; void setTxId(const QString &txid); + void getTxKey(); private slots: void selectSpendProof(); diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp index d3e35af..629d788 100644 --- a/src/libwalletqt/Wallet.cpp +++ b/src/libwalletqt/Wallet.cpp @@ -969,12 +969,14 @@ QString Wallet::getTxKey(const QString &txid) const return QString::fromStdString(m_walletImpl->getTxKey(txid.toStdString())); } -//void Wallet::getTxKeyAsync(const QString &txid, const QJSValue &callback) -//{ -// m_scheduler.run([this, txid] { -// return QJSValueList({txid, getTxKey(txid)}); -// }, callback); -//} +void Wallet::getTxKeyAsync(const QString &txid, const std::function &callback) +{ + m_scheduler.run([this, txid] { + QVariantMap map; + map["tx_key"] = getTxKey(txid); + return map; + }, callback); +} QString Wallet::checkTxKey(const QString &txid, const QString &tx_key, const QString &address) { diff --git a/src/libwalletqt/Wallet.h b/src/libwalletqt/Wallet.h index 5f0a65a..bcdb65c 100644 --- a/src/libwalletqt/Wallet.h +++ b/src/libwalletqt/Wallet.h @@ -393,7 +393,8 @@ public: bool setUserNote(const QString &txid, const QString ¬e); QString getUserNote(const QString &txid) const; QString getTxKey(const QString &txid) const; - //void getTxKeyAsync(const QString &txid, const QJSValue &callback); + void getTxKeyAsync(const QString &txid, const std::function &callback); + QString checkTxKey(const QString &txid, const QString &tx_key, const QString &address); TxProof getTxProof(const QString &txid, const QString &address, const QString &message) const; // void getTxProofAsync(const QString &txid, const QString &address, const QString &message, const QJSValue &callback); diff --git a/src/utils/scheduler.cpp b/src/utils/scheduler.cpp index e8b8503..bc9bb36 100644 --- a/src/utils/scheduler.cpp +++ b/src/utils/scheduler.cpp @@ -45,32 +45,27 @@ QPair> FutureScheduler::run(std::function function) }); } -//QPair> FutureScheduler::run(std::function function, const QJSValue &callback) -//{ -// if (!callback.isCallable()) -// { -// throw std::runtime_error("js callback must be callable"); -// } - -// return execute([this, function, callback](QFutureWatcher *watcher) { -// connect(watcher, &QFutureWatcher::finished, [watcher, callback] { -// QJSValue(callback).call(watcher->future().result()); -// }); -// return QtConcurrent::run([this, function] { -// QJSValueList result; -// try -// { -// result = function(); -// } -// catch (const std::exception &exception) -// { -// qWarning() << "Exception thrown from async function: " << exception.what(); -// } -// done(); -// return result; -// }); -// }); -//} +QPair> FutureScheduler::run(const std::function &function, const std::function &callback) noexcept +{ + return execute([this, function, callback](QFutureWatcher *watcher) { + connect(watcher, &QFutureWatcher::finished, [watcher, callback] { + callback(watcher->future().result()); + }); + return QtConcurrent::run([this, function] { + QVariantMap result; + try + { + result = function(); + } + catch (const std::exception &exception) + { + qWarning() << "Exception thrown from async function: " << exception.what(); + } + done(); + return result; + }); + }); +} bool FutureScheduler::stopping() const noexcept { diff --git a/src/utils/scheduler.h b/src/utils/scheduler.h index 1dd9e23..df02703 100644 --- a/src/utils/scheduler.h +++ b/src/utils/scheduler.h @@ -25,6 +25,8 @@ public: void shutdownWaitForFinished() noexcept; QPair> run(std::function function) noexcept; + QPair> run(const std::function& function, const std::function& callback) noexcept; + // QPair> run(std::function function, const QJSValue &callback); bool stopping() const noexcept; @@ -57,7 +59,7 @@ private: } QFutureWatcher schedule(std::function function); - //QFutureWatcher schedule(std::function function, const QJSValue &callback); +// QFutureWatcher schedule(std::function function, std::function &callback); private: size_t Alive;