PRCYCoin  2.0.0.7rc1
P2P Digital Currency
bip38tooldialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Copyright (c) 2014-2015 The Dash developers
3 // Copyright (c) 2015-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT/X11 software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #include "bip38tooldialog.h"
9 #include "ui_bip38tooldialog.h"
10 
11 #include "addressbookpage.h"
12 #include "guiutil.h"
13 #include "walletmodel.h"
14 
15 #include "base58.h"
16 #include "bip38.h"
17 #include "init.h"
18 #include "wallet/wallet.h"
19 #include "askpassphrasedialog.h"
20 
21 #include <string>
22 #include <vector>
23 
24 #include <QClipboard>
25 
26 Bip38ToolDialog::Bip38ToolDialog(QWidget* parent) : QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
27  ui(new Ui::Bip38ToolDialog),
28  model(0)
29 {
30  ui->setupUi(this);
31 
32  ui->decryptedKeyOut_DEC->setPlaceholderText(tr("Click \"Decrypt Key\" to compute key"));
33 
34  GUIUtil::setupAddressWidget(ui->addressIn_ENC, this);
35  ui->addressIn_ENC->installEventFilter(this);
36  ui->passphraseIn_ENC->installEventFilter(this);
37  ui->encryptedKeyOut_ENC->installEventFilter(this);
38  ui->encryptedKeyIn_DEC->installEventFilter(this);
39  ui->passphraseIn_DEC->installEventFilter(this);
40  ui->decryptedKeyOut_DEC->installEventFilter(this);
41 }
42 
44 {
45  delete ui;
46 }
47 
49 {
50  this->model = model;
51 }
52 
53 void Bip38ToolDialog::setAddress_ENC(const QString& address)
54 {
55  ui->addressIn_ENC->setText(address);
56  ui->passphraseIn_ENC->setFocus();
57 }
58 
59 void Bip38ToolDialog::setAddress_DEC(const QString& address)
60 {
61  ui->encryptedKeyIn_DEC->setText(address);
62  ui->passphraseIn_DEC->setFocus();
63 }
64 
66 {
67  ui->tabWidget->setCurrentIndex(0);
68  if (fShow)
69  this->show();
70 }
71 
73 {
74  ui->tabWidget->setCurrentIndex(1);
75  if (fShow)
76  this->show();
77 }
78 
80 {
81  if (model && model->getAddressTableModel()) {
84  if (dlg.exec()) {
86  }
87  }
88 }
89 
91 {
92  setAddress_ENC(QApplication::clipboard()->text());
93 }
94 
95 QString specialChar = "\"@!#$%&'()*+,-./:;<=>?`{|}~^_[]\\";
96 QString validChar = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + specialChar;
97 bool isValidPassphrase(QString strPassphrase, QString& strInvalid)
98 {
99  for (int i = 0; i < strPassphrase.size(); i++) {
100  if (!validChar.contains(strPassphrase[i], Qt::CaseSensitive)) {
101  if (QString("\"'").contains(strPassphrase[i]))
102  continue;
103 
104  strInvalid = strPassphrase[i];
105  return false;
106  }
107  }
108 
109  return true;
110 }
111 
113 {
114  if (!model)
115  return;
116 
117  QString qstrPassphrase = ui->passphraseIn_ENC->text();
118  QString strInvalid;
119  if (!isValidPassphrase(qstrPassphrase, strInvalid)) {
120  ui->statusLabel_ENC->setStyleSheet("QLabel { color: red; }");
121  ui->statusLabel_ENC->setText(tr("The entered passphrase is invalid. ") + strInvalid + QString(" is not valid") + QString(" ") + tr("Allowed: 0-9,a-z,A-Z,") + specialChar);
122  return;
123  }
124 
125  CBitcoinAddress addr(ui->addressIn_ENC->text().toStdString());
126  if (!addr.IsValid()) {
127  ui->statusLabel_ENC->setStyleSheet("QLabel { color: red; }");
128  ui->statusLabel_ENC->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
129  return;
130  }
131 
132  CKeyID keyID;
133  if (!addr.GetKeyID(keyID)) {
134  ui->addressIn_ENC->setValid(false);
135  ui->statusLabel_ENC->setStyleSheet("QLabel { color: red; }");
136  ui->statusLabel_ENC->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
137  return;
138  }
139 
141  if (!ctx.isValid()) {
142  ui->statusLabel_ENC->setStyleSheet("QLabel { color: red; }");
143  ui->statusLabel_ENC->setText(tr("Wallet unlock was cancelled."));
144  return;
145  }
146 
147  CKey key;
148  if (!pwalletMain->GetKey(keyID, key)) {
149  ui->statusLabel_ENC->setStyleSheet("QLabel { color: red; }");
150  ui->statusLabel_ENC->setText(tr("Private key for the entered address is not available."));
151  return;
152  }
153 
154  std::string encryptedKey = BIP38_Encrypt(addr.ToString(), qstrPassphrase.toStdString(), key.GetPrivKey_256(), key.IsCompressed());
155  ui->encryptedKeyOut_ENC->setText(QString::fromStdString(encryptedKey));
156 }
157 
159 {
160  GUIUtil::setClipboard(ui->encryptedKeyOut_ENC->text());
161 }
162 
164 {
165  ui->addressIn_ENC->clear();
166  ui->passphraseIn_ENC->clear();
167  ui->encryptedKeyOut_ENC->clear();
168  ui->statusLabel_ENC->clear();
169 
170  ui->addressIn_ENC->setFocus();
171 }
172 
175 {
176  // Paste text from clipboard into recipient field
177  ui->encryptedKeyIn_DEC->setText(QApplication::clipboard()->text());
178 }
179 
181 {
182  std::string strPassphrase = ui->passphraseIn_DEC->text().toStdString();
183  std::string strKey = ui->encryptedKeyIn_DEC->text().toStdString();
184 
185  uint256 privKey;
186  bool fCompressed;
187  if (!BIP38_Decrypt(strPassphrase, strKey, privKey, fCompressed)) {
188  ui->statusLabel_DEC->setStyleSheet("QLabel { color: red; }");
189  ui->statusLabel_DEC->setText(tr("Failed to decrypt.") + QString(" ") + tr("Please check the key and passphrase and try again."));
190  return;
191  }
192 
193  key.Set(privKey.begin(), privKey.end(), fCompressed);
194  CPubKey pubKey = key.GetPubKey();
195  CBitcoinAddress address(pubKey.GetID());
196 
197  ui->decryptedKeyOut_DEC->setText(QString::fromStdString(CBitcoinSecret(key).ToString()));
198  ui->addressOut_DEC->setText(QString::fromStdString(address.ToString()));
199 }
200 
202 {
204  if (!ctx.isValid()) {
205  ui->statusLabel_DEC->setStyleSheet("QLabel { color: red; }");
206  ui->statusLabel_DEC->setText(tr("Wallet unlock was cancelled."));
207  return;
208  }
209 
210  CBitcoinAddress address(ui->addressOut_DEC->text().toStdString());
211  CPubKey pubkey = key.GetPubKey();
212 
213  if (!address.IsValid() || !key.IsValid() || CBitcoinAddress(pubkey.GetID()).ToString() != address.ToString()) {
214  ui->statusLabel_DEC->setStyleSheet("QLabel { color: red; }");
215  ui->statusLabel_DEC->setText(tr("Data Not Valid.") + QString(" ") + tr("Please try again."));
216  return;
217  }
218 
219  CKeyID vchAddress = pubkey.GetID();
220  {
221  ui->statusLabel_DEC->setStyleSheet("QLabel { color: red; }");
222  ui->statusLabel_DEC->setText(tr("Please wait while key is imported"));
223 
225  pwalletMain->SetAddressBook(vchAddress, "", "receive");
226 
227  // Don't throw error in case a key is already there
228  if (pwalletMain->HaveKey(vchAddress)) {
229  ui->statusLabel_DEC->setStyleSheet("QLabel { color: red; }");
230  ui->statusLabel_DEC->setText(tr("Key Already Held By Wallet"));
231  return;
232  }
233 
234  pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1;
235 
236  if (!pwalletMain->AddKeyPubKey(key, pubkey)) {
237  ui->statusLabel_DEC->setStyleSheet("QLabel { color: red; }");
238  ui->statusLabel_DEC->setText(tr("Error Adding Key To Wallet"));
239  return;
240  }
241 
242  // whenever a key is imported, we need to scan the whole chain
243  pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
245  }
246 
247  ui->statusLabel_DEC->setStyleSheet("QLabel { color: green; }");
248  ui->statusLabel_DEC->setText(tr("Successfully Added Private Key To Wallet"));
249 }
250 
252 {
253  ui->encryptedKeyIn_DEC->clear();
254  ui->decryptedKeyOut_DEC->clear();
255  ui->passphraseIn_DEC->clear();
256  ui->statusLabel_DEC->clear();
257 
258  ui->encryptedKeyIn_DEC->setFocus();
259 }
260 
261 bool Bip38ToolDialog::eventFilter(QObject* object, QEvent* event)
262 {
263  if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::FocusIn) {
264  if (ui->tabWidget->currentIndex() == 0) {
265  /* Clear status message on focus change */
266  ui->statusLabel_ENC->clear();
267 
268  /* Select generated signature */
269  if (object == ui->encryptedKeyOut_ENC) {
270  ui->encryptedKeyOut_ENC->selectAll();
271  return true;
272  }
273  } else if (ui->tabWidget->currentIndex() == 1) {
274  /* Clear status message on focus change */
275  ui->statusLabel_DEC->clear();
276  }
277  }
278  return QDialog::eventFilter(object, event);
279 }
CKey::IsCompressed
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:107
Bip38ToolDialog::setAddress_ENC
void setAddress_ENC(const QString &address)
Definition: bip38tooldialog.cpp:53
isValidPassphrase
bool isValidPassphrase(QString strPassphrase, QString &strInvalid)
Definition: bip38tooldialog.cpp:97
AddressBookPage::setModel
void setModel(AddressTableModel *model)
Definition: addressbookpage.cpp:110
base_uint::end
unsigned char * end()
Definition: arith_uint256.h:245
WalletModel::UnlockContext::isValid
bool isValid() const
Definition: walletmodel.h:195
Bip38ToolDialog::on_clearButton_DEC_clicked
void on_clearButton_DEC_clicked()
Definition: bip38tooldialog.cpp:251
CWallet::ScanForWalletTransactions
int ScanForWalletTransactions(CBlockIndex *pindexStart, bool fUpdate=false, bool fromStartup=false, int height=-1)
Scan the block chain (starting in pindexStart) for transactions from or to us.
Definition: wallet.cpp:2062
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:102
CBitcoinAddress::GetKeyID
bool GetKeyID(CKeyID &keyID) const
Definition: base58.cpp:281
Bip38ToolDialog::on_encryptKeyButton_ENC_clicked
void on_encryptKeyButton_ENC_clicked()
Definition: bip38tooldialog.cpp:112
addressbookpage.h
base_uint::begin
unsigned char * begin()
Definition: arith_uint256.h:240
chainActive
CChain chainActive
The currently-connected chain of blocks.
Definition: main.cpp:70
walletmodel.h
CBitcoinAddress
base58-encoded PRCY addresses.
Definition: base58.h:109
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel()
Definition: walletmodel.cpp:363
CKey::Set
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:83
specialChar
QString specialChar
Definition: bip38tooldialog.cpp:95
wallet.h
Bip38ToolDialog::on_pasteButton_ENC_clicked
void on_pasteButton_ENC_clicked()
Definition: bip38tooldialog.cpp:90
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
Bip38ToolDialog::Bip38ToolDialog
Bip38ToolDialog(QWidget *parent)
Definition: bip38tooldialog.cpp:26
CBase58Data::ToString
std::string ToString() const
Definition: base58.cpp:200
Bip38ToolDialog::setModel
void setModel(WalletModel *model)
Definition: bip38tooldialog.cpp:48
CWallet::AddKeyPubKey
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Adds a key to the store, and saves it to disk.
Definition: wallet.cpp:240
CBitcoinSecret
A base58-encoded secret key.
Definition: base58.h:131
GUIUtil::setClipboard
void setClipboard(const QString &str)
Definition: guiutil.cpp:785
CWallet::SetAddressBook
bool SetAddressBook(const CTxDestination &address, const std::string &strName, const std::string &purpose)
Definition: wallet.cpp:4589
CWallet::MarkDirty
void MarkDirty()
Definition: wallet.cpp:1162
CKey::GetPrivKey_256
uint256 GetPrivKey_256()
Definition: key.cpp:58
Bip38ToolDialog::ui
Ui::Bip38ToolDialog * ui
Definition: bip38tooldialog.h:36
Bip38ToolDialog::on_addressBookButton_ENC_clicked
void on_addressBookButton_ENC_clicked()
Definition: bip38tooldialog.cpp:79
Bip38ToolDialog::on_copyKeyButton_ENC_clicked
void on_copyKeyButton_ENC_clicked()
Definition: bip38tooldialog.cpp:158
init.h
Bip38ToolDialog::setAddress_DEC
void setAddress_DEC(const QString &address)
Definition: bip38tooldialog.cpp:59
Bip38ToolDialog::on_importAddressButton_DEC_clicked
void on_importAddressButton_DEC_clicked()
Definition: bip38tooldialog.cpp:201
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:104
AddressBookPage
Widget that shows a list of sending or receiving addresses.
Definition: addressbookpage.h:28
guiutil.h
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
Bip38ToolDialog::eventFilter
bool eventFilter(QObject *object, QEvent *event)
Definition: bip38tooldialog.cpp:261
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:79
Bip38ToolDialog::showTab_ENC
void showTab_ENC(bool fShow)
Definition: bip38tooldialog.cpp:65
Bip38ToolDialog::on_pasteButton_DEC_clicked
void on_pasteButton_DEC_clicked()
Definition: bip38tooldialog.cpp:174
bip38tooldialog.h
Bip38ToolDialog
Definition: bip38tooldialog.h:17
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
Ui
Definition: 2faconfirmdialog.h:7
CKey
An encapsulated private key.
Definition: key.h:39
WalletModel::UnlockContext
Definition: walletmodel.h:189
CWallet::HaveKey
bool HaveKey(const CKeyID &address) const
Check whether a key corresponding to a given address is present in the store.
Definition: wallet.cpp:6932
validChar
QString validChar
Definition: bip38tooldialog.cpp:96
key
CKey key
Definition: bip38tooldialog.cpp:173
AskPassphraseDialog::Context::BIP_38
@ BIP_38
Send PRCY.
Bip38ToolDialog::model
WalletModel * model
Definition: bip38tooldialog.h:37
Bip38ToolDialog::showTab_DEC
void showTab_DEC(bool fShow)
Definition: bip38tooldialog.cpp:72
BIP38_Encrypt
std::string BIP38_Encrypt(std::string strAddress, std::string strPassphrase, uint256 privKey, bool fCompressed)
Definition: bip38.cpp:81
CWallet::mapKeyMetadata
std::map< CKeyID, CKeyMetadata > mapKeyMetadata
Definition: wallet.h:308
base58.h
AddressBookPage::getReturnValue
const QString & getReturnValue() const
Definition: addressbookpage.h:47
Bip38ToolDialog::~Bip38ToolDialog
~Bip38ToolDialog()
Definition: bip38tooldialog.cpp:43
Bip38ToolDialog::on_decryptKeyButton_DEC_clicked
void on_decryptKeyButton_DEC_clicked()
Definition: bip38tooldialog.cpp:180
CBitcoinAddress::IsValid
bool IsValid() const
Definition: base58.cpp:254
GUIUtil::setupAddressWidget
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:86
pwalletMain
CWallet * pwalletMain
Definition: wallet.cpp:49
CWallet::GetKey
bool GetKey(const CKeyID &address, CKey &keyOut) const
GetKey implementation that can derive a HD private key on the fly.
Definition: wallet.cpp:6906
BIP38_Decrypt
bool BIP38_Decrypt(std::string strPassphrase, std::string strEncryptedKey, uint256 &privKey, bool &fCompressed)
Definition: bip38.cpp:132
WalletModel::requestUnlock
UnlockContext requestUnlock(AskPassphraseDialog::Context context, bool relock=false)
Definition: walletmodel.cpp:546
askpassphrasedialog.h
CWallet::nTimeFirstKey
int64_t nTimeFirstKey
Definition: wallet.h:363
Bip38ToolDialog::on_clearButton_ENC_clicked
void on_clearButton_ENC_clicked()
Definition: bip38tooldialog.cpp:163
CChain::Genesis
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or NULL if none.
Definition: chain.h:590
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:143
AddressBookPage::ForSelection
@ ForSelection
Open address book to pick address.
Definition: addressbookpage.h:39
bip38.h
AddressBookPage::ReceivingTab
@ ReceivingTab
Definition: addressbookpage.h:35