Skip to content

Commit

Permalink
update UnlockForMint code
Browse files Browse the repository at this point in the history
  • Loading branch information
presstab committed Aug 5, 2015
1 parent 8633e99 commit b251037
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 81 deletions.
35 changes: 21 additions & 14 deletions src/qt/askpassphrasedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <QPushButton>
#include <QKeyEvent>

extern bool fWalletUnlockStakingOnly;

AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
QDialog(parent),
Expand All @@ -35,10 +34,6 @@ AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
ui->warningLabel->setText(tr("Enter the new passphrase to the wallet.<br/>Please use a passphrase of <b>10 or more random characters</b>, or <b>eight or more words</b>."));
setWindowTitle(tr("Encrypt wallet"));
break;
case UnlockStaking:
ui->stakingCheckBox->setChecked(true);
ui->stakingCheckBox->show();
// fallthru
case Unlock: // Ask passphrase
ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
ui->passLabel2->hide();
Expand All @@ -47,6 +42,15 @@ AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
ui->passEdit3->hide();
setWindowTitle(tr("Unlock wallet"));
break;
case UnlockStaking:
case UnlockForMint: // Ask passphrase to unlock wallet for minting
ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet to allow PoS."));
ui->passLabel2->hide();
ui->passEdit2->hide();
ui->passLabel3->hide();
ui->passEdit3->hide();
setWindowTitle(tr("Unlock wallet for Stake"));
break;
case Decrypt: // Ask passphrase
ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
ui->passLabel2->hide();
Expand Down Expand Up @@ -144,19 +148,21 @@ void AskPassphraseDialog::accept()
QDialog::reject(); // Cancelled
}
} break;
case UnlockStaking:
case Unlock:
if(!model->setWalletLocked(false, oldpass))
{
if(!model->setWalletLocked(false, oldpass, false))
QMessageBox::critical(this, tr("Wallet unlock failed"),
tr("The passphrase entered for the wallet decryption was incorrect."));
}
else
{
fWalletUnlockStakingOnly = ui->stakingCheckBox->isChecked();
QDialog::accept(); // Success
}
break;
break;
case UnlockStaking:
case UnlockForMint:
if(!model->setWalletLocked(false, oldpass,true))
QMessageBox::critical(this, tr("Wallet unlock failed"),
tr("The passphrase entered for the wallet decryption was incorrect."));
else
QDialog::accept(); // Success
break;
case Decrypt:
if(!model->setWalletEncrypted(false, oldpass))
{
Expand Down Expand Up @@ -201,8 +207,9 @@ void AskPassphraseDialog::textChanged()
case Encrypt: // New passphrase x2
acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
break;
case UnlockStaking:
case Unlock: // Old passphrase x1
case UnlockStaking:
case UnlockForMint: // Old passphrase x1
case Decrypt:
acceptable = !ui->passEdit1->text().isEmpty();
break;
Expand Down
1 change: 1 addition & 0 deletions src/qt/askpassphrasedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AskPassphraseDialog : public QDialog
Encrypt, /**< Ask passphrase twice and encrypt */
UnlockStaking, /**< Ask passphrase and unlock */
Unlock, /**< Ask passphrase and unlock */
UnlockForMint, /**< Ask passphrase and unlock for minting */
ChangePass, /**< Ask old passphrase + new passphrase twice */
Decrypt /**< Ask passphrase and decrypt wallet */
};
Expand Down
98 changes: 73 additions & 25 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
encryptWalletAction(0),
changePassphraseAction(0),
unlockWalletAction(0),
lockWalletAction(0),
lockWalletToggleAction(0),
aboutQtAction(0),
trayIcon(0),
notificator(0),
Expand Down Expand Up @@ -284,10 +284,9 @@ void BitcoinGUI::createActions()
backupWalletAction->setToolTip(tr("Backup wallet to another location"));
changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase..."), this);
changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
unlockWalletAction = new QAction(QIcon(":/icons/lock_open"), tr("&Unlock Wallet..."), this);
unlockWalletAction->setToolTip(tr("Unlock wallet"));
lockWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Lock Wallet"), this);
lockWalletAction->setToolTip(tr("Lock wallet"));
unlockWalletAction = new QAction(QIcon(":/icons/lock_open"), tr("&Unlock Wallet For PoS..."), this);
unlockWalletAction->setStatusTip(tr("Unlock the wallet for PoS"));
lockWalletToggleAction = new QAction(this);
signMessageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message..."), this);
verifyMessageAction = new QAction(QIcon(":/icons/transaction_0"), tr("&Verify message..."), this);

