Exemplo n.º 1
0
        public void GetPeersLength()
        {
            var seedPeers = new SeedPeers(NetworkParameters.ProdNet());
            var addresses = seedPeers.GetPeers();

            Assert.AreEqual(addresses.Count(), SeedPeers.SeedAddrs.Length);
        }
Exemplo n.º 2
0
        /// <exception cref="PeerDiscoveryException"/>
        private static void PrintDns()
        {
            var start = Environment.TickCount;
            var dns   = new DnsDiscovery(NetworkParameters.ProdNet());

            PrintAddresses(dns.GetPeers());
            PrintElapsed(start);
        }
Exemplo n.º 3
0
        public void TestPeerAddressRoundtrip()
        {
            // copied verbatim from https://en.bitcoin.it/wiki/Protocol_specification#Network_address
            const string fromSpec     = "010000000000000000000000000000000000ffff0a000001208d";
            var          pa           = new PeerAddress(NetworkParameters.ProdNet(), Hex.Decode(fromSpec), 0, 0);
            var          reserialized = Utils.BytesToHexString(pa.BitcoinSerialize());

            Assert.AreEqual(reserialized, fromSpec);
        }
        public void TestVerack()
        {
            var bs = new BitcoinSerializer(NetworkParameters.ProdNet(), false);

            // the actual data from https://en.bitcoin.it/wiki/Protocol_specification#verack
            using (var bais = new MemoryStream(Hex.Decode("f9beb4d976657261636b00000000000000000000")))
            {
                bs.Deserialize(bais);
            }
        }
Exemplo n.º 5
0
        public void GetPeerAll()
        {
            var seedPeers = new SeedPeers(NetworkParameters.ProdNet());

            for (var i = 0; i < SeedPeers.SeedAddrs.Length; ++i)
            {
                Assert.IsNotNull(seedPeers.GetPeer(), "Failed on index: " + i);
            }
            Assert.IsNull(seedPeers.GetPeer());
        }
Exemplo n.º 6
0
        public void GetPeerAll()
        {
            var seedPeers = new SeedPeers(NetworkParameters.ProdNet());

            for (var i = 0; i < SeedPeers.SeedAddrs.Length; ++i)
            {
                Assert.That(seedPeers.GetPeer(), Is.Not.Null, "Failed on index: " + i);
            }
            Assert.That(seedPeers.GetPeer(), Is.EqualTo(null));
        }
Exemplo n.º 7
0
        public static void Run(string[] args)
        {
            // TODO: Assumes production network not testnet. Make it selectable.
            var @params = NetworkParameters.ProdNet();

            try
            {
                // Decode the private key from Satoshi's Base58 variant. If 51 characters long then it's from BitCoins
                // "dumpprivkey" command and includes a version byte and checksum. Otherwise assume it's a raw key.
                EcKey key;
                if (args[0].Length == 51)
                {
                    var dumpedPrivateKey = new DumpedPrivateKey(@params, args[0]);
                    key = dumpedPrivateKey.Key;
                }
                else
                {
                    var privKey = Base58.DecodeToBigInteger(args[0]);
                    key = new EcKey(privKey);
                }
                Console.WriteLine("Address from private key is: " + key.ToAddress(@params));
                // And the address ...
                var destination = new Address(@params, args[1]);

                // Import the private key to a fresh wallet.
                var wallet = new Wallet(@params);
                wallet.AddKey(key);

                // Find the transactions that involve those coins.
                using (var blockStore = new MemoryBlockStore(@params))
                {
                    var chain = new BlockChain(@params, wallet, blockStore);

                    var peerGroup = new PeerGroup(blockStore, @params, chain);
                    peerGroup.AddAddress(new PeerAddress(IPAddress.Loopback));
                    peerGroup.Start();
                    peerGroup.DownloadBlockChain();
                    peerGroup.Stop();

                    // And take them!
                    Console.WriteLine("Claiming " + Utils.BitcoinValueToFriendlyString(wallet.GetBalance()) + " coins");
                    wallet.SendCoins(peerGroup, destination, wallet.GetBalance());
                    // Wait a few seconds to let the packets flush out to the network (ugly).
                    Thread.Sleep(5000);
                }
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("First arg should be private key in Base58 format. Second argument should be address to send to.");
            }
        }
        public void TestAddr()
        {
            var bs = new BitcoinSerializer(NetworkParameters.ProdNet(), true);

            // the actual data from https://en.bitcoin.it/wiki/Protocol_specification#addr
            using (var bais = new MemoryStream(Hex.Decode("f9beb4d96164647200000000000000001f000000" +
                                                          "ed52399b01e215104d010000000000000000000000000000000000ffff0a000001208d")))
            {
                var a = (AddressMessage)bs.Deserialize(bais);
                Assert.AreEqual(1, a.Addresses.Count);
                var pa = a.Addresses[0];
                Assert.AreEqual(8333, pa.Port);
                Assert.AreEqual("10.0.0.1", pa.Addr.ToString());
            }
        }
        public void TestVersion()
        {
            var bs = new BitcoinSerializer(NetworkParameters.ProdNet(), false);

            // the actual data from https://en.bitcoin.it/wiki/Protocol_specification#version
            using (var bais = new MemoryStream(Hex.Decode("f9beb4d976657273696f6e0000000000550000009" +
                                                          "c7c00000100000000000000e615104d00000000010000000000000000000000000000000000ffff0a000001daf6010000" +
                                                          "000000000000000000000000000000ffff0a000002208ddd9d202c3ab457130055810100")))
            {
                var vm = (VersionMessage)bs.Deserialize(bais);
                Assert.AreEqual(31900U, vm.ClientVersion);
                Assert.AreEqual(1292899814UL, vm.Time);
                Assert.AreEqual(98645U, vm.BestHeight);
            }
        }
