예제 #1
0
        protected override void EndProcessing()
        {
            var config = this.ReadConfig();

            IAccountDerivation accountDerivation;

            if (string.IsNullOrWhiteSpace(Mnemonic))
            {
                var hdWalletAccountDerivation = HDAccountDerivation.Create();
                accountDerivation = hdWalletAccountDerivation;
                Host.UI.WriteLine($"Using mnemonic phrase: '{hdWalletAccountDerivation.MnemonicPhrase}'");
                Host.UI.WriteErrorLine("Warning: this private key generation is not secure and should not be used in production.");
            }
            else
            {
                accountDerivation = new HDAccountDerivation(Mnemonic);
            }

            var accountKeys = new List <(Address Address, EthereumEcdsa Account)>();

            foreach (var account in EthereumEcdsa.Generate(config.AccountCount, accountDerivation))
            {
                // Get an account from the public key hash.
                var address = account.EcdsaKeyPairToAddress();
                accountKeys.Add((address, account));
            }

            var accounts = accountKeys.ToArray();

            GlobalVariables.AccountKeys = accounts;

            Console.WriteLine($"Created {accounts.Length} accounts. Use '{CmdLetExtensions.GetCmdletName<WriteAccountsCommand>()}' to save account keys to disk.");
        }
예제 #2
0
        static async Task Main(string[] args)
        {
            var opts = ProcessArgs.Parse(args);

            if (!string.IsNullOrWhiteSpace(opts.Proxy))
            {
                // TODO: testnode.host proxy support
                throw new NotImplementedException();
            }

            IPAddress host;

            if (string.IsNullOrWhiteSpace(opts.Host) || opts.Host.Equals("localhost", StringComparison.OrdinalIgnoreCase))
            {
                host = IPAddress.Loopback;
            }
            else if (opts.Host == "*")
            {
                host = IPAddress.Any;
            }
            else if (IPAddress.TryParse(opts.Host, out var addr))
            {
                host = addr;
            }
            else
            {
                var entry = await Dns.GetHostEntryAsync(opts.Host);

                host = entry.AddressList[0];
            }

            // Setup account derivation / keys
            IAccountDerivation accountDerivation;

            if (string.IsNullOrWhiteSpace(opts.Mnemonic))
            {
                var hdWalletAccountDerivation = HDAccountDerivation.Create();
                accountDerivation = hdWalletAccountDerivation;
                Console.WriteLine($"Using mnemonic phrase: '{hdWalletAccountDerivation.MnemonicPhrase}'");
                Console.WriteLine("Warning: this private key generation is not secure and should not be used in production.");
            }
            else
            {
                accountDerivation = new HDAccountDerivation(opts.Mnemonic);
            }


            var accountConfig = new AccountConfiguration
            {
                AccountGenerationCount     = (int)opts.AccountCount,
                DefaultAccountEtherBalance = opts.AccountBalance,
                AccountDerivationMethod    = accountDerivation
            };

            // Create our local test node.
            using (var testNodeServer = new TestNodeServer(
                       port: (int)opts.Port,
                       address: host,
                       accountConfig: accountConfig))
            {
                Console.WriteLine("Starting server...");

                testNodeServer.RpcServer.DebugLogger = Console.WriteLine;

                // Start our local test node.
                await testNodeServer.RpcServer.StartAsync();

                // Create an RPC client for our local test node.
                var serverAddresses = string.Join(", ", testNodeServer.RpcServer.ServerAddresses);
                Console.WriteLine($"Test node server listening on: {serverAddresses}");

                // Listen for exit request.
                var exitEvent = new SemaphoreSlim(0, 1);
                Console.CancelKeyPress += (s, e) =>
                {
                    exitEvent.Release();
                    e.Cancel = true;
                };

                // Shutdown.
                await exitEvent.WaitAsync();

                Console.WriteLine("Stopping server and exiting...");
                await testNodeServer.RpcServer.StopAsync();
            }
        }