69e14c09c106520fcaee3d05f3d7dcb990dc0e5b
[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> <encrypted stream>\n"
34             "Decrypt octet stream.\n");
35
36     EnsureWalletIsUnlocked();
37     CBitcoinAddress addr(params[0].get_str());
38
39     CKeyID keyID;
40     addr.GetKeyID(keyID);
41
42     CKey key;
43     pwalletMain->GetKey(keyID, key);
44
45     vector<unsigned char> vchDecrypted;
46     key.DecryptData(ParseHex(params[1].get_str()), vchDecrypted);
47
48     return HexStr(vchDecrypted);
49 }
50
51 Value encryptmessage(const Array& params, bool fHelp)
52 {
53     if (fHelp || params.size() != 2)
54         throw runtime_error(
55             "encryptmessage <public key> <message string>\n"
56             "Encrypt message with provided public key.\n");
57
58     CPubKey pubKey(ParseHex(params[0].get_str()));
59
60     vector<unsigned char> vchEncrypted;
61     string strData = params[1].get_str();
62     pubKey.EncryptData(vector<unsigned char>(strData.begin(), strData.end()), vchEncrypted);
63
64     return EncodeBase58Check(vchEncrypted);
65 }
66
67 Value decryptmessage(const Array& params, bool fHelp)
68 {
69     if (fHelp || params.size() != 2)
70         throw runtime_error(
71             "decryptmessage <novacoin address> <encrypted message>\n"
72             "Decrypt message string.\n");
73
74     EnsureWalletIsUnlocked();
75     CBitcoinAddress addr(params[0].get_str());
76
77     CKeyID keyID;
78     addr.GetKeyID(keyID);
79
80     CKey key;
81     pwalletMain->GetKey(keyID, key);
82
83     vector<unsigned char> vchEncrypted;
84     if (!DecodeBase58Check(params[1].get_str(), vchEncrypted))
85         throw runtime_error("Incorrect string");
86     vector<unsigned char> vchDecrypted;
87     key.DecryptData(vchEncrypted, vchDecrypted);
88
89     return std::string((const char*)&vchDecrypted[0], vchDecrypted.size());
90 }