Merge branch 'patch' of ssh://github.com/svost/novacoin into svost-patch
[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 "walletdb.h"
8 #include "bitcoinrpc.h"
9 #include "init.h"
10 #include "util.h"
11 #include "ntp.h"
12 #include "base58.h"
13
14 using namespace json_spirit;
15 using namespace std;
16
17 Value encryptdata(const Array& params, bool fHelp)
18 {
19     if (fHelp || params.size() != 2)
20         throw runtime_error(
21             "encryptdata <public key> <hex data>\n"
22             "Encrypt octet stream with provided public key..\n");
23
24     CPubKey pubKey(ParseHex(params[0].get_str()));
25
26     vector<unsigned char> vchEncrypted;
27     pubKey.EncryptData(ParseHex(params[1].get_str()), vchEncrypted);
28
29     return HexStr(vchEncrypted);
30 }
31
32 Value decryptdata(const Array& params, bool fHelp)
33 {
34     if (fHelp || params.size() != 2)
35         throw runtime_error(
36             "decryptdata <novacoin address> <encrypted stream>\n"
37             "Decrypt octet stream.\n");
38
39     EnsureWalletIsUnlocked();
40     CBitcoinAddress addr(params[0].get_str());
41
42     CKeyID keyID;
43     addr.GetKeyID(keyID);
44
45     CKey key;
46     pwalletMain->GetKey(keyID, key);
47
48     vector<unsigned char> vchDecrypted;
49     key.DecryptData(ParseHex(params[1].get_str()), vchDecrypted);
50
51     return HexStr(vchDecrypted);
52 }
53
54 Value encryptmessage(const Array& params, bool fHelp)
55 {
56     if (fHelp || params.size() != 2)
57         throw runtime_error(
58             "encryptmessage <public key> <message string>\n"
59             "Encrypt message with provided public key.\n");
60
61     CPubKey pubKey(ParseHex(params[0].get_str()));
62
63     vector<unsigned char> vchEncrypted;
64     string strData = params[1].get_str();
65     pubKey.EncryptData(vector<unsigned char>(strData.begin(), strData.end()), vchEncrypted);
66
67     return EncodeBase58Check(vchEncrypted);
68 }
69
70 Value decryptmessage(const Array& params, bool fHelp)
71 {
72     if (fHelp || params.size() != 2)
73         throw runtime_error(
74             "decryptdata <novacoin address> <encrypted message>\n"
75             "Decrypt message string.\n");
76
77     EnsureWalletIsUnlocked();
78     CBitcoinAddress addr(params[0].get_str());
79
80     CKeyID keyID;
81     addr.GetKeyID(keyID);
82
83     CKey key;
84     pwalletMain->GetKey(keyID, key);
85
86     vector<unsigned char> vchEncrypted;
87     if (!DecodeBase58Check(params[1].get_str(), vchEncrypted))
88         throw runtime_error("Incorrect string");
89     vector<unsigned char> vchDecrypted;
90     key.DecryptData(vchEncrypted, vchDecrypted);
91
92     return std::string((const char*)&vchDecrypted[0], vchDecrypted.size());
93 }