RPC: now decryptdata/decryptmessage methods are able to understand WIF formatted...
[novacoin.git] / src / rpccrypt.cpp
index 0569176..91f0302 100644 (file)
@@ -4,11 +4,8 @@
 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
 
 #include "wallet.h"
-#include "walletdb.h"
 #include "bitcoinrpc.h"
 #include "init.h"
-#include "util.h"
-#include "ntp.h"
 #include "base58.h"
 
 using namespace json_spirit;
@@ -33,20 +30,80 @@ Value decryptdata(const Array& params, bool fHelp)
 {
     if (fHelp || params.size() != 2)
         throw runtime_error(
-            "decryptdata <novacoin address> <encrypted stream>\n"
+            "decryptdata <novacoin address or private key> <encrypted stream>\n"
             "Decrypt octet stream.\n");
 
     EnsureWalletIsUnlocked();
+    CKey key;
     CBitcoinAddress addr(params[0].get_str());
+    if (addr.IsValid()) {
+        CKeyID keyID;
+        addr.GetKeyID(keyID);
+        if (!pwalletMain->GetKey(keyID, key))
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "We have no private key for this address");
+    }
+    else {
+        CBitcoinSecret vchSecret;
+        if (!vchSecret.SetString(params[0].get_str()))
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Provided private key is inconsistent.");
+        bool fCompressed;
+        CSecret secret = vchSecret.GetSecret(fCompressed);
+        key.SetSecret(secret, fCompressed);
+    }
+
+    vector<unsigned char> vchDecrypted;
+    key.DecryptData(ParseHex(params[1].get_str()), vchDecrypted);
+
+    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()));
 
-    CKeyID keyID;
-    addr.GetKeyID(keyID);
+    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(
+            "decryptmessage <novacoin address or private key> <encrypted message>\n"
+            "Decrypt message string.\n");
+
+    EnsureWalletIsUnlocked();
 
     CKey key;
-    pwalletMain->GetKey(keyID, key);
+    CBitcoinAddress addr(params[0].get_str());
+    if (addr.IsValid()) {
+        CKeyID keyID;
+        addr.GetKeyID(keyID);
+        if (!pwalletMain->GetKey(keyID, key))
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "We have no private key for this address");
+    }
+    else {
+        CBitcoinSecret vchSecret;
+        if (!vchSecret.SetString(params[0].get_str()))
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Provided private key is inconsistent.");
+        bool fCompressed;
+        CSecret secret = vchSecret.GetSecret(fCompressed);
+        key.SetSecret(secret, fCompressed);
+    }
 
+    vector<unsigned char> vchEncrypted;
+    if (!DecodeBase58Check(params[1].get_str(), vchEncrypted))
+        throw runtime_error("Incorrect string");
     vector<unsigned char> vchDecrypted;
-    key.DecryptData(ParseHex(params[1].get_str()), vchDecrypted);
+    key.DecryptData(vchEncrypted, vchDecrypted);
 
-    return HexStr(vchDecrypted);
+    return std::string((const char*)&vchDecrypted[0], vchDecrypted.size());
 }