Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //Clients.Add(IPAddress.Parse("192.168.43.174"));
            Clients.Add(IPAddress.Parse("192.168.43.153"));
            //Clients.Add(IPAddress.Parse("91.121.50.14"));
            Clients.Add(IPAddress.Parse("192.168.43.47"));
            //Clients.Add(IPAddress.Parse("192.168.43.41"));

            foreach (var ip in Discoverer.GetAllLocalIPv4())
            {
                Clients.Add(ip);
                Console.WriteLine("Local IP: " + ip.ToString());
            }

            Console.WriteLine("Starting node server...");

            Task.Run(() => Serve());

            Discoverer.PeerJoined = x => Connect(x);
            Discoverer.PeerLeft   = x => Disconnect(x);
            Discoverer.Start();

            Task.Delay(1000).Wait();

            Task.Run(() => Update());

            Task.Delay(1000).Wait();

            if (!File.Exists("privatekey.txt"))
            {
                Console.WriteLine("Generating key...");
                CngKeyCreationParameters keyCreationParameters = new CngKeyCreationParameters();
                keyCreationParameters.ExportPolicy = CngExportPolicies.AllowPlaintextExport;
                keyCreationParameters.KeyUsage     = CngKeyUsages.Signing;

                CngKey key = CngKey.Create(CngAlgorithm.ECDsaP256, null, keyCreationParameters);

                ECDsaCng dsa        = new ECDsaCng(key);
                byte[]   privateKey = dsa.Key.Export(CngKeyBlobFormat.EccPrivateBlob);
                File.WriteAllText("privatekey.txt", String.Join(",", privateKey));
            }

            CngKey importedKey = CngKey.Import(File.ReadAllText("privatekey.txt").Split(',').Select(m => byte.Parse(m)).ToArray(), CngKeyBlobFormat.EccPrivateBlob);

            importedDSA = new ECDsaCng(importedKey);

            publicKey = importedDSA.Key.Export(CngKeyBlobFormat.EccPublicBlob);

            using (SHA1 sha1 = SHA1.Create())
            {
                pubKeyHash = sha1.ComputeHash(publicKey);
            }

            Console.Write("Address: " + BitConverter.ToString(pubKeyHash).Replace("-", string.Empty));
            Console.WriteLine();


            var httpServer = new HttpServer(new HttpRequestProvider());
            var lhgdkg     = new TcpListener(IPAddress.Any, 80);

            //lhgdkg.Server.ReceiveTimeout = 10000;
            httpServer.Use(new TcpListenerAdapter(lhgdkg));

            Thread abc = new Thread(_ => (new UI()).ShowDialog());

            abc.Start(null);

            /*httpServer.Use((a, b) => {
             *
             *  string ach = Program.template;
             *  string data = "";
             *  foreach (var pair in Program.Balances)
             *  {
             *      data += "<tr><td>";
             *      data += BitConverter.ToString(pair.Key).Replace("-", string.Empty);
             *      data += "</td><td>";
             *      data += pair.Value;
             *      data += "</td></tr>";
             *  }
             *  ach = ach.Replace("{data}", data);
             *  while (true)
             *  {
             *      try
             *      {
             *          Thread.Sleep(100);
             *          var tw = new StreamWriter("index.html");
             *          tw.Write(ach);
             *          tw.Close();
             *          break;
             *      }
             *      catch { }
             *  }
             *  Thread.Sleep(100);
             *  //File.WriteAllText("index.html", ach);
             *  return b();
             * });*/
            httpServer.Use(new fix());
            httpServer.Start();

            while (true)
            {
                Console.Write("> ");
                string   cmd     = Console.ReadLine();
                string[] command = cmd.Split(' ');
                switch (command[0])
                {
                case "help":
                {
                    Console.WriteLine("help : view help");
                    Console.WriteLine("tx <address> <amount> : send money");
                    Console.WriteLine("mine [threads] [address] : start/stop mining");
                    Console.WriteLine("bal [address] : view balance");
                    Console.WriteLine("exit : close the program");
                    break;
                }

                case "exit":
                {
                    httpServer.Dispose();
                    Environment.Exit(0);
                    break;
                }

                case "bal":
                {
                    byte[] address = new byte[20];
                    if (command.Length > 1)
                    {
                        for (int i = 0; i < 20; i++)
                        {
                            address[i] = Convert.ToByte(command[1].Substring(i * 2, 2), 16);
                        }
                    }
                    else
                    {
                        address = pubKeyHash;
                    }
                    double balance = 0;
                    if (Balances.ContainsKey(address))
                    {
                        balance = Balances[address];
                    }
                    Console.WriteLine("Current balance: " + balance);
                    break;
                }

                case "tx":
                {
                    if (command.Length != 3)
                    {
                        Console.WriteLine("Missing arguments.");
                        break;
                    }
                    if (command[1].Length != 40)
                    {
                        Console.WriteLine("Invalid address.");
                        break;
                    }
                    byte[] addr = new byte[20];
                    int    i    = 0;
                    for (; i < 20; i++)
                    {
                        try
                        {
                            addr[i] = Convert.ToByte(command[1].Substring(i * 2, 2), 16);
                        }
                        catch { Console.WriteLine("Invalid address."); break; }
                    }
                    if (i < 19)
                    {
                        break;
                    }
                    double amount = double.Parse(command[2]);

                    var tx = new Transaction();
                    tx.target    = addr;
                    tx.amount    = amount;
                    tx.source    = publicKey;
                    tx.timestamp = (uint)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                    tx.signature = importedDSA.SignData(tx.GetBytesToSign(), HashAlgorithmName.SHA256);

                    IPEndPoint ipep2   = new IPEndPoint(IPAddress.Any, 53417);
                    UdpClient  newsock = new UdpClient(ipep2);
                    newsock.Client.ReceiveTimeout = 1000;
                    newsock.Client.SendTimeout    = 1000;

                    foreach (IPAddress ip in Clients)
                    {
                        if (Discoverer.GetAllLocalIPv4().Contains(ip))
                        {
                            continue;
                        }

                        IPEndPoint ipep = new IPEndPoint(ip, 53418);
                        byte[]     data = null;

                        byte[] transaction = tx.GetBytesTotal();

                        List <byte> packet = new List <byte>();
                        packet.Add(0x02);
                        packet.AddRange(BitConverter.GetBytes((ushort)transaction.Length));
                        packet.AddRange(transaction);
                        try
                        {
                            newsock.Send(packet.ToArray(), packet.Count, ipep);
                        }
                        catch { continue; }

                        try
                        {
                            data = newsock.Receive(ref ipep);
                        }
                        catch { continue; }
                    }

                    newsock.Close();

                    Swarm.Add(tx);
                    break;
                }

                case "mine":
                {
                    ulong threads = 1;
                    if (command.Length > 1)
                    {
                        threads = ulong.Parse(command[1]);
                    }
                    if (isMining)
                    {
                        Console.WriteLine("Stopped mining.");
                        miningThreads.ForEach(x => x.Abort());
                        miningThreads.Clear();
                    }
                    else
                    {
                        Console.WriteLine("Started mining.");
                        ulong  nonceOffset = ulong.MaxValue / threads;
                        byte[] address     = new byte[20];
                        if (command.Length > 2)
                        {
                            for (int i = 0; i < 20; i++)
                            {
                                address[i] = Convert.ToByte(command[2].Substring(i * 2, 2), 16);
                            }
                        }
                        else
                        {
                            address = pubKeyHash;
                        }
                        Random r = new Random();
                        for (ulong i = 0; i < threads; i++)
                        {
                            Thread miningThread = new Thread((param) => Mine(address, (ulong)param));
                            byte[] nonce        = new byte[8];
                            r.NextBytes(nonce);
                            nonce[7] = (byte)i;
                            miningThread.Start(BitConverter.ToUInt64(nonce, 0));
                            miningThreads.Add(miningThread);
                        }
                    }
                    isMining = !isMining;
                    break;
                }
                }
            }
        }
