Skip to content

Commit

Permalink
Merge upstream pull request xpc-wg#92
Browse files Browse the repository at this point in the history
  • Loading branch information
naomi-mcrn committed Jul 30, 2019
2 parents 5e1e4c6 + e69f60b commit 90b4ccb
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 17 deletions.
10 changes: 10 additions & 0 deletions src/interfaces/wallet.cpp
Expand Up @@ -475,6 +475,16 @@ class WalletImpl : public Wallet
return m_wallet.vRewardDistributionPcts;
}

bool getMintOnly() override
{
return m_wallet.fWalletUnlockMintOnly;
}

void setMintOnly(bool f) override
{
m_wallet.fWalletUnlockMintOnly = f;
}

std::shared_ptr<CWallet> m_shared_wallet;
CWallet& m_wallet;
};
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/wallet.h
Expand Up @@ -284,6 +284,9 @@ class Wallet

//! Get list of percentages of staking reward distribution.
virtual std::vector<std::pair<std::string, std::uint8_t>> getRewardDistributionPcts() = 0;

virtual bool getMintOnly() = 0;
virtual void setMintOnly(bool f) = 0;
};

//! Tracking object returned by CreateTransaction and passed to CommitTransaction.
Expand Down
6 changes: 4 additions & 2 deletions src/qt/bitcoingui.cpp
Expand Up @@ -1090,6 +1090,7 @@ void BitcoinGUI::setHDStatus(int hdEnabled)

void BitcoinGUI::setEncryptionStatus(int status)
{
bool mintonly;
switch(status)
{
case WalletModel::Unencrypted:
Expand All @@ -1107,8 +1108,9 @@ void BitcoinGUI::setEncryptionStatus(int status)
encryptWalletAction->setChecked(true);
changePassphraseAction->setEnabled(true);
encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
decryptForMintingAction->setEnabled(fWalletUnlockMintOnly);
decryptForMintingAction->setChecked(fWalletUnlockMintOnly);
mintonly = walletFrame->currentWalletView()->getWalletModel()->getMintOnly();
decryptForMintingAction->setEnabled(mintonly);
decryptForMintingAction->setChecked(mintonly);
break;
case WalletModel::Locked:
labelWalletEncryptionIcon->show();
Expand Down
16 changes: 11 additions & 5 deletions src/qt/walletmodel.cpp
Expand Up @@ -462,7 +462,7 @@ void WalletModel::unsubscribeFromCoreSignals()
WalletModel::UnlockContext WalletModel::requestUnlock()
{
bool was_locked = getEncryptionStatus() == Locked;
if ((!was_locked) && fWalletUnlockMintOnly)
if ((!was_locked) && getMintOnly())
{
setWalletLocked(true);
was_locked = getEncryptionStatus() == Locked;
Expand All @@ -475,7 +475,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 && !fWalletUnlockMintOnly);
return UnlockContext(this, valid, was_locked && !getMintOnly());
}

WalletModel::UnlockContext::UnlockContext(WalletModel *_wallet, bool _valid, bool _relock):
Expand Down Expand Up @@ -600,6 +600,12 @@ bool WalletModel::isMultiwallet()
return m_node.getWallets().size() > 1;
}

// xpchain: optional setting to unlock wallet for block minting only;
// serves to disable the trivial sendmoney when OS account compromised
bool fWalletUnlockMintOnly = false;
bool WalletModel::getMintOnly()
{
return m_wallet->getMintOnly();
}

void WalletModel::setMintOnly(bool f)
{
m_wallet->setMintOnly(f);
}
5 changes: 3 additions & 2 deletions src/qt/walletmodel.h
Expand Up @@ -21,8 +21,6 @@

#include <QObject>

extern bool fWalletUnlockMintOnly;

enum class OutputType;

class AddressTableModel;
Expand Down Expand Up @@ -213,6 +211,9 @@ class WalletModel : public QObject
bool isMultiwallet();

AddressTableModel* getAddressTableModel() const { return addressTableModel; }

bool getMintOnly();
void setMintOnly(bool f);
private:
std::unique_ptr<interfaces::Wallet> m_wallet;
std::unique_ptr<interfaces::Handler> m_handler_unload;
Expand Down
7 changes: 3 additions & 4 deletions src/qt/walletview.cpp
Expand Up @@ -274,13 +274,13 @@ void WalletView::decryptForMinting(bool status)
if(walletModel->getEncryptionStatus() != WalletModel::Locked)
return;

fWalletUnlockMintOnly = true;
walletModel->setMintOnly(true);
AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
dlg.setModel(walletModel);
dlg.exec();

if(walletModel->getEncryptionStatus() != WalletModel::Unlocked){
fWalletUnlockMintOnly = false;
walletModel->setMintOnly(false);
updateEncryptionStatus();
return;
}
Expand All @@ -290,11 +290,10 @@ void WalletView::decryptForMinting(bool status)
if(walletModel->getEncryptionStatus() != WalletModel::Unlocked)
return;

if (!fWalletUnlockMintOnly)
if (!walletModel->getMintOnly())
return;

walletModel->setWalletLocked(true);
fWalletUnlockMintOnly = false;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Expand Up @@ -69,6 +69,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "listaccounts", 0, "minconf" },
{ "listaccounts", 1, "include_watchonly" },
{ "walletpassphrase", 1, "timeout" },
{ "walletpassphrase", 2, "mintonly" },
{ "getblocktemplate", 0, "template_request" },
{ "listsinceblock", 1, "target_confirmations" },
{ "listsinceblock", 2, "include_watchonly" },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/protocol.h
Expand Up @@ -90,6 +90,7 @@ enum RPCErrorCode

