Пример #1
0
        public override WalletAccount CreateAccount(UInt160 scriptHash)
        {
            UserWalletAccount account = new UserWalletAccount(scriptHash);

            AddAccount(account, true);
            return(account);
        }
Пример #2
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);
     }
 }
Пример #3
0
        public override WalletAccount CreateAccount(byte[] privateKey)
        {
            KeyPair key = new KeyPair(privateKey);
            VerificationContract contract = new VerificationContract
            {
                Script        = SmartContract.Contract.CreateSignatureRedeemScript(key.PublicKey),
                ParameterList = new[] { ContractParameterType.Signature }
            };
            UserWalletAccount account = new UserWalletAccount(contract.ScriptHash)
            {
                Key      = key,
                Contract = contract
            };

            AddAccount(account, false);
            return(account);
        }
Пример #4
0
        public override WalletAccount CreateAccount(SmartContract.Contract contract, KeyPair key = null)
        {
            VerificationContract verification_contract = contract as VerificationContract;

            if (verification_contract == null)
            {
                verification_contract = new VerificationContract
                {
                    Script        = contract.Script,
                    ParameterList = contract.ParameterList
                };
            }
            UserWalletAccount account = new UserWalletAccount(verification_contract.ScriptHash)
            {
                Key      = key,
                Contract = verification_contract
            };

            AddAccount(account, false);
            return(account);
        }
Пример #5
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
         {
             WalletIndexer.RegisterAccounts(new[] { account.ScriptHash }, is_import ? 0 : Blockchain.Default?.Height ?? 0);
         }
         accounts[account.ScriptHash] = account;
     }
     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);
             using (account.Key.Decrypt())
             {
                 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();
     }
 }