PPCoin: stakeonly mode for RPC command 'walletpassphrase'
authorSunny King <p2pcoin@gmail.com>
Sat, 3 Mar 2012 18:57:18 +0000 (18:57 +0000)
committerSunny King <p2pcoin@gmail.com>
Sat, 3 Mar 2012 18:57:18 +0000 (18:57 +0000)
src/bitcoinrpc.cpp
src/wallet.cpp
src/wallet.h

index fd753eb..2678d28 100644 (file)
@@ -909,6 +909,8 @@ Value sendmany(const Array& params, bool fHelp)
 
     if (pwalletMain->IsLocked())
         throw JSONRPCError(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.");
+    if (fWalletUnlockStakeOnly)
+        throw JSONRPCError(-13, "Error: Wallet unlocked for coinstake only.");
 
     // Check funds
     int64 nBalance = GetAccountBalance(strAccount, nMinDepth);
@@ -1442,17 +1444,18 @@ void ThreadCleanWalletPassphrase(void* parg)
 
 Value walletpassphrase(const Array& params, bool fHelp)
 {
-    if (pwalletMain->IsCrypted() && (fHelp || params.size() != 2))
+    if (pwalletMain->IsCrypted() && (fHelp || params.size() < 2 || params.size() > 3))
         throw runtime_error(
-            "walletpassphrase <passphrase> <timeout>\n"
-            "Stores the wallet decryption key in memory for <timeout> seconds.");
+            "walletpassphrase <passphrase> <timeout> [stakeonly]\n"
+            "Stores the wallet decryption key in memory for <timeout> seconds."
+            "stakeonly is an optional true/false allowing only stake creation.");
     if (fHelp)
         return true;
     if (!pwalletMain->IsCrypted())
         throw JSONRPCError(-15, "Error: running with an unencrypted wallet, but walletpassphrase was called.");
 
     if (!pwalletMain->IsLocked())
-        throw JSONRPCError(-17, "Error: Wallet is already unlocked.");
+        throw JSONRPCError(-17, "Error: Wallet is already unlocked, use walletlock first if need to change unlock settings.");
 
     // Note that the walletpassphrase is stored in params[0] which is not mlock()ed
     SecureString strWalletPass;
@@ -1475,6 +1478,12 @@ Value walletpassphrase(const Array& params, bool fHelp)
     int* pnSleepTime = new int(params[1].get_int());
     CreateThread(ThreadCleanWalletPassphrase, pnSleepTime);
 
+    // ppcoin: if user OS account compromised prevent trivial sendmoney commands
+    if (params.size() > 2)
+        fWalletUnlockStakeOnly = params[2].get_bool();
+    else
+        fWalletUnlockStakeOnly = false;
+
     return Value::null;
 }
 
@@ -2552,6 +2561,7 @@ int CommandLineRPC(int argc, char *argv[])
         if (strMethod == "listtransactions"       && n > 2) ConvertTo<boost::int64_t>(params[2]);
         if (strMethod == "listaccounts"           && n > 0) ConvertTo<boost::int64_t>(params[0]);
         if (strMethod == "walletpassphrase"       && n > 1) ConvertTo<boost::int64_t>(params[1]);
+        if (strMethod == "walletpassphrase"       && n > 2) ConvertTo<bool>(params[2]);
         if (strMethod == "listsinceblock"         && n > 1) ConvertTo<boost::int64_t>(params[1]);
         if (strMethod == "sendmany"               && n > 1)
         {
index 0ef2167..564362b 100644 (file)
@@ -43,6 +43,10 @@ bool CWallet::AddCryptedKey(const vector<unsigned char> &vchPubKey, const vector
     return false;
 }
 
+// ppcoin: optional setting to create coinstake only when unlocked;
+//         serves to disable the trivial sendmoney when OS account compromised
+bool fWalletUnlockStakeOnly = false;
+
 bool CWallet::Unlock(const SecureString& strWalletPassphrase)
 {
     if (!IsLocked())
@@ -1184,6 +1188,12 @@ string CWallet::SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew,
         printf("SendMoney() : %s", strError.c_str());
         return strError;
     }
+    if (fWalletUnlockStakeOnly)
+    {
+        string strError = _("Error: Wallet unlocked for coinstake only, unable to create transaction.");
+        printf("SendMoney() : %s", strError.c_str());
+        return strError;
+    }
     if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired))
     {
         string strError;
index eec8a06..8769588 100644 (file)
@@ -10,6 +10,8 @@
 #include "key.h"
 #include "script.h"
 
+extern bool fWalletUnlockStakeOnly;
+
 class CWalletTx;
 class CReserveKey;
 class CWalletDB;