2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 using SQLite.Net.Attributes;
22 using SQLite.Net.Interop;
23 using SQLite.Net.Platform.Generic;
30 /// Key storage item and structure
38 [PrimaryKey, AutoIncrement]
39 public int ItemId { get; set; }
44 public byte[] KeyID { get; set; }
49 public byte[] PublicKey { get; set; }
54 public byte[] PrivateKey { get; set; }
57 /// Compressed key flag
59 public bool IsCompressed { get; set; }
62 /// Is this key a part of KeyPool?
65 public bool IsUsed { get; set; }
68 /// Item creation time
71 public int nTime { get; set; }
75 /// Script storage item and structure
77 [Table("ScriptStorage")]
78 class ScriptStorageItem
81 /// Script item number
83 [PrimaryKey, AutoIncrement]
84 public int ItemId { get; set; }
89 public byte[] ScriptID { get; set; }
94 public byte[] ScriptCode { get; set; }
98 /// select count(...) as Count from ... where ...
102 public int Count { get; set; }
105 public class CKeyStore
107 private object LockObj = new object();
108 private SQLiteConnection dbConn = null;
110 public CKeyStore(string strDatabasePath="KeyStore.db")
112 bool firstInit = File.Exists(strDatabasePath);
114 dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), strDatabasePath);
120 dbConn.CreateTable<KeyStorageItem>(CreateFlags.AutoIncPK);
121 dbConn.CreateTable<ScriptStorageItem>(CreateFlags.AutoIncPK);
123 dbConn.BeginTransaction();
126 for (int i = 0; i < 1000; i++)
128 var keyPair = new CKeyPair();
130 var res = dbConn.Insert(new KeyStorageItem()
132 KeyID = keyPair.KeyID.hashBytes,
133 PublicKey = keyPair.PublicBytes,
134 PrivateKey = keyPair.SecretBytes,
135 IsCompressed = keyPair.IsCompressed,
137 nTime = Interop.GetTime()
140 // TODO: Additional initialization
158 /// Insert key data into table
160 /// <param name="keyPair">CKeyPair instance</param>
161 /// <returns>Result</returns>
162 public bool AddKey(CKeyPair keyPair)
166 var res = dbConn.Insert(new KeyStorageItem()
168 KeyID = keyPair.KeyID.hashBytes,
169 PublicKey = keyPair.PublicBytes,
170 PrivateKey = keyPair.SecretBytes,
171 IsCompressed = keyPair.IsCompressed,
173 nTime = Interop.GetTime()
185 public bool HaveKey(CKeyID keyID)
187 var QueryCount = dbConn.Query<CountQuery>("select count([ItemID]) as [Count] from [KeyStorage] where [KeyID] = ?", keyID.hashBytes);
189 return QueryCount.First().Count == 1;
193 /// Get the key pair object.
195 /// <param name="keyID">Hash of public key.</param>
196 /// <param name="keyPair">Instance of CKeyPair or null.</param>
197 /// <returns>Result</returns>
198 public bool GetKey(CKeyID keyID, out CKeyPair keyPair)
200 var QueryGet = dbConn.Query<KeyStorageItem>("select * from [KeyStorage] where [KeyID] = ?", keyID.hashBytes);
202 if (QueryGet.Count() == 1)
204 keyPair = new CKeyPair(QueryGet.First().PrivateKey);
212 public bool AddScript(CScript script)
216 var res = dbConn.Insert(new ScriptStorageItem()
218 ScriptID = script.ScriptID.hashBytes,
219 ScriptCode = script.Bytes
231 public bool HaveScript(CScriptID scriptID)
233 var QueryGet = dbConn.Query<CountQuery>("select count([ItemID]) from [ScriptStorage] where [ScriptID] = ?", scriptID.hashBytes);
235 return QueryGet.First().Count == 1;
238 public bool GetScript(CScriptID scriptID, out CScript script)
240 var QueryGet = dbConn.Query<ScriptStorageItem>("select * from [ScriptStorage] where [ScriptID] = ?", scriptID.hashBytes);
242 if (QueryGet.Count() == 1)
244 script = new CScript(QueryGet.First().ScriptCode);