RPC: Add new methods suitable for malleable key pairs management;
[novacoin.git] / src / keystore.cpp
index 089f2d5..fdbecba 100644 (file)
@@ -29,8 +29,20 @@ bool CBasicKeyStore::AddKey(const CKey& key)
     return true;
 }
 
+bool CBasicKeyStore::AddMalleableKey(const CMalleableKey& mKey)
+{
+    {
+        LOCK(cs_KeyStore);
+        mapMalleableKeys[CMalleableKeyView(mKey)] = mKey;
+    }
+    return true;
+}
+
 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
 {
+    if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
+        return error("CBasicKeyStore::AddCScript() : redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
+
     {
         LOCK(cs_KeyStore);
         mapScripts[redeemScript.GetID()] = redeemScript;
@@ -63,25 +75,42 @@ bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut)
     return false;
 }
 
-bool CBasicKeyStore::AddWatchOnly(const CTxDestination &dest)
+bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
 {
     LOCK(cs_KeyStore);
-    CKeyID keyID;
-    CBitcoinAddress(dest).GetKeyID(keyID);
 
-    if (HaveKey(keyID))
-        return false;
+    CTxDestination address;
+    if (ExtractDestination(dest, address)) {
+        CKeyID keyID;
+        CBitcoinAddress(address).GetKeyID(keyID);
+        if (HaveKey(keyID))
+            return false;
+    }
 
     setWatchOnly.insert(dest);
     return true;
 }
 
-bool CBasicKeyStore::HaveWatchOnly(const CTxDestination &dest) const
+
+bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
+{
+    LOCK(cs_KeyStore);
+    setWatchOnly.erase(dest);
+    return true;
+}
+
+bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
 {
     LOCK(cs_KeyStore);
     return setWatchOnly.count(dest) > 0;
 }
 
+bool CBasicKeyStore::HaveWatchOnly() const
+{
+    LOCK(cs_KeyStore);
+    return (!setWatchOnly.empty());
+}
+
 bool CCryptoKeyStore::SetCrypted()
 {
     {
@@ -134,6 +163,7 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
                 break;
             return false;
         }
+
         vMasterKey = vMasterKeyIn;
     }
     NotifyStatusChanged(this);
@@ -145,8 +175,10 @@ bool CCryptoKeyStore::AddKey(const CKey& key)
     {
         LOCK(cs_KeyStore);
 
-        CTxDestination address = key.GetPubKey().GetID();
-        if (HaveWatchOnly(address))
+        CScript script;
+        script.SetDestination(key.GetPubKey().GetID());
+
+        if (HaveWatchOnly(script))
             return false;
 
         if (!IsCrypted())
@@ -247,3 +279,33 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
     }
     return true;
 }
+
+bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn)
+{
+    {
+        LOCK(cs_KeyStore);
+        if (!IsCrypted())
+            return false;
+
+        CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
+        for (; mi != mapCryptedKeys.end(); ++mi)
+        {
+            const CPubKey &vchPubKey = (*mi).second.first;
+            const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
+            CSecret vchSecret;
+            if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
+                return false;
+            if (vchSecret.size() != 32)
+                return false;
+            CKey key;
+            key.SetPubKey(vchPubKey);
+            key.SetSecret(vchSecret);
+            if (!CBasicKeyStore::AddKey(key))
+                return false;
+        }
+
+        mapCryptedKeys.clear();
+    }
+
+    return true;
+}