Esempio n. 1
0
 protected override byte[] LoadStoredData(string name)
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         return(ctx.Keys.FirstOrDefault(p => p.Name == name)?.Value);
     }
 }
Esempio n. 2
0
 public IEnumerable <TransactionInfo> LoadTransactions()
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         return(GetTransactionInfo(ctx.Transactions.ToArray()));
     }
 }
Esempio n. 3
0
        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 }));
            }
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
 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();
     }
 }
Esempio n. 6
0
 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();
     }
 }
Esempio n. 7
0
 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();
     }
 }
Esempio n. 8
0
 protected override void BuildDatabase()
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         ctx.Database.EnsureDeleted();
         ctx.Database.EnsureCreated();
     }
 }
Esempio n. 9
0
 protected override void SaveStoredData(string name, byte[] value)
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         SaveStoredData(ctx, name, value);
         ctx.SaveChanges();
     }
 }
Esempio n. 10
0
 protected override IEnumerable <WalletContract> LoadContracts()
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         foreach (Contract contract in ctx.Contracts)
         {
             yield return(contract.RawData.AsSerializable <WalletContract>());
         }
     }
 }
Esempio n. 11
0
 public override IEnumerable <CoreTransaction> GetTransactions()
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         foreach (Transaction tx in ctx.Transactions)
         {
             yield return(CoreTransaction.DeserializeFrom(tx.RawData));
         }
     }
 }
Esempio n. 12
0
 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));
         }
     }
 }
Esempio n. 13
0
 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));
         }
     }
 }
Esempio n. 14
0
 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));
         }
     }
 }
Esempio n. 15
0
 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));
         }
     }
 }
Esempio n. 16
0
 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));
     }
 }
Esempio n. 17
0
 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));
     }
 }
Esempio n. 18
0
 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));
     }
 }
Esempio n. 19
0
        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));
                }
            }
        }
Esempio n. 20
0
 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();
         }
     }
 }
Esempio n. 21
0
 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);
         }
     }
 }
Esempio n. 22
0
 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)));
     }
 }
Esempio n. 23
0
 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();
         }
     }
 }
Esempio n. 24
0
 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));
         }
     }
 }
Esempio n. 25
0
 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();
         }
     }
 }
Esempio n. 26
0
        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();
                }
        }
Esempio n. 27
0
        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;
            }
        }
Esempio n. 28
0
        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);
        }
Esempio n. 29
0
        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);
        }
Esempio n. 30
0
        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));
        }