示例#1
0
文件: UserWallet.cs 项目: xzlib/neo
 private byte[] LoadStoredData(string name)
 {
     using (WalletDataContext ctx = new WalletDataContext(path))
     {
         return(ctx.Keys.FirstOrDefault(p => p.Name == name)?.Value);
     }
 }
示例#2
0
 private void BuildDatabase()
 {
     using (WalletDataContext ctx = new WalletDataContext(path))
     {
         ctx.Database.EnsureDeleted();
         ctx.Database.EnsureCreated();
     }
 }
示例#3
0
 private void SaveStoredData(string name, byte[] value)
 {
     lock (db_lock)
         using (WalletDataContext ctx = new WalletDataContext(path))
         {
             SaveStoredData(ctx, name, value);
             ctx.SaveChanges();
         }
 }
示例#4
0
 private Dictionary <UInt160, UserWalletAccount> LoadAccounts()
 {
     using (WalletDataContext ctx = new WalletDataContext(path))
     {
         Dictionary <UInt160, UserWalletAccount> accounts = ctx.Addresses.Select(p => p.ScriptHash).AsEnumerable().Select(p => new UserWalletAccount(new UInt160(p))).ToDictionary(p => p.ScriptHash);
         foreach (Contract db_contract in ctx.Contracts.Include(p => p.Account))
         {
             VerificationContract contract = db_contract.RawData.AsSerializable <VerificationContract>();
             UserWalletAccount    account  = accounts[contract.ScriptHash];
             account.Contract = contract;
             account.Key      = new KeyPair(DecryptPrivateKey(db_contract.Account.PrivateKeyEncrypted));
         }
         return(accounts);
     }
 }
示例#5
0
 private Dictionary <UInt160, UserWalletAccount> LoadAccounts()
 {
     using (WalletDataContext ctx = new WalletDataContext(path))
     {
         string passphrase = Encoding.UTF8.GetString(masterKey);
         Dictionary <UInt160, UserWalletAccount> accounts = ctx.Addresses.Select(p => p.ScriptHash).AsEnumerable().Select(p => new UserWalletAccount(new UInt160(p))).ToDictionary(p => p.ScriptHash);
         foreach (Contract db_contract in ctx.Contracts.Include(p => p.Account))
         {
             VerificationContract contract = db_contract.RawData.AsSerializable <VerificationContract>();
             UserWalletAccount    account  = accounts[contract.ScriptHash];
             account.Contract = contract;
             account.Key      = new KeyPair(GetPrivateKeyFromNEP2(db_contract.Account.Nep2key, passphrase, scrypt.N, scrypt.R, scrypt.P));
         }
         return(accounts);
     }
 }
示例#6
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;
            }
        }
示例#7
0
        public override bool DeleteAccount(UInt160 scriptHash)
        {
            UserWalletAccount account;

            lock (accounts)
            {
                if (accounts.TryGetValue(scriptHash, out account))
                {
                    accounts.Remove(scriptHash);
                }
            }
            if (account != null)
            {
                indexer.UnregisterAccounts(new[] { scriptHash });
                lock (db_lock)
                    using (WalletDataContext ctx = new WalletDataContext(path))
                    {
                        if (account.HasKey)
                        {
                            Account db_account = ctx.Accounts.First(p => p.PublicKeyHash.SequenceEqual(account.Key.PublicKeyHash.ToArray()));
                            ctx.Accounts.Remove(db_account);
                        }
                        if (account.Contract != null)
                        {
                            Contract db_contract = ctx.Contracts.First(p => p.ScriptHash.SequenceEqual(scriptHash.ToArray()));
                            ctx.Contracts.Remove(db_contract);
                        }
                        //delete address
                        {
                            Address db_address = ctx.Addresses.First(p => p.ScriptHash.SequenceEqual(scriptHash.ToArray()));
                            ctx.Addresses.Remove(db_address);
                        }
                        ctx.SaveChanges();
                    }
                return(true);
            }
            return(false);
        }