//! Unused reserved codes, kept around for backwards compatibility. Do not reuse.
RPC_FORBIDDEN_BY_SAFE_MODE = -2, //!< Server is in safe mode, and command is not allowed in safe mode
RPC_WALLET_MINTONLY = -102,
};

UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/crypter.h
Expand Up @@ -143,7 +143,7 @@ class CCryptoKeyStore : public CBasicKeyStore

bool IsCrypted() const { return fUseCrypto; }
bool IsLocked() const;
bool Lock();
virtual bool Lock();

virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
Expand Down
14 changes: 11 additions & 3 deletions src/wallet/rpcwallet.cpp
Expand Up @@ -90,6 +90,9 @@ void EnsureWalletIsUnlocked(CWallet * const pwallet)
if (pwallet->IsLocked()) {
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
}
if (pwallet->fWalletUnlockMintOnly) {
throw JSONRPCError(RPC_WALLET_MINTONLY, "Error: Wallet is unlocked for minting only.");
}
}

static void WalletTxToJSON(const CWalletTx& wtx, UniValue& entry)
Expand Down Expand Up @@ -2558,14 +2561,15 @@ static UniValue walletpassphrase(const JSONRPCRequest& request)
return NullUniValue;
}

if (request.fHelp || request.params.size() != 2) {
if (request.fHelp || request.params.size() < 2 || request.params.size() > 3) {
throw std::runtime_error(
"walletpassphrase \"passphrase\" timeout\n"
"walletpassphrase \"passphrase\" timeout ( mintonly )\n"
"\nStores the wallet decryption key in memory for 'timeout' seconds.\n"
"This is needed prior to performing transactions related to private keys such as sending xpchains\n"
"\nArguments:\n"
"1. \"passphrase\" (string, required) The wallet passphrase\n"
"2. timeout (numeric, required) The time to keep the decryption key in seconds; capped at 100000000 (~3 years).\n"
"3. mintonly (boolean, optional, default: false) allowing only block minting.\n"
"\nNote:\n"
"Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
"time that overrides the old one.\n"
Expand Down Expand Up @@ -2620,6 +2624,10 @@ static UniValue walletpassphrase(const JSONRPCRequest& request)
pwallet->nRelockTime = GetTime() + nSleepTime;
RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), std::bind(LockWallet, pwallet), nSleepTime);

if(request.params.size() > 2)
pwallet->fWalletUnlockMintOnly = request.params[2].get_bool();
else
pwallet->fWalletUnlockMintOnly = false;
return NullUniValue;
}

Expand Down Expand Up @@ -5011,7 +5019,7 @@ static const CRPCCommand commands[] =
{ "wallet", "unloadwallet", &unloadwallet, {"wallet_name"} },
{ "wallet", "walletlock", &walletlock, {} },
{ "wallet", "walletpassphrasechange", &walletpassphrasechange, {"oldpassphrase","newpassphrase"} },
{ "wallet", "walletpassphrase", &walletpassphrase, {"passphrase","timeout"} },
{ "wallet", "walletpassphrase", &walletpassphrase, {"passphrase","timeout","mintonly"} },
{ "wallet", "removeprunedfunds", &removeprunedfunds, {"txid"} },
{ "wallet", "rescanblockchain", &rescanblockchain, {"start_height", "stop_height"} },
{ "wallet", "sethdseed", &sethdseed, {"newkeypool","seed"} },
Expand Down
5 changes: 5 additions & 0 deletions src/wallet/wallet.cpp
Expand Up @@ -4569,3 +4569,8 @@ std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outpu
}
return groups;
}

bool CWallet::Lock() {
fWalletUnlockMintOnly = false;
return CCryptoKeyStore::Lock();
}
3 changes: 3 additions & 0 deletions src/wallet/wallet.h
Expand Up @@ -1221,6 +1221,9 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface

/** Clear percentages of staking reward distribution, and erase it from database file */
bool DelRewardDistributionPcts();

bool fWalletUnlockMintOnly = false;
bool Lock() override;
};

/** A key allocated from the key pool. */
Expand Down

0 comments on commit 90b4ccb

Please sign in to comment.