Ejemplo n.º 1
0
        /// <summary>
        /// create new contract address
        /// </summary>
        /// <param name="parameterTypes"></param>
        /// <param name="script"></param>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public async Task <object> CreateContractAddress(ContractParameterType[] parameterTypes, string script, string privateKey)
        {
            if (CurrentWallet == null)
            {
                return(Error(ErrorCode.WalletNotOpen));
            }
            Contract contract = Contract.Create(parameterTypes, script.HexToBytes());

            if (contract == null)
            {
                return(Error(ErrorCode.CreateContractAddressFail));
            }
            byte[] keyBytes   = privateKey.ToPrivateKeyBytes();
            var    key        = new KeyPair(keyBytes);
            var    newAccount = CurrentWallet.CreateAccount(contract, key);

            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }
            return(new AccountModel()
            {
                AccountType = AccountType.NonStandard,
                Address = newAccount.Address,
                ScriptHash = newAccount.ScriptHash,
            });
        }
Ejemplo n.º 2
0
        private void OnImportMultisigAddress(ushort m, ECPoint[] publicKeys)
        {
            if (NoWallet())
            {
                return;
            }

            int n = publicKeys.Length;

            if (m < 1 || m > n || n > 1024)
            {
                Console.WriteLine("Error. Invalid parameters.");
                return;
            }

            Contract multiSignContract = Contract.CreateMultiSigContract(m, publicKeys);
            KeyPair  keyPair           = CurrentWallet.GetAccounts().FirstOrDefault(p => p.HasKey && publicKeys.Contains(p.GetKey().PublicKey))?.GetKey();

            WalletAccount account = CurrentWallet.CreateAccount(multiSignContract, keyPair);

            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }

            Console.WriteLine("Multisig. Addr.: " + multiSignContract.Address);
        }
Ejemplo n.º 3
0
        private void OnImportWatchOnlyCommand(string addressOrFile)
        {
            if (NoWallet())
            {
                return;
            }
            UInt160 address = null;

            try
            {
                address = StringToAddress(addressOrFile, NeoSystem.Settings.AddressVersion);
            }
            catch (FormatException) { }
            if (address is null)
            {
                var fileInfo = new FileInfo(addressOrFile);

                if (!fileInfo.Exists)
                {
                    ConsoleHelper.Warning($"File '{fileInfo.FullName}' doesn't exists");
                    return;
                }

                if (fileInfo.Length > 1024 * 1024)
                {
                    if (!ReadUserInput($"The file '{fileInfo.FullName}' is too big, do you want to continue? (yes|no)", false).IsYes())
                    {
                        return;
                    }
                }

                string[] lines = File.ReadAllLines(fileInfo.FullName).Where(u => !string.IsNullOrEmpty(u)).ToArray();
                using (var percent = new ConsolePercent(0, lines.Length))
                {
                    for (int i = 0; i < lines.Length; i++)
                    {
                        address = StringToAddress(lines[i], NeoSystem.Settings.AddressVersion);
                        CurrentWallet.CreateAccount(address);
                        percent.Value++;
                    }
                }
            }
            else
            {
                WalletAccount account = CurrentWallet.GetAccount(address);
                if (account is not null)
                {
                    ConsoleHelper.Warning("This address is already in your wallet");
                }
                else
                {
                    account = CurrentWallet.CreateAccount(address);
                    ConsoleHelper.Info("Address: ", account.Address);
                }
            }
            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }
        }
Ejemplo n.º 4
0
        private void OnImportMultisigAddress(ushort m, ECPoint[] publicKeys)
        {
            if (NoWallet())
            {
                return;
            }
            int n = publicKeys.Length;

            if (m < 1 || m > n || n > 1024)
            {
                ConsoleHelper.Error("Invalid parameters.");
                return;
            }

            Contract multiSignContract = Contract.CreateMultiSigContract(m, publicKeys);
            KeyPair  keyPair           = CurrentWallet.GetAccounts().FirstOrDefault(p => p.HasKey && publicKeys.Contains(p.GetKey().PublicKey))?.GetKey();

            WalletAccount account = CurrentWallet.CreateAccount(multiSignContract, keyPair);

            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }

            ConsoleHelper.Info("Multisig. Addr.: ", multiSignContract.ScriptHash.ToAddress(NeoSystem.Settings.AddressVersion));
        }
