Skip to content

Commit

Permalink
unlockformint
Browse files Browse the repository at this point in the history
  • Loading branch information
presstab committed Sep 25, 2014
1 parent a34c338 commit 520e754
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 29 deletions.
27 changes: 21 additions & 6 deletions src/qt/askpassphrasedialog.cpp
Expand Up @@ -47,6 +47,16 @@ AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
ui->passEdit3->hide();
setWindowTitle(tr("Unlock wallet"));
break;

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 @@ -146,17 +156,21 @@ void AskPassphraseDialog::accept()
} break;
case UnlockMinting:
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
{
fWalletUnlockMintOnly = ui->mintingCheckBox->isChecked();
QDialog::accept(); // Success
}
break;

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 @@ -203,6 +217,7 @@ void AskPassphraseDialog::textChanged()
break;
case UnlockMinting:
case Unlock: // Old passphrase x1
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
Expand Up @@ -20,6 +20,7 @@ class AskPassphraseDialog : public QDialog
Encrypt, /**< Ask passphrase twice and encrypt */
UnlockMinting, /**< Ask passphrase and unlock for minting only */
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
57 changes: 53 additions & 4 deletions src/qt/bitcoingui.cpp
Expand Up @@ -170,7 +170,7 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
frameBlocksLayout->setContentsMargins(3,0,3,0);
frameBlocksLayout->setSpacing(3);
labelEncryptionIcon = new QLabel();
labelEncryptionIcon = new GUIUtil::ClickableLabel();
labelMintingIcon = new QLabel();
labelConnectionsIcon = new QLabel();
labelBlocksIcon = new QLabel();
Expand Down Expand Up @@ -344,8 +344,8 @@ void BitcoinGUI::createActions()
encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet..."), this);
encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
encryptWalletAction->setCheckable(true);
unlockWalletAction = new QAction(QIcon(":/icons/lock_open"), tr("&Unlock Wallet"), this);
unlockWalletAction->setToolTip(tr("Unlock an Encrypted Wallet"));
unlockWalletAction = new QAction(QIcon(":/icons/lock_open"), tr("&Unlock Wallet For PoS..."), this);
unlockWalletAction->setStatusTip(tr("Unlock the wallet for PoS"));
unlockWalletAction->setCheckable(true);
backupWalletAction = new QAction(QIcon(":/icons/filesave"), tr("&Backup Wallet..."), this);
backupWalletAction->setToolTip(tr("Backup wallet to another location"));
Expand Down Expand Up @@ -383,7 +383,7 @@ void BitcoinGUI::createActions()
connect(lockWalletToggleAction, SIGNAL(triggered()), this, SLOT(lockWalletToggle()));
connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab()));
connect(unlockWalletAction, SIGNAL(triggered()), this, SLOT(unlockWallet()));
connect(unlockWalletAction, SIGNAL(triggered()), this, SLOT(unlockWalletForMint()));

connect(blockAction, SIGNAL(triggered()), this, SLOT(gotoBlockBrowser()));
//connect(blocksIconAction, SIGNAL(triggered()), this, SLOT(blocksIconClicked()));
Expand Down Expand Up @@ -616,6 +616,15 @@ void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
}
#endif

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

if(walletModel->getEncryptionStatus() == WalletModel::Locked)
unlockWalletForMint();
}

void BitcoinGUI::optionsClicked()
{
if(!clientModel || !clientModel->getOptionsModel())
Expand Down Expand Up @@ -974,8 +983,11 @@ void BitcoinGUI::setEncryptionStatus(int status)
labelEncryptionIcon->hide();
encryptWalletAction->setChecked(false);
encryptWalletAction->setEnabled(true);
labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
disconnect(labelEncryptionIcon,SIGNAL(clicked()), this, SLOT(lockIconClicked()));
changePassphraseAction->setEnabled(false);
lockWalletToggleAction->setVisible(false);
unlockWalletAction->setChecked(false);
break;
case WalletModel::Unlocked:
labelEncryptionIcon->show();
Expand Down Expand Up @@ -1004,6 +1016,7 @@ void BitcoinGUI::setEncryptionStatus(int status)
lockWalletToggleAction->setIcon(QIcon(":/icons/lock_open"));
lockWalletToggleAction->setText(tr("&Unlock Wallet..."));
lockWalletToggleAction->setToolTip(tr("Unlock wallet"));
disconnect(labelEncryptionIcon,SIGNAL(clicked()), this, SLOT(lockIconClicked()));
break;
}
}
Expand Down Expand Up @@ -1126,6 +1139,42 @@ void BitcoinGUI::unlockWallet()
}
}

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

// 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)
{
// activateWindow() (sometimes) helps with keyboard focus on Windows
Expand Down
10 changes: 7 additions & 3 deletions src/qt/bitcoingui.h
Expand Up @@ -184,7 +184,8 @@ private slots:
void gotoSignMessageTab(QString addr = "");
/** Show Sign/Verify Message dialog and switch to verify message tab */
void gotoVerifyMessageTab(QString addr = "");

/** Allow user to unlock wallet from click */
void lockIconClicked();
/** Show configuration dialog */
void optionsClicked();
/** Show about dialog */
Expand All @@ -208,11 +209,14 @@ private slots:
void backupWallet();
/** Change encrypted wallet passphrase */
void changePassphrase();
/** Lock Wallet */
void lockWallet();
/** Toggle unlocking wallet temporarily */
void lockWalletToggle();

/** Ask for passphrase to unlock wallet temporarily */
void unlockWallet();

/** Ask for passphrase to unlock wallet for the session to mint */
void unlockWalletForMint();
/** Show window if hidden, unminimize when minimized, rise when obscured or show if hidden and fToggleHidden is true */
void showNormalIfMinimized(bool fToggleHidden = false);
/** simply calls showNormalIfMinimized(true) for use in SLOT() macro */
Expand Down
14 changes: 14 additions & 0 deletions src/qt/guiutil.cpp
Expand Up @@ -453,5 +453,19 @@ void HelpMessageBox::showOrPrint()
#endif
}

