/// <summary>
 /// Parses the given private key as created by the "dumpprivkey" BitCoin C++ RPC.
 /// </summary>
 /// <param name="params">The expected network parameters of the key. If you don't care, provide null.</param>
 /// <param name="encoded">The base58 encoded string.</param>
 /// <exception cref="AddressFormatException">If the string is invalid or the header byte doesn't match the network params.</exception>
 public DumpedPrivateKey(NetworkParameters @params, string encoded)
     : base(encoded)
 {
     if (@params != null && Version != @params.DumpedPrivateKeyHeader)
         throw new AddressFormatException("Mismatched version number, trying to cross networks? " + Version +
                                          " vs " + @params.DumpedPrivateKeyHeader);
 }
 /// <summary>
 /// Construct an address from parameters and the standard "human readable" form.
 /// </summary>
 /// <remarks>
 /// Example:<p/>
 /// <pre>new Address(NetworkParameters.prodNet(), "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");</pre>
 /// </remarks>
 /// <exception cref="AddressFormatException"/>
 public Address(NetworkParameters @params, string address)
     : base(address)
 {
     if (Version != @params.AddressHeader)
         throw new AddressFormatException("Mismatched version number, trying to cross networks? " + Version +
                                          " vs " + @params.AddressHeader);
 }
 /// <summary>
 /// Used only in creation of the genesis block.
 /// </summary>
 internal TransactionInput(NetworkParameters @params, Transaction parentTransaction, byte[] scriptBytes)
     : base(@params)
 {
     ScriptBytes = scriptBytes;
     Outpoint = new TransactionOutPoint(@params, -1, null);
     _sequence = uint.MaxValue;
     ParentTransaction = parentTransaction;
 }
 internal Transaction(NetworkParameters @params)
     : base(@params)
 {
     _version = 1;
     _inputs = new List<TransactionInput>();
     _outputs = new List<TransactionOutput>();
     // We don't initialize appearsIn deliberately as it's only useful for transactions stored in the wallet.
 }
        /// <summary>
        /// Connect to the given IP address using the port specified as part of the network parameters. Once construction
        /// is complete a functioning network channel is set up and running.
        /// </summary>
        /// <param name="peerAddress">IP address to connect to. IPv6 is not currently supported by BitCoin. If port is not positive the default port from params is used.</param>
        /// <param name="params">Defines which network to connect to and details of the protocol.</param>
        /// <param name="bestHeight">How many blocks are in our best chain</param>
        /// <param name="connectTimeout">Timeout in milliseconds when initially connecting to peer</param>
        /// <exception cref="IOException">If there is a network related failure.</exception>
        /// <exception cref="ProtocolException">If the version negotiation failed.</exception>
        public NetworkConnection(PeerAddress peerAddress, NetworkParameters @params, uint bestHeight, int connectTimeout)
        {
            _params = @params;
            _remoteIp = peerAddress.Addr;

            var port = (peerAddress.Port > 0) ? peerAddress.Port : @params.Port;

            var address = new IPEndPoint(_remoteIp, port);
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.Connect(address);
            _socket.SendTimeout = _socket.ReceiveTimeout = connectTimeout;

            _out = new NetworkStream(_socket, FileAccess.Write);
            _in = new NetworkStream(_socket, FileAccess.Read);

            // the version message never uses check-summing. Update check-summing property after version is read.
            _serializer = new BitcoinSerializer(@params, false);

            // Announce ourselves. This has to come first to connect to clients beyond v0.30.20.2 which wait to hear
            // from us until they send their version message back.
            WriteMessage(new VersionMessage(@params, bestHeight));
            // When connecting, the remote peer sends us a version message with various bits of
            // useful data in it. We need to know the peer protocol version before we can talk to it.
            _versionMessage = (VersionMessage) ReadMessage();
            // Now it's our turn ...
            // Send an ACK message stating we accept the peers protocol version.
            WriteMessage(new VersionAck());
            // And get one back ...
            ReadMessage();
            // Switch to the new protocol version.
            var peerVersion = _versionMessage.ClientVersion;
            _log.InfoFormat("Connected to peer: version={0}, subVer='{1}', services=0x{2:X}, time={3}, blocks={4}",
                            peerVersion,
                            _versionMessage.SubVer,
                            _versionMessage.LocalServices,
                            UnixTime.FromUnixTime(_versionMessage.Time),
                            _versionMessage.BestHeight
                );
            // BitCoinSharp is a client mode implementation. That means there's not much point in us talking to other client
            // mode nodes because we can't download the data from them we need to find/verify transactions.
            if (!_versionMessage.HasBlockChain())
            {
                // Shut down the socket
                try
                {
                    Shutdown();
                }
                catch (IOException)
                {
                    // ignore exceptions while aborting
                }
                throw new ProtocolException("Peer does not have a copy of the block chain.");
            }
            // newer clients use check-summing
            _serializer.UseChecksumming(peerVersion >= 209);
            // Handshake is done!
        }
 /// <summary>
 /// Creates an UNSIGNED input that links to the given output
 /// </summary>
 internal TransactionInput(NetworkParameters @params, Transaction parentTransaction, TransactionOutput output)
     : base(@params)
 {
     var outputIndex = output.Index;
     Outpoint = new TransactionOutPoint(@params, outputIndex, output.ParentTransaction);
     ScriptBytes = EmptyArray;
     _sequence = uint.MaxValue;
     ParentTransaction = parentTransaction;
 }
 /// <summary>
 /// Creates a new, empty wallet with no keys and no transactions. If you want to restore a wallet from disk instead,
 /// see loadFromFile.
 /// </summary>
 public Wallet(NetworkParameters @params)
 {
     _params = @params;
     Keychain = new List<EcKey>();
     Unspent = new Dictionary<Sha256Hash, Transaction>();
     Spent = new Dictionary<Sha256Hash, Transaction>();
     _inactive = new Dictionary<Sha256Hash, Transaction>();
     Pending = new Dictionary<Sha256Hash, Transaction>();
     _dead = new Dictionary<Sha256Hash, Transaction>();
 }
 public VersionMessage(NetworkParameters @params, uint newBestHeight)
     : base(@params)
 {
     ClientVersion = NetworkParameters.ProtocolVersion;
     LocalServices = 0;
     Time = UnixTime.ToUnixTime(DateTime.UtcNow);
     // Note that the official client doesn't do anything with these, and finding out your own external IP address
     // is kind of tricky anyway, so we just put nonsense here for now.
     MyAddr = new PeerAddress(IPAddress.Loopback, @params.Port, 0);
     TheirAddr = new PeerAddress(IPAddress.Loopback, @params.Port, 0);
     SubVer = "BitCoinSharp 0.3-SNAPSHOT";
     BestHeight = newBestHeight;
 }
 internal TransactionOutPoint(NetworkParameters @params, int index, Transaction fromTx)
     : base(@params)
 {
     Index = index;
     if (fromTx != null)
     {
         Hash = fromTx.Hash;
         FromTx = fromTx;
     }
     else
     {
         // This happens when constructing the genesis block.
         Hash = Sha256Hash.ZeroHash;
     }
 }
 /// <exception cref="ProtocolException"/>
 internal Message(NetworkParameters @params, byte[] msg, int offset, uint protocolVersion = NetworkParameters.ProtocolVersion)
 {
     ProtocolVersion = protocolVersion;
     Params = @params;
     Bytes = msg;
     Cursor = Offset = offset;
     Parse();
     #if SELF_CHECK
     // Useful to ensure serialize/deserialize are consistent with each other.
     if (GetType() != typeof (VersionMessage))
     {
         var msgbytes = new byte[Cursor - offset];
         Array.Copy(msg, offset, msgbytes, 0, Cursor - offset);
         var reserialized = BitcoinSerialize();
         if (!reserialized.SequenceEqual(msgbytes))
             throw new Exception("Serialization is wrong: " + Environment.NewLine +
                                 Utils.BytesToHexString(reserialized) + " vs " + Environment.NewLine +
                                 Utils.BytesToHexString(msgbytes));
     }
     #endif
     Bytes = null;
 }
 /// <exception cref="ProtocolException"/>
 public GetDataMessage(NetworkParameters @params, byte[] payloadBytes)
     : base(@params, payloadBytes)
 {
 }
