Beispiel #1
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
            });
        }
Beispiel #2
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
            });
        }