public override Account GetAccount(UInt160 publicKeyHash) { using (WalletDataContext ctx = new WalletDataContext(connectionString)) { return(GetAccountInternal(ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == publicKeyHash.ToArray())?.PrivateKeyEncrypted)); } }
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; }
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(); } }
public override WAccount GetAccount(UInt160 publicKeyHash) { using (WalletDataContext ctx = new WalletDataContext(path)) { return GetAccountInternal(ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == publicKeyHash.ToArray())?.PrivateKeyEncrypted); } }
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; }
protected override void BuildDatabase() { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { ctx.Database.EnsureDeleted(); ctx.Database.EnsureCreated(); } }
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); } } } }
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)); } } }
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)); } } }
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); }
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)); } } }
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 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)); } }
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)); } }
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(); } } }
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; }
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); } }
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)); } }
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(); } }
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); }
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(); } }
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(); } }
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) }; } } }
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 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); } } }
protected override byte[] LoadStoredData(string name) { using (WalletDataContext ctx = new WalletDataContext(DbPath)) { return ctx.Keys.FirstOrDefault(p => p.Name == name)?.Value; } }
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<Account> GetAccounts() { using (WalletDataContext ctx = new WalletDataContext(connectionString)) { foreach (byte[] encryptedPrivateKey in ctx.Accounts.Select(p => p.PrivateKeyEncrypted)) { yield return GetAccountInternal(encryptedPrivateKey); } } }
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); } } }
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); } }
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); } } }
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); } }
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; }