Ejemplo n.º 5
0
        private bool OnCreateAddressCommand(string[] args)
        {
            if (NoWallet())
            {
                return(true);
            }
            if (args.Length > 3)
            {
                Console.WriteLine("error");
                return(true);
            }

            string path = "address.txt";

            if (File.Exists(path))
            {
                if (!ReadUserInput($"The file '{path}' already exists, do you want to overwrite it? (yes|no)", false).IsYes())
                {
                    return(true);
                }
            }

            ushort count;

            if (args.Length >= 3)
            {
                count = ushort.Parse(args[2]);
            }
            else
            {
                count = 1;
            }

            List <string> addresses = new List <string>();

            using (var percent = new ConsolePercent(0, count))
            {
                Parallel.For(0, count, (i) =>
                {
                    WalletAccount account = CurrentWallet.CreateAccount();
                    addresses.Add(account.Address);
                    lock (addresses)
                    {
                        addresses.Add(account.Address);
                        percent.Value++;
                    }
                });
            }

            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }

            Console.WriteLine($"export addresses to {path}");
            File.WriteAllLines(path, addresses);
            return(true);
        }
Ejemplo n.º 6
0
        private void OnImportKeyCommand(string wifOrFile)
        {
            byte[] prikey = null;
            try
            {
                prikey = Wallet.GetPrivateKeyFromWIF(wifOrFile);
            }
            catch (FormatException) { }
            if (prikey == null)
            {
                var fileInfo = new FileInfo(wifOrFile);

                if (!fileInfo.Exists)
                {
                    Console.WriteLine($"Error: File '{fileInfo.FullName}' doesn't exists");
                    return;
                }

                if (wifOrFile.Length > 1024 * 1024)
                {
                    if (!ReadUserInput($"The file '{fileInfo.FullName}' is too big, do you want to continue? (yes|no)", false).IsYes())
                    {
                        return;
                    }
                }

                string[] lines = File.ReadAllLines(fileInfo.FullName).Where(u => !string.IsNullOrEmpty(u)).ToArray();
                using (var percent = new ConsolePercent(0, lines.Length))
                {
                    for (int i = 0; i < lines.Length; i++)
                    {
                        if (lines[i].Length == 64)
                        {
                            prikey = lines[i].HexToBytes();
                        }
                        else
                        {
                            prikey = Wallet.GetPrivateKeyFromWIF(lines[i]);
                        }
                        CurrentWallet.CreateAccount(prikey);
                        Array.Clear(prikey, 0, prikey.Length);
                        percent.Value++;
                    }
                }
            }
            else
            {
                WalletAccount account = CurrentWallet.CreateAccount(prikey);
                Array.Clear(prikey, 0, prikey.Length);
                Console.WriteLine($"Address: {account.Address}");
                Console.WriteLine($" Pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}");
            }
            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// create new standard address
        /// </summary>
        /// <returns></returns>
        public async Task <object> CreateAddress()
        {
            if (CurrentWallet == null)
            {
                return(Error(ErrorCode.WalletNotOpen));
            }
            var newAccount = CurrentWallet.CreateAccount();

            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }
            return(new AccountModel()
            {
                AccountType = AccountType.Standard,
                Address = newAccount.Address,
                ScriptHash = newAccount.ScriptHash,
            });
        }
Ejemplo n.º 8
0
        private void OnCreateAddressCommand(ushort count = 1)
        {
            if (NoWallet())
            {
                return;
            }

            string path = "address.txt";

            if (File.Exists(path))
            {
                if (!ReadUserInput($"The file '{path}' already exists, do you want to overwrite it? (yes|no)", false).IsYes())
                {
                    return;
                }
            }

            List <string> addresses = new List <string>();

            using (var percent = new ConsolePercent(0, count))
            {
                Parallel.For(0, count, (i) =>
                {
                    WalletAccount account = CurrentWallet.CreateAccount();
                    lock (addresses)
                    {
                        addresses.Add(account.Address);
                        percent.Value++;
                    }
                });
            }

            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }

            Console.WriteLine($"Export addresses to {path}");
            File.WriteAllLines(path, addresses);
        }
