RPC: now decryptdata/decryptmessage methods are able to understand WIF formatted...
[novacoin.git] / src / rpccrypt.cpp
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #include "wallet.h"
7 #include "bitcoinrpc.h"
8 #include "init.h"
9 #include "base58.h"
10
11 using namespace json_spirit;
12 using namespace std;
13
14 Value encryptdata(const Array& params, bool fHelp)
15 {
16     if (fHelp || params.size() != 2)
17         throw runtime_error(
18             "encryptdata <public key> <hex data>\n"
19             "Encrypt octet stream with provided public key..\n");
20
21     CPubKey pubKey(ParseHex(params[0].get_str()));
22
23     vector<unsigned char> vchEncrypted;
24     pubKey.EncryptData(ParseHex(params[1].get_str()), vchEncrypted);
25
26     return HexStr(vchEncrypted);
27 }
28
29 Value decryptdata(const Array& params, bool fHelp)
30 {
31     if (fHelp || params.size() != 2)
32         throw runtime_error(
33             "decryptdata <novacoin address or private key> <encrypted stream>\n"
34             "Decrypt octet stream.\n");
35
36     EnsureWalletIsUnlocked();
37     CKey key;
38     CBitcoinAddress addr(params[0].get_str());
39     if (addr.IsValid()) {
40         CKeyID keyID;
41         addr.GetKeyID(keyID);
42         if (!pwalletMain->GetKey(keyID, key))
43             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "We have no private key for this address");
44     }
45     else {
46         CBitcoinSecret vchSecret;
47         if (!vchSecret.SetString(params[0].get_str()))
48             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Provided private key is inconsistent.");
49         bool fCompressed;
50         CSecret secret = vchSecret.GetSecret(fCompressed);
51         key.SetSecret(secret, fCompressed);
52     }
53
54     vector<unsigned char> vchDecrypted;
55     key.DecryptData(ParseHex(params[1].get_str()), vchDecrypted);
56
57     return HexStr(vchDecrypted);
58 }
59
60 Value encryptmessage(const Array& params, bool fHelp)
61 {
62     if (fHelp || params.size() != 2)
63         throw runtime_error(
64             "encryptmessage <public key> <message string>\n"
65             "Encrypt message with provided public key.\n");
66
67     CPubKey pubKey(ParseHex(params[0].get_str()));
68
69     vector<unsigned char> vchEncrypted;
70     string strData = params[1].get_str();
71     pubKey.EncryptData(vector<unsigned char>(strData.begin(), strData.end()), vchEncrypted);
72
73     return EncodeBase58Check(vchEncrypted);
74 }
75
76 Value decryptmessage(const Array& params, bool fHelp)
77 {
78     if (fHelp || params.size() != 2)
79         throw runtime_error(
80             "decryptmessage <novacoin address or private key> <encrypted message>\n"
81             "Decrypt message string.\n");
82
83     EnsureWalletIsUnlocked();
84
85     CKey key;
86     CBitcoinAddress addr(params[0].get_str());
87     if (addr.IsValid()) {
88         CKeyID keyID;
89         addr.GetKeyID(keyID);
90         if (!pwalletMain->GetKey(keyID, key))
91             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "We have no private key for this address");
92     }
93     else {
94         CBitcoinSecret vchSecret;
95         if (!vchSecret.SetString(params[0].get_str()))
96             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Provided private key is inconsistent.");
97         bool fCompressed;
98         CSecret secret = vchSecret.GetSecret(fCompressed);
99         key.SetSecret(secret, fCompressed);
100     }
101
102     vector<unsigned char> vchEncrypted;
103     if (!DecodeBase58Check(params[1].get_str(), vchEncrypted))
104         throw runtime_error("Incorrect string");
105     vector<unsigned char> vchDecrypted;
106     key.DecryptData(vchEncrypted, vchDecrypted);
107
108     return std::string((const char*)&vchDecrypted[0], vchDecrypted.size());
109 }