示例#1
0
        static void Main(string[] args)
        {
            var serviceProvider = ConfigureForMongoDB("DigitalCurrency", 10500);

            //var serviceProvider = ConfigureForLiteDb("node.db", 10500);

            _host           = serviceProvider.GetService <IBlockchainNode>();
            _miner          = serviceProvider.GetService <IBlockMiner>();
            _network        = serviceProvider.GetService <IPeerNetwork>();
            _sigService     = serviceProvider.GetService <ISignatureService>();
            _addressEncoder = serviceProvider.GetService <IAddressEncoder>();
            _txnRepo        = serviceProvider.GetService <ICustomInstructionRepository>();
            _blockRepo      = serviceProvider.GetService <IBlockRepository>();
            _txnBuilder     = serviceProvider.GetService <ITransactionBuilder>();

            Console.Write("Enter passphrase:");
            var phrase  = Console.ReadLine();
            var keys    = _sigService.GetKeyPairFromPhrase(phrase);
            var address = _addressEncoder.EncodeAddress(keys.PublicKey, 0);

            Console.WriteLine($"Your address is {address}");

            _network.Open();

            PrintHelp();
            while (true)
            {
                Console.Write(">");
                var command = Console.ReadLine();

                if (command == "exit")
                {
                    break;
                }

                RunCommand(command, keys);
            }
            _network.Close();
        }
示例#2
0
        static void RunCommand(string command, KeyPair keys)
        {
            var args       = command.Split(' ');
            var ownAddress = _addressEncoder.EncodeAddress(keys.PublicKey, 0);

            switch (args[0])
            {
            case "help":
                PrintHelp();
                break;

            case "mine-genesis":
                Console.WriteLine("Mining...");
                _miner.Start(keys, true);
                break;

            case "mine":
                Console.WriteLine("Mining...");
                _miner.Start(keys, false);
                break;

            case "stop-mining":
                Console.WriteLine("Stopping...");
                _miner.Stop();
                break;

            case "peers":
                var peersIn  = _network.GetPeersIn();
                var peersOut = _network.GetPeersOut();
                Console.WriteLine("Incoming peers");
                PrintPeerList(peersIn);
                Console.WriteLine("Outgoing peers");
                PrintPeerList(peersOut);
                break;

            case "balance":
                if (args.Length == 1)
                {
                    Console.WriteLine($"Balance = {_txnRepo.GetAccountBalance(ownAddress)}");
                }
                else
                {
                    Console.WriteLine($"Balance = {_txnRepo.GetAccountBalance(args[1])}");
                }
                break;

            case "best-block":
                var header = _blockRepo.GetBestBlockHeader().Result;
                Console.WriteLine($"Height: {header.Height}, Id: {BitConverter.ToString(header.BlockId)}");
                break;

            case "gen-key":
                var genkeys = _sigService.GetKeyPairFromPhrase(args[1]);
                var address = _addressEncoder.EncodeAddress(genkeys.PublicKey, 0);
                Console.WriteLine($"{address}");
                break;

            case "avg-time":
                var avgTime = _blockRepo.GetAverageBlockTimeInSecs(DateTime.UtcNow.AddHours(-1), DateTime.UtcNow).Result;
                Console.WriteLine($"Avg time: {avgTime}s");
                break;

            case "send":
                if (args.Length != 3)
                {
                    Console.WriteLine("invalid command");
                    return;
                }

                var instruction = new TransferInstruction()
                {
                    PublicKey   = keys.PublicKey,
                    Amount      = Convert.ToInt32(args[2]),
                    Destination = _addressEncoder.ExtractPublicKeyHash(args[1])
                };

                Console.WriteLine($"Signing instruction");
                _sigService.SignInstruction(instruction, keys.PrivateKey);
                var txn = _txnBuilder.Build(new List <Instruction>()
                {
                    instruction
                }).Result;

                Console.WriteLine($"Sending transaction {BitConverter.ToString(txn.TransactionId)}");
                _host.SendTransaction(txn);
                break;

            default:
                Console.WriteLine("invalid command");
                break;
            }
        }