Ejemplo n.º 9
0
        private bool OnImportMultisigAddress(string[] args)
        {
            if (NoWallet())
            {
                return(true);
            }

            if (args.Length < 4)
            {
                Console.WriteLine("Error. Invalid parameters.");
                return(true);
            }

            int m = int.Parse(args[2]);
            int n = args.Length - 3;

            if (m < 1 || m > n || n > 1024)
            {
                Console.WriteLine("Error. Invalid parameters.");
                return(true);
            }

            ECPoint[] publicKeys = args.Skip(3).Select(p => ECPoint.Parse(p, ECCurve.Secp256r1)).ToArray();

            Contract multiSignContract = Contract.CreateMultiSigContract(m, publicKeys);
            KeyPair  keyPair           = CurrentWallet.GetAccounts().FirstOrDefault(p => p.HasKey && publicKeys.Contains(p.GetKey().PublicKey))?.GetKey();

            WalletAccount account = CurrentWallet.CreateAccount(multiSignContract, keyPair);

            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }

            Console.WriteLine("Multisig. Addr.: " + multiSignContract.ScriptHash.ToAddress());

            return(true);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// create new multi address
        /// </summary>
        /// <returns></returns>
        public async Task <object> CreateMultiAddress(int limit, string[] publicKeys)
        {
            if (CurrentWallet == null)
            {
                return(Error(ErrorCode.WalletNotOpen));
            }

            ECPoint[] points = null;
            try
            {
                points = publicKeys.Select(p => ECPoint.DecodePoint(Helper.HexToBytes(p), ECCurve.Secp256r1)).ToArray();
            }
            catch (FormatException ex)
            {
                return(Error(ErrorCode.InvalidPara, ex.Message));
            }
            Contract contract = Contract.CreateMultiSigContract(limit, points);

            if (contract == null)
            {
                return(Error(ErrorCode.CreateMultiContractFail));
            }
            var hashSet    = new HashSet <ECPoint>(points);
            var key        = CurrentWallet.GetAccounts().FirstOrDefault(p => p.HasKey && hashSet.Contains(p.GetKey().PublicKey))?.GetKey();
            var newAccount = CurrentWallet.CreateAccount(contract, key);

            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }
            return(new AccountModel()
            {
                AccountType = AccountType.MultiSignature,
                Address = newAccount.Address,
                ScriptHash = newAccount.ScriptHash,
            });
        }
Ejemplo n.º 11
0
        /// <summary>
        /// import wif or hex private keys
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public async Task <object> ImportAccounts(string[] keys)
        {
            if (CurrentWallet == null)
            {
                return(Error(ErrorCode.WalletNotOpen));
            }

            var importKeys = new List <byte[]>();

            foreach (var key in keys)
            {
                var priKey = key.TryGetPrivateKey();
                if (priKey.IsEmpty())
                {
                    return(Error(ErrorCode.InvalidPrivateKey));
                }
                importKeys.Add(priKey);
            }
            var importedAccounts = new List <AccountModel>();

            foreach (var privateKey in importKeys)
            {
                var account = CurrentWallet.CreateAccount(privateKey);
                importedAccounts.Add(new AccountModel
                {
                    Address    = account.Address,
                    ScriptHash = account.ScriptHash,
                });
            }
            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }
            GetNeoAndGas(importedAccounts);
            return(importedAccounts);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// import watch only addresses
        /// </summary>
        /// <param name="addresses"></param>
        /// <returns></returns>
        public async Task <object> ImportWatchOnlyAddress(UInt160[] addresses)
        {
            if (CurrentWallet == null)
            {
                return(Error(ErrorCode.WalletNotOpen));
            }
            var importedAccounts = new List <AccountModel>();

            foreach (var address in addresses)
            {
                var account = CurrentWallet.CreateAccount(address);
                importedAccounts.Add(new AccountModel
                {
                    Address    = account.Address,
                    ScriptHash = account.ScriptHash,
                });
            }
            if (CurrentWallet is NEP6Wallet wallet)
            {
                wallet.Save();
            }
            GetNeoAndGas(importedAccounts);
            return(importedAccounts);
        }