Beispiel #12
0
 /// <summary>
 /// Creates a PeerGroup with the given parameters and a default 5 second connection timeout.
 /// </summary>
 public PeerGroup(IBlockStore blockStore, NetworkParameters @params, BlockChain chain)
     : this(blockStore, @params, chain, DefaultConnectionDelayMillis)
 {
 }
 /// <summary>
 /// Creates a transaction by reading payload starting from offset bytes in. Length of a transaction is fixed.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public Transaction(NetworkParameters @params, byte[] payload, int offset)
     : base(@params, payload, offset)
 {
     // inputs/outputs will be created in parse()
 }
Beispiel #14
0
 /// <exception cref="ProtocolException"/>
 public VersionMessage(NetworkParameters @params, byte[] msg)
     : base(@params, msg, 0)
 {
 }
 internal Message(NetworkParameters @params)
 {
     Params = @params;
 }
 /// <summary>
 /// Constructs a BitcoinSerializer with the given behavior.
 /// </summary>
 /// <param name="params">MetworkParams used to create Messages instances and determining packetMagic</param>
 /// <param name="usesChecksumming">Set to true if checksums should be included and expected in headers</param>
 public BitcoinSerializer(NetworkParameters @params, bool usesChecksumming)
 {
     _params           = @params;
     _usesChecksumming = usesChecksumming;
 }
 /// <exception cref="ProtocolException"/>
 internal AddressMessage(NetworkParameters @params, byte[] payload)
     : base(@params, payload, 0)
 {
 }
 /// <summary>
 /// Creates a PeerGroup with the given parameters and a default 5 second connection timeout.
 /// </summary>
 public PeerGroup(IBlockStore blockStore, NetworkParameters @params, BlockChain chain)
     : this(blockStore, @params, chain, DefaultConnectionDelayMillis)
 {
 }
 /// <summary>
 /// Deserializes the message. This is usually part of a transaction message.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public TransactionOutPoint(NetworkParameters @params, byte[] payload, int offset)
     : base(@params, payload, offset)
 {
 }
 /// <summary>
 /// Special case constructor, used for the genesis node, cloneAsHeader and unit tests.
 /// </summary>
 internal Block(NetworkParameters @params)
     : base(@params)
 {
     // Set up a few basic things. We are not complete after this though.
     _version = 1;
     _difficultyTarget = 0x1d07fff8;
     _time = (uint) UnixTime.ToUnixTime(DateTime.UtcNow);
     _prevBlockHash = Sha256Hash.ZeroHash;
 }
 /// <summary>
 /// Constructs a block object from the BitCoin wire format.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public Block(NetworkParameters @params, byte[] payloadBytes)
     : base(@params, payloadBytes, 0)
 {
 }
 /// <summary>
 /// Constructs a BlockChain connected to the given wallet and store. To obtain a <see cref="Wallet"/> you can construct
 /// one from scratch, or you can deserialize a saved wallet from disk using <see cref="Wallet.LoadFromFile"/>.
 /// </summary>
 /// <remarks>
 /// For the store you can use a <see cref="MemoryBlockStore"/> if you don't care about saving the downloaded data, or a
 /// <see cref="BoundedOverheadBlockStore"/> if you'd like to ensure fast start-up the next time you run the program.
 /// </remarks>
 /// <exception cref="BlockStoreException"/>
 public BlockChain(NetworkParameters @params, Wallet wallet, IBlockStore blockStore)
     : this(@params, new List<Wallet>(), blockStore)
 {
     if (wallet != null)
         AddWallet(wallet);
 }
 /// <summary>
 /// Deserializes the message. This is usually part of a transaction message.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public TransactionOutPoint(NetworkParameters @params, byte[] payload, int offset)
     : base(@params, payload, offset)
 {
 }
 public GetDataMessage(NetworkParameters @params)
     : base(@params)
 {
 }
 /// <exception cref="ProtocolException"/>
 internal AddressMessage(NetworkParameters @params, byte[] payload, int offset)
     : base(@params, payload, offset)
 {
 }
