From da21c0a2e47a2a12270569a4fe4a2e861253ddf9 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Fri, 19 Dec 2014 19:41:02 -0200 Subject: [PATCH] rpc for development only - generates crypto testvector --- src/bitcoinrpc.cpp | 1 + src/bitcoinrpc.h | 1 + src/rpcdump.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index f23882e8..daf30aac 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -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 }, diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index 0c438f17..c34a185c 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -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); diff --git a/src/rpcdump.cpp b/src/rpcdump.cpp index c1cce38d..21c3275b 100644 --- a/src/rpcdump.cpp +++ b/src/rpcdump.cpp @@ -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 \n" + "Returns encryption testvectors using 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(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 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; +} +