示例#1
0
        /// <summary>
        ///     Creates an Account, ecrypts it using NEP2 and returns it.
        /// </summary>
        /// <returns></returns>
        public async Task <Account> CreateAccount(string label, string password)
        {
            var privateKey = new byte[32];

            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(privateKey);
            }

            var key = new KeyPair(privateKey);

            Array.Clear(privateKey, 0, privateKey.Length);
            var contract = new Contract
            {
                Script     = Helper.CreateSignatureRedeemScript(key.PublicKey),
                Parameters = new List <Parameter>
                {
                    new Parameter("signature", ParameterType.Signature)
                },
                Deployed = false
            };
            var encryptedKey = await Nep2.Encrypt(key.Export(), password);

            var account = new Account(contract.ScriptHash, key)
            {
                Nep2Key  = encryptedKey,
                Contract = contract,
                Label    = label
            };

            AddAccount(account);
            return(account);
        }
示例#2
0
        /// <summary>
        ///     Decrypts and add the account to the Wallet Account List, using NEP2.
        /// </summary>
        /// <param name="label"></param>
        /// <param name="encryptedPrivateKey"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task <Account> ImportAccount(string encryptedPrivateKey, string password, string label)
        {
            var privateKey = await Nep2.Decrypt(encryptedPrivateKey, password, _wallet.Scrypt);

            var key = new KeyPair(privateKey);

            Array.Clear(privateKey, 0, privateKey.Length);

            var contract = new Contract
            {
                Script     = Helper.CreateSignatureRedeemScript(key.PublicKey),
                Parameters = new List <Parameter>
                {
                    new Parameter("signature", ParameterType.Signature)
                },
                Deployed = false
            };

            var account = new Account(contract.ScriptHash, key)
            {
                Nep2Key   = encryptedPrivateKey,
                Label     = label,
                Contract  = contract,
                IsDefault = false
            };

            AddAccount(account);
            return(account);
        }
示例#3
0
        /// <summary>
        ///     Gets the KeyPair object using the encrypted key and password pair.
        /// </summary>
        /// <param name="nep2Key"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task <KeyPair> GetKey(string nep2Key, string password)
        {
            if (nep2Key == null)
            {
                return(null);
            }
            var decryptedKey = await Nep2.Decrypt(nep2Key, password, _wallet.Scrypt);

            var privKey = new KeyPair(decryptedKey);

            return(privKey);
        }
示例#4
0
        /// <summary>
        ///     Verifies if the provided encrypted key and password are correct.
        /// </summary>
        /// <param name="nep2Key"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task <bool> VerifyPassword(string nep2Key, string password)
        {
            try
            {
                await Nep2.Decrypt(nep2Key, password, _wallet.Scrypt);

                return(true);
            }
            catch (FormatException)
            {
                return(false);
            }
        }