Merge pull request #1358 from luke-jr/shared_lockchk

Shared code for wallet lock help and check
This commit is contained in:
Pieter Wuille 2012-05-19 08:34:27 -07:00
commit bd795bd543
1 changed files with 31 additions and 35 deletions

View File

@ -115,6 +115,21 @@ HexBits(unsigned int nBits)
return HexStr(BEGIN(uBits.cBits), END(uBits.cBits)); return HexStr(BEGIN(uBits.cBits), END(uBits.cBits));
} }
static std::string
HelpRequiringPassphrase()
{
return pwalletMain->IsCrypted()
? "\nrequires wallet passphrase to be set with walletpassphrase first"
: "";
}
static inline void
EnsureWalletIsUnlocked()
{
if (pwalletMain->IsLocked())
throw JSONRPCError(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.");
}
enum DecomposeMode { enum DecomposeMode {
DM_NONE = 0, DM_NONE = 0,
DM_HASH, DM_HASH,
@ -731,15 +746,11 @@ Value settxfee(const Array& params, bool fHelp)
Value sendtoaddress(const Array& params, bool fHelp) Value sendtoaddress(const Array& params, bool fHelp)
{ {
if (pwalletMain->IsCrypted() && (fHelp || params.size() < 2 || params.size() > 4)) if (fHelp || params.size() < 2 || params.size() > 4)
throw runtime_error( throw runtime_error(
"sendtoaddress <bitcoinaddress> <amount> [comment] [comment-to]\n" "sendtoaddress <bitcoinaddress> <amount> [comment] [comment-to]\n"
"<amount> is a real and is rounded to the nearest 0.00000001\n" "<amount> is a real and is rounded to the nearest 0.00000001"
"requires wallet passphrase to be set with walletpassphrase first"); + HelpRequiringPassphrase());
if (!pwalletMain->IsCrypted() && (fHelp || params.size() < 2 || params.size() > 4))
throw runtime_error(
"sendtoaddress <bitcoinaddress> <amount> [comment] [comment-to]\n"
"<amount> is a real and is rounded to the nearest 0.00000001");
CBitcoinAddress address(params[0].get_str()); CBitcoinAddress address(params[0].get_str());
if (!address.IsValid()) if (!address.IsValid())
@ -772,8 +783,7 @@ Value signmessage(const Array& params, bool fHelp)
"signmessage <bitcoinaddress> <message>\n" "signmessage <bitcoinaddress> <message>\n"
"Sign a message with the private key of an address"); "Sign a message with the private key of an address");
if (pwalletMain->IsLocked()) EnsureWalletIsUnlocked();
throw JSONRPCError(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.");
string strAddress = params[0].get_str(); string strAddress = params[0].get_str();
string strMessage = params[1].get_str(); string strMessage = params[1].get_str();
@ -1054,15 +1064,11 @@ Value movecmd(const Array& params, bool fHelp)
Value sendfrom(const Array& params, bool fHelp) Value sendfrom(const Array& params, bool fHelp)
{ {
if (pwalletMain->IsCrypted() && (fHelp || params.size() < 3 || params.size() > 6)) if (fHelp || params.size() < 3 || params.size() > 6)
throw runtime_error( throw runtime_error(
"sendfrom <fromaccount> <tobitcoinaddress> <amount> [minconf=1] [comment] [comment-to]\n" "sendfrom <fromaccount> <tobitcoinaddress> <amount> [minconf=1] [comment] [comment-to]\n"
"<amount> is a real and is rounded to the nearest 0.00000001\n" "<amount> is a real and is rounded to the nearest 0.00000001"
"requires wallet passphrase to be set with walletpassphrase first"); + HelpRequiringPassphrase());
if (!pwalletMain->IsCrypted() && (fHelp || params.size() < 3 || params.size() > 6))
throw runtime_error(
"sendfrom <fromaccount> <tobitcoinaddress> <amount> [minconf=1] [comment] [comment-to]\n"
"<amount> is a real and is rounded to the nearest 0.00000001");
string strAccount = AccountFromValue(params[0]); string strAccount = AccountFromValue(params[0]);
CBitcoinAddress address(params[1].get_str()); CBitcoinAddress address(params[1].get_str());
@ -1080,8 +1086,7 @@ Value sendfrom(const Array& params, bool fHelp)
if (params.size() > 5 && params[5].type() != null_type && !params[5].get_str().empty()) if (params.size() > 5 && params[5].type() != null_type && !params[5].get_str().empty())
wtx.mapValue["to"] = params[5].get_str(); wtx.mapValue["to"] = params[5].get_str();
if (pwalletMain->IsLocked()) EnsureWalletIsUnlocked();
throw JSONRPCError(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.");
// Check funds // Check funds
int64 nBalance = GetAccountBalance(strAccount, nMinDepth); int64 nBalance = GetAccountBalance(strAccount, nMinDepth);
@ -1099,15 +1104,11 @@ Value sendfrom(const Array& params, bool fHelp)
Value sendmany(const Array& params, bool fHelp) Value sendmany(const Array& params, bool fHelp)
{ {
if (pwalletMain->IsCrypted() && (fHelp || params.size() < 2 || params.size() > 4)) if (fHelp || params.size() < 2 || params.size() > 4)
throw runtime_error( throw runtime_error(
"sendmany <fromaccount> {address:amount,...} [minconf=1] [comment]\n" "sendmany <fromaccount> {address:amount,...} [minconf=1] [comment]\n"
"amounts are double-precision floating point numbers\n" "amounts are double-precision floating point numbers"
"requires wallet passphrase to be set with walletpassphrase first"); + HelpRequiringPassphrase());
if (!pwalletMain->IsCrypted() && (fHelp || params.size() < 2 || params.size() > 4))
throw runtime_error(
"sendmany <fromaccount> {address:amount,...} [minconf=1] [comment]\n"
"amounts are double-precision floating point numbers");
string strAccount = AccountFromValue(params[0]); string strAccount = AccountFromValue(params[0]);
Object sendTo = params[1].get_obj(); Object sendTo = params[1].get_obj();
@ -1142,8 +1143,7 @@ Value sendmany(const Array& params, bool fHelp)
vecSend.push_back(make_pair(scriptPubKey, nAmount)); vecSend.push_back(make_pair(scriptPubKey, nAmount));
} }
if (pwalletMain->IsLocked()) EnsureWalletIsUnlocked();
throw JSONRPCError(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.");
// Check funds // Check funds
int64 nBalance = GetAccountBalance(strAccount, nMinDepth); int64 nBalance = GetAccountBalance(strAccount, nMinDepth);
@ -1742,17 +1742,13 @@ Value backupwallet(const Array& params, bool fHelp)
Value keypoolrefill(const Array& params, bool fHelp) Value keypoolrefill(const Array& params, bool fHelp)
{ {
if (pwalletMain->IsCrypted() && (fHelp || params.size() > 0)) if (fHelp || params.size() > 0)
throw runtime_error( throw runtime_error(
"keypoolrefill\n" "keypoolrefill\n"
"Fills the keypool, requires wallet passphrase to be set."); "Fills the keypool."
if (!pwalletMain->IsCrypted() && (fHelp || params.size() > 0)) + HelpRequiringPassphrase());
throw runtime_error(
"keypoolrefill\n"
"Fills the keypool.");
if (pwalletMain->IsLocked()) EnsureWalletIsUnlocked();
throw JSONRPCError(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.");
pwalletMain->TopUpKeyPool(); pwalletMain->TopUpKeyPool();