Block and transaction verifications
[NovacoinLibrary.git] / Novacoin / CKeyStore.cs
index 2736d68..8eb9e9d 100644 (file)
@@ -21,6 +21,7 @@ using SQLite.Net;
 using SQLite.Net.Attributes;
 using SQLite.Net.Interop;
 using SQLite.Net.Platform.Generic;
+using System;
 using System.IO;
 using System.Linq;
 
@@ -68,7 +69,7 @@ namespace Novacoin
         /// Item creation time
         /// </summary>
         [Indexed]
-        public int nTime { get; set; }
+        public uint nTime { get; set; }
     }
 
     /// <summary>
@@ -106,11 +107,11 @@ namespace Novacoin
     /// <summary>
     /// Key storage
     /// </summary>
-    public class CKeyStore
+    public class CKeyStore : IDisposable
     {
+        private bool disposed = false;
         private object LockObj = new object();
         private SQLiteConnection dbConn = null;
-
         private int nKeyPoolSize = 100;
 
         /// <summary>
@@ -139,13 +140,35 @@ namespace Novacoin
 
         ~CKeyStore()
         {
-            if (dbConn != null)
+            Dispose(false);
+        }
+
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        protected virtual void Dispose(bool disposing)
+        {
+            if (!disposed)
             {
-                dbConn.Close();
-                dbConn = null;
+                if (disposing)
+                {
+                    // Free other state (managed objects).
+                }
+
+                if (dbConn != null)
+                {
+                    dbConn.Close();
+                    dbConn = null;
+                }
+
+                disposed = true;
             }
         }
 
+
         /// <summary>
         /// Generate keys and insert them to key store.
         /// </summary>
@@ -163,9 +186,9 @@ namespace Novacoin
 
                     var res = dbConn.Insert(new KeyStorageItem()
                     {
-                        KeyID = keyPair.KeyID.hashBytes,
-                        PublicKey = keyPair.PublicBytes,
-                        PrivateKey = keyPair.SecretBytes,
+                        KeyID = keyPair.KeyID,
+                        PublicKey = keyPair.PubKey,
+                        PrivateKey = keyPair,
                         IsCompressed = keyPair.IsCompressed,
                         IsUsed = false,
                         nTime = Interop.GetTime()
@@ -189,9 +212,9 @@ namespace Novacoin
             {
                 var res = dbConn.Insert(new KeyStorageItem()
                 {
-                    KeyID = keyPair.KeyID.hashBytes,
-                    PublicKey = keyPair.PublicBytes,
-                    PrivateKey = keyPair.SecretBytes,
+                    KeyID = keyPair.KeyID,
+                    PublicKey = keyPair.PubKey,
+                    PrivateKey = keyPair,
                     IsCompressed = keyPair.IsCompressed,
                     IsUsed = true,
                     nTime = Interop.GetTime()
@@ -213,7 +236,7 @@ namespace Novacoin
         /// <returns>Checking result</returns>
         public bool HaveKey(CKeyID keyID)
         {
-            var QueryCount = dbConn.Query<NumQuery>("select count([ItemID]) from [KeyStorage] where [KeyID] = ?", keyID.hashBytes);
+            var QueryCount = dbConn.Query<NumQuery>("select count([ItemID]) from [KeyStorage] where [KeyID] = ?", (byte[])keyID);
 
             return QueryCount.First().Num == 1;
         }
@@ -226,7 +249,7 @@ namespace Novacoin
         /// <returns>Result</returns>
         public bool GetKey(CKeyID keyID, out CKeyPair keyPair)
         {
-            var QueryGet = dbConn.Query<KeyStorageItem>("select * from [KeyStorage] where [KeyID] = ?", keyID.hashBytes);
+            var QueryGet = dbConn.Query<KeyStorageItem>("select * from [KeyStorage] where [KeyID] = ?", (byte[])keyID);
 
             if (QueryGet.Count() == 1)
             {
@@ -249,8 +272,8 @@ namespace Novacoin
             {
                 var res = dbConn.Insert(new ScriptStorageItem()
                 {
-                    ScriptID = script.ScriptID.hashBytes,
-                    ScriptCode = script.Bytes
+                    ScriptID = script.ScriptID,
+                    ScriptCode = script
                 });
 
                 if (res == 0)
@@ -269,7 +292,7 @@ namespace Novacoin
         /// <returns>Checking result</returns>
         public bool HaveScript(CScriptID scriptID)
         {
-            var QueryGet = dbConn.Query<NumQuery>("select count([ItemID]) from [ScriptStorage] where [ScriptID] = ?", scriptID.hashBytes);
+            var QueryGet = dbConn.Query<NumQuery>("select count([ItemID]) from [ScriptStorage] where [ScriptID] = ?", (byte[])scriptID);
 
             return QueryGet.First().Num == 1;
         }
@@ -282,7 +305,7 @@ namespace Novacoin
         /// <returns>Result</returns>
         public bool GetScript(CScriptID scriptID, out CScript script)
         {
-            var QueryGet = dbConn.Query<ScriptStorageItem>("select * from [ScriptStorage] where [ScriptID] = ?", scriptID.hashBytes);
+            var QueryGet = dbConn.Query<ScriptStorageItem>("select * from [ScriptStorage] where [ScriptID] = ?", (byte[])scriptID);
 
             if (QueryGet.Count() == 1)
             {
@@ -311,7 +334,7 @@ namespace Novacoin
         /// <returns>CKeyID instance</returns>
         public CKeyID SelectKey(out int nKeyIndex)
         {
-            var QueryGet = dbConn.Query<ReservedKey>("select ItemId, KeyID from [KeyStorage] where not IsUsed order by [nTime] asc limit 1");
+            var QueryGet = dbConn.Query<ReservedKey>("select ItemId, KeyID from [KeyStorage] where not [IsUsed] order by [nTime] asc limit 1");
 
             if (QueryGet.Count() == 1)
             {
@@ -362,7 +385,7 @@ namespace Novacoin
         {
             lock (LockObj)
             {
-                dbConn.Execute("delete from [KeyStorage] where [IsUsed] = false");
+                dbConn.Execute("delete from [KeyStorage] where not [IsUsed]");
                 GenerateKeys(nKeyPoolSize);
             }
         }