ClickableLabel::ClickableLabel( const QString& text, QWidget * parent ) : QLabel(parent)
{
this->setText(text);
}

ClickableLabel::~ClickableLabel()
{
}

void ClickableLabel::mouseReleaseEvent ( QMouseEvent * event )
{
emit clicked();
}

} // namespace GUIUtil

18 changes: 18 additions & 0 deletions src/qt/guiutil.h
Expand Up @@ -4,6 +4,7 @@
#include <QString>
#include <QObject>
#include <QMessageBox>
#include <QLabel>

QT_BEGIN_NAMESPACE
class QFont;
Expand All @@ -12,6 +13,7 @@ class QWidget;
class QDateTime;
class QUrl;
class QAbstractItemView;
class QLabel;
QT_END_NAMESPACE
class SendCoinsRecipient;

Expand Down Expand Up @@ -114,6 +116,22 @@ namespace GUIUtil
QString coreOptions;
QString uiOptions;
};

class ClickableLabel : public QLabel
{

Q_OBJECT

public:
explicit ClickableLabel( const QString& text ="", QWidget * parent = 0 );
~ClickableLabel();

signals:
void clicked();

protected:
void mouseReleaseEvent ( QMouseEvent * event );
};

} // namespace GUIUtil

Expand Down
18 changes: 15 additions & 3 deletions src/qt/walletmodel.cpp
Expand Up @@ -280,7 +280,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 @@ -290,7 +290,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 @@ -423,6 +428,13 @@ void WalletModel::unsubscribeFromCoreSignals()
WalletModel::UnlockContext WalletModel::requestUnlock()
{
bool 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 @@ -431,7 +443,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);
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
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
Expand Up @@ -47,7 +47,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 (fWalletUnlockMintOnly) // ppcoin: no importprivkey in mint-only mode
if (pwalletMain->fWalletUnlockMintOnly) // No importprivkey in mint-only mode
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");

CKey key;
Expand Down Expand Up @@ -82,7 +82,7 @@ Value dumpprivkey(const Array& params, bool fHelp)
CBitcoinAddress address;
if (!address.SetString(strAddress))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid HyperStake address");
if (fWalletUnlockMintOnly) // ppcoin: no dumpprivkey in mint-only mode
if (pwalletMain->fWalletUnlockMintOnly) // No dumpprivkey in mint-only mode
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
CKeyID keyID;
if (!address.GetKeyID(keyID))
Expand Down
8 changes: 4 additions & 4 deletions src/rpcwallet.cpp
Expand Up @@ -31,7 +31,7 @@ void EnsureWalletIsUnlocked()
{
if (pwalletMain->IsLocked())
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
if (fWalletUnlockMintOnly)
if (pwalletMain->fWalletUnlockMintOnly)
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Wallet unlocked for block minting only.");
}

Expand Down Expand Up @@ -1783,10 +1783,10 @@ Value walletpassphrase(const Array& params, bool fHelp)

// ppcoin: if user OS account compromised prevent trivial sendmoney commands
if (params.size() > 2)
fWalletUnlockMintOnly = params[2].get_bool();
pwalletMain->fWalletUnlockMintOnly = params[2].get_bool();
else
fWalletUnlockMintOnly = false;

pwalletMain->fWalletUnlockMintOnly = false;
return Value::null;
}

Expand Down
4 changes: 0 additions & 4 deletions src/wallet.cpp
Expand Up @@ -85,10 +85,6 @@ bool CWallet::AddCScript(const CScript& redeemScript)
return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
}

// ppcoin: optional setting to unlock wallet for block minting only;
// serves to disable the trivial sendmoney when OS account compromised
bool fWalletUnlockMintOnly = false;

bool CWallet::Unlock(const SecureString& strWalletPassphrase)
{
if (!IsLocked())
Expand Down
4 changes: 3 additions & 1 deletion src/wallet.h
Expand Up @@ -18,7 +18,6 @@
#include "util.h"
#include "walletdb.h"

extern bool fWalletUnlockMintOnly;
class CAccountingEntry;
class CWalletTx;
class CReserveKey;
Expand Down Expand Up @@ -91,6 +90,7 @@ class CWallet : public CCryptoKeyStore
int64 nStakeForCharityMin;
int64 nStakeForCharityMax;
CBitcoinAddress strStakeForCharityAddress;
bool fWalletUnlockMintOnly;

std::set<int64> setKeyPool;

Expand All @@ -113,6 +113,7 @@ class CWallet : public CCryptoKeyStore
strStakeForCharityAddress = "";
nStakeForCharityMin = 0;
nStakeForCharityMax = 0;
fWalletUnlockMintOnly = false;
}
CWallet(std::string strWalletFileIn)
{
Expand All @@ -129,6 +130,7 @@ class CWallet : public CCryptoKeyStore
strStakeForCharityAddress = "";
nStakeForCharityMin = MIN_TXOUT_AMOUNT;
nStakeForCharityMax = MAX_MONEY;
fWalletUnlockMintOnly = false;
}

std::map<uint256, CWalletTx> mapWallet;
Expand Down

0 comments on commit 520e754

Please sign in to comment.