rpccrypt.cpp: Add message encryption and decryption methods.
authorAlex <balthazar.ad@gmail.com>
Thu, 18 Feb 2016 15:05:08 +0000 (07:05 -0800)
committerAlex <balthazar.ad@gmail.com>
Thu, 18 Feb 2016 15:05:08 +0000 (07:05 -0800)
doc/EncryptMsgAPI.txt [new file with mode: 0644]
src/bitcoinrpc.cpp
src/bitcoinrpc.h
src/rpccrypt.cpp

diff --git a/doc/EncryptMsgAPI.txt b/doc/EncryptMsgAPI.txt
new file mode 100644 (file)
index 0000000..6cdeca6
--- /dev/null
@@ -0,0 +1,19 @@
+> getnewaddress
+4GELig4Rh4VZVnEnvp4v9cN29SH8F3JEeT
+> validateaddress 4GELig4Rh4VZVnEnvp4v9cN29SH8F3JEeT
+{
+"isvalid" : true,
+"address" : "4GELig4Rh4VZVnEnvp4v9cN29SH8F3JEeT",
+"ismine" : true,
+"watchonly" : false,
+"isscript" : false,
+"pubkey" : "027742dfa3fd23b86f4fd6bfa4707da5dfe433db609d7d458f6576b7c84f20dcef",
+"iscompressed" : true,
+"account" : ""
+}
+
+> encryptmessage 027742dfa3fd23b86f4fd6bfa4707da5dfe433db609d7d458f6576b7c84f20dcef "Hello world!"
+BHzjL9u1h8K1f5pqEZ4KLzmYXLD4qdZWWPnQPJdRgttWJ8QU4LbZLu4KxZpYcNNrgePkxVv2Ps8XV1AC34VCt3oBMdMW7HuaSB6
+
+> decryptmessage 4GELig4Rh4VZVnEnvp4v9cN29SH8F3JEeT BHzjL9u1h8K1f5pqEZ4KLzmYXLD4qdZWWPnQPJdRgttWJ8QU4LbZLu4KxZpYcNNrgePkxVv2Ps8XV1AC34VCt3oBMdMW7HuaSB6
+Hello world!
index 5535b9b..bf82c30 100644 (file)
@@ -323,6 +323,8 @@ static const CRPCCommand vRPCCommands[] =
     { "dumpmalleablekey",       &dumpmalleablekey,       false,  false},
     { "encryptdata",            &encryptdata,            false,  false },
     { "decryptdata",            &decryptdata,            false,  false },
+    { "encryptmessage",         &encryptmessage,         false,  false },
+    { "decryptmessage",         &decryptmessage,         false,  false },
     { "sendalert",              &sendalert,              false,  false},
 };
 
index d18e5f6..9d032b9 100644 (file)
@@ -214,6 +214,8 @@ extern json_spirit::Value dumpmalleablekey(const json_spirit::Array& params, boo
 
 extern json_spirit::Value encryptdata(const json_spirit::Array& params, bool fHelp); // in rpccrypt.cpp
 extern json_spirit::Value decryptdata(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value encryptmessage(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value decryptmessage(const json_spirit::Array& params, bool fHelp);
 
 extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp
 extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp);
index 0569176..aaccb70 100644 (file)
@@ -50,3 +50,44 @@ Value decryptdata(const Array& params, bool fHelp)
 
     return HexStr(vchDecrypted);
 }
+
+Value encryptmessage(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 2)
+        throw runtime_error(
+            "encryptmessage <public key> <message string>\n"
+            "Encrypt message with provided public key.\n");
+
+    CPubKey pubKey(ParseHex(params[0].get_str()));
+
+    vector<unsigned char> vchEncrypted;
+    string strData = params[1].get_str();
+    pubKey.EncryptData(vector<unsigned char>(strData.begin(), strData.end()), vchEncrypted);
+
+    return EncodeBase58Check(vchEncrypted);
+}
+
+Value decryptmessage(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 2)
+        throw runtime_error(
+            "decryptdata <novacoin address> <encrypted message>\n"
+            "Decrypt message string.\n");
+
+    EnsureWalletIsUnlocked();
+    CBitcoinAddress addr(params[0].get_str());
+
+    CKeyID keyID;
+    addr.GetKeyID(keyID);
+
+    CKey key;
+    pwalletMain->GetKey(keyID, key);
+
+    vector<unsigned char> vchEncrypted;
+    if (!DecodeBase58Check(params[1].get_str(), vchEncrypted))
+        throw runtime_error("Incorrect string");
+    vector<unsigned char> vchDecrypted;
+    key.DecryptData(vchEncrypted, vchDecrypted);
+
+    return std::string((const char*)&vchDecrypted[0], vchDecrypted.size());
+}