Esempio n. 1
0
 public void SetUp()
 {
     _defaultWallet = new DefaultWallet(_params);
     _blockStore = new MemoryBlockStore(_params);
     var chain = new BlockChain(_params, _defaultWallet, _blockStore);
     _peerGroup = new PeerGroup(_blockStore, _params, chain);
 }
Esempio n. 2
0
        public static void Run(string[] args)
        {
            var wallet = new DefaultWallet(NetworkParameters);

            using (var blockStore = new MemoryBlockStore(NetworkParameters))
            {
                var chain = new BlockChain(NetworkParameters, wallet, blockStore);

                var peerGroup = new PeerGroup(blockStore, NetworkParameters, chain);
                //peerGroup.AddAddress(new PeerAddress(new IPAddress(new byte[]{ 192, 168, 1, 136 }), 18333));
                peerGroup.AddPeerDiscovery(new DnsDiscovery(NetworkParameters));
                peerGroup.Start();

                // Act
                peerGroup.DownloadBlockChain();

                peerGroup.Stop();
            }

            //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.Transaction.HashAsString);
            //            Console.WriteLine(e.Transaction.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());
        }
Esempio n. 3
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 DefaultWallet(@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.");
            }
        }
Esempio n. 4
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.
            IDefaultWallet defaultWallet;
            var walletFile = new FileInfo(filePrefix + ".wallet");
            try
            {
                defaultWallet = DefaultWallet.LoadFromFile(walletFile);
            }
            catch (IOException)
            {
                defaultWallet = new DefaultWallet(@params);
                defaultWallet.Keychain.Add(new EcKey());
                defaultWallet.SaveToFile(walletFile);
            }
            // Fetch the first key in the wallet (should be the only key).
            var key = defaultWallet.Keychain[0];

            Console.WriteLine(defaultWallet);

            // 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, defaultWallet, blockStore);

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

                // We want to know when the balance changes.
                defaultWallet.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.Transaction.TransactionInputs[0];
                        var from = input.FromAddress;
                        var value = e.Transaction.GetValueSentToMe(defaultWallet);
                        Console.WriteLine("Received " + Utils.BitcoinValueToFriendlyString(value) + " from " + from);
                        // Now send the coins back!
                        var sendTx = defaultWallet.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);
                        defaultWallet.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.
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Sends coins to the given address, via the given <see cref="BitcoinSharp.Core.Network.PeerGroup"/>.
        /// Change is returned to the first key in the wallet.
        /// </summary>
        /// <param name="peerGroup">The peer group to send via.</param>
        /// <param name="toAddress">Which address to send coins to.</param>
        /// <param name="nanocoins">How many nanocoins to send. You can use Utils.toNanoCoins() to calculate this.</param>
        /// <returns>
        /// The <see cref="BitcoinSharp.Core.Messages.Transaction"/> that was created or null if there was insufficient balance to send the coins.
        /// </returns>
        /// <exception cref="System.IO.IOException">If there was a problem broadcasting the transaction.</exception>
        public Transaction SendCoins(PeerGroup peerGroup, Address toAddress, ulong nanocoins)
        {
            lock (this)
            {
                var transaction = CreateSend(toAddress, nanocoins);
                if (transaction == null) // Not enough money! :-(
                    return null;
                if (!peerGroup.BroadcastTransaction(transaction))
                {
                    throw new IOException("Failed to broadcast tx to all connected peers");
                }

                // TODO - retry logic
                ConfirmSend(transaction);
                return transaction;
            }
        }