Prevent import keys for watch-only addresses and vice versa.
[novacoin.git] / src / keystore.cpp
index e0cf805..089f2d5 100644 (file)
@@ -5,6 +5,9 @@
 
 #include "keystore.h"
 #include "script.h"
+#include "base58.h"
+
+extern bool fWalletUnlockMintOnly;
 
 bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
 {
@@ -60,6 +63,25 @@ bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut)
     return false;
 }
 
+bool CBasicKeyStore::AddWatchOnly(const CTxDestination &dest)
+{
+    LOCK(cs_KeyStore);
+    CKeyID keyID;
+    CBitcoinAddress(dest).GetKeyID(keyID);
+
+    if (HaveKey(keyID))
+        return false;
+
+    setWatchOnly.insert(dest);
+    return true;
+}
+
+bool CBasicKeyStore::HaveWatchOnly(const CTxDestination &dest) const
+{
+    LOCK(cs_KeyStore);
+    return setWatchOnly.count(dest) > 0;
+}
+
 bool CCryptoKeyStore::SetCrypted()
 {
     {
@@ -81,6 +103,7 @@ bool CCryptoKeyStore::Lock()
     {
         LOCK(cs_KeyStore);
         vMasterKey.clear();
+        fWalletUnlockMintOnly = false;
     }
 
     NotifyStatusChanged(this);
@@ -121,6 +144,11 @@ bool CCryptoKeyStore::AddKey(const CKey& key)
 {
     {
         LOCK(cs_KeyStore);
+
+        CTxDestination address = key.GetPubKey().GetID();
+        if (HaveWatchOnly(address))
+            return false;
+
         if (!IsCrypted())
             return CBasicKeyStore::AddKey(key);