Exemplo n.º 1
0
        public byte[] UserAgentBytes;       // varstring

        public BitcoinVersionPayload(BitcoinVarString userAgent, byte[] nonceBytes, BitcoinNetworkAddressPayload sender,
                                     BitcoinNetworkAddressPayload receiver)
        {
            this.ProtocolVersion      = ChatClientConfiguration.ProtocolVersion;
            this.ProtocolVersionBytes = BitConverter.GetBytes(ChatClientConfiguration.ProtocolVersion);

            this.Services      = ChatClientConfiguration.OwnPeerServices;
            this.ServicesBytes = BitConverter.GetBytes((ulong)this.Services);

            this.TimeStamp      = DateTimeOffset.UtcNow;
            this.TimestampBytes = BitConverter.GetBytes((ulong)this.TimeStamp.ToUnixTimeSeconds());

            this.Sender             = sender;
            this.SenderAddressBytes = sender.SerializeForVersion();

            this.Receiver             = receiver;
            this.ReceiverAddressBytes = receiver.SerializeForVersion();

            this.NonceBytes = nonceBytes;

            this.UserAgent      = userAgent;
            this.UserAgentBytes = userAgent.VarStringBytes;

            this.StartHeightBytes = new byte[4];
            this.StartHeight      = BitConverter.ToUInt32(this.StartHeightBytes, 0);
            this.RelayBytes       = new byte[1];
            this.RelayBytes[0]    = 1;
        }
Exemplo n.º 2
0
        public BitcoinAddrPayload(byte[] serialized)
        {
            this.Addresses = new List <BitcoinNetworkAddressPayload>();
            this.AddrCount = new BitcoinVarInt(serialized);
            var currentOffset = this.AddrCount.SerializedLength;

            for (var i = currentOffset; i < 30 * (int)this.AddrCount.Value; i += 30)
            {
                var addr = new BitcoinNetworkAddressPayload(serialized, i, true);
                this.Addresses.Add(addr);
            }
        }
Exemplo n.º 3
0
        public static Peer ToPeer(this BitcoinNetworkAddressPayload payload)
        {
            var p = new Peer
            {
                Id           = CreatePeerId(payload.IPAddress, payload.Port),
                IPAddress    = payload.IPAddress,
                ProtocolPort = payload.Port,
                LastSeen     = payload.Timestamp.DateTime,
                PeerServices = payload.PeerServices,
                LastError    = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc)
            };

            return(p);
        }
Exemplo n.º 4
0
        public BitcoinVersionPayload(byte[] serialized)
        {
            this.ProtocolVersionBytes = new byte[4];
            Buffer.BlockCopy(serialized, 0, this.ProtocolVersionBytes, 0, 4);
            this.ProtocolVersion = BitConverter.ToUInt32(this.ProtocolVersionBytes, 0);

            this.ServicesBytes = new byte[8];
            Buffer.BlockCopy(serialized, 4, this.ServicesBytes, 0, 8);
            this.Services = (PeerServices)BitConverter.ToUInt64(this.ServicesBytes, 0);

            this.TimestampBytes = new byte[8];
            Buffer.BlockCopy(serialized, 12, this.TimestampBytes, 0, 8);
            this.TimeStamp = DateTimeOffset.FromUnixTimeSeconds((long)BitConverter.ToUInt64(this.TimestampBytes, 0));

            this.ReceiverAddressBytes = new byte[26];
            Buffer.BlockCopy(serialized, 20, this.ReceiverAddressBytes, 0, 26);
            this.Receiver = new BitcoinNetworkAddressPayload(this.ReceiverAddressBytes, 0, false);

            this.SenderAddressBytes = new byte[26];
            Buffer.BlockCopy(serialized, 46, this.SenderAddressBytes, 0, 26);
            this.Sender = new BitcoinNetworkAddressPayload(this.SenderAddressBytes, 0, false);

            this.NonceBytes = new byte[8];
            Buffer.BlockCopy(serialized, 72, this.NonceBytes, 0, 8);

            this.UserAgent      = new BitcoinVarString(serialized, 80);
            this.UserAgentBytes = this.UserAgent.VarStringBytes;

            this.StartHeightBytes = new byte[4];
            Buffer.BlockCopy(serialized, 80 + this.UserAgent.SerializedLength, this.StartHeightBytes, 0, 4);
            this.StartHeight = BitConverter.ToUInt32(this.StartHeightBytes, 0);


            this.RelayBytes    = new byte[1];
            this.RelayBytes[0] = serialized[80 + this.UserAgent.SerializedLength + 4];
        }
Exemplo n.º 5
0
        async Task AddToAddressBookIfNotExistsAsync(BitcoinNetworkAddressPayload receivedAddress)
        {
            var peer = receivedAddress.ToPeer();

            await this.peerRepository.AddIfNotExistsAsync(peer);
        }