예제 #1
0
        private Message MakeMessage(string command, byte[] payloadBytes, P2PNetworkParameters netParams)
        {
            // We use an if ladder rather than reflection because reflection can be slow on some platforms.
            if (command.Equals("version"))
            {
                return(new VersionMessage(payloadBytes, netParams));
            }
            if (command.Equals("verack"))
            {
                return(new VersionAck(payloadBytes, netParams));
            }
            if (command.Equals("reject"))
            {
                return(new RejectMessage(payloadBytes, netParams));
            }
            if (command.Equals("ping"))
            {
                return(new Ping(payloadBytes, netParams));
            }
            if (command.Equals("pong"))
            {
                return(new Pong(payloadBytes, netParams));
            }
            if (command.Equals("inv"))
            {
                return(new InventoryMessage(payloadBytes, netParams));
            }
            if (command.Equals("addr"))
            {
                return(new AddressMessage(payloadBytes, netParams));
            }
            if (command.Equals("getaddr"))
            {
                return(new GetAddresses(payloadBytes, netParams));
            }
            if (command.Equals("block"))
            {
                return(new BlockMessage(payloadBytes, netParams));
            }            /*
                          * if (command.Equals("getdata"))
                          * {
                          *     return new GetDataMessage(_params, payloadBytes);
                          * }
                          * if (command.Equals("tx"))
                          * {
                          *     return new TransactionMessage(_params, payloadBytes);
                          * }
                          */

            throw new Exception("No support for deserializing message with name " + command);
        }
예제 #2
0
        public DatabaseConnection(P2PNetworkParameters netParams)
        {
            _networkParameters = netParams;

            if (_networkParameters.IsTestNet)
            {
                _connectionString = @"Data Source = (localdb)\ProjectsV12; Initial Catalog = Blox.Net TestNet DB; Integrated Security = True; Connect Timeout = 30; Encrypt = False; TrustServerCertificate = False";
            }
            else
            {
                _connectionString = @"Data Source = (localdb)\ProjectsV12;Initial Catalog = Blox.NET DB; Integrated Security = True; Connect Timeout = 30; Encrypt=False;TrustServerCertificate=False";
            }

            _sqlConnectionObj = new SqlConnection(_connectionString);
        }
예제 #3
0
 public VersionMessage(IPAddress remoteIpAddress, ushort remotePort, Socket sock, uint newBestHeight, uint remoteClientVersion, P2PNetworkParameters netParams, ulong remoteServices = (ulong)P2PNetworkParameters.NODE_NETWORK.FULL_NODE) : base(netParams)
 {
     _localServices    = P2PNetParameters.Services;
     _time             = P2PConnectionManager.GetUTCNowWithOffset();
     _myAddr           = new PeerAddress(IPAddress.Loopback, Convert.ToUInt16(((IPEndPoint)sock.LocalEndPoint).Port), netParams.Services, P2PNetParameters, true);
     _theirAddr        = new PeerAddress(remoteIpAddress, remotePort, remoteServices, P2PNetParameters, true);
     _nonce            = P2PNetworkParameters.VersionConnectNonce;
     _userAgent        = P2PNetworkParameters.UserAgentString;
     _startBlockHeight = newBestHeight;
     _relay            = P2PNetParameters.Relay;
 }
예제 #4
0
 protected ListMessage(P2PNetworkParameters netParams) : base(netParams)
 {
     _items = new List <InventoryItem>();
 }
예제 #5
0
 // this is needed by the BitcoinSerializer
 public VersionAck(byte[] payload, P2PNetworkParameters netParams) : base(payload, 0, true, netParams)
 {
 }
예제 #6
0
 public Pong(ulong nonce, P2PNetworkParameters netParams) : base(netParams)
 {
     _nonce = nonce;
 }
예제 #7
0
 internal AddressMessage(List <PeerAddress> payloadAddresses, P2PNetworkParameters netParams) : base(netParams)
 {
     Addresses = payloadAddresses;
 }