Exemplo n.º 2
0
        static void Update()
        {
            Console.WriteLine("Running initial update...");
            IPEndPoint ipep2   = new IPEndPoint(IPAddress.Any, 53419);
            UdpClient  newsock = new UdpClient(ipep2);

            newsock.Client.ReceiveTimeout = 1000;
            newsock.Client.SendTimeout    = 1000;
            foreach (IPAddress ip in Clients)
            {
                IPEndPoint ipep = new IPEndPoint(ip, 53418);
                byte[]     data = null;
                try
                {
                    newsock.Send(new byte[] { 0x03 }, 1, ipep);
                }
                catch
                {
                    continue;
                }
                try
                {
                    data = newsock.Receive(ref ipep);
                }
                catch
                {
                    continue;
                }

                if (data != null)
                {
                    int num   = BitConverter.ToInt32(data, 0);
                    int index = 4;
                    for (int i = 0; i < num; i++)
                    {
                        ushort len         = BitConverter.ToUInt16(data, index);
                        byte[] transaction = new byte[len];
                        Array.Copy(data, index + 4, transaction, 0, len);
                        var tx = Transaction.FromBytes(transaction, false);
                        if (!Swarm.Any(x => x.signature.SequenceEqual(tx.signature)))
                        {
                            Swarm.Add(tx);
                        }
                        index += 4 + len;
                    }
                }
            }
            int j     = 0;
            var stuff = Discoverer.GetAllLocalIPv4();

            while (true)
            {
                foreach (IPAddress ip in Clients)
                {
                    if (stuff.Contains(ip))
                    {
                        continue;
                    }
                    IPEndPoint ipep = new IPEndPoint(ip, 53418);
                    byte[]     data = null;

                    try
                    {
                        newsock.Send(new byte[] { 0x00 }, 1, ipep);
                    }
                    catch
                    {
                        continue;
                    }

                    try
                    {
                        data = newsock.Receive(ref ipep);
                    }
                    catch
                    {
                        continue;
                    }

                    if (data != null)
                    {
                        byte[] hash = new byte[32];
                        Array.Copy(data, 0, hash, 0, 32);
                        if (!hash.SequenceEqual(LastBlockHash) && !Blocks.Any(x => x.previousBlockHash.SequenceEqual(hash)))
                        {
                            while (!LastBlockHash.SequenceEqual(hash))
                            {
                                List <byte> packet = new List <byte>();
                                packet.Add(0x01);
                                packet.AddRange(LastBlockHash);
                                newsock.Send(packet.ToArray(), packet.Count, ipep);
                                try
                                {
                                    data = newsock.Receive(ref ipep);
                                }
                                catch
                                {
                                    continue;
                                }
                                int len = BitConverter.ToInt32(data, 0);
                                if (len != 0)
                                {
                                    byte[] block = new byte[len];
                                    Array.Copy(data, 4, block, 0, len);
                                    var blk = Block.FromBytes(block);
                                    if (blk != null)
                                    {
                                        Blocks.Add(blk);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Received invalid block");
                                        break;
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("Received invalid block hash");
                                    LastBlockHash = Blocks[Blocks.Count - 1].previousBlockHash;
                                    var toBeReverted = Blocks[Blocks.Count - 1];
                                    foreach (var tx in toBeReverted.transactions)
                                    {
                                        byte[] pubKeyHash = new byte[20];
                                        using (SHA1 sha1 = SHA1.Create())
                                        {
                                            pubKeyHash = sha1.ComputeHash(tx.source);
                                        }
                                        Balances[pubKeyHash] += tx.amount;
                                        Balances[tx.target]  -= tx.amount;
                                    }
                                    Balances[toBeReverted.rewardTarget] -= Block.GetBlockReward();
                                    if (toBeReverted.numTransactions == 0)
                                    {
                                        EmptyBlocks--;
                                    }
                                    else
                                    {
                                        TotalTransactions -= (uint)toBeReverted.numTransactions;
                                    }
                                    Blocks.Remove(toBeReverted);
                                    break;
                                }
                            }
                        }
                    }
                    Task.Delay(10).Wait();
                }

                Task.Delay(20).Wait();

                /*if (j < 30)
                 *  j++;
                 * else
                 * {
                 *  foreach (IPAddress ip in Clients)
                 *  {
                 *      IPEndPoint ipep = new IPEndPoint(ip, 53418);
                 *      byte[] data = null;
                 *
                 *      try
                 *      {
                 *          newsock.Send(new byte[] { 0x04 }, 1, ipep);
                 *      }
                 *      catch
                 *      {
                 *          continue;
                 *      }
                 *
                 *      try
                 *      {
                 *          data = newsock.Receive(ref ipep);
                 *      }
                 *      catch
                 *      {
                 *          continue;
                 *      }
                 *
                 *      if (data != null)
                 *      {
                 *          int count = BitConverter.ToInt32(data, 0);
                 *          for (int i = 0; i < count; i++)
                 *          {
                 *              byte[] iparr = new byte[4];
                 *              Array.Copy(data, 4 + (i * 4), iparr, 0, 4);
                 *              IPAddress ipadr = new IPAddress(iparr);
                 *              if (!Clients.Contains(ipadr))
                 *                  Clients.Add(ipadr);
                 *          }
                 *      }
                 *      Task.Delay(20).Wait();
                 *  }
                 * }*/
                //Task.Delay(500).Wait();
            }
        }
Exemplo n.º 3
0
        private void button2_Click(object sender, EventArgs e)
        {
            byte[] addr = new byte[20];
            int    i    = 0;

            for (; i < 20; i++)
            {
                try
                {
                    addr[i] = Convert.ToByte(textBox1.Text.Substring(i * 2, 2), 16);
                }
                catch { Console.WriteLine("Invalid address."); break; }
            }
            if (i < 19)
            {
                return;
            }
            double amount = double.Parse(textBox2.Text);
            var    tx     = new Transaction();

            tx.target    = addr;
            tx.amount    = amount;
            tx.source    = Program.publicKey;
            tx.timestamp = (uint)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            tx.signature = Program.importedDSA.SignData(tx.GetBytesToSign(), HashAlgorithmName.SHA256);

            IPEndPoint ipep2   = new IPEndPoint(IPAddress.Any, 53417);
            UdpClient  newsock = new UdpClient(ipep2);

            newsock.Client.ReceiveTimeout = 1000;
            newsock.Client.SendTimeout    = 1000;

            foreach (IPAddress ip in Program.Clients)
            {
                if (Discoverer.GetAllLocalIPv4().Contains(ip))
                {
                    continue;
                }

                IPEndPoint ipep = new IPEndPoint(ip, 53418);
                byte[]     data = null;

                byte[] transaction = tx.GetBytesTotal();

                List <byte> packet = new List <byte>();
                packet.Add(0x02);
                packet.AddRange(BitConverter.GetBytes((ushort)transaction.Length));
                packet.AddRange(transaction);
                try
                {
                    newsock.Send(packet.ToArray(), packet.Count, ipep);
                }
                catch { continue; }

                try
                {
                    data = newsock.Receive(ref ipep);
                }
                catch { continue; }
            }

            newsock.Close();

            Program.Swarm.Add(tx);
        }