Browse Source

rpc for development only - generates crypto testvector

miguelfreitas
Miguel Freitas 10 years ago
parent
commit
da21c0a2e4
  1. 1
      src/bitcoinrpc.cpp
  2. 1
      src/bitcoinrpc.h
  3. 70
      src/rpcdump.cpp

1
src/bitcoinrpc.cpp

@ -233,6 +233,7 @@ static const CRPCCommand vRPCCommands[] = @@ -233,6 +233,7 @@ static const CRPCCommand vRPCCommands[] =
{ "listsinceblock", &listsinceblock, false, false, false },
{ "dumpprivkey", &dumpprivkey, true, false, false },
{ "dumppubkey", &dumppubkey, false, false, false },
{ "testvector", &testvector, false, false, false },
{ "dumpwallet", &dumpwallet, true, false, false },
{ "importprivkey", &importprivkey, false, false, false },
{ "importwallet", &importwallet, false, false, false },

1
src/bitcoinrpc.h

@ -150,6 +150,7 @@ extern json_spirit::Value getaddednodeinfo(const json_spirit::Array& params, boo @@ -150,6 +150,7 @@ extern json_spirit::Value getaddednodeinfo(const json_spirit::Array& params, boo
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 testvector(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);

70
src/rpcdump.cpp

@ -301,3 +301,73 @@ Value dumpwallet(const Array& params, bool fHelp) @@ -301,3 +301,73 @@ Value dumpwallet(const Array& params, bool fHelp)
file.close();
return Value::null;
}
Value testvector(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"testvector <username>\n"
"Returns encryption testvectors using <username> private key");
EnsureWalletIsUnlocked();
Object obj;
string strUsername = params[0].get_str();
CKeyID keyID;
bool keyInWallet = pwalletMain->GetKeyIdFromUsername(strUsername, keyID);
if( !keyInWallet ) {
throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Error: no such user in wallet");
}
CKey key;
if (!pwalletMain->GetKey(keyID, key))
throw JSONRPCError(RPC_WALLET_ERROR, "Error: could not obtain privkey");
obj.push_back(Pair("secret",CBitcoinSecret(key).ToString()));
CPubKey pubkey;
getUserPubKey(strUsername, pubkey);
string strPubkey = string( reinterpret_cast<const char *>(pubkey.begin()), pubkey.size());
obj.push_back(Pair("pubkey",HexStr(strPubkey)));
CHashWriter ssMagic(SER_GETHASH, 0);
ssMagic << strMessageMagic;
obj.push_back(Pair("hashMagic",ssMagic.GetHash().GetHex()));
string plainText = "The quick brown fox jumps over the lazy dog";
obj.push_back(Pair("plaintext",plainText));
CHashWriter ss(SER_GETHASH, 0);
ss << strMessageMagic;
ss << plainText;
uint256 hash = ss.GetHash();
obj.push_back(Pair("hash",hash.GetHex()));
vector<unsigned char> vchSig;
if (!key.SignCompact(hash, vchSig))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
CPubKey pubkeyRec;
if (!pubkeyRec.RecoverCompact(hash, vchSig) ||
pubkeyRec.GetID() != pubkey.GetID() )
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Check Sign failed");
obj.push_back(Pair("sign",HexStr(vchSig)));
ecies_secure_t sec;
bool encrypted = pubkey.Encrypt(plainText, sec);
if( encrypted ) {
Object objSec;
objSec.push_back(Pair("key",HexStr(sec.key)));
objSec.push_back(Pair("mac",HexStr(sec.mac)));
objSec.push_back(Pair("orig",sec.orig));
objSec.push_back(Pair("body",HexStr(sec.body)));
obj.push_back(Pair("sec",objSec));
}
return obj;
}

Loading…
Cancel
Save