Example #1
0
 public override Account GetAccount(UInt160 publicKeyHash)
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         return(GetAccountInternal(ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == publicKeyHash.ToArray())?.PrivateKeyEncrypted));
     }
 }
Example #2
0
 public static UserWallet CreateDatabase(string path, string password)
 {
     SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
     sb.AttachDBFilename = path;
     sb.DataSource = @"(LocalDB)\v11.0";
     sb.IntegratedSecurity = true;
     using (WalletDataContext ctx = new WalletDataContext(sb.ToString()))
     using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
     {
         byte[] passwordKey = password.ToAesKey();
         byte[] masterKey = new byte[32];
         byte[] iv = new byte[16];
         rng.GetNonZeroBytes(masterKey);
         rng.GetNonZeroBytes(iv);
         masterKey.AesEncrypt(passwordKey, iv);
         Array.Clear(passwordKey, 0, passwordKey.Length);
         ctx.Database.Delete();
         ctx.Database.Create();
         ctx.Keys.Add(new Key
         {
             Name = Key.MasterKey,
             Value = masterKey
         });
         ctx.Keys.Add(new Key
         {
             Name = Key.IV,
             Value = iv
         });
         ctx.SaveChanges();
     }
     UserWallet wallet = OpenDatabase(path, password);
     wallet.CreateAccount();
     return wallet;
 }
Example #3
0
 protected override void SaveAccount(Account account)
 {
     byte[] decryptedPrivateKey = new byte[96];
     Buffer.BlockCopy(account.PublicKey, 0, 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(connectionString))
     {
         DbAccount db_account = ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == account.PublicKeyHash.ToArray());
         if (db_account == null)
         {
             db_account = ctx.Accounts.Add(new DbAccount
             {
                 PrivateKeyEncrypted = encryptedPrivateKey,
                 PublicKeyHash       = account.PublicKeyHash.ToArray()
             });
         }
         else
         {
             db_account.PrivateKeyEncrypted = encryptedPrivateKey;
         }
         ctx.SaveChanges();
     }
 }
Example #4
0
 public override WAccount GetAccount(UInt160 publicKeyHash)
 {
     using (WalletDataContext ctx = new WalletDataContext(path))
     {
         return GetAccountInternal(ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == publicKeyHash.ToArray())?.PrivateKeyEncrypted);
     }
 }
Example #5
0
 public static UserWallet CreateDatabase(string path, string password)
 {
     using (WalletDataContext ctx = new WalletDataContext(path))
     using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
     {
         byte[] passwordKey = password.ToAesKey();
         byte[] masterKey = new byte[32];
         byte[] iv = new byte[16];
         rng.GetNonZeroBytes(masterKey);
         rng.GetNonZeroBytes(iv);
         masterKey.AesEncrypt(passwordKey, iv);
         Array.Clear(passwordKey, 0, passwordKey.Length);
         ctx.Database.EnsureDeleted();
         ctx.Database.EnsureCreated();
         ctx.Keys.Add(new Key
         {
             Name = Key.MasterKey,
             Value = masterKey
         });
         ctx.Keys.Add(new Key
         {
             Name = Key.IV,
             Value = iv
         });
         ctx.SaveChanges();
     }
     UserWallet wallet = OpenDatabase(path, password);
     wallet.CreateAccount();
     return wallet;
 }
Example #6
0
 protected override void BuildDatabase()
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         ctx.Database.EnsureDeleted();
         ctx.Database.EnsureCreated();
     }
 }
Example #7
0
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: NeoWalletDump *.db3 wallet-password");
                Console.WriteLine("ex)NeoWalletDump foo.db3 Password1");
                return;
            }
            var db3Path = args[0];

            byte[] passwordKey = args[1].ToAesKey();

            SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());

            using (WalletDataContext ctx = new WalletDataContext(db3Path))
            {
                {
                    WriteTableName("Key");
                    var PasswordHash = ReadSqlitItem(ctx, "PasswordHash");
                    var IV           = ReadSqlitItem(ctx, "IV");
                    var MasterKey    = ReadSqlitItem(ctx, "MasterKey").AesDecrypt(passwordKey, IV);

                    DumpSqliteColumn(nameof(PasswordHash), PasswordHash);
                    DumpSqliteColumn(nameof(IV), IV);
                    DumpSqliteColumn(nameof(MasterKey), MasterKey);
                }
                {
                    WriteTableName("Account");
                    foreach (var x in ctx.Accounts)
                    {
                        var PublicKeyHash = x.PublicKeyHash;
                        DumpSqliteColumn(nameof(PublicKeyHash), PublicKeyHash);
                        var PrivateKeyEncrypted = x.PrivateKeyEncrypted;
                        DumpSqliteColumn(nameof(PrivateKeyEncrypted), PrivateKeyEncrypted);
                    }
                }
                {
                    WriteTableName("Address");
                    foreach (var x in ctx.Addresses)
                    {
                        var ScriptHash = x.ScriptHash;
                        DumpSqliteColumn(nameof(ScriptHash), ScriptHash);
                    }
                }
                {
                    WriteTableName("Contract");
                    foreach (var x in ctx.Contracts)
                    {
                        var ScriptHash = x.ScriptHash;
                        DumpSqliteColumn(nameof(ScriptHash), ScriptHash);
                        var PublicKeyHash = x.PublicKeyHash;
                        DumpSqliteColumn(nameof(PublicKeyHash), PublicKeyHash);
                        var RawData = x.RawData;
                        DumpSqliteColumn(nameof(RawData), RawData);
                    }
                }
            }
        }
