Example #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);
        }
Example #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);
        }
Example #3
0
        /// <summary>
        ///     Creates an Account and returns it.
        /// </summary>
        /// <returns></returns>
        public Account CreateAccount(string label)
        {
            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 account = new Account(contract.ScriptHash, key)
            {
                Contract = contract,
                Label    = label
            };

            AddAccount(account);
            return(account);
        }
Example #4
0
        /// <summary>
        ///     Decrypts and add the account to the Wallet Account List, using WIF.
        /// </summary>
        /// <param name="privateKey"></param>
        /// <param name="label"></param>
        /// <returns></returns>
        public Account ImportAccount(byte[] privateKey, string label)
        {
            var key      = new KeyPair(privateKey);
            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)
            {
                Contract = contract,
                Label    = label
            };

            AddAccount(account);
            return(account);
        }
Example #5
0
        public static byte[] Sign(KeyPair key, Transaction tx, bool signed = true)
        {
            var txData = tx.SerializeTransaction();

            var signature = Sign(txData, key.PrivateKey);

            if (signed)
            {
                var invocationScript   = ("40" + signature.ToHexString()).HexToBytes();
                var verificationScript = Helper.CreateSignatureRedeemScript(key.PublicKey);
                tx.Witnesses = new[]
                {
                    new Witness
                    {
                        InvocationScript   = invocationScript,
                        VerificationScript = verificationScript
                    }
                };
                return(tx.ToArray());
            }

            return(signature);
        }