Expand All @@ -304,8 +303,8 @@ void BitcoinGUI::createActions()
connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
connect(backupWalletAction, SIGNAL(triggered()), this, SLOT(backupWallet()));
connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
connect(unlockWalletAction, SIGNAL(triggered()), this, SLOT(unlockWallet()));
connect(lockWalletAction, SIGNAL(triggered()), this, SLOT(lockWallet()));
connect(unlockWalletAction, SIGNAL(triggered()), this, SLOT(unlockWalletForMint()));
connect(lockWalletToggleAction, SIGNAL(triggered()), this, SLOT(lockWalletToggle()));
connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab()));

Expand Down Expand Up @@ -351,8 +350,8 @@ void BitcoinGUI::createMenuBar()
QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
settings->addAction(encryptWalletAction);
settings->addAction(changePassphraseAction);
settings->addAction(lockWalletToggleAction);
settings->addAction(unlockWalletAction);
settings->addAction(lockWalletAction);
settings->addSeparator();
settings->addAction(optionsAction);

Expand Down Expand Up @@ -385,6 +384,7 @@ void BitcoinGUI::createToolBars()

QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolbar2->addAction(lockWalletToggleAction);
toolbar2->addAction(exportAction);
}

Expand Down Expand Up @@ -850,30 +850,37 @@ void BitcoinGUI::setEncryptionStatus(int status)
case WalletModel::Unencrypted:
labelEncryptionIcon->hide();
encryptWalletAction->setChecked(false);
changePassphraseAction->setEnabled(false);
unlockWalletAction->setVisible(false);
lockWalletAction->setVisible(false);
encryptWalletAction->setEnabled(true);
labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
changePassphraseAction->setEnabled(false);
lockWalletToggleAction->setVisible(false);
unlockWalletAction->setChecked(false);
break;
case WalletModel::Unlocked:
labelEncryptionIcon->show();
labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
encryptWalletAction->setChecked(true);
changePassphraseAction->setEnabled(true);
unlockWalletAction->setVisible(false);
lockWalletAction->setVisible(true);
encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
unlockWalletAction->setChecked(false);
unlockWalletAction->setEnabled(false);
changePassphraseAction->setEnabled(true);
lockWalletToggleAction->setVisible(true);
lockWalletToggleAction->setIcon(QIcon(":/icons/lock_closed"));
lockWalletToggleAction->setText(tr("&Lock Wallet"));
lockWalletToggleAction->setToolTip(tr("Lock wallet"));
break;
case WalletModel::Locked:
labelEncryptionIcon->show();
labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
labelEncryptionIcon->hide();
encryptWalletAction->setChecked(true);
changePassphraseAction->setEnabled(true);
unlockWalletAction->setVisible(true);
lockWalletAction->setVisible(false);
encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
unlockWalletAction->setChecked(false);
unlockWalletAction->setEnabled(true);
changePassphraseAction->setEnabled(true);
lockWalletToggleAction->setVisible(true);
lockWalletToggleAction->setIcon(QIcon(":/icons/lock_open"));
lockWalletToggleAction->setText(tr("&Unlock Wallet..."));
lockWalletToggleAction->setToolTip(tr("Unlock wallet"));
break;
}
}
Expand Down Expand Up @@ -908,27 +915,68 @@ void BitcoinGUI::changePassphrase()
dlg.exec();
}

void BitcoinGUI::lockWalletToggle()
{
if(!walletModel)
return;
// Unlock wallet when requested by wallet model
if(walletModel->getEncryptionStatus() == WalletModel::Locked)
{
AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
dlg.setModel(walletModel);
dlg.exec();
}
else
walletModel->setWalletLocked(true);
}

void BitcoinGUI::unlockWallet()
{
if(!walletModel)
return;
// Unlock wallet when requested by wallet model
if(walletModel->getEncryptionStatus() == WalletModel::Locked)
{
AskPassphraseDialog::Mode mode = sender() == unlockWalletAction ?
AskPassphraseDialog::UnlockStaking : AskPassphraseDialog::Unlock;
AskPassphraseDialog dlg(mode, this);
AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
dlg.setModel(walletModel);
dlg.exec();
}
}

void BitcoinGUI::lockWallet()
void BitcoinGUI::unlockWalletForMint()
{
if(!walletModel)
return;

walletModel->setWalletLocked(true);
// Unlock wallet when requested by user
if(walletModel->getEncryptionStatus() == WalletModel::Locked)
{
AskPassphraseDialog dlg(AskPassphraseDialog::UnlockForMint, this);
dlg.setModel(walletModel);
dlg.exec();

// Only show message if unlock is sucessfull.
if(walletModel->getEncryptionStatus() == WalletModel::Unlocked)
notificator->notify(Notificator::Warning,
tr("Unlock Wallet Information"),
tr("Wallet has been unlocked. \n"
"Proof of Stake has started.\n"));
}
}