Beispiel #26
0
 /// <summary>
 /// Construct a peer that handles the given network connection and reads/writes from the given block chain. Note that
 /// communication won't occur until you call connect().
 /// </summary>
 public Peer(NetworkParameters @params, PeerAddress address, BlockChain blockChain)
     : this(@params, address, 0, blockChain)
 {
 }
        private byte[] _programCopy; // TODO: remove this

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Construct a Script using the given network parameters and a range of the programBytes array.
        /// </summary>
        /// <param name="params">Network parameters.</param>
        /// <param name="programBytes">Array of program bytes from a transaction.</param>
        /// <param name="offset">How many bytes into programBytes to start reading from.</param>
        /// <param name="length">How many bytes to read.</param>
        /// <exception cref="ScriptException"/>
        public Script(NetworkParameters @params, byte[] programBytes, int offset, int length)
        {
            _params = @params;
            Parse(programBytes, offset, length);
        }
 /// <summary>
 /// Returns the address that corresponds to the public part of this ECKey. Note that an address is derived from
 /// the RIPEMD-160 hash of the public key and is not the public key itself (which is too large to be convenient).
 /// </summary>
 public Address ToAddress(NetworkParameters @params)
 {
     var hash160 = Utils.Sha256Hash160(_pub);
     return new Address(@params, hash160);
 }
 // Used by EcKey.PrivateKeyEncoded
 internal DumpedPrivateKey(NetworkParameters @params, byte[] keyBytes)
     : base(@params.DumpedPrivateKeyHeader, keyBytes)
 {
     if (keyBytes.Length != 32) // 256 bit keys
         throw new ArgumentException("Keys are 256 bits, so you must provide 32 bytes, got " + keyBytes.Length + " bytes", "keyBytes");
 }
