Fix walletpassphrase rpc

This commit is contained in:
myriadcoin
2014-03-19 23:15:53 +02:00
parent 8088b4a295
commit d70127e8c7
4 changed files with 83 additions and 10 deletions
+3
View File
@@ -72,6 +72,8 @@ Value importprivkey(const Array& params, bool fHelp)
"importprivkey <myriadcoinprivkey> [label] [rescan=true]\n"
"Adds a private key (as returned by dumpprivkey) to your wallet.");
EnsureWalletIsUnlocked();
string strSecret = params[0].get_str();
string strLabel = "";
if (params.size() > 1)
@@ -88,6 +90,7 @@ Value importprivkey(const Array& params, bool fHelp)
if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
CKey key = vchSecret.GetKey();
if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range");
CPubKey pubkey = key.GetPubKey();
CKeyID vchAddress = pubkey.GetID();
{
+66 -10
View File
@@ -1277,11 +1277,63 @@ Value keypoolrefill(const Array& params, bool fHelp)
}
static void LockWallet(CWallet* pWallet)
//static void LockWallet(CWallet* pWallet)
//{
// LOCK(cs_nWalletUnlockTime);
// nWalletUnlockTime = 0;
// pWallet->Lock();
//}
void ThreadCleanWalletPassphrase(void* parg)
{
LOCK(cs_nWalletUnlockTime);
nWalletUnlockTime = 0;
pWallet->Lock();
// Make this thread recognisable as the wallet relocking thread
RenameThread("bitcoin-lock-wa");
int64 nMyWakeTime = GetTimeMillis() + *((int64*)parg) * 1000;
ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime);
if (nWalletUnlockTime == 0)
{
nWalletUnlockTime = nMyWakeTime;
do
{
if (nWalletUnlockTime==0)
break;
int64 nToSleep = nWalletUnlockTime - GetTimeMillis();
if (nToSleep <= 0)
break;
LEAVE_CRITICAL_SECTION(cs_nWalletUnlockTime);
MilliSleep(nToSleep);
ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime);
} while(1);
if (nWalletUnlockTime)
{
nWalletUnlockTime = 0;
pwalletMain->Lock();
}
}
else
{
if (nWalletUnlockTime < nMyWakeTime)
nWalletUnlockTime = nMyWakeTime;
}
LEAVE_CRITICAL_SECTION(cs_nWalletUnlockTime);
delete (int64*)parg;
}
void ThreadTopUpKeyPool(void* parg)
{
// Make this thread recognisable as the key-topping-up thread
RenameThread("bitcoin-key-top");
pwalletMain->TopUpKeyPool();
}
Value walletpassphrase(const Array& params, bool fHelp)
@@ -1312,13 +1364,17 @@ Value walletpassphrase(const Array& params, bool fHelp)
"walletpassphrase <passphrase> <timeout>\n"
"Stores the wallet decryption key in memory for <timeout> seconds.");
pwalletMain->TopUpKeyPool();
int64 nSleepTime = params[1].get_int64();
LOCK(cs_nWalletUnlockTime);
nWalletUnlockTime = GetTime() + nSleepTime;
RPCRunLater("lockwallet", boost::bind(LockWallet, pwalletMain), nSleepTime);
//pwalletMain->TopUpKeyPool();
//int64 nSleepTime = params[1].get_int64();
//LOCK(cs_nWalletUnlockTime);
//nWalletUnlockTime = GetTime() + nSleepTime;
//RPCRunLater("lockwallet", boost::bind(LockWallet, pwalletMain), nSleepTime);
NewThread(ThreadTopUpKeyPool, NULL);
int64* pnSleepTime = new int64(params[1].get_int64());
NewThread(ThreadCleanWalletPassphrase, pnSleepTime);
return Value::null;
}
+12
View File
@@ -1468,3 +1468,15 @@ void RenameThread(const char* name)
(void)name;
#endif
}
bool NewThread(void(*pfn)(void*), void* parg)
{
try
{
boost::thread(pfn, parg); // thread detaches when out of scope
} catch(boost::thread_resource_error &e) {
printf("Error creating thread: %s\n", e.what());
return false;
}
return true;
}
+2
View File
@@ -580,4 +580,6 @@ template <typename Callable> void TraceThread(const char* name, Callable func)
}
}
bool NewThread(void(*pfn)(void*), void* parg);
#endif