private static void ConnectCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket)ar.AsyncState; // Complete the connection. client.EndConnect(ar); var blockchain = NetworkFileProvider <Blockchain.Blockchain> .GetModel(Constants.BlockchainStoragePath); if (!blockchain.Nodes.Any(n => n.Address == client.RemoteEndPoint.ToString())) { blockchain.RegisterNode(client.RemoteEndPoint.ToString()); } NetworkFileProvider <Blockchain.Blockchain> .SetModel(Constants.BlockchainStoragePath, blockchain); // Signal that the connection has been made. connectDone.Set(); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public static void RunCommand(string commandName, string input) { blockchain = NetworkFileProvider <Blockchain.Blockchain> .GetModel(Constants.BlockchainStoragePath); if (commandName == "connect") { string template = "{0} -ip {1} -p {2}"; var args = ReverseStringFormat(template, input); int port = int.Parse(args[2]); IPAddress clientIp; if (IPAddress.TryParse(args[1], out clientIp)) { blockchain.RegisterNode($"{clientIp}:{port}"); AsynchronousClient.Connect(clientIp, port); } else { Logger.LogLine("incorect command parameters", ConsoleColor.Red); } } if (commandName == "peers") { if (blockchain.Nodes.Count > 0) { foreach (var node in blockchain.Nodes) { Logger.LogLine(node.Address, ConsoleColor.Green); } } else { Logger.LogLine("no peers", ConsoleColor.Red); } } if (commandName == "start-wallet") { WalletProvider.Initialize(); } if (commandName == "sync") { var result = blockchain.ResolveConflicts(); if (result) { Logger.LogLine("successfully sync network", ConsoleColor.Green); } else { Logger.LogLine("error while sync network", ConsoleColor.Red); } } }
public static void Initialize() { wallet = NetworkFileProvider <Wallet> .GetModel(Constants.WalletStoragePrivateKey); BigInteger privateKey; if (wallet.Address == null || wallet.Address == string.Empty) { Logger.LogLine("set password (must be 16 symbols):", ConsoleColor.Gray); var password = Console.ReadLine(); var hex = CryptographyHelper.GetRandomHexNumber(62); privateKey = Hex.HexToBigInteger(hex); Logger.LogLine($"your private key is {privateKey}"); var encryptedPrivateKey = CryptographyHelper.Encrypt(privateKey.ToString(), password); ECPoint publicKey = Secp256k1.Secp256k1.G.Multiply(privateKey); string bitcoinAddressUncompressed = publicKey.GetBitcoinAddress(false); string bitcoinAddressCompressed = publicKey.GetBitcoinAddress(compressed: true); wallet.PublicKey = publicKey; wallet.Address = bitcoinAddressCompressed; wallet.EncryptedPrivateKey = encryptedPrivateKey; NetworkFileProvider <Wallet> .SetModel(Constants.WalletStoragePrivateKey, wallet); } else { Logger.LogLine("your password:"******"your private key is {GetPrivateKey(password)}"); } Logger.LogLine($"your address is {wallet.Address}"); }