예제 #8
0
 public InventoryMessage(P2PNetworkParameters netParams) : base(netParams)
 {
 }
예제 #9
0
 /// <summary>
 /// Deserializes an input message. This is usually part of a transaction message.
 /// </summary>
 /// <exception cref="BitCoinSharp.ProtocolException" />
 public TransactionInput(TransactionMessage parentTransaction, byte[] payload, int offset, P2PNetworkParameters netParams) : base(payload, offset, true, netParams)
 {
     ParentTransaction = parentTransaction;
 }
예제 #10
0
 /// <summary>
 /// Creates an UNSIGNED input that links to the given output
 /// </summary>
 internal TransactionInput(TransactionMessage parentTransaction, Byte[] outpointHash, uint outpointIndex, P2PNetworkParameters netParams) : base(netParams)
 {
     _outpointHash     = outpointHash;
     _outpointIndex    = outpointIndex;
     ScriptBytes       = EmptyArray;
     _sequence         = uint.MaxValue;
     ParentTransaction = parentTransaction;
 }
예제 #11
0
 /// <summary>
 /// Used only in creation of the genesis block.
 /// </summary>
 internal TransactionInput(TransactionMessage parentTransaction, byte[] scriptBytes, P2PNetworkParameters netParams) : base(netParams)
 {
     ScriptBytes       = scriptBytes;
     _sequence         = uint.MaxValue;
     ParentTransaction = parentTransaction;
 }
예제 #12
0
 /// <summary>
 /// Constructs a block object from the BitCoin wire format.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public BlockMessage(byte[] payloadBytes, P2PNetworkParameters netParams) : base(payloadBytes, 0, true, netParams)
 {
 }
예제 #13
0
 /// <summary>
 /// Creates a transaction by reading payload starting from offset bytes in. Length of a transaction is fixed.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public TransactionMessage(byte[] payload, int offset, P2PNetworkParameters netParams)
     : base(payload, offset, true, netParams)
 {
     // inputs/outputs will be created in parse()
 }
예제 #14
0
 /// <summary>
 /// Creates a transaction from the given serialized bytes, eg, from a block or a tx network message.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public TransactionMessage(byte[] payloadBytes, P2PNetworkParameters netParams) : base(payloadBytes, 0, true, netParams)
 {
 }
예제 #15
0
 public GetAddresses(P2PNetworkParameters netParams) : base(new byte[] { }, 0, true, netParams)
 {
 }
예제 #16
0
 // this is needed by the BitcoinSerializer
 public GetAddresses(byte[] payload, P2PNetworkParameters netParams) : base(payload, 0, true, netParams)
 {
 }
예제 #17
0
 public NullMessage(P2PNetworkParameters netParams) : base(netParams)
 {
 }
예제 #18
0
 public InventoryMessage(byte[] bytes, P2PNetworkParameters netParams) : base(bytes, netParams)
 {
 }
예제 #19
0
 public Ping(P2PNetworkParameters netParams) : base(netParams)
 {
     _nonce = Convert.ToUInt64(DateTime.UtcNow.Ticks);
 }
예제 #20
0
 internal AddressMessage(byte[] payload, P2PNetworkParameters netParams) : base(payload, 0, true, netParams)
 {
 }
