aboutsummaryrefslogtreecommitdiff
path: root/src/qt/importkeysdialog.cpp
blob: 665c7cfe0013459b816922a1a5658c81ba74e6ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2021 The Discoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#if defined(HAVE_CONFIG_H)
#include "config/bitcoin-config.h"
#endif

#include "importkeysdialog.h"
#include "ui_importkeysdialog.h"

#include "base58.h"
#include "guiutil.h"
#include "platformstyle.h"
#include "validation.h"
#include "wallet/wallet.h"
#include "walletmodel.h"
#include "util.h"

#include <QThread>
#include <QDebug>

/* Object for executing key import commands in a separate thread.
*/
class ImportKeyExecutor : public QObject
{
    Q_OBJECT

public Q_SLOTS:
    void rescan(CWallet*, CBlockIndex*);

Q_SIGNALS:
    void rescanWallet(CWallet*, CBlockIndex*);
};

#include "importkeysdialog.moc"

void ImportKeyExecutor::rescan(CWallet* pwallet, CBlockIndex* genesisBlock)
{
    qWarning() << "started import key thread";
    pwallet->UpdateTimeFirstKey(1);
    pwallet->ScanForWalletTransactions(genesisBlock, true);
    qWarning() << "quitting import key thread";
    QObject::thread()->quit();
}

ImportKeysDialog::ImportKeysDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ImportKeysDialog),
    platformStyle(_platformStyle)
{
    ui->setupUi(this);

    /* Main elements init */
    if (fPruneMode) {
        ui->rescanCheckBox->setEnabled(false);
    }
}

ImportKeysDialog::~ImportKeysDialog()
{
    thread.wait();
    delete ui;
}

void ImportKeysDialog::on_okButton_clicked()
{
    if (importKey()) {
        resetDialogValues();
        accept();
    };
}

void ImportKeysDialog::on_cancelButton_clicked()
{
    reject();
}

void ImportKeysDialog::on_resetButton_clicked()
{
    resetDialogValues();
}

bool ImportKeysDialog::importKey()
{
    const QString privateKey      = ui->privateKey->text();
    const QString privateKeyLabel = ui->privateKeyLabel->text();
    const bool rescan             = ui->rescanCheckBox->isChecked();

    resetDialogValues();

    CBitcoinSecret vchSecret;
    bool fGood = vchSecret.SetString(privateKey.toStdString());
    if (!fGood) {
        vchSecret.SetString("");
        ui->privateKeyImportTextMessage->setText(tr("Invalid private key; please check and try again!"));
        return false;
    }

    CKey key = vchSecret.GetKey();
    if (!key.IsValid()) {
        vchSecret.SetString("");
        ui->privateKeyImportTextMessage->setText(tr("Invalid private key; please check and try again!"));
        return false;
    }

    CPubKey pubkey = key.GetPubKey();
    assert(key.VerifyPubKey(pubkey));
    CKeyID vchAddress = pubkey.GetID();

    pwalletMain->MarkDirty();
    pwalletMain->SetAddressBook(vchAddress, privateKeyLabel.toStdString(), "receive");

    if (pwalletMain->HaveKey(vchAddress)) {
        vchSecret.SetString("");
        ui->privateKeyImportTextMessage->setText(
            tr("Invalid address generated from private key; please check and try again!"
        ));
        return false;
    }

    pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1;

    if (!pwalletMain->AddKeyPubKey(key, pubkey)) {
        vchSecret.SetString("");
        ui->privateKeyImportTextMessage->setText(tr("Failed to add private key."));
        return false;
    }

    pwalletMain->UpdateTimeFirstKey(1);

    if (rescan) {
        ImportKeyExecutor *executor = new ImportKeyExecutor();
        executor->moveToThread(&thread);
        connect(this, SIGNAL(rescanWallet(CWallet*, CBlockIndex*)), executor, SLOT(rescan(CWallet*, CBlockIndex*)));

        // On stopExecutor signal
        // - quit the Qt event loop in the execution thread
        connect(this, SIGNAL(stopExecutor()), &thread, SLOT(quit()));
        // - queue executor for deletion (in execution thread)
        connect(&thread, SIGNAL(finished()), executor, SLOT(deleteLater()), Qt::DirectConnection);

        // Default implementation of QThread::run() simply spins up an event loop in the thread,
        // which is what we want.
        thread.start();
        ui->privateKeyImportTextMessage->setText(tr("Rescanning..."));
        Q_EMIT rescanWallet(pwalletMain, chainActive.Genesis());
    }

    vchSecret.SetString("");
    return true;
}

void ImportKeysDialog::resetDialogValues()
{
    ui->privateKey->clear();
    ui->privateKeyLabel->clear();
    ui->rescanCheckBox->setCheckState(Qt::Unchecked);
}

void ImportKeysDialog::setOkButtonState(bool fState)
{
    ui->okButton->setEnabled(fState);
}