]> Nutra Git (v2) - gamesguru/feather.git/commitdiff
Trezor: get tx key async
authortobtoht <thotbot@protonmail.com>
Fri, 2 Jul 2021 17:35:21 +0000 (19:35 +0200)
committertobtoht <thotbot@protonmail.com>
Fri, 2 Jul 2021 17:35:21 +0000 (19:35 +0200)
src/dialog/TxInfoDialog.cpp
src/dialog/TxInfoDialog.h
src/dialog/TxProofDialog.cpp
src/dialog/TxProofDialog.h
src/libwalletqt/Wallet.cpp
src/libwalletqt/Wallet.h
src/utils/scheduler.cpp
src/utils/scheduler.h

index a056d11ddaaac98a0a2666e88ca4ac0c681106ed..80ba5af86581804f7a133f14e1914124093143d8 100644 (file)
@@ -20,18 +20,13 @@ TxInfoDialog::TxInfoDialog(QSharedPointer<AppContext> 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<AppContext> 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
index 9a5bea64d71f1bee791037b888b717028863c21f..a1ad1b92635922be91faf64a0e4371de820ef528 100644 (file)
@@ -36,7 +36,6 @@ private:
     QSharedPointer<AppContext> m_ctx;
     TransactionInfo *m_txInfo;
     TxProofDialog *m_txProofDialog;
-    QString m_txKey;
     QString m_txid;
 };
 
index 2a601df79c7b001f6147ede10b8085925ce8946a..96c81ea6efc13a6641c35213f53c1753f5efc3b5 100644 (file)
@@ -18,7 +18,7 @@ TxProofDialog::TxProofDialog(QWidget *parent, QSharedPointer<AppContext> 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<AppContext> 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);
 }
index 9de0ec33d2157e40df36fa0b10b9d6c1b8862cbd..ba47a57f0760cd310445e8f22cf8d05bf69857b2 100644 (file)
@@ -21,6 +21,7 @@ public:
     explicit TxProofDialog(QWidget *parent, QSharedPointer<AppContext> ctx, TransactionInfo *txid);
     ~TxProofDialog() override;
     void setTxId(const QString &txid);
+    void getTxKey();
 
 private slots:
     void selectSpendProof();
index d3e35af4f9e59f8f3eebb3428bf00e8f7aa8200b..629d7885a22ca1866ca9cd118529fc035cf18d21 100644 (file)
@@ -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<void (QVariantMap)> &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)
 {
index 5f0a65a8cdc48b87e56e38f4ff33faa585f80d09..bcdb65c51d565ea5b78078f8b2ae51a440a618c3 100644 (file)
@@ -393,7 +393,8 @@ public:
     bool setUserNote(const QString &txid, const QString &note);
     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<void (QVariantMap)> &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);
index e8b850367cc5efaffe7b91d4579d5d01bf9adf49..bc9bb3669a0100598ecbe7b932b7e84fd01e7231 100644 (file)
@@ -45,32 +45,27 @@ QPair<bool, QFuture<void>> FutureScheduler::run(std::function<void()> function)
     });
 }
 
-//QPair<bool, QFuture<QJSValueList>> FutureScheduler::run(std::function<QJSValueList()> function, const QJSValue &callback)
-//{
-//    if (!callback.isCallable())
-//    {
-//        throw std::runtime_error("js callback must be callable");
-//    }
-
-//    return execute<QJSValueList>([this, function, callback](QFutureWatcher<QJSValueList> *watcher) {
-//        connect(watcher, &QFutureWatcher<QJSValueList>::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<bool, QFuture<QVariantMap>> FutureScheduler::run(const std::function<QVariantMap()> &function, const std::function<void (QVariantMap)> &callback) noexcept
+{
+    return execute<QVariantMap>([this, function, callback](QFutureWatcher<QVariantMap> *watcher) {
+        connect(watcher, &QFutureWatcher<QVariantMap>::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
 {
index 1dd9e23d931c4e29be6ffb5e410dc46cb3b2989b..df02703f83d27616107943c95cc9108d7006ed2b 100644 (file)
@@ -25,6 +25,8 @@ public:
     void shutdownWaitForFinished() noexcept;
 
     QPair<bool, QFuture<void>> run(std::function<void()> function) noexcept;
+    QPair<bool, QFuture<QVariantMap>> run(const std::function<QVariantMap()>& function, const std::function<void (QVariantMap)>& callback) noexcept;
+
    // QPair<bool, QFuture<QJSValueList>> run(std::function<QJSValueList()> function, const QJSValue &callback);
    bool stopping() const noexcept;
 
@@ -57,7 +59,7 @@ private:
     }
 
     QFutureWatcher<void> schedule(std::function<void()> function);
-    //QFutureWatcher<QJSValueList> schedule(std::function<QJSValueList() noexcept> function, const QJSValue &callback);
+//    QFutureWatcher<QVariantMap> schedule(std::function<QVariantMap() noexcept> function, std::function<void> &callback);
 
 private:
     size_t Alive;