예제 #21
0
        /// <summary>
        /// Reads a message from the given InputStream and returns it.
        /// </summary>
        /// <exception cref="IOException"/>
        public Message Deserialize(Stream @in, P2PNetworkParameters netParams)
        {
            // A BitCoin protocol message has the following format.
            //
            //   - 4 byte magic number: 0xfabfb5da for the testnet or
            //                          0xf9beb4d9 for production
            //   - 12 byte command in ASCII
            //   - 4 byte payload size
            //   - 4 byte checksum
            //   - Payload data
            //
            // The checksum is the first 4 bytes of a SHA256 hash of the message payload. It isn't
            // present for all messages, notably, the first one on a connection.
            //
            // Satoshi's implementation ignores garbage before the magic header bytes. We have to do the same because
            // sometimes it sends us stuff that isn't part of any message.
            SeekPastMagicBytes(@in, netParams.PacketMagic);
            // Now read in the header.
            var header     = new byte[_commandLen + 8];
            var readCursor = 0;

            while (readCursor < header.Length)
            {
                var bytesRead = @in.Read(header, readCursor, header.Length - readCursor);
                if (bytesRead == -1)
                {
                    // There's no more data to read.
                    throw new IOException("Socket is disconnected");
                }
                readCursor += bytesRead;
            }

            var cursor = 0;

            // The command is a NULL terminated string, unless the command fills all twelve bytes
            // in which case the termination is implicit.
            var mark = cursor;

            for (; header[cursor] != 0 && cursor - mark < _commandLen; cursor++)
            {
            }
            var commandBytes = new byte[cursor - mark];

            Array.Copy(header, mark, commandBytes, 0, commandBytes.Length);
            for (var i = 0; i < commandBytes.Length; i++)
            {
                // Emulate ASCII by replacing extended characters with question marks.
                if (commandBytes[i] >= 0x80)
                {
                    commandBytes[i] = 0x3F;
                }
            }
            var command = Encoding.UTF8.GetString(commandBytes, 0, commandBytes.Length);

            cursor = mark + _commandLen;

            var size = Utilities.ReadUint32(header, cursor);

            cursor += 4;

            if (size > Message.MaxSize)
            {
                throw new Exception("Message size too large: " + size);
            }

            // Old clients don't send the checksum.
            var checksum = new byte[4];

            // Note that the size read above includes the checksum bytes.
            Array.Copy(header, cursor, checksum, 0, 4);

            // Now try to read the whole message.
            readCursor = 0;
            var payloadBytes = new byte[size];

            while (readCursor < payloadBytes.Length - 1)
            {
                var bytesRead = @in.Read(payloadBytes, readCursor, (int)(size - readCursor));
                if (bytesRead == -1)
                {
                    throw new IOException("Socket is disconnected");
                }
                readCursor += bytesRead;
            }

            // Verify the checksum.

            var hash = Utilities.DoubleDigest(payloadBytes);

            if (checksum[0] != hash[0] || checksum[1] != hash[1] || checksum[2] != hash[2] || checksum[3] != hash[3])
            {
                throw new Exception("Checksum failed to verify, actual " + Utilities.BytesToHexString(hash) + " vs " + Utilities.BytesToHexString(checksum));
            }


            try
            {
                return(MakeMessage(command, payloadBytes, netParams));
            }
            catch (Exception e)
            {
                throw new Exception("Error deserializing message " + Utilities.BytesToHexString(payloadBytes) + Environment.NewLine + e.Message, e);
            }
        }
예제 #22
0
 public Pong(byte[] payload, P2PNetworkParameters netParams) : base(payload, 0, true, netParams)
 {
 }
예제 #23
0
 internal Message(P2PNetworkParameters netParams)
 {
     _networkParameters = netParams;
     _packetMagic       = _networkParameters.PacketMagic;
     _protocolVersion   = _networkParameters.ClientVersion;
 }
예제 #24
0
 /// <summary>
 /// Construct a peer address from a serialized payload.
 /// </summary>
 public PeerAddress(byte[] payload, int offset, bool isInVersionMessage, P2PNetworkParameters netParams) : base(payload, offset, false, netParams)
 {
     _isInVersionMessage = isInVersionMessage;
     Parse();
 }
예제 #25
0
 public VersionMessage(byte[] msg, P2PNetworkParameters netParams)
     : base(msg, 0, true, netParams)
 {
 }
예제 #26
0
 public VersionAck(P2PNetworkParameters netParams) : base(new byte[] { }, 0, true, netParams)
 {
 }
예제 #27
0
 protected ListMessage(byte[] bytes, P2PNetworkParameters netParams) : base(bytes, 0, true, netParams)
 {
 }