Do some cleanup, add importmalleablekey RPC call
authorCryptoManiac <balthazar@yandex.ru>
Mon, 22 Feb 2016 20:18:34 +0000 (23:18 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Mon, 22 Feb 2016 20:18:34 +0000 (23:18 +0300)
src/bitcoinrpc.cpp
src/bitcoinrpc.h
src/key.cpp
src/key.h
src/keystore.h
src/rpcdump.cpp
src/rpcwallet.cpp

index bf82c30..80674d6 100644 (file)
@@ -321,6 +321,8 @@ static const CRPCCommand vRPCCommands[] =
     { "adjustmalleablepubkey",  &adjustmalleablepubkey,  false,  false},
     { "listmalleableviews",     &listmalleableviews,     false,  false},
     { "dumpmalleablekey",       &dumpmalleablekey,       false,  false},
+    { "validatemalleablepubkey",&validatemalleablepubkey,true,   false },
+    { "importmalleablekey",     &importmalleablekey,     true,   false },
     { "encryptdata",            &encryptdata,            false,  false },
     { "decryptdata",            &decryptdata,            false,  false },
     { "encryptmessage",         &encryptmessage,         false,  false },
index 9d032b9..84dd6a4 100644 (file)
@@ -211,6 +211,8 @@ extern json_spirit::Value adjustmalleablekey(const json_spirit::Array& params, b
 extern json_spirit::Value adjustmalleablepubkey(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value listmalleableviews(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value dumpmalleablekey(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value validatemalleablepubkey(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value importmalleablekey(const json_spirit::Array& params, bool fHelp);
 
 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);
index a1ab2a3..c11da92 100644 (file)
@@ -780,15 +780,6 @@ CMalleableKey::CMalleableKey(const CSecret &L, const CSecret &H)
     SetSecrets(L, H);
 }
 
-/*
-CMalleableKey& CMalleableKey::operator=(const CMalleableKey &b)
-{
-    SetSecrets(b.vchSecretL, b.vchSecretH);
-
-    return (*this);
-}
-*/
-
 CMalleableKey::~CMalleableKey()
 {
 }
@@ -816,12 +807,6 @@ bool CMalleableKey::SetSecrets(const CSecret &pvchSecretL, const CSecret &pvchSe
     return true;
 }
 
-void CMalleableKey::GetSecrets(CSecret &pvchSecretL, CSecret &pvchSecretH) const
-{
-    pvchSecretL = vchSecretL;
-    pvchSecretH = vchSecretH;
-}
-
 CMalleablePubKey CMalleableKey::GetMalleablePubKey() const
 {
     CKey L, H;
@@ -1012,7 +997,7 @@ bool CMalleableKey::SetString(const std::string& strMutableKey)
     CDataStream ssKey(vchTemp, SER_NETWORK, PROTOCOL_VERSION);
     ssKey >> *this;
 
-    return IsNull();
+    return IsValid();
 }
 
 // CMalleableKeyView
@@ -1041,13 +1026,6 @@ CMalleableKeyView::CMalleableKeyView(const CMalleableKeyView &b)
     nVersion = CURRENT_VERSION;
 }
 
-CMalleableKeyView::CMalleableKeyView(const CSecret &L, const CPubKey &pvchPubKeyH)
-{
-    vchSecretL = L;
-    vchPubKeyH = pvchPubKeyH.Raw();
-    nVersion = CURRENT_VERSION;
-}
-
 CMalleableKeyView& CMalleableKeyView::operator=(const CMalleableKey &b)
 {
     vchSecretL = b.vchSecretL;
@@ -1152,7 +1130,7 @@ bool CMalleableKeyView::SetString(const std::string& strMutableKey)
     CDataStream ssKey(vchTemp, SER_NETWORK, PROTOCOL_VERSION);
     ssKey >> *this;
 
-    return IsNull();
+    return IsValid();
 }
 
 std::vector<unsigned char> CMalleableKeyView::Raw() const
index 38ebe76..67c414e 100644 (file)
--- a/src/key.h
+++ b/src/key.h
@@ -226,7 +226,6 @@ public:
     }
     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; }
 
     IMPLEMENT_SERIALIZE(
         READWRITE(this->nVersion);
@@ -299,8 +298,11 @@ public:
     void Reset();
     void MakeNewKeys();
     bool IsNull() const;
+    bool IsValid() const { return !IsNull() && GetMalleablePubKey().IsValid(); }
     bool SetSecrets(const CSecret &pvchSecretL, const CSecret &pvchSecretH);
-    void GetSecrets(CSecret &pvchSecretL, CSecret &pvchSecretH) const;
+
+    CSecret GetSecretL() const { return vchSecretL; }
+    CSecret GetSecretH() const { return vchSecretH; }
 
     CKeyID GetID() const {
         return GetMalleablePubKey().GetID();
@@ -322,13 +324,11 @@ private:
 public:
     CMalleableKeyView() { nVersion = 0; };
     CMalleableKeyView(const CMalleableKey &b);
-    CMalleableKeyView(const CSecret &L, const CPubKey &pvchPubKeyH);
 
     CMalleableKeyView(const CMalleableKeyView &b);
     CMalleableKeyView& operator=(const CMalleableKey &b);
     ~CMalleableKeyView();
 
-
     IMPLEMENT_SERIALIZE(
         READWRITE(this->nVersion);
         nVersion = this->nVersion;
@@ -337,6 +337,7 @@ public:
     )
 
     bool IsNull() const;
+    bool IsValid() const { return !IsNull() && GetMalleablePubKey().IsValid(); }
     std::string ToString() const;
     bool SetString(const std::string& strMalleablePubKey);
     std::vector<unsigned char> Raw() const;
@@ -351,6 +352,7 @@ public:
         return GetMalleablePubKey().GetID();
     }
     CMalleablePubKey GetMalleablePubKey() const;
+    CMalleableKey GetMalleableKey(const CSecret &vchSecretH) const { return CMalleableKey(vchSecretL, vchSecretH); }
     bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const;
 
     bool operator <(const CMalleableKeyView& kv) const { return vchPubKeyH.GetID() < kv.vchPubKeyH.GetID(); }
index eb38ddd..fa4f33c 100644 (file)
@@ -200,6 +200,22 @@ public:
                 malleableViewList.push_back(CMalleableKeyView(mi->first));
         }
     }