示例#8
0
 private void AddAccount(UserWalletAccount account)
 {
     lock (accounts)
     {
         if (accounts.TryGetValue(account.ScriptHash, out UserWalletAccount account_old))
         {
             if (account.Contract == null)
             {
                 account.Contract = account_old.Contract;
             }
         }
         accounts[account.ScriptHash] = account;
     }
     lock (db_lock)
         using (WalletDataContext ctx = new WalletDataContext(path))
         {
             if (account.HasKey)
             {
                 string  passphrase = Encoding.UTF8.GetString(masterKey);
                 Account db_account = ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == account.Key.PublicKeyHash.ToArray());
                 if (db_account == null)
                 {
                     db_account = ctx.Accounts.Add(new Account
                     {
                         Nep2key       = account.Key.Export(passphrase, scrypt.N, scrypt.R, scrypt.P),
                         PublicKeyHash = account.Key.PublicKeyHash.ToArray()
                     }).Entity;
                 }
                 else
                 {
                     db_account.Nep2key = account.Key.Export(passphrase, scrypt.N, scrypt.R, scrypt.P);
                 }
             }
             if (account.Contract != null)
             {
                 Contract db_contract = ctx.Contracts.FirstOrDefault(p => p.ScriptHash == account.Contract.ScriptHash.ToArray());
                 if (db_contract != null)
                 {
                     db_contract.PublicKeyHash = account.Key.PublicKeyHash.ToArray();
                 }
                 else
                 {
                     ctx.Contracts.Add(new Contract
                     {
                         RawData       = ((VerificationContract)account.Contract).ToArray(),
                         ScriptHash    = account.Contract.ScriptHash.ToArray(),
                         PublicKeyHash = account.Key.PublicKeyHash.ToArray()
                     });
                 }
             }
             //add address
             {
                 Address db_address = ctx.Addresses.FirstOrDefault(p => p.ScriptHash == account.ScriptHash.ToArray());
                 if (db_address == null)
                 {
                     ctx.Addresses.Add(new Address
                     {
                         ScriptHash = account.ScriptHash.ToArray()
                     });
                 }
             }
             ctx.SaveChanges();
         }
 }
示例#9
0
 private void AddAccount(UserWalletAccount account, bool is_import)
 {
     lock (accounts)
     {
         if (accounts.TryGetValue(account.ScriptHash, out UserWalletAccount account_old))
         {
             if (account.Contract == null)
             {
                 account.Contract = account_old.Contract;
             }
         }
         else
         {
             indexer.RegisterAccounts(new[] { account.ScriptHash }, is_import ? 0 : Blockchain.Singleton.Height);
         }
         accounts[account.ScriptHash] = account;
     }
     lock (db_lock)
         using (WalletDataContext ctx = new WalletDataContext(path))
         {
             if (account.HasKey)
             {
                 byte[] decryptedPrivateKey = new byte[96];
                 Buffer.BlockCopy(account.Key.PublicKey.EncodePoint(false), 1, decryptedPrivateKey, 0, 64);
                 Buffer.BlockCopy(account.Key.PrivateKey, 0, decryptedPrivateKey, 64, 32);
                 byte[] encryptedPrivateKey = EncryptPrivateKey(decryptedPrivateKey);
                 Array.Clear(decryptedPrivateKey, 0, decryptedPrivateKey.Length);
                 Account db_account = ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash.SequenceEqual(account.Key.PublicKeyHash.ToArray()));
                 if (db_account == null)
                 {
                     db_account = ctx.Accounts.Add(new Account
                     {
                         PrivateKeyEncrypted = encryptedPrivateKey,
                         PublicKeyHash       = account.Key.PublicKeyHash.ToArray()
                     }).Entity;
                 }
                 else
                 {
                     db_account.PrivateKeyEncrypted = encryptedPrivateKey;
                 }
             }
             if (account.Contract != null)
             {
                 Contract db_contract = ctx.Contracts.FirstOrDefault(p => p.ScriptHash.SequenceEqual(account.Contract.ScriptHash.ToArray()));
                 if (db_contract != null)
                 {
                     db_contract.PublicKeyHash = account.Key.PublicKeyHash.ToArray();
                 }
                 else
                 {
                     ctx.Contracts.Add(new Contract
                     {
                         RawData       = ((VerificationContract)account.Contract).ToArray(),
                         ScriptHash    = account.Contract.ScriptHash.ToArray(),
                         PublicKeyHash = account.Key.PublicKeyHash.ToArray()
                     });
                 }
             }
             //add address
             {
                 Address db_address = ctx.Addresses.FirstOrDefault(p => p.ScriptHash.SequenceEqual(account.Contract.ScriptHash.ToArray()));
                 if (db_address == null)
                 {
                     ctx.Addresses.Add(new Address
                     {
                         ScriptHash = account.Contract.ScriptHash.ToArray()
                     });
                 }
             }
             ctx.SaveChanges();
         }
 }