Merge pull request #277 from svost/patch
authorCryptoManiac <CryptoManiac@users.noreply.github.com>
Thu, 18 Feb 2016 15:07:28 +0000 (18:07 +0300)
committerCryptoManiac <CryptoManiac@users.noreply.github.com>
Thu, 18 Feb 2016 15:07:28 +0000 (18:07 +0300)
Minor fix

doc/EncryptMsgAPI.txt [new file with mode: 0644]
src/bitcoinrpc.cpp
src/bitcoinrpc.h
src/key.cpp
src/key.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 bf0f99d..a1ab2a3 100644 (file)
@@ -803,7 +803,7 @@ bool CMalleableKey::SetSecrets(const CSecret &pvchSecretL, const CSecret &pvchSe
     Reset();
     CKey L, H;
 
-    if (pvchSecretL.size() != 32 || !pvchSecretH.size() != 32 || !L.SetSecret(pvchSecretL, true) || !H.SetSecret(pvchSecretH, true))
+    if (pvchSecretL.size() != 32 || pvchSecretH.size() != 32 || !L.SetSecret(pvchSecretL, true) || !H.SetSecret(pvchSecretH, true))
     {
         nVersion = 0;
         return false;
index 1ce14f8..38ebe76 100644 (file)
--- a/src/key.h
+++ b/src/key.h
@@ -218,6 +218,12 @@ private:
 
 public:
     CMalleablePubKey() { nVersion = CMalleablePubKey::CURRENT_VERSION; }
+    CMalleablePubKey(const CMalleablePubKey& mpk)
+    {
+        nVersion = mpk.nVersion;
+        pubKeyL = mpk.pubKeyL;
+        pubKeyH = mpk.pubKeyH;
+    }
     CMalleablePubKey(const std::string& strMalleablePubKey) { SetString(strMalleablePubKey); }
     CMalleablePubKey(const CPubKey &pubKeyInL, const CPubKey &pubKeyInH) : pubKeyL(pubKeyInL), pubKeyH(pubKeyInH) { nVersion = CMalleablePubKey::CURRENT_VERSION; }
     CMalleablePubKey(const std::vector<unsigned char> &pubKeyInL, const std::vector<unsigned char> &pubKeyInH) : pubKeyL(pubKeyInL), pubKeyH(pubKeyInH) { nVersion = CMalleablePubKey::CURRENT_VERSION; }
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());
+}