Exemplo n.º 10
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Connecting to node");
            var @params = NetworkParameters.ProdNet();

            using (var blockStore = new MemoryBlockStore(@params))
            {
                var chain = new BlockChain(@params, blockStore);
                var peer  = new Peer(@params, new PeerAddress(IPAddress.Loopback), chain);
                peer.Connect();
                new Thread(peer.Run).Start();

                var blockHash = new Sha256Hash(args[0]);
                var future    = peer.BeginGetBlock(blockHash, null, null);
                Console.WriteLine("Waiting for node to send us the requested block: " + blockHash);
                var block = peer.EndGetBlock(future);
                Console.WriteLine(block);
                peer.Disconnect();
            }
        }
Exemplo n.º 11
0
        public void GetPeerOne()
        {
            var seedPeers = new SeedPeers(NetworkParameters.ProdNet());

            Assert.IsNotNull(seedPeers.GetPeer());
        }
Exemplo n.º 12
0
        public static void Run(string[] args)
        {
            var testNet    = args.Length > 0 && string.Equals(args[0], "testnet", StringComparison.InvariantCultureIgnoreCase);
            var @params    = testNet ? NetworkParameters.TestNet() : NetworkParameters.ProdNet();
            var filePrefix = testNet ? "pingservice-testnet" : "pingservice-prodnet";

            // Try to read the wallet from storage, create a new one if not possible.
            Wallet wallet;
            var    walletFile = new FileInfo(filePrefix + ".wallet");

            try
            {
                wallet = Wallet.LoadFromFile(walletFile);
            }
            catch (IOException)
            {
                wallet = new Wallet(@params);
                wallet.Keychain.Add(new EcKey());
                wallet.SaveToFile(walletFile);
            }
            // Fetch the first key in the wallet (should be the only key).
            var key = wallet.Keychain[0];

            Console.WriteLine(wallet);

            // Load the block chain, if there is one stored locally.
            Console.WriteLine("Reading block store from disk");
            using (var blockStore = new BoundedOverheadBlockStore(@params, new FileInfo(filePrefix + ".blockchain")))
            {
                // Connect to the localhost node. One minute timeout since we won't try any other peers
                Console.WriteLine("Connecting ...");
                var chain = new BlockChain(@params, wallet, blockStore);

                var peerGroup = new PeerGroup(blockStore, @params, chain);
                peerGroup.AddAddress(new PeerAddress(IPAddress.Loopback));
                peerGroup.Start();

                // We want to know when the balance changes.
                wallet.CoinsReceived +=
                    (sender, e) =>
                {
                    // Running on a peer thread.
                    Debug.Assert(!e.NewBalance.Equals(0));
                    // It's impossible to pick one specific identity that you receive coins from in BitCoin as there
                    // could be inputs from many addresses. So instead we just pick the first and assume they were all
                    // owned by the same person.
                    var input = e.Tx.Inputs[0];
                    var from  = input.FromAddress;
                    var value = e.Tx.GetValueSentToMe(wallet);
                    Console.WriteLine("Received " + Utils.BitcoinValueToFriendlyString(value) + " from " + from);
                    // Now send the coins back!
                    var sendTx = wallet.SendCoins(peerGroup, from, value);
                    Debug.Assert(sendTx != null);     // We should never try to send more coins than we have!
                    Console.WriteLine("Sent coins back! Transaction hash is " + sendTx.HashAsString);
                    wallet.SaveToFile(walletFile);
                };

                peerGroup.DownloadBlockChain();
                Console.WriteLine("Send coins to: " + key.ToAddress(@params));
                Console.WriteLine("Waiting for coins to arrive. Press Ctrl-C to quit.");
                // The PeerGroup thread keeps us alive until something kills the process.
            }
        }
