From 440991a0f8dbdd6147e56ec55e3423429cee6e90 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 18 Feb 2013 02:00:13 +0400 Subject: [PATCH] Add getnewpubkey RPC call --- src/bitcoinrpc.cpp | 29 +++++++++++++++++++++++++++++ src/wallet.cpp | 4 ++-- src/wallet.h | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 147e6b1..e2f7d27 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -1996,6 +1996,34 @@ Value getmemorypool(const Array& params, bool fHelp) } } +Value getnewpubkey(const Array& params, bool fHelp) +{ + if (fHelp || params.size() > 1) + throw runtime_error( + "getnewpubkey [account]\n" + "Returns new public key for coinbase generation."); + + // 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]); + + if (!pwalletMain->IsLocked()) + pwalletMain->TopUpKeyPool(); + + // Generate a new key that is added to wallet + std::vector newKey = pwalletMain->GenerateNewKey(false); + + if(!newKey.size()) + throw JSONRPCError(-12, "Error: Unable to create key"); + + CBitcoinAddress address(newKey); + pwalletMain->SetAddressBookName(address, strAccount); + + return HexStr(newKey.begin(), newKey.end()); +} + + Value getblockhash(const Array& params, bool fHelp) { if (fHelp || params.size() != 1) @@ -2264,6 +2292,7 @@ static const CRPCCommand vRPCCommands[] = { "getinfo", &getinfo, true }, { "getmininginfo", &getmininginfo, true }, { "getnewaddress", &getnewaddress, true }, + { "getnewpubkey", &getnewpubkey, true }, { "getaccountaddress", &getaccountaddress, true }, { "setaccount", &setaccount, true }, { "getaccount", &getaccount, false }, diff --git a/src/wallet.cpp b/src/wallet.cpp index e955965..e4a4ec3 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -18,9 +18,9 @@ using namespace std; // mapWallet // -std::vector CWallet::GenerateNewKey() +std::vector CWallet::GenerateNewKey(bool bCompressed = true) { - bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets + bool fCompressed = bCompressed ? CanSupportFeature(FEATURE_COMPRPUBKEY) : false; // default to compressed public keys if we want 0.6.0 wallets RandAddSeedPerfmon(); CKey key; diff --git a/src/wallet.h b/src/wallet.h index 88e8648..a531944 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -119,7 +119,7 @@ public: // keystore implementation // Generate a new key - std::vector GenerateNewKey(); + std::vector GenerateNewKey(bool bCompressed); // Adds a key to the store, and saves it to disk. bool AddKey(const CKey& key); // Adds a key to the store, without saving it to disk (used by LoadWallet) -- 1.7.1