public static PongMessage Parse(byte[] payload)
        {
            var reader = new PayloadReader(payload);
            var nonce  = reader.ReadUInt64();

            reader.ThrowIfNotEndReached();

            return(new PongMessage(nonce));
        }
Esempio n. 2
0
        public static AlertMessage Parse(byte[] payload)
        {
            // Compute checksum
            var checksum = DoubleSHA256.ComputeChecksum(payload);


            // Parse
            var reader = new PayloadReader(payload);

            var alertSize = reader.ReadVarInt();

            var alertFormatVersion = reader.ReadInt32();
            var relayUntil         = reader.ReadDateTimeOffset(longer: true);
            var expiration         = reader.ReadDateTimeOffset(longer: true);
            var alertID            = reader.ReadInt32();

            var cancel0  = reader.ReadInt32();
            var nCancel  = reader.ReadVarInt() + 1;
            var toCancel = new Int32[nCancel];

            toCancel[0] = cancel0;

            for (ulong i = 1; i < nCancel; i++)
            {
                toCancel[i] = reader.ReadInt32();
            }

            var minVersion = reader.ReadInt32();
            var maxVersion = reader.ReadInt32();

            var nSubVersions = reader.ReadVarInt();
            var subVersions  = new string[nSubVersions];

            for (ulong j = 0; j < nSubVersions; j++)
            {
                subVersions[j] = reader.ReadString();
            }

            var priority  = reader.ReadInt32();
            var comment   = reader.ReadString();
            var statusBar = reader.ReadString();
            var reserved  = reader.ReadString();

            var signature = reader.ReadArray(72);

            reader.ThrowIfNotEndReached();

            // Return
            return(new AlertMessage {
                IsFinalAlert = checksum.SequenceEqual(FinalAlertHash),
                StatusBar = statusBar
            });
        }
        public GetBlocksMessage Parse(byte[] payload)
        {
            var reader = new PayloadReader(payload);

            var version = reader.ReadUInt32();
            var count   = reader.ReadVarInt();

            throw new NotImplementedException();

            for (ulong i = 0; i < count; i++)
            {
            }

            reader.ThrowIfNotEndReached();
        }
Esempio n. 4
0
        public static InventoryMessage Parse(byte[] payload)
        {
            var reader = new PayloadReader(payload);

            var count   = reader.ReadVarInt(); // TODO: warn when count > 50,000
            var objects = new InventoryVector[count];

            for (ulong i = 0; i < count; i++)
            {
                objects[i] = reader.ReadInventoryVector();
            }

            reader.ThrowIfNotEndReached();

            return(new InventoryMessage
            {
                Objects = objects
            });
        }
Esempio n. 5
0
        public GetDataMessage Parse(byte[] payload)
        {
            var reader = new PayloadReader(payload);

            var count   = reader.ReadVarInt();
            var objects = new InventoryVector[count];

            for (ulong i = 0; i < count; i++)
            {
                objects[i] = reader.ReadInventoryVector();
            }

            reader.ThrowIfNotEndReached();

            return(new GetDataMessage
            {
                Objects = objects
            });
        }
Esempio n. 6
0
        static readonly byte[] Services = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };  // TODO: Fill in services

        public static VersionMessage Parse(byte[] payload)
        {
            var reader = new PayloadReader(payload);

            var protocolVersion = reader.ReadUInt32();

            reader.Seek(8, SeekOrigin.Current);
            var timestamp = reader.ReadDateTimeOffset(true);

            reader.Seek(8, SeekOrigin.Current);
            var to = reader.ReadIPEndPoint();

            reader.Seek(8, SeekOrigin.Current);
            var from        = reader.ReadIPEndPoint();
            var nonce       = reader.ReadUInt64();
            var userAgent   = reader.ReadString();
            var startHeight = reader.ReadInt32();

            bool relay = true;

            if (reader.Position < payload.Length)
            {
                relay = reader.ReadBool();
            }

            if (reader.Position != payload.Length)
            {
                throw new Exception($"Unparsed data remaining in {nameof(VersionMessage)}");
            }

            return(new VersionMessage(
                       protocolVersion,
                       timestamp,
                       to,
                       from,
                       nonce,
                       userAgent,
                       startHeight,
                       relay
                       ));
        }
        public static AddrMessage Parse(byte[] payload)
        {
            var reader = new PayloadReader(payload);

            var count     = reader.ReadVarInt(); // TODO: Warn when count exceeds 1000, this ought to be a maximum
            var nodeInfos = new NodeInfo[count]; // TODO: Don't allow peer to force us to allocate a lot of memory here

            for (UInt64 i = 0; i < count; i++)
            {
                var timestamp = reader.ReadDateTimeOffset(longer: false); // Clients with protocol version >= 31402 will provide this timestamp
                reader.Seek(8, SeekOrigin.Current);
                var endpoint = reader.ReadIPEndPoint();

                nodeInfos[i] = new NodeInfo(timestamp, endpoint);
            }

            reader.ThrowIfNotEndReached();

            return(new AddrMessage
            {
                NodeInfos = nodeInfos
            });
        }