From f764674865abcb0afd71a785913c08542bdd04a0 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Thu, 25 Jul 2013 09:36:57 -0300 Subject: [PATCH] create/list users in wallet --- src/bitcoinrpc.cpp | 7 +-- src/bitcoinrpc.h | 7 +-- src/rpcwallet.cpp | 153 ++++++++------------------------------------- 3 files changed, 29 insertions(+), 138 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index f8b4f2a3..2ac58748 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -206,11 +206,8 @@ static const CRPCCommand vRPCCommands[] = { "gethashespersec", &gethashespersec, true, false }, { "getinfo", &getinfo, true, false }, { "getmininginfo", &getmininginfo, true, false }, - { "getnewaddress", &getnewaddress, true, false }, - { "getaccountaddress", &getaccountaddress, true, false }, - { "setaccount", &setaccount, true, false }, - { "getaccount", &getaccount, false, false }, - { "getaddressesbyaccount", &getaddressesbyaccount, true, false }, + { "createuserkey", &createuserkey, true, false }, + { "listusernames", &listusernames, true, false }, { "sendtoaddress", &sendtoaddress, false, false }, { "getreceivedbyaddress", &getreceivedbyaddress, false, false }, { "getreceivedbyaccount", &getreceivedbyaccount, false, false }, diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index 4d5599be..72df9c33 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -159,11 +159,8 @@ extern json_spirit::Value getwork(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value getblocktemplate(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value submitblock(const json_spirit::Array& params, bool fHelp); -extern json_spirit::Value getnewaddress(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp -extern json_spirit::Value getaccountaddress(const json_spirit::Array& params, bool fHelp); -extern json_spirit::Value setaccount(const json_spirit::Array& params, bool fHelp); -extern json_spirit::Value getaccount(const json_spirit::Array& params, bool fHelp); -extern json_spirit::Value getaddressesbyaccount(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value createuserkey(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp +extern json_spirit::Value listusernames(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value sendtoaddress(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value signmessage(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value verifymessage(const json_spirit::Array& params, bool fHelp); diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index f9e85376..b593280b 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -10,6 +10,7 @@ #include "bitcoinrpc.h" #include "init.h" #include "base58.h" +#include "main.h" using namespace std; using namespace boost; @@ -90,155 +91,51 @@ Value getinfo(const Array& params, bool fHelp) -Value getnewaddress(const Array& params, bool fHelp) +Value createuserkey(const Array& params, bool fHelp) { - if (fHelp || params.size() > 1) + if (fHelp || params.size() != 1) throw runtime_error( - "getnewaddress [account]\n" + "createuserkey \n" "Returns a new Bitcoin address for receiving payments. " "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 account first so we don't generate a key if there's an error - string strAccount; - if (params.size() > 0) - strAccount = AccountFromValue(params[0]); - - // Generate a new key that is added to wallet - CPubKey newKey; - if (!pwalletMain->GetKeyFromPool(newKey, false)) - throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first"); - CKeyID keyID = newKey.GetID(); - - pwalletMain->SetAddressBookName(keyID, strAccount); - - return CBitcoinAddress(keyID).ToString(); -} - - -CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false) -{ - CWalletDB walletdb(pwalletMain->strWalletFile); - - CAccount account; - walletdb.ReadAccount(strAccount, account); - - bool bKeyUsed = false; -/* - // Check if the current key has been used - if (account.vchPubKey.IsValid()) - { - CScript scriptPubKey; - scriptPubKey.SetDestination(account.vchPubKey.GetID()); - for (map::iterator it = pwalletMain->mapWallet.begin(); - it != pwalletMain->mapWallet.end() && account.vchPubKey.IsValid(); - ++it) - { - const CWalletTx& wtx = (*it).second; - BOOST_FOREACH(const CTxOut& txout, wtx.vout) - if (txout.scriptPubKey == scriptPubKey) - bKeyUsed = true; - } - } -*/ - // Generate a new key - if (!account.vchPubKey.IsValid() || bForceNew || bKeyUsed) - { - if (!pwalletMain->GetKeyFromPool(account.vchPubKey, false)) - throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first"); - - pwalletMain->SetAddressBookName(account.vchPubKey.GetID(), strAccount); - walletdb.WriteAccount(strAccount, account); - } - - return CBitcoinAddress(account.vchPubKey.GetID()); -} - -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 = AccountFromValue(params[0]); - - Value ret; - - ret = GetAccountAddress(strAccount).ToString(); - - return ret; -} - - - -Value setaccount(const Array& params, bool fHelp) -{ - if (fHelp || params.size() < 1 || params.size() > 2) - throw runtime_error( - "setaccount \n" - "Sets the account associated with the given address."); - - CBitcoinAddress address(params[0].get_str()); - if (!address.IsValid()) - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address"); - - - string strAccount; - if (params.size() > 1) - strAccount = AccountFromValue(params[1]); - - // Detect when changing the account of an address that is the 'unused current key' of another account: - if (pwalletMain->mapAddressBook.count(address.Get())) - { - string strOldAccount = pwalletMain->mapAddressBook[address.Get()]; - if (address == GetAccountAddress(strOldAccount)) - GetAccountAddress(strOldAccount, true); - } + EnsureWalletIsUnlocked(); - pwalletMain->SetAddressBookName(address.Get(), strAccount); + string strUsername = params[0].get_str(); - return Value::null; -} + CKeyID keyID; + if( pwalletMain->GetKeyIdFromUsername(strUsername, keyID) ) + throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Error: this username exists in wallet"); + uint256 userhash = SerializeHash(strUsername); + printf("usernamehash(%s) = %s\n", strUsername.c_str(), userhash.GetHex().c_str()); -Value getaccount(const Array& params, bool fHelp) -{ - if (fHelp || params.size() != 1) - throw runtime_error( - "getaccount \n" - "Returns the account associated with the given address."); + CTransaction txOut; + uint256 hashBlock; + if( GetTransaction(userhash, txOut, hashBlock) ) + throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Error: this username exists in tx database"); - CBitcoinAddress address(params[0].get_str()); - if (!address.IsValid()) - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address"); + // Generate a new key that is added to wallet + CPubKey newKey = pwalletMain->GenerateNewKey(strUsername); + keyID = newKey.GetID(); - string strAccount; - map::iterator mi = pwalletMain->mapAddressBook.find(address.Get()); - if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty()) - strAccount = (*mi).second; - return strAccount; + return CBitcoinAddress(keyID).ToString(); } -Value getaddressesbyaccount(const Array& params, bool fHelp) +Value listusernames(const Array& params, bool fHelp) { - if (fHelp || params.size() != 1) + if (fHelp || params.size() != 0) throw runtime_error( - "getaddressesbyaccount \n" - "Returns the list of addresses for the given account."); - - string strAccount = AccountFromValue(params[0]); + "listusernames\n" + "Returns the list of usernames."); // Find all addresses that have the given account Array ret; - BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(CKeyID, CKeyMetadata)& item, pwalletMain->mapKeyMetadata) { - const CBitcoinAddress& address = item.first; - const string& strName = item.second; - if (strName == strAccount) - ret.push_back(address.ToString()); + ret.push_back(item.second.username); } return ret; }