+
+    bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view)
+    {
+        const CKeyID &mpkID = mpk.GetID();
+        {
+            LOCK(cs_KeyStore);
+            for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
+                if (mi->first.GetID() == mpkID)
+                {
+                    view = CMalleableKeyView(mi->first);
+                    return true;
+                }
+        }
+
+        return false;
+    }
 };
 
 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
index d29738d..b0693ed 100644 (file)
@@ -239,3 +239,49 @@ Value dumpwallet(const Array& params, bool fHelp)
 
     return Value::null;
 }
+
+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");
+
+    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", mKey.ToString()));
+    result.push_back(Pair("PublicPair", mKey.GetMalleablePubKey().ToString()));
+
+    return result;
+}
+
+Value importmalleablekey(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 1)
+        throw runtime_error (
+            "importmalleablekey <Key data>\n"
+            "Imports the private key pair into your wallet.\n");
+
+    CMalleableKey mKey;
+    bool fSuccess = mKey.SetString(params[0].get_str());
+
+    Object result;
+
+    if (fSuccess)
+    {
+        fSuccess = pwalletMain->AddMalleableKey(mKey);
+        result.push_back(Pair("Successful", fSuccess));
+        result.push_back(Pair("PublicPair", mKey.GetMalleablePubKey().ToString()));
+        result.push_back(Pair("KeyView", CMalleableKeyView(mKey).ToString()));
+    }
+    else
+        result.push_back(Pair("Successful", false));
+
+    return result;
+}
index 0c27a90..ff627c1 100644 (file)
@@ -1912,23 +1912,28 @@ Value newmalleablekey(const Array& params, bool fHelp)
     return result;
 }
 
-Value dumpmalleablekey(const Array& params, bool fHelp)
+Value validatemalleablepubkey(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");
+        throw runtime_error(
+            "validatemalleablekey <Malleable public key data>\n"
+            "Check the validity and ownership for priovided malleable public key.\n");
 
-    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");
+    CMalleablePubKey mpk;
+    bool isValid = mpk.SetString(params[0].get_str());
 
     Object result;
-    result.push_back(Pair("PrivatePair", mKey.ToString()));
-    result.push_back(Pair("PublicPair", mKey.GetMalleablePubKey().ToString()));
+    result.push_back(Pair("isvalid", isValid));
+
+    if (isValid)
+    {
+        CMalleableKeyView view;
+        bool isMine = pwalletMain->GetMalleableView(mpk, view);
+        result.push_back(Pair("ismine", isMine));
+
+        if (isMine)
+            result.push_back(Pair("KeyView", view.ToString()));
+    }
 
     return result;
 }