using SQLite.Net.Attributes;
using SQLite.Net.Interop;
using SQLite.Net.Platform.Generic;
+using System;
using System.IO;
using System.Linq;
/// Item creation time
/// </summary>
[Indexed]
- public int nTime { get; set; }
+ public uint nTime { get; set; }
}
/// <summary>
/// <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>
~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>
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()
{
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()
/// <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;
}
/// <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)
{
{
var res = dbConn.Insert(new ScriptStorageItem()
{
- ScriptID = script.ScriptID.hashBytes,
- ScriptCode = script.Bytes
+ ScriptID = script.ScriptID,
+ ScriptCode = script
});
if (res == 0)
/// <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;
}
/// <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)
{
/// <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)
{
{
lock (LockObj)
{
- dbConn.Execute("delete from [KeyStorage] where [IsUsed] = false");
+ dbConn.Execute("delete from [KeyStorage] where not [IsUsed]");
GenerateKeys(nKeyPoolSize);
}
}