Example #8
0
 public override IEnumerable <Account> GetAccounts()
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         foreach (byte[] encryptedPrivateKey in ctx.Accounts.Select(p => p.PrivateKeyEncrypted))
         {
             yield return(GetAccountInternal(encryptedPrivateKey));
         }
     }
 }
Example #9
0
 public override IEnumerable <Contract> GetContracts()
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         foreach (byte[] redeemScript in ctx.Contracts.Select(p => p.RedeemScript))
         {
             yield return(new Contract(redeemScript));
         }
     }
 }
Example #10
0
        private static byte[] ReadSqlitItem(WalletDataContext ctx, string name)
        {
            byte[] ret = ctx.Keys.FirstOrDefault(p => p.Name == name)?.Value;
            //Console.WriteLine(name);
            //Console.WriteLine("BitConverter:" + BitConverter.ToString(ret));
            //Console.WriteLine("ASCII:" + Encoding.ASCII.GetString(ret));
            //Console.WriteLine();

            return(ret);
        }
Example #11
0
 public override IEnumerable <UInt160> GetAddresses()
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         foreach (byte[] scriptHash in ctx.Contracts.Select(p => p.ScriptHash))
         {
             yield return(new UInt160(scriptHash));
         }
     }
 }
Example #12
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);
     }
 }
Example #13
0
 public override Contract GetContract(UInt160 scriptHash)
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         byte[] redeemScript = ctx.Contracts.FirstOrDefault(p => p.ScriptHash == scriptHash.ToArray())?.RedeemScript;
         if (redeemScript == null)
         {
             return(null);
         }
         return(new Contract(redeemScript));
     }
 }
Example #14
0
 public override Account GetAccountByScriptHash(UInt160 scriptHash)
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         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));
     }
 }
Example #15
0
 protected override void DeleteAccount(UInt160 publicKeyHash)
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         DbAccount 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();
         }
     }
 }
Example #16
0
 public override bool DeleteAccount(UInt160 publicKeyHash)
 {
     bool flag = base.DeleteAccount(publicKeyHash);
     if (flag)
     {
         using (WalletDataContext ctx = new WalletDataContext(DbPath))
         {
             Account account = ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash.SequenceEqual(publicKeyHash.ToArray()));
             if (account != null)
             {
                 ctx.Accounts.Remove(account);
                 ctx.SaveChanges();
             }
         }
     }
     return flag;
 }
Example #17
0
 public static UserWallet OpenDatabase(string path, string password)
 {
     SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
     sb.AttachDBFilename = path;
     sb.DataSource = @"(LocalDB)\MSSQLLocalDB";
     sb.IntegratedSecurity = true;
     string connectionString = sb.ToString();
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         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(connectionString, masterKey, iv);
     }
 }
Example #18
0
        public static UserWallet OpenDatabase(string path, string password)
        {
            SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();

            sb.AttachDBFilename   = path;
            sb.DataSource         = @"(LocalDB)\MSSQLLocalDB";
            sb.IntegratedSecurity = true;
            string connectionString = sb.ToString();

            using (WalletDataContext ctx = new WalletDataContext(connectionString))
            {
                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(connectionString, masterKey, iv));
            }
        }
Example #19
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 = ctx.Contracts.Add(new Contract
             {
                 Type = contract.GetType().ToString(),
                 RawData = contract.ToArray(),
                 ScriptHash = contract.ScriptHash.ToArray(),
                 PublicKeyHash = contract.PublicKeyHash.ToArray()
             }).Entity;
         }
         else
         {
             db_contract.PublicKeyHash = contract.PublicKeyHash.ToArray();
         }
         ctx.SaveChanges();
     }
 }
Example #20
0
        public static UserWallet CreateDatabase(string path, string password)
        {
            SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();

            sb.AttachDBFilename   = path;
            sb.DataSource         = @"(LocalDB)\v11.0";
            sb.IntegratedSecurity = true;
            using (WalletDataContext ctx = new WalletDataContext(sb.ToString()))
                using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
                {
                    byte[] passwordKey = password.ToAesKey();
                    byte[] masterKey   = new byte[32];
                    byte[] iv          = new byte[16];
                    rng.GetNonZeroBytes(masterKey);
                    rng.GetNonZeroBytes(iv);
                    masterKey.AesEncrypt(passwordKey, iv);
                    Array.Clear(passwordKey, 0, passwordKey.Length);
                    ctx.Database.Delete();
                    ctx.Database.Create();
                    ctx.Keys.Add(new Key
                    {
                        Name  = Key.MasterKey,
                        Value = masterKey
                    });
                    ctx.Keys.Add(new Key
                    {
                        Name  = Key.IV,
                        Value = iv
                    });
                    ctx.SaveChanges();
                }
            UserWallet wallet = OpenDatabase(path, password);

            wallet.CreateAccount();
            return(wallet);
        }
