Example #1
0
        /// <summary>
        /// Create a multi-sig account from a list of public keys.
        ///
        /// </summary>
        /// <param name="signingThreshold">Minimum number of signatures required for verification. Must be larger than 0 and less than number of keys provided.</param>
        /// <param name="pubKeys">List of public keys to form the account. 2-16 keys allowed. Order is important.</param>
        /// <param name="label"></param>
        public Account CreateMultiSigAccount(int signingThreshold, string[] pubKeys, string label)
        {
            if (signingThreshold >= pubKeys.Length)
            {
                throw new ArgumentOutOfRangeException($"{nameof(signingThreshold)} must be larger than 0 and less than number of keys provided");
            }

            var publicKeys = new List <NeoECPoint>();

            foreach (var pubKey in pubKeys)
            {
                publicKeys.Add(NeoECPoint.Parse(pubKey, ECCurve.Secp256r1));
            }

            var verificationScript = Helper.CreateMultiSigRedeemScript(signingThreshold, publicKeys.ToArray());
            var contract           = new Contract
            {
                Script     = verificationScript,
                Parameters = Enumerable.Repeat(new Parameter("signature", ParameterType.Signature), signingThreshold).ToList(),
                Deployed   = false
            };

            var account = new Account(verificationScript.ToScriptHash(), label, false, false, null, contract, null);

            AddAccount(account);
            return(account);
        }
Example #2
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 #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 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 #5
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 #6
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);
        }