void BitcoinGUI::lockWallet()
{
if(!walletModel)
return;

// Lock wallet when requested by user
if(walletModel->getEncryptionStatus() == WalletModel::Unlocked)
walletModel->setWalletLocked(true,"",true);
notificator->notify(Notificator::Warning,
tr("Lock Wallet Information"),
tr("Wallet has been unlocked. \n"
"Proof of Stake has stopped.\n"));

}

void BitcoinGUI::showNormalIfMinimized(bool fToggleHidden)
Expand Down
7 changes: 5 additions & 2 deletions src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class BitcoinGUI : public QMainWindow
QAction *backupWalletAction;
QAction *changePassphraseAction;
QAction *unlockWalletAction;
QAction *lockWalletAction;
QAction *lockWalletToggleAction;
QAction *aboutQtAction;
QAction *openRPCConsoleAction;

Expand Down Expand Up @@ -180,8 +180,11 @@ private slots:
void changePassphrase();
/** Ask for passphrase to unlock wallet temporarily */
void unlockWallet();

/** Ask for passphrase to unlock wallet for the session to mint */
void unlockWalletForMint();

void lockWallet();
void lockWalletToggle();

/** Show window if hidden, unminimize when minimized, rise when obscured or show if hidden and fToggleHidden is true */
void showNormalIfMinimized(bool fToggleHidden = false);
Expand Down
16 changes: 0 additions & 16 deletions src/qt/forms/askpassphrasedialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,6 @@
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="stakingCheckBox">
<property name="enabled">
<bool>true</bool>
</property>
<property name="toolTip">
<string>Serves to disable the trivial sendmoney when OS account compromised. Provides no real security.</string>
</property>
<property name="text">
<string>For staking only</string>
</property>
<property name="visible">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down
25 changes: 15 additions & 10 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ bool WalletModel::setWalletEncrypted(bool encrypted, const SecureString &passphr
}
}

bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase)
bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase, bool formint)
{
if(locked)
{
Expand All @@ -291,7 +291,12 @@ bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase)
else
{
// Unlock
return wallet->Unlock(passPhrase);
bool rc;
rc = wallet->Unlock(passPhrase);
if (rc && formint)
wallet->fWalletUnlockMintOnly=true;
return rc;

}
}

Expand Down Expand Up @@ -356,13 +361,13 @@ void WalletModel::unsubscribeFromCoreSignals()
WalletModel::UnlockContext WalletModel::requestUnlock()
{
bool was_locked = getEncryptionStatus() == Locked;
if ((!was_locked) && fWalletUnlockStakingOnly)
{
setWalletLocked(true);
was_locked = getEncryptionStatus() == Locked;

}
if ((!was_locked) && wallet->fWalletUnlockMintOnly)
{
setWalletLocked(true);
was_locked = getEncryptionStatus() == Locked;
}
if(was_locked)
{
// Request UI to unlock wallet
Expand All @@ -371,7 +376,7 @@ WalletModel::UnlockContext WalletModel::requestUnlock()
// If wallet is still locked, unlock was failed or cancelled, mark context as invalid
bool valid = getEncryptionStatus() != Locked;

return UnlockContext(this, valid, was_locked && !fWalletUnlockStakingOnly);
return UnlockContext(this, valid, was_locked && !wallet->fWalletUnlockMintOnly);
}

WalletModel::UnlockContext::UnlockContext(WalletModel *wallet, bool valid, bool relock):
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class WalletModel : public QObject
// Wallet encryption
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
// Passphrase only needed when unlocking
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString(), bool formint=false);
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
// Wallet backup
bool backupWallet(const QString &filename);
Expand Down
4 changes: 2 additions & 2 deletions src/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Value importprivkey(const Array& params, bool fHelp)
bool fGood = vchSecret.SetString(strSecret);

if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
if (fWalletUnlockStakingOnly)
if (pwalletMain->fWalletUnlockMintOnly) // No dumpprivkey in mint-only mode
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for staking only.");

CKey key;
Expand Down Expand Up @@ -245,7 +245,7 @@ Value dumpprivkey(const Array& params, bool fHelp)
CBitcoinAddress address;
if (!address.SetString(strAddress))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid RATECoin address");
if (fWalletUnlockStakingOnly)
if (pwalletMain->fWalletUnlockMintOnly) // No importprivkey in mint-only mode
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for staking only.");
CKeyID keyID;
if (!address.GetKeyID(keyID))
Expand Down

0 comments on commit b251037

Please sign in to comment.