Example #1
0
        public static void Run(string[] args)
        {
            var file = new FileInfo(args[0]);
            var wallet = Wallet.LoadFromFile(file);
            Console.WriteLine(wallet.ToString());

            // Set up the components and link them together.
            var @params = NetworkParameters.TestNet();
            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();

                wallet.CoinsReceived +=
                    (sender, e) =>
                    {
                        Console.WriteLine();
                        Console.WriteLine("Received tx " + e.Tx.HashAsString);
                        Console.WriteLine(e.Tx.ToString());
                    };

                // Now download and process the block chain.
                peerGroup.DownloadBlockChain();
                peerGroup.Stop();
            }

            wallet.SaveToFile(file);
            Console.WriteLine();
            Console.WriteLine("Done!");
            Console.WriteLine();
            Console.WriteLine(wallet.ToString());
        }
Example #2
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.");
            }
        }
Example #3
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();
            }
        }