Beispiel #30
0
 /// <summary>
 /// Constructs a BlockChain that has no wallet at all. This is helpful when you don't actually care about sending
 /// and receiving coins but rather, just want to explore the network data structures.
 /// </summary>
 /// <exception cref="BlockStoreException"/>
 public BlockChain(NetworkParameters @params, IBlockStore blockStore)
     : this(@params, new List <Wallet>(), blockStore)
 {
 }
 /// <summary>
 /// Creates a transaction from the given serialized bytes, eg, from a block or a tx network message.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public Transaction(NetworkParameters @params, byte[] payloadBytes)
     : base(@params, payloadBytes, 0)
 {
 }
Beispiel #32
0
 /// <exception cref="IOException"/>
 /// <exception cref="ProtocolException"/>
 public NetworkConnection(IPAddress inetAddress, NetworkParameters @params, uint bestHeight, int connectTimeout)
     : this(new PeerAddress(inetAddress), @params, bestHeight, connectTimeout)
 {
 }
        /// <summary>
        /// Creates a PeerGroup with the given parameters. The connectionDelayMillis parameter controls how long the
        /// PeerGroup will wait between attempts to connect to nodes or read from any added peer discovery sources.
        /// </summary>
        public PeerGroup(IBlockStore blockStore, NetworkParameters @params, BlockChain chain, int connectionDelayMillis)
        {
            _blockStore = blockStore;
            _params = @params;
            _chain = chain;
            _connectionDelayMillis = connectionDelayMillis;

            _inactives = new LinkedBlockingQueue<PeerAddress>();
            _peers = new SynchronizedHashSet<Peer>();
            _peerDiscoverers = new SynchronizedHashSet<IPeerDiscovery>();
            _peerPool = new ThreadPoolExecutor(_coreThreads, _defaultConnections,
                                               TimeSpan.FromSeconds(_threadKeepAliveSeconds),
                                               new LinkedBlockingQueue<IRunnable>(1),
                                               new PeerGroupThreadFactory());
        }
 /// <summary>
 /// Construct an address from parameters and the hash160 form.
 /// </summary>
 /// <remarks>
 /// Example:<p/>
 /// <pre>new Address(NetworkParameters.prodNet(), Hex.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));</pre>
 /// </remarks>
 public Address(NetworkParameters @params, byte[] hash160)
     : base(@params.AddressHeader, hash160)
 {
     if (hash160.Length != 20) // 160 = 8 * 20
         throw new ArgumentException("Addresses are 160-bit hashes, so you must provide 20 bytes", "hash160");
 }
 /// <summary>
 /// Exports the private key in the form used by the Satoshi client "dumpprivkey" and "importprivkey" commands. Use
 /// the <see cref="DumpedPrivateKey.ToString"/> method to get the string.
 /// </summary>
 /// <param name="params">The network this key is intended for use on.</param>
 /// <returns>Private key bytes as a <see cref="DumpedPrivateKey"/>.</returns>
 public DumpedPrivateKey GetPrivateKeyEncoded(NetworkParameters @params)
 {
     return new DumpedPrivateKey(@params, GetPrivKeyBytes());
 }
 public InventoryMessage(NetworkParameters @params)
     : base(@params)
 {
 }
 /// <summary>
 /// Constructs a BitcoinSerializer with the given behavior.
 /// </summary>
 /// <param name="params">MetworkParams used to create Messages instances and determining packetMagic</param>
 /// <param name="usesChecksumming">Set to true if checksums should be included and expected in headers</param>
 public BitcoinSerializer(NetworkParameters @params, bool usesChecksumming)
 {
     _params = @params;
     _usesChecksumming = usesChecksumming;
 }
Beispiel #38
0
 public GetBlocksMessage(NetworkParameters @params, IList <Sha256Hash> locator, Sha256Hash stopHash)
     : base(@params)
 {
     _locator  = locator;
     _stopHash = stopHash;
 }