Exemplo n.º 13
0
        public static void Run(string[] args)
        {
            var testNet    = args.Length > 0 && string.Equals(args[0], "testnet", StringComparison.InvariantCultureIgnoreCase);
            var @params    = testNet ? NetworkParameters.TestNet() : NetworkParameters.ProdNet();
            var filePrefix = testNet ? "pingservice-testnet" : "pingservice-prodnet";

            // Try to read the wallet from storage, create a new one if not possible.
            Wallet wallet;
            var    walletFile = new FileInfo(filePrefix + ".wallet");

            try
            {
                wallet = Wallet.LoadFromFile(walletFile);
            }
            catch (IOException)
            {
                wallet = new Wallet(@params);
                wallet.Keychain.Add(new EcKey());
                wallet.SaveToFile(walletFile);
            }
            // Fetch the first key in the wallet (should be the only key).
            var key = wallet.Keychain[0];

            // Load the block chain, if there is one stored locally.
            Console.WriteLine("Reading block store from disk");
            using (var blockStore = new BoundedOverheadBlockStore(@params, new FileInfo(filePrefix + ".blockchain")))
            {
                // Connect to the localhost node. One minute timeout since we won't try any other peers
                Console.WriteLine("Connecting ...");
                using (var conn = new NetworkConnection(IPAddress.Loopback, @params, blockStore.GetChainHead().Height, 60000))
                {
                    var chain = new BlockChain(@params, wallet, blockStore);
                    var peer  = new Peer(@params, conn, chain);
                    peer.Start();

                    // We want to know when the balance changes.
                    wallet.CoinsReceived +=
                        (sender, e) =>
                    {
                        // Running on a peer thread.
                        Debug.Assert(!e.NewBalance.Equals(0));
                        // It's impossible to pick one specific identity that you receive coins from in BitCoin as there
                        // could be inputs from many addresses. So instead we just pick the first and assume they were all
                        // owned by the same person.
                        var input = e.Tx.Inputs[0];
                        var from  = input.FromAddress;
                        var value = e.Tx.GetValueSentToMe(wallet);
                        Console.WriteLine("Received " + Utils.BitcoinValueToFriendlyString(value) + " from " + from);
                        // Now send the coins back!
                        var sendTx = wallet.SendCoins(peer, from, value);
                        Debug.Assert(sendTx != null);     // We should never try to send more coins than we have!
                        Console.WriteLine("Sent coins back! Transaction hash is " + sendTx.HashAsString);
                        wallet.SaveToFile(walletFile);
                    };

                    var progress = peer.StartBlockChainDownload();
                    var max      = progress.Count; // Racy but no big deal.
                    if (max > 0)
                    {
                        Console.WriteLine("Downloading block chain. " + (max > 1000 ? "This may take a while." : ""));
                        var current     = max;
                        var lastPercent = 0;
                        while (current > 0)
                        {
                            var pct = 100.0 - (100.0 * (current / (double)max));
                            if ((int)pct != lastPercent)
                            {
                                Console.WriteLine(string.Format("Chain download {0}% done", (int)pct));
                                lastPercent = (int)pct;
                            }
                            progress.Await(TimeSpan.FromSeconds(1));
                            current = progress.Count;
                        }
                    }
                    Console.WriteLine("Send coins to: " + key.ToAddress(@params));
                    Console.WriteLine("Waiting for coins to arrive. Press Ctrl-C to quit.");
                    // The peer thread keeps us alive until something kills the process.
                }
            }
        }
Exemplo n.º 14
0
        public void GetPeerOne()
        {
            var seedPeers = new SeedPeers(NetworkParameters.ProdNet());

            Assert.That(seedPeers.GetPeer(), Is.Not.Null);
        }