protected override IEnumerable <WalletKeyPairBase> LoadKeyPairs()
 {
     using (WalletDataContext ctx = new WalletDataContext(DbPath))
     {
         foreach (Account item in ctx.Accounts.Select(p => p))
         {
             if ((KeyType)item.nVersion == KeyType.Anonymous || (KeyType)item.nVersion == KeyType.Transparent)
             {
                 byte[]        decryptedPrivateKey = DecryptPrivateKey(item.PrivateKeyEncrypted);
                 WalletKeyPair account             = new WalletKeyPair(decryptedPrivateKey, (KeyType)item.nVersion);
                 Array.Clear(decryptedPrivateKey, 0, decryptedPrivateKey.Length);
                 yield return(account);
             }
             else if ((KeyType)item.nVersion == KeyType.Stealth)
             {
                 byte[]         decryptedPrivateKey = DecryptPrivateKey(item.PrivateKeyEncrypted);
                 byte[]         decryptedViewKey    = DecryptPrivateKey(item.PrivateViewKeyEncrypted);
                 StealthKeyPair account             = new StealthKeyPair(decryptedPrivateKey, decryptedViewKey);
                 Array.Clear(decryptedPrivateKey, 0, decryptedPrivateKey.Length);
                 Array.Clear(decryptedViewKey, 0, decryptedViewKey.Length);
                 yield return(account);
             }
         }
     }
 }
        public override StealthKeyPair CreateKey(byte[] payloadPrivKey, byte[] viewPrivKey)
        {
            StealthKeyPair account = base.CreateKey(payloadPrivKey, viewPrivKey);

            OnCreateAccount(account);
            AddContract(VerificationContract.CreateRingSignatureContract(account.ToStelathPubKeys()));
            return(account);
        }
        private void OnCreateAccount(StealthKeyPair account)
        {
            byte[] decryptedPayloadPrivKey = new byte[96];
            Buffer.BlockCopy(account.PayloadPubKey.EncodePoint(false), 1, decryptedPayloadPrivKey, 0, 64);

            using (account.DecryptPayloadKey())
            {
                Buffer.BlockCopy(account.PayloadPrivKey, 0, decryptedPayloadPrivKey, 64, 32);
            }
            byte[] encryptedPayloadPrivKey = EncryptPrivateKey(decryptedPayloadPrivKey);
            Array.Clear(decryptedPayloadPrivKey, 0, decryptedPayloadPrivKey.Length);

            byte[] decryptedViewPrivKey = new byte[96];
            Buffer.BlockCopy(account.ViewPubKey.EncodePoint(false), 1, decryptedViewPrivKey, 0, 64);

            using (account.DecryptViewKey())
            {
                Buffer.BlockCopy(account.ViewPrivKey, 0, decryptedViewPrivKey, 64, 32);
            }
            byte[] encryptedViewPrivKey = EncryptPrivateKey(decryptedViewPrivKey);
            Array.Clear(decryptedViewPrivKey, 0, decryptedViewPrivKey.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
                    {
                        nVersion                = (int)account.nVersion,
                        PrivateKeyEncrypted     = encryptedPayloadPrivKey,
                        PublicKeyHash           = account.PublicKeyHash.ToArray(),
                        PrivateViewKeyEncrypted = encryptedViewPrivKey
                    }).Entity;
                }
                else
                {
                    db_account.nVersion                = (int)account.nVersion;
                    db_account.PrivateKeyEncrypted     = encryptedPayloadPrivKey;
                    db_account.PrivateViewKeyEncrypted = encryptedViewPrivKey;
                }

                try
                {
                    ctx.SaveChanges();
                }
                catch (Exception ex)
                {
                    string str = ex.Message;
                }
            }
        }