From e4ff4e6898d378b1a3e83791034a7af455fde3ab Mon Sep 17 00:00:00 2001 From: gavinandresen Date: Mon, 22 Nov 2010 15:53:20 +0000 Subject: Depracate "label" API, replacing with account New RPC methods: move, sendfrom Change to getbalance (now takes optional [account] argument) Renamed methods with "label" in their names. sendtoaddress returns hexadecimal transaction ID instead of "sent". git-svn-id: https://bitcoin.svn.sourceforge.net/svnroot/bitcoin/trunk@188 1a98c847-1fd6-4fd8-948a-caf3550aa51b --- rpc.cpp | 420 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 341 insertions(+), 79 deletions(-) (limited to 'rpc.cpp') diff --git a/rpc.cpp b/rpc.cpp index 0c71993c2..271d36c2f 100644 --- a/rpc.cpp +++ b/rpc.cpp @@ -58,6 +58,17 @@ void PrintConsole(const char* format, ...) } +int64 AmountFromValue(const Value& value) +{ + double dAmount = value.get_real(); + if (dAmount <= 0.0 || dAmount > 21000000.0) + throw JSONRPCError(-3, "Invalid amount"); + int64 nAmount = roundint64(dAmount * 100.00) * CENT; + if (!MoneyRange(nAmount)) + throw JSONRPCError(-3, "Invalid amount"); + return nAmount; +} + @@ -86,7 +97,8 @@ Value help(const Array& params, bool fHelp) string strMethod = (*mi).first; // We already filter duplicates, but these deprecated screw up the sort order if (strMethod == "getamountreceived" || - strMethod == "getallreceived") + strMethod == "getallreceived" || + (strMethod.find("label") != string::npos)) continue; if (strCommand != "" && strMethod != strCommand) continue; @@ -183,17 +195,6 @@ Value getdifficulty(const Array& params, bool fHelp) } -Value getbalance(const Array& params, bool fHelp) -{ - if (fHelp || params.size() != 0) - throw runtime_error( - "getbalance\n" - "Returns the server's available balance."); - - return ((double)GetBalance() / (double)COIN); -} - - Value getgenerate(const Array& params, bool fHelp) { if (fHelp || params.size() != 0) @@ -273,71 +274,120 @@ Value getnewaddress(const Array& params, bool fHelp) { if (fHelp || params.size() > 1) throw runtime_error( - "getnewaddress [label]\n" + "getnewaddress [account]\n" "Returns a new bitcoin address for receiving payments. " - "If [label] is specified (recommended), it is added to the address book " - "so payments received with the address will be labeled."); + "If [account] is specified (recommended), it is added to the address book " + "so payments received with the address will be credited to [account]."); - // Parse the label first so we don't generate a key if there's an error - string strLabel; + // Parse the account first so we don't generate a key if there's an error + string strAccount; if (params.size() > 0) - strLabel = params[0].get_str(); + strAccount = params[0].get_str(); // Generate a new key that is added to wallet string strAddress = PubKeyToAddress(CWalletDB().GetKeyFromKeyPool()); - SetAddressBookName(strAddress, strLabel); + SetAddressBookName(strAddress, strAccount); return strAddress; } -Value setlabel(const Array& params, bool fHelp) +Value getaccountaddress(const Array& params, bool fHelp) +{ + if (fHelp || params.size() != 1) + throw runtime_error( + "getaccountaddress \n" + "Returns the current bitcoin address for receiving payments to this account."); + + // Parse the account first so we don't generate a key if there's an error + string strAccount = params[0].get_str(); + + CRITICAL_BLOCK(cs_mapWallet) + { + CWalletDB walletdb; + walletdb.TxnBegin(); + + CAccount account; + walletdb.ReadAccount(strAccount, account); + + // Check if the current key has been used + if (!account.vchPubKey.empty()) + { + CScript scriptPubKey; + scriptPubKey.SetBitcoinAddress(account.vchPubKey); + for (map::iterator it = mapWallet.begin(); + it != mapWallet.end() && !account.vchPubKey.empty(); + ++it) + { + const CWalletTx& wtx = (*it).second; + foreach(const CTxOut& txout, wtx.vout) + if (txout.scriptPubKey == scriptPubKey) + account.vchPubKey.clear(); + } + } + + // Generate a new key + if (account.vchPubKey.empty()) + { + account.vchPubKey = CWalletDB().GetKeyFromKeyPool(); + string strAddress = PubKeyToAddress(account.vchPubKey); + SetAddressBookName(strAddress, strAccount); + walletdb.WriteAccount(strAccount, account); + } + + walletdb.TxnCommit(); + return PubKeyToAddress(account.vchPubKey); + } +} + + +Value setaccount(const Array& params, bool fHelp) { if (fHelp || params.size() < 1 || params.size() > 2) throw runtime_error( - "setlabel