Merge pull request #396 from jayschwa/nsis-branding
[novacoin.git] / src / rpc.cpp
index 3a57304..fbed626 100644 (file)
@@ -36,6 +36,9 @@ void ThreadRPCServer2(void* parg);
 typedef Value(*rpcfn_type)(const Array& params, bool fHelp);
 extern map<string, rpcfn_type> mapCallTable;
 
+static int64 nWalletUnlockTime;
+static CCriticalSection cs_nWalletUnlockTime;
+
 
 Object JSONRPCError(int code, const string& message)
 {
@@ -311,6 +314,8 @@ Value getinfo(const Array& params, bool fHelp)
     obj.push_back(Pair("keypoololdest", (boost::int64_t)pwalletMain->GetOldestKeyPoolTime()));
     obj.push_back(Pair("keypoolsize",   pwalletMain->GetKeyPoolSize()));
     obj.push_back(Pair("paytxfee",      ValueFromAmount(nTransactionFee)));
+    if (pwalletMain->IsCrypted())
+        obj.push_back(Pair("unlocked_until", (boost::int64_t)nWalletUnlockTime));
     obj.push_back(Pair("errors",        GetWarnings("statusbar")));
     return obj;
 }
@@ -1360,31 +1365,29 @@ void ThreadTopUpKeyPool(void* parg)
 
 void ThreadCleanWalletPassphrase(void* parg)
 {
-    static int64 nWakeTime;
     int64 nMyWakeTime = GetTime() + *((int*)parg);
-    static CCriticalSection cs_nWakeTime;
 
-    if (nWakeTime == 0)
+    if (nWalletUnlockTime == 0)
     {
-        CRITICAL_BLOCK(cs_nWakeTime)
+        CRITICAL_BLOCK(cs_nWalletUnlockTime)
         {
-            nWakeTime = nMyWakeTime;
+            nWalletUnlockTime = nMyWakeTime;
         }
 
-        while (GetTime() < nWakeTime)
-            Sleep(GetTime() - nWakeTime);
+        while (GetTime() < nWalletUnlockTime)
+            Sleep(GetTime() - nWalletUnlockTime);
 
-        CRITICAL_BLOCK(cs_nWakeTime)
+        CRITICAL_BLOCK(cs_nWalletUnlockTime)
         {
-            nWakeTime = 0;
+            nWalletUnlockTime = 0;
         }
     }
     else
     {
-        CRITICAL_BLOCK(cs_nWakeTime)
+        CRITICAL_BLOCK(cs_nWalletUnlockTime)
         {
-            if (nWakeTime < nMyWakeTime)
-                nWakeTime = nMyWakeTime;
+            if (nWalletUnlockTime < nMyWakeTime)
+                nWalletUnlockTime = nMyWakeTime;
         }
         free(parg);
         return;
@@ -1485,6 +1488,29 @@ Value walletpassphrasechange(const Array& params, bool fHelp)
 }
 
 
+Value walletlock(const Array& params, bool fHelp)
+{
+    if (pwalletMain->IsCrypted() && (fHelp || params.size() != 0))
+        throw runtime_error(
+            "walletlock\n"
+            "Removes the wallet encryption key from memory, locking the wallet.\n"
+            "After calling this method, you will need to call walletpassphrase again\n"
+            "before being able to call any methods which require the wallet to be unlocked.");
+    if (fHelp)
+        return true;
+    if (!pwalletMain->IsCrypted())
+        throw JSONRPCError(-15, "Error: running with an unencrypted wallet, but walletlock was called.");
+
+    pwalletMain->Lock();
+    CRITICAL_BLOCK(cs_nWalletUnlockTime)
+    {
+        nWalletUnlockTime = 0;
+    }
+
+    return Value::null;
+}
+
+
 Value encryptwallet(const Array& params, bool fHelp)
 {
     if (!pwalletMain->IsCrypted() && (fHelp || params.size() != 1))
@@ -1701,6 +1727,7 @@ pair<string, rpcfn_type> pCallTable[] =
     make_pair("keypoolrefill",          &keypoolrefill),
     make_pair("walletpassphrase",       &walletpassphrase),
     make_pair("walletpassphrasechange", &walletpassphrasechange),
+    make_pair("walletlock",             &walletlock),
     make_pair("encryptwallet",          &encryptwallet),
     make_pair("validateaddress",        &validateaddress),
     make_pair("getbalance",             &getbalance),
@@ -1737,6 +1764,7 @@ string pAllowInSafeMode[] =
     "backupwallet",
     "keypoolrefill",
     "walletpassphrase",
+    "walletlock",
     "validateaddress",
     "getwork",
 };