Example #21
0
 protected override void OnProcessNewBlock(IEnumerable<TransactionInput> spent, IEnumerable<WalletUnspentCoin> unspent)
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         foreach (TransactionInput input in spent)
         {
             UnspentCoin unspent_coin = ctx.UnspentCoins.FirstOrDefault(p => p.TxId.SequenceEqual(input.PrevHash.ToArray()) && p.Index == input.PrevIndex);
             if (unspent_coin != null)
                 ctx.UnspentCoins.Remove(unspent_coin);
         }
         foreach (WalletUnspentCoin coin in unspent)
         {
             UnspentCoin unspent_coin = ctx.UnspentCoins.FirstOrDefault(p => p.TxId.SequenceEqual(coin.Input.PrevHash.ToArray()) && p.Index == coin.Input.PrevIndex);
             if (unspent_coin == null)
             {
                 unspent_coin = ctx.UnspentCoins.Add(new UnspentCoin
                 {
                     TxId = coin.Input.PrevHash.ToArray(),
                     Index = coin.Input.PrevIndex,
                     AssetId = coin.AssetId.ToArray(),
                     Value = coin.Value.GetData(),
                     ScriptHash = coin.ScriptHash.ToArray(),
                     IsChange = false
                 }).Entity;
             }
             else
             {
                 unspent_coin.IsChange = false;
             }
         }
         ctx.Keys.First(p => p.Name == "Height").Value = BitConverter.GetBytes(WalletHeight);
         ctx.SaveChanges();
     }
 }
Example #22
0
 protected override void SaveStoredData(string name, byte[] value)
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         Key key = ctx.Keys.FirstOrDefault(p => p.Name == name);
         if (key == null)
         {
             key = ctx.Keys.Add(new Key
             {
                 Name = name,
                 Value = value
             }).Entity;
         }
         else
         {
             key.Value = value;
         }
         ctx.SaveChanges();
     }
 }
Example #23
0
 protected override IEnumerable<WalletUnspentCoin> LoadUnspentCoins(bool is_change)
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         foreach (UnspentCoin coin in ctx.UnspentCoins.Where(p => p.IsChange == is_change))
         {
             yield return new WalletUnspentCoin
             {
                 Input = new TransactionInput
                 {
                     PrevHash = new UInt256(coin.TxId),
                     PrevIndex = coin.Index
                 },
                 AssetId = new UInt256(coin.AssetId),
                 Value = new Fixed8(coin.Value),
                 ScriptHash = new UInt160(coin.ScriptHash)
             };
         }
     }
 }
Example #24
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();
     }
 }
Example #25
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);
         }
     }
 }
Example #26
0
 protected override byte[] LoadStoredData(string name)
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         return ctx.Keys.FirstOrDefault(p => p.Name == name)?.Value;
     }
 }
Example #27
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;
         }
     }
 }
Example #28
0
 public override IEnumerable<Account> GetAccounts()
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         foreach (byte[] encryptedPrivateKey in ctx.Accounts.Select(p => p.PrivateKeyEncrypted))
         {
             yield return GetAccountInternal(encryptedPrivateKey);
         }
     }
 }
Example #29
0
 public override IEnumerable<UInt160> GetAddresses()
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         foreach (byte[] scriptHash in ctx.Contracts.Select(p => p.ScriptHash))
         {
             yield return new UInt160(scriptHash);
         }
     }
 }
Example #30
0
 public override Contract GetContract(UInt160 scriptHash)
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         byte[] redeemScript = ctx.Contracts.FirstOrDefault(p => p.ScriptHash == scriptHash.ToArray())?.RedeemScript;
         if (redeemScript == null) return null;
         return new Contract(redeemScript);
     }
 }
Example #31
0
 protected override void SaveAccount(Account account)
 {
     byte[] decryptedPrivateKey = new byte[96];
     Buffer.BlockCopy(account.PublicKey, 0, 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(connectionString))
     {
         DbAccount db_account = ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == account.PublicKeyHash.ToArray());
         if (db_account == null)
         {
             db_account = ctx.Accounts.Add(new DbAccount
             {
                 PrivateKeyEncrypted = encryptedPrivateKey,
                 PublicKeyHash = account.PublicKeyHash.ToArray()
             });
         }
         else
         {
             db_account.PrivateKeyEncrypted = encryptedPrivateKey;
         }
         ctx.SaveChanges();
     }
 }
Example #32
0
 public override IEnumerable<Contract> GetContracts()
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         foreach (byte[] redeemScript in ctx.Contracts.Select(p => p.RedeemScript))
         {
             yield return new Contract(redeemScript);
         }
     }
 }
Example #33
0
 public override Account GetAccountByScriptHash(UInt160 scriptHash)
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         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);
     }
 }
Example #34
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;
 }
Example #35
0
 protected override void DeleteAccount(UInt160 publicKeyHash)
 {
     using (WalletDataContext ctx = new WalletDataContext(connectionString))
     {
         DbAccount 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();
         }
     }
 }