RPC: Add new methods suitable for malleable key pairs management;
[novacoin.git] / src / rpcwallet.cpp
index c1be536..8b3780e 100644 (file)
@@ -1866,17 +1866,39 @@ Value newmalleablekey(const Array& params, bool fHelp)
             "newmalleablekey\n"
             "Make a malleable public/private key pair.\n");
 
-    CMalleableKey malleableKey;
-    malleableKey.MakeNewKeys();
-    CMalleablePubKey malleablePubKey = malleableKey.GetMalleablePubKey();
+    if (!fTestNet)
+        throw runtime_error("This feature has been disabled for mainNet clients");
+
+    CMalleableKeyView keyView = pwalletMain->GenerateNewMalleableKey();
+
+    CMalleableKey mKey;
+    if (!pwalletMain->GetMalleableKey(keyView, mKey))
+        throw runtime_error("Unable to generate new malleable key");
+
+    Object result;
+    result.push_back(Pair("PublicPair", mKey.GetMalleablePubKey().ToString()));
+    result.push_back(Pair("KeyView", keyView.ToString()));
+
+    return result;
+}
+
+Value dumpmalleablekey(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 1)
+        throw runtime_error (
+            "dumpmalleablekey <Key view>\n"
+            "Dump the private and public key pairs, which correspond to provided key view.\n");
 
-    CDataStream ssPublicBytes(SER_NETWORK, PROTOCOL_VERSION);
-    ssPublicBytes << malleablePubKey;
+    CMalleableKey mKey;
+    CMalleableKeyView keyView;
+    keyView.SetString(params[0].get_str());
+
+    if (!pwalletMain->GetMalleableKey(keyView, mKey))
+        throw runtime_error("There is no such item in the wallet");
 
     Object result;
-    result.push_back(Pair("PrivatePair", malleableKey.ToString()));
-    result.push_back(Pair("PublicPair", malleablePubKey.ToString()));
-    result.push_back(Pair("PublicBytes", HexStr(ssPublicBytes.begin(), ssPublicBytes.end())));
+    result.push_back(Pair("PrivatePair", mKey.ToString()));
+    result.push_back(Pair("PublicPair", mKey.GetMalleablePubKey().ToString()));
 
     return result;
 }
@@ -1894,23 +1916,7 @@ Value adjustmalleablekey(const Array& params, bool fHelp)
     CKey privKeyVariant;
     CPubKey vchPubKeyVariant = CPubKey(ParseHex(params[1].get_str()));
 
-    std::vector<unsigned char> vchR = ParseHex(params[2].get_str());
-
-    CPubKey R;
-    if (vchR.size() == 33)
-        R = CPubKey(vchR);
-    else {
-        // R is encrypted
-        CSecret pvchSecretL, pvchSecretH;
-
-        malleableKey.GetSecrets(pvchSecretL, pvchSecretH);
-        CKey key;
-        key.SetSecret(pvchSecretL);
-
-        std::vector<unsigned char> vchDecryptedR;
-        key.DecryptData(vchR, vchDecryptedR);
-        R = CPubKey(vchDecryptedR);
-    }
+    CPubKey R(ParseHex(params[2].get_str()));
 
     if (!malleableKey.CheckKeyVariant(R,vchPubKeyVariant, privKeyVariant)) {
         throw runtime_error("Unable to calculate the private key");
@@ -1944,15 +1950,30 @@ Value adjustmalleablepubkey(const Array& params, bool fHelp)
     CPubKey R, vchPubKeyVariant;
     malleablePubKey.GetVariant(R, vchPubKeyVariant);
 
-    std::vector<unsigned char> encryptedR;
-    malleablePubKey.GetL().EncryptData(R.Raw(), encryptedR);
-
     Object result;
     result.push_back(Pair("R", HexStr(R.Raw())));
-    result.push_back(Pair("Rcrypted", HexStr(encryptedR)));
     result.push_back(Pair("PubkeyVariant", HexStr(vchPubKeyVariant.Raw())));
     result.push_back(Pair("KeyVariantID", CBitcoinAddress(vchPubKeyVariant.GetID()).ToString()));
 
+    return result;
+}
+
+Value listmalleableviews(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 0)
+        throw runtime_error(
+            "listmalleableviews\n"
+            "Get list of views for generated malleable keys.\n");
+
+    std::list<CMalleableKeyView> keyViewList;
+    pwalletMain->ListMalleableViews(keyViewList);
+
+    Array result;
+    BOOST_FOREACH(const CMalleableKeyView &keyView, keyViewList)
+    {
+        result.push_back(keyView.ToString());
+    }
 
     return result;
 }
+