protected override byte[] LoadStoredData(string name) { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { return(ctx.Keys.FirstOrDefault(p => p.Name == name)?.Value); } }
public IEnumerable <TransactionInfo> LoadTransactions() { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { return(GetTransactionInfo(ctx.Transactions.ToArray())); } }
protected override void OnSaveTransaction(CoreTransaction tx, IEnumerable <WalletCoin> added, IEnumerable <WalletCoin> changed) { Transaction tx_changed = null; using (WalletDataContext ctx = new WalletDataContext(DbPath)) { if (IsWalletTransaction(tx)) { tx_changed = ctx.Transactions.Add(new Transaction { Hash = tx.Hash.ToArray(), Type = tx.Type, RawData = tx.ToArray(), Height = null, Time = DateTime.Now }).Entity; } OnCoinsChanged(ctx, added, changed, Enumerable.Empty <WalletCoin>()); ctx.SaveChanges(); } if (tx_changed != null) { TransactionsChanged?.Invoke(this, GetTransactionInfo(new[] { tx_changed })); } }
public override bool DeleteKey(UInt160 publicKeyHash) { bool flag = base.DeleteKey(publicKeyHash); if (flag) { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { Account account = ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash.SequenceEqual(publicKeyHash.ToArray())); if (account != null) { foreach (byte[] hash in ctx.Contracts.Where(p => p.PublicKeyHash.SequenceEqual(publicKeyHash.ToArray())).Select(p => p.ScriptHash)) { Address address = ctx.Addresses.FirstOrDefault(p => p.ScriptHash.SequenceEqual(hash)); if (address != null) { ctx.Addresses.Remove(address); } } ctx.Accounts.Remove(account); ctx.SaveChanges(); } } } return(flag); }
public override void AddContract(WalletContract contract) { base.AddContract(contract); using (WalletDataContext ctx = new WalletDataContext(DbPath)) { Contract db_contract = ctx.Contracts.FirstOrDefault(p => p.ScriptHash.SequenceEqual(contract.ScriptHash.ToArray())); if (db_contract != null) { db_contract.PublicKeyHash = contract.PublicKeyHash.ToArray(); } else { Address db_address = ctx.Addresses.FirstOrDefault(p => p.ScriptHash.SequenceEqual(contract.ScriptHash.ToArray())); if (db_address == null) { ctx.Addresses.Add(new Address { ScriptHash = contract.ScriptHash.ToArray() }); } ctx.Contracts.Add(new Contract { RawData = contract.ToArray(), ScriptHash = contract.ScriptHash.ToArray(), PublicKeyHash = contract.PublicKeyHash.ToArray() }); } ctx.SaveChanges(); } }
private void OnCreateAccount(WalletAccount account) { byte[] decryptedPrivateKey = new byte[96]; Buffer.BlockCopy(account.PublicKey.EncodePoint(false), 1, decryptedPrivateKey, 0, 64); using (account.Decrypt()) { Buffer.BlockCopy(account.PrivateKey, 0, decryptedPrivateKey, 64, 32); } byte[] encryptedPrivateKey = EncryptPrivateKey(decryptedPrivateKey); Array.Clear(decryptedPrivateKey, 0, decryptedPrivateKey.Length); using (WalletDataContext ctx = new WalletDataContext(DbPath)) { Account db_account = ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash.SequenceEqual(account.PublicKeyHash.ToArray())); if (db_account == null) { db_account = ctx.Accounts.Add(new Account { PrivateKeyEncrypted = encryptedPrivateKey, PublicKeyHash = account.PublicKeyHash.ToArray() }).Entity; } else { db_account.PrivateKeyEncrypted = encryptedPrivateKey; } ctx.SaveChanges(); } }
protected override void OnProcessNewBlock(IEnumerable <WalletCoin> added, IEnumerable <WalletCoin> changed, IEnumerable <WalletCoin> deleted) { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { foreach (WalletCoin coin in added) { ctx.Coins.Add(new Coin { TxId = coin.Input.PrevHash.ToArray(), Index = coin.Input.PrevIndex, AssetId = coin.AssetId.ToArray(), Value = coin.Value.GetData(), ScriptHash = coin.ScriptHash.ToArray(), State = CoinState.Unspent }); } foreach (WalletCoin coin in changed) { ctx.Coins.First(p => p.TxId.SequenceEqual(coin.Input.PrevHash.ToArray()) && p.Index == coin.Input.PrevIndex).State = coin.State; } foreach (WalletCoin coin in deleted) { Coin unspent_coin = ctx.Coins.FirstOrDefault(p => p.TxId.SequenceEqual(coin.Input.PrevHash.ToArray()) && p.Index == coin.Input.PrevIndex); ctx.Coins.Remove(unspent_coin); } ctx.Keys.First(p => p.Name == "Height").Value = BitConverter.GetBytes(WalletHeight); ctx.SaveChanges(); } }
protected override void BuildDatabase() { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { ctx.Database.EnsureDeleted(); ctx.Database.EnsureCreated(); } }
protected override void SaveStoredData(string name, byte[] value) { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { SaveStoredData(ctx, name, value); ctx.SaveChanges(); } }
protected override IEnumerable <WalletContract> LoadContracts() { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { foreach (Contract contract in ctx.Contracts) { yield return(contract.RawData.AsSerializable <WalletContract>()); } } }
public override IEnumerable <CoreTransaction> GetTransactions() { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { foreach (Transaction tx in ctx.Transactions) { yield return(CoreTransaction.DeserializeFrom(tx.RawData)); } } }
public override IEnumerable <WContract> GetContracts() { using (WalletDataContext ctx = new WalletDataContext(path)) { foreach (byte[] redeemScript in ctx.Contracts.Select(p => p.RedeemScript)) { yield return(new WContract(redeemScript)); } } }
public override IEnumerable <UInt160> GetAddresses() { using (WalletDataContext ctx = new WalletDataContext(path)) { foreach (byte[] scriptHash in ctx.Contracts.Select(p => p.ScriptHash)) { yield return(new UInt160(scriptHash)); } } }
public override IEnumerable <WAccount> GetAccounts() { using (WalletDataContext ctx = new WalletDataContext(path)) { foreach (byte[] encryptedPrivateKey in ctx.Accounts.Select(p => p.PrivateKeyEncrypted)) { yield return(GetAccountInternal(encryptedPrivateKey)); } } }
protected override IEnumerable <UInt160> LoadWatchOnly() { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { foreach (byte[] hash in ctx.Addresses.Select(p => p.ScriptHash).Except(ctx.Contracts.Select(p => p.ScriptHash))) { yield return(new UInt160(hash)); } } }
public static UserWallet OpenDatabase(string path, string password) { using (WalletDataContext ctx = new WalletDataContext(path)) { byte[] masterKey = ctx.Keys.First(p => p.Name == Key.MasterKey).Value; byte[] passwordKey = password.ToAesKey(); byte[] iv = ctx.Keys.First(p => p.Name == Key.IV).Value; masterKey.AesDecrypt(passwordKey, iv); Array.Clear(passwordKey, 0, passwordKey.Length); return(new UserWallet(path, masterKey, iv)); } }
public override WContract GetContract(UInt160 scriptHash) { using (WalletDataContext ctx = new WalletDataContext(path)) { byte[] redeemScript = ctx.Contracts.FirstOrDefault(p => p.ScriptHash == scriptHash.ToArray())?.RedeemScript; if (redeemScript == null) { return(null); } return(new WContract(redeemScript)); } }
public override WAccount GetAccountByScriptHash(UInt160 scriptHash) { using (WalletDataContext ctx = new WalletDataContext(path)) { byte[] publicKeyHash = ctx.Contracts.FirstOrDefault(p => p.ScriptHash == scriptHash.ToArray())?.PublicKeyHash; if (publicKeyHash == null) { return(null); } return(GetAccountInternal(ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == publicKeyHash)?.PrivateKeyEncrypted)); } }
public override IEnumerable <T> GetTransactions <T>() { TransactionType type = (TransactionType)Enum.Parse(typeof(TransactionType), typeof(T).Name); using (WalletDataContext ctx = new WalletDataContext(DbPath)) { foreach (Transaction tx in ctx.Transactions.Where(p => p.Type == type)) { yield return((T)CoreTransaction.DeserializeFrom(tx.RawData)); } } }
protected override void DeleteAccount(UInt160 publicKeyHash) { using (WalletDataContext ctx = new WalletDataContext(path)) { Account account = ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == publicKeyHash.ToArray()); if (account != null) { ctx.Contracts.RemoveRange(ctx.Contracts.Where(p => p.PublicKeyHash == publicKeyHash.ToArray())); ctx.Accounts.Remove(account); ctx.SaveChanges(); } } }
protected override IEnumerable <WalletAccount> LoadAccounts() { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { foreach (byte[] encryptedPrivateKey in ctx.Accounts.Select(p => p.PrivateKeyEncrypted)) { byte[] decryptedPrivateKey = DecryptPrivateKey(encryptedPrivateKey); WalletAccount account = new WalletAccount(decryptedPrivateKey); Array.Clear(decryptedPrivateKey, 0, decryptedPrivateKey.Length); yield return(account); } } }
public override IEnumerable <T> GetTransactions <T>() { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { IQueryable <Transaction> transactions = ctx.Transactions; if (typeof(T).GetTypeInfo().IsSubclassOf(typeof(CoreTransaction))) { TransactionType type = (TransactionType)Enum.Parse(typeof(TransactionType), typeof(T).Name); transactions = transactions.Where(p => p.Type == type); } return(transactions.Select(p => p.RawData).ToArray().Select(p => (T)CoreTransaction.DeserializeFrom(p))); } }
public override void Rebuild() { lock (SyncRoot) { base.Rebuild(); using (WalletDataContext ctx = new WalletDataContext(DbPath)) { ctx.Keys.First(p => p.Name == "Height").Value = BitConverter.GetBytes(0); ctx.Database.ExecuteSqlCommand($"DELETE FROM [{nameof(Transaction)}]"); ctx.Database.ExecuteSqlCommand($"DELETE FROM [{nameof(Coin)}]"); ctx.SaveChanges(); } } }
protected override IEnumerable <WalletContract> LoadContracts() { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { foreach (Contract contract in ctx.Contracts) { Type type = Type.GetType(contract.Type, false); if (type == null || !typeof(WalletContract).IsAssignableFrom(type)) { continue; } yield return((WalletContract)contract.RawData.AsSerializable(type)); } } }
public override void AddWatchOnly(UInt160 scriptHash) { base.AddWatchOnly(scriptHash); using (WalletDataContext ctx = new WalletDataContext(DbPath)) { if (ctx.Addresses.All(p => !p.ScriptHash.SequenceEqual(scriptHash.ToArray()))) { ctx.Addresses.Add(new Address { ScriptHash = scriptHash.ToArray() }); ctx.SaveChanges(); } } }
public static void Migrate(string path_old, string path_new) { Version current_version = typeof(UserWallet).GetTypeInfo().Assembly.GetName().Version; using (WalletDataContext ctx_old = new WalletDataContext(path_old)) using (WalletDataContext ctx_new = new WalletDataContext(path_new)) { ctx_new.Database.EnsureCreated(); ctx_new.Accounts.AddRange(ctx_old.Accounts); ctx_new.Contracts.AddRange(ctx_old.Contracts); ctx_new.Keys.AddRange(ctx_old.Keys.Where(p => p.Name != "Height" && p.Name != "Version")); SaveStoredData(ctx_new, "Height", BitConverter.GetBytes(0)); SaveStoredData(ctx_new, "Version", new[] { current_version.Major, current_version.Minor, current_version.Build, current_version.Revision }.Select(p => BitConverter.GetBytes(p)).SelectMany(p => p).ToArray()); ctx_new.SaveChanges(); } }
private static void SaveStoredData(WalletDataContext ctx, string name, byte[] value) { Key key = ctx.Keys.FirstOrDefault(p => p.Name == name); if (key == null) { ctx.Keys.Add(new Key { Name = name, Value = value }); } else { key.Value = value; } }
public override bool DeleteAddress(UInt160 scriptHash) { bool flag = base.DeleteAddress(scriptHash); if (flag) { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { Address address = ctx.Addresses.FirstOrDefault(p => p.ScriptHash.SequenceEqual(scriptHash.ToArray())); if (address != null) { ctx.Addresses.Remove(address); ctx.SaveChanges(); } } } return(flag); }
public override bool DeleteContract(UInt160 scriptHash) { bool flag = base.DeleteContract(scriptHash); if (flag) { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { Contract contract = ctx.Contracts.FirstOrDefault(p => p.ScriptHash.SequenceEqual(scriptHash.ToArray())); if (contract != null) { ctx.Contracts.Remove(contract); ctx.SaveChanges(); } } } return(flag); }
public static Version GetVersion(string path) { byte[] buffer; using (WalletDataContext ctx = new WalletDataContext(path)) { buffer = ctx.Keys.FirstOrDefault(p => p.Name == "Version")?.Value; } if (buffer == null) { return(new Version(0, 0)); } int major = BitConverter.ToInt32(buffer, 0); int minor = BitConverter.ToInt32(buffer, 4); int build = BitConverter.ToInt32(buffer, 8); int revision = BitConverter.ToInt32(buffer, 12); return(new Version(major, minor, build, revision)); }