Browse Source

implement new rpc dumppubkey.

importprivkey now verifies if username exists in txdb
miguelfreitas
Miguel Freitas 11 years ago
parent
commit
6ade53ce53
  1. 2
      src/bitcoinrpc.cpp
  2. 1
      src/bitcoinrpc.h
  3. 37
      src/rpcdump.cpp
  4. 2
      src/twister.cpp
  5. 1
      src/twister.h

2
src/bitcoinrpc.cpp

@ -230,6 +230,7 @@ static const CRPCCommand vRPCCommands[] = @@ -230,6 +230,7 @@ static const CRPCCommand vRPCCommands[] =
{ "submitblock", &submitblock, false, false },
{ "listsinceblock", &listsinceblock, false, false },
{ "dumpprivkey", &dumpprivkey, true, false },
{ "dumppubkey", &dumppubkey, false, false },
{ "dumpwallet", &dumpwallet, true, false },
{ "importprivkey", &importprivkey, false, false },
{ "importwallet", &importwallet, false, false },
@ -1241,6 +1242,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri @@ -1241,6 +1242,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
if (strMethod == "lockunspent" && n > 0) ConvertTo<bool>(params[0]);
if (strMethod == "lockunspent" && n > 1) ConvertTo<Array>(params[1]);
if (strMethod == "importprivkey" && n > 2) ConvertTo<bool>(params[2]);
if (strMethod == "importprivkey" && n > 3) ConvertTo<bool>(params[3]);
if (strMethod == "verifychain" && n > 0) ConvertTo<boost::int64_t>(params[0]);
if (strMethod == "verifychain" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "dhtput" && n > 3) ConvertToValue(params[3]);

1
src/bitcoinrpc.h

@ -146,6 +146,7 @@ extern json_spirit::Value addnode(const json_spirit::Array& params, bool fHelp); @@ -146,6 +146,7 @@ extern json_spirit::Value addnode(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getaddednodeinfo(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value dumpprivkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp
extern json_spirit::Value dumppubkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp
extern json_spirit::Value importprivkey(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value dumpwallet(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value importwallet(const json_spirit::Array& params, bool fHelp);

37
src/rpcdump.cpp

@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
#include "bitcoinrpc.h"
#include "ui_interface.h"
#include "base58.h"
#include "twister.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/lexical_cast.hpp>
@ -67,9 +68,9 @@ std::string DecodeDumpString(const std::string &str) { @@ -67,9 +68,9 @@ std::string DecodeDumpString(const std::string &str) {
Value importprivkey(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 2 || params.size() > 3)
if (fHelp || params.size() < 2 || params.size() > 4)
throw runtime_error(
"importprivkey <bitcoinprivkey> <username> [rescan=true]\n"
"importprivkey <bitcoinprivkey> <username> [rescan=true] [allow_new_user=false]\n"
"Adds a private key (as returned by dumpprivkey) to your wallet.");
string strSecret = params[0].get_str();
@ -80,11 +81,24 @@ Value importprivkey(const Array& params, bool fHelp) @@ -80,11 +81,24 @@ Value importprivkey(const Array& params, bool fHelp)
if (params.size() > 2)
fRescan = params[2].get_bool();
bool fAllowNewUser = false;
if (params.size() > 3)
fAllowNewUser = params[3].get_bool();
CBitcoinSecret vchSecret;
bool fGood = vchSecret.SetString(strSecret);
if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
if( !fAllowNewUser ) {
CTransaction txOut;
uint256 hashBlock;
uint256 userhash = SerializeHash(strUsername);
if( !GetTransaction(userhash, txOut, hashBlock) ) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "User must exist (or allow_new_user flag must be set)");
}
}
CKey key = vchSecret.GetKey();
CPubKey pubkey = key.GetPubKey();
CKeyID vchAddress = pubkey.GetID();
@ -211,6 +225,25 @@ Value dumpprivkey(const Array& params, bool fHelp) @@ -211,6 +225,25 @@ Value dumpprivkey(const Array& params, bool fHelp)
return CBitcoinSecret(vchSecret).ToString();
}
Value dumppubkey(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"dumppubkey <username>\n"
"Returns the public key corresponding to <username>.");
string strUsername = params[0].get_str();
CPubKey pubkey;
bool gotKey = getUserPubKey(strUsername, pubkey);
if( !gotKey )
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Username not found");
string strPubkey = string( reinterpret_cast<const char *>(pubkey.begin()), pubkey.size());
return strPubkey;
}
Value dumpwallet(const Array& params, bool fHelp)
{

2
src/twister.cpp

@ -1552,7 +1552,7 @@ Value listusernamespartial(const Array& params, bool fHelp) @@ -1552,7 +1552,7 @@ Value listusernamespartial(const Array& params, bool fHelp)
{
if (fHelp || (params.size() < 2 || params.size() > 3))
throw runtime_error(
"listusernamespartial <username_starts_with> <count> [exact_match]\n"
"listusernamespartial <username_starts_with> <count> [exact_match=false]\n"
"get list of usernames starting with");
string userStartsWith = params[0].get_str();

1
src/twister.h

@ -23,6 +23,7 @@ public: @@ -23,6 +23,7 @@ public:
void startSessionTorrent(boost::thread_group& threadGroup);
void stopSessionTorrent();
bool getUserPubKey(std::string const &strUsername, CPubKey &pubkey);
std::string createSignature(std::string const &strMessage, CKeyID &keyID);
std::string createSignature(std::string const &strMessage, std::string const &strUsername);
bool verifySignature(std::string const &strMessage, std::string const &strUsername, std::string const &strSign);

Loading…
Cancel
Save