예제 #1
0
        public void Test()
        {
            byte[] inBytes = new byte[]
                             {
                                 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x04, 0xFF, 0xFF, 0x00, 0x1D, 0x01,
                                 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0xF2, 0x05, 0x2A, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41,
                                 0x04, 0x96, 0xB5, 0x38, 0xE8, 0x53, 0x51, 0x9C, 0x72, 0x6A, 0x2C, 0x91, 0xE6, 0x1E, 0xC1, 0x16,
                                 0x00, 0xAE, 0x13, 0x90, 0x81, 0x3A, 0x62, 0x7C, 0x66, 0xFB, 0x8B, 0xE7, 0x94, 0x7B, 0xE6, 0x3C,
                                 0x52, 0xDA, 0x75, 0x89, 0x37, 0x95, 0x15, 0xD4, 0xE0, 0xA6, 0x04, 0xF8, 0x14, 0x17, 0x81, 0xE6,
                                 0x22, 0x94, 0x72, 0x11, 0x66, 0xBF, 0x62, 0x1E, 0x73, 0xA8, 0x2C, 0xBF, 0x23, 0x42, 0xC8, 0x58,
                                 0xEE, 0xAC, 0x00, 0x00, 0x00, 0x00,
                             };

            TxMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = TxMessage.Read(reader);
            }

            Assert.That(message.Transaction.Inputs.Length, Is.EqualTo(1));
            Assert.That(message.Transaction.Outputs.Length, Is.EqualTo(1));
            Assert.That(message.Transaction.Outputs[0].Value, Is.EqualTo(5000000000));

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
        public void Test()
        {
            byte[] inBytes = new byte[]
                           {
                               0x02,
                               0x01, 0x00, 0x00, 0x00,
                               0x67, 0xB8, 0x12, 0xE7, 0x09, 0x06, 0xB0, 0x33, 0x4C, 0x01, 0xED, 0x03, 0x5B, 0x50, 0xDB, 0x68,
                               0xCD, 0xF3, 0xCF, 0x67, 0x20, 0xBD, 0xB8, 0xBF, 0xF5, 0x31, 0x34, 0xE7, 0xD6, 0x94, 0x6D, 0xA4,
                               0x01, 0x00, 0x00, 0x00,
                               0xE4, 0x99, 0x3F, 0xAE, 0xCF, 0xC8, 0xFA, 0x6D, 0x1A, 0x75, 0x63, 0xE7, 0xA6, 0xCF, 0x1F, 0xC4,
                               0xFA, 0x58, 0x5A, 0x5C, 0xC3, 0x65, 0x1F, 0x9F, 0x0B, 0xDD, 0xDA, 0xC0, 0x8F, 0x1F, 0xB9, 0x80
                           };

            InvMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = InvMessage.Read(reader);
            }

            Assert.That(message.Inventory.Count, Is.EqualTo(2));
            Assert.That(message.Inventory[0].Type, Is.EqualTo(InventoryVectorType.MsgTx));
            Assert.That(message.Inventory[1].Type, Is.EqualTo(InventoryVectorType.MsgTx));
            Assert.That(message.Inventory[0].Hash.Length, Is.EqualTo(32));
            Assert.That(message.Inventory[1].Hash.Length, Is.EqualTo(32));

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
        public void Test()
        {
            byte[] inBytes = new byte[]
            {
                // type of message rejected: 'test'
                0x04, 0x74, 0x65, 0x73, 0x74,
                // reason: invalid
                0x10,
                // reason text: 'Unknown command.'
                0x10, 0x55, 0x6E, 0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0x20, 0x63, 0x6F, 0x6D, 0x6D, 0x61, 0x6E, 0x64, 0x2E
            };

            RejectMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = RejectMessage.Read(reader);
            }

            Assert.That(message.RejectedCommand, Is.EqualTo("test"));
            Assert.That(message.Reason, Is.EqualTo(RejectMessage.RejectReason.Invalid));
            Assert.That(message.ReasonText, Is.EqualTo("Unknown command."));
            Assert.That(message.Data, Is.Null);

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
        public void Test()
        {
            byte[] inBytes = new byte[]
                             {
                                 0x01,
                                 0x03, 0x00, 0x00, 0x00,
                                 0x48, 0x60, 0xEB, 0x18, 0xBF, 0x1B, 0x16, 0x20, 0xE3, 0x7E, 0x94, 0x90, 0xFC, 0x8A, 0x42, 0x75,
                                 0x14, 0x41, 0x6F, 0xD7, 0x51, 0x59, 0xAB, 0x86, 0x68, 0x8E, 0x9A, 0x83, 0x00, 0x00, 0x00, 0x00
                             };

            GetDataMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = GetDataMessage.Read(reader);
            }

            Assert.That(message.Inventory.Count, Is.EqualTo(1));
            Assert.That(message.Inventory[0].Type, Is.EqualTo(InventoryVectorType.MsgFilteredBlock));
            Assert.That(message.Inventory[0].Hash, Is.EqualTo(new byte[]
                                                              {
                                                                  0x48, 0x60, 0xEB, 0x18, 0xBF, 0x1B, 0x16, 0x20, 0xE3, 0x7E, 0x94, 0x90, 0xFC, 0x8A, 0x42, 0x75,
                                                                  0x14, 0x41, 0x6F, 0xD7, 0x51, 0x59, 0xAB, 0x86, 0x68, 0x8E, 0x9A, 0x83, 0x00, 0x00, 0x00, 0x00
                                                              }));

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
예제 #5
0
 public static NetAddr Read(BitcoinStreamReader reader)
 {
     uint timestamp = reader.ReadUInt32();
     ulong services = reader.ReadUInt64();
     IPAddress address = reader.ReadAddress();
     ushort port = reader.ReadUInt16BigEndian();
     return new NetAddr(timestamp, services, address, port);
 }
예제 #6
0
 public static Tx Read(BitcoinStreamReader reader)
 {
     uint version = reader.ReadUInt32();
     TxIn[] inputs = reader.ReadArray(1024*1024, TxIn.Read);
     TxOut[] outputs = reader.ReadArray(1024*1024, TxOut.Read);
     uint lockTime = reader.ReadUInt32();
     return new Tx(version, inputs, outputs, lockTime);
 }
예제 #7
0
        public static BlockMessage Read(BitcoinStreamReader reader)
        {
            BlockHeader blockHeader = BlockHeader.Read(reader);

            //todo: see if there is actual limitation for this field
            Tx[] transactions = reader.ReadArray(1024 * 1024, Tx.Read);

            return new BlockMessage(blockHeader, transactions);
        }
예제 #8
0
 public static TxOut Read(BitcoinStreamReader reader)
 {
     ulong value = reader.ReadUInt64();
     ulong pubkeyScriptLength = reader.ReadUInt64Compact();
     if (pubkeyScriptLength > 10000)
     {
         //todo: handle correctly
         throw new Exception("Pubkey script is too long.");
     }
     byte[] pubkeyScript = reader.ReadBytes((int) pubkeyScriptLength);
     return new TxOut(value, pubkeyScript);
 }
        public static IBitcoinMessage Parse(BitcoinMessage message)
        {
            Func<BitcoinStreamReader, IBitcoinMessage> readMethod;
            if (!messageReadMethods.TryGetValue(message.Command, out readMethod))
            {
                return null;
            }

            MemoryStream mem = new MemoryStream(message.Payload);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(mem))
            {
                return readMethod(reader);
            }
        }
예제 #10
0
        public static TxIn Read(BitcoinStreamReader reader)
        {
            TxOutPoint previousOutput = TxOutPoint.Read(reader);
            ulong signatureScriptLength = reader.ReadUInt64Compact();
            if (signatureScriptLength > 1024*1024) //todo: see if there is actual limitation for this field
            {
                //todo: handle correctly
                throw new Exception("Too many transactions.");
            }
            byte[] signatureScript = reader.ReadBytes((int) signatureScriptLength);
            uint sequence = reader.ReadUInt32();

            return new TxIn(previousOutput, signatureScript, sequence);
        }
예제 #11
0
 public static HeadersMessage Read(BitcoinStreamReader reader)
 {
     BlockHeader[] headers = reader.ReadArray(MaxHeadersPerMessage, r =>
     {
         BlockHeader header = BlockHeader.Read(r);
         ulong txCount = r.ReadUInt64Compact();
         if (txCount != 0)
         {
             throw new BitcoinNetworkException($"Invalid transaction count in {Command} message: {txCount}.");
         }
         return header;
     });
     return new HeadersMessage(headers);
 }
        public void Test()
        {
            byte[] inBytes = new byte[0];

            VerAckMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = VerAckMessage.Read(reader);
            }

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
예제 #13
0
        public static GetDataMessage Read(BitcoinStreamReader reader)
        {
            ulong count = reader.ReadUInt64Compact();
            if (count > 50000)
            {
                //todo: handle correctly
                throw new Exception("Too many inventory vectors.");
            }

            InventoryVector[] inventory = new InventoryVector[count];
            for (ulong i = 0; i < count; i++)
            {
                inventory[i] = InventoryVector.Read(reader);
            }

            return new GetDataMessage(inventory);
        }
        public void Test()
        {
            byte[] inBytes = new byte[]
                             {
                                 0x01, 0x00, 0x00, 0x00,
                                 0x82, 0xbb, 0x86, 0x9c, 0xf3, 0xa7, 0x93, 0x43, 0x2a, 0x66, 0xe8, 0x26, 0xe0, 0x5a, 0x6f, 0xc3,
                                 0x74, 0x69, 0xf8, 0xef, 0xb7, 0x42, 0x1d, 0xc8, 0x80, 0x67, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
                                 0x7f, 0x16, 0xc5, 0x96, 0x2e, 0x8b, 0xd9, 0x63, 0x65, 0x9c, 0x79, 0x3c, 0xe3, 0x70, 0xd9, 0x5f,
                                 0x09, 0x3b, 0xc7, 0xe3, 0x67, 0x11, 0x7b, 0x3c, 0x30, 0xc1, 0xf8, 0xfd, 0xd0, 0xd9, 0x72, 0x87,
                                 0x76, 0x38, 0x1b, 0x4d,
                                 0x4c, 0x86, 0x04, 0x1b,
                                 0x55, 0x4b, 0x85, 0x29,
                                 0x07, 0x00, 0x00, 0x00,
                                 0x04,
                                 0x36, 0x12, 0x26, 0x26, 0x24, 0x04, 0x7e, 0xe8, 0x76, 0x60, 0xbe, 0x1a, 0x70, 0x75, 0x19, 0xa4,
                                 0x43, 0xb1, 0xc1, 0xce, 0x3d, 0x24, 0x8c, 0xbf, 0xc6, 0xc1, 0x58, 0x70, 0xf6, 0xc5, 0xda, 0xa2,
                                 0x01, 0x9f, 0x5b, 0x01, 0xd4, 0x19, 0x5e, 0xcb, 0xc9, 0x39, 0x8f, 0xbf, 0x3c, 0x3b, 0x1f, 0xa9,
                                 0xbb, 0x31, 0x83, 0x30, 0x1d, 0x7a, 0x1f, 0xb3, 0xbd, 0x17, 0x4f, 0xcf, 0xa4, 0x0a, 0x2b, 0x65,
                                 0x41, 0xed, 0x70, 0x55, 0x1d, 0xd7, 0xe8, 0x41, 0x88, 0x3a, 0xb8, 0xf0, 0xb1, 0x6b, 0xf0, 0x41,
                                 0x76, 0xb7, 0xd1, 0x48, 0x0e, 0x4f, 0x0a, 0xf9, 0xf3, 0xd4, 0xc3, 0x59, 0x57, 0x68, 0xd0, 0x68,
                                 0x20, 0xd2, 0xa7, 0xbc, 0x99, 0x49, 0x87, 0x30, 0x2e, 0x5b, 0x1a, 0xc8, 0x0f, 0xc4, 0x25, 0xfe,
                                 0x25, 0xf8, 0xb6, 0x31, 0x69, 0xea, 0x78, 0xe6, 0x8f, 0xba, 0xae, 0xfa, 0x59, 0x37, 0x9b, 0xbf,
                                 0x01,
                                 0x1d
                             };

            MerkleBlockMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = MerkleBlockMessage.Read(reader);
            }

            Assert.That(message.BlockHeader.Version, Is.EqualTo(1));
            Assert.That(message.BlockHeader.Timestamp, Is.EqualTo(1293629558));
            Assert.That(message.BlockHeader.NBits, Is.EqualTo(0x1B04864C));
            Assert.That(message.BlockHeader.Nonce, Is.EqualTo(0x29854B55));
            Assert.That(message.TotalTransactions, Is.EqualTo(7));
            Assert.That(message.Hashes.Count, Is.EqualTo(4));
            Assert.That(message.Flags, Is.EqualTo(new byte[] {0x1D}));

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
        public void Test()
        {
            byte[] inBytes = new byte[]
            {
                // 70002 (protocol version 70002)
                0x72, 0x11, 0x01, 0x00,
                // 1 (NODE_NETWORK services)
                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                // Tue Dec 18 10:12:33 PST 2012
                0x11, 0xB2, 0xD0, 0x50, 0x00, 0x00, 0x00, 0x00,
                // Recipient address info - see Network Address,
                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                // Sender address info - see Network Address
                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                // Node ID
                0x3B, 0x2E, 0xB3, 0x5D, 0x8C, 0xE6, 0x17, 0x65,
                // "/Satoshi:0.9.0/" sub-version string (string is 15 bytes long)
                0x0F, 0x2F, 0x53, 0x61, 0x74, 0x6F, 0x73, 0x68, 0x69, 0x3A, 0x30, 0x2E, 0x39, 0x2E, 0x30, 0x2F,
                // Last block sending node has is block #212672
                0xC0, 0x3E, 0x03, 0x00,
                // relay=true
                0x01
            };

            VersionMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = VersionMessage.Read(reader);
            }

            Assert.That(message.ProtocolVersion, Is.EqualTo(70002));
            Assert.That(message.UserAgent, Is.EqualTo("/Satoshi:0.9.0/"));
            Assert.That(message.Services, Is.EqualTo(1));
            Assert.That(message.StartHeight, Is.EqualTo(212672));
            Assert.That(message.AcceptBroadcasts, Is.True);

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
        public void Test()
        {
            byte[] inBytes = new byte[]
            {
                0xEF, 0xCD, 0xAB, 0x90, 0x78, 0x56, 0x34, 0x12
            };

            PingMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = PingMessage.Read(reader);
            }

            Assert.That(message.Nonce, Is.EqualTo(0x1234567890ABCDEF));

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
        public static GetBlocksMessage Read(BitcoinStreamReader reader)
        {
            int protocolVersion = reader.ReadInt32();

            ulong count = reader.ReadUInt64Compact();
            if (count > 10000)
            {
                //todo: handle correctly
                throw new Exception("Too many locator hashes.");
            }

            byte[][] locatorHashes = new byte[count][];
            for (ulong i = 0; i < count; i++)
            {
                locatorHashes[i] = reader.ReadBytes(32);
            }

            byte[] hashStop = reader.ReadBytes(32);

            return new GetBlocksMessage(protocolVersion, locatorHashes, hashStop);
        }
        public void Test()
        {
            byte[] inBytes = new byte[]
                             {
                                 //block header
                                 0x01, 0x00, 0x00, 0x00, 0x6F, 0xE2, 0x8C, 0x0A, 0xB6, 0xF1, 0xB3, 0x72, 0xC1, 0xA6, 0xA2, 0x46,
                                 0xAE, 0x63, 0xF7, 0x4F, 0x93, 0x1E, 0x83, 0x65, 0xE1, 0x5A, 0x08, 0x9C, 0x68, 0xD6, 0x19, 0x00,
                                 0x00, 0x00, 0x00, 0x00, 0x98, 0x20, 0x51, 0xFD, 0x1E, 0x4B, 0xA7, 0x44, 0xBB, 0xBE, 0x68, 0x0E,
                                 0x1F, 0xEE, 0x14, 0x67, 0x7B, 0xA1, 0xA3, 0xC3, 0x54, 0x0B, 0xF7, 0xB1, 0xCD, 0xB6, 0x06, 0xE8,
                                 0x57, 0x23, 0x3E, 0x0E, 0x61, 0xBC, 0x66, 0x49, 0xFF, 0xFF, 0x00, 0x1D, 0x01, 0xE3, 0x62, 0x99,
                                 //transactions
                                 0x01,
                                 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x04, 0xFF, 0xFF, 0x00, 0x1D, 0x01,
                                 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0xF2, 0x05, 0x2A, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41,
                                 0x04, 0x96, 0xB5, 0x38, 0xE8, 0x53, 0x51, 0x9C, 0x72, 0x6A, 0x2C, 0x91, 0xE6, 0x1E, 0xC1, 0x16,
                                 0x00, 0xAE, 0x13, 0x90, 0x81, 0x3A, 0x62, 0x7C, 0x66, 0xFB, 0x8B, 0xE7, 0x94, 0x7B, 0xE6, 0x3C,
                                 0x52, 0xDA, 0x75, 0x89, 0x37, 0x95, 0x15, 0xD4, 0xE0, 0xA6, 0x04, 0xF8, 0x14, 0x17, 0x81, 0xE6,
                                 0x22, 0x94, 0x72, 0x11, 0x66, 0xBF, 0x62, 0x1E, 0x73, 0xA8, 0x2C, 0xBF, 0x23, 0x42, 0xC8, 0x58,
                                 0xEE, 0xAC, 0x00, 0x00, 0x00, 0x00
                             };

            BlockMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = BlockMessage.Read(reader);
            }

            Assert.That(message.BlockHeader.Version, Is.EqualTo(1));
            Assert.That(message.Transactions.Length, Is.EqualTo(1));
            Assert.That(message.Transactions[0].Inputs.Length, Is.EqualTo(1));
            Assert.That(message.Transactions[0].Outputs.Length, Is.EqualTo(1));
            Assert.That(message.Transactions[0].Outputs[0].Value, Is.EqualTo(5000000000));

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
        public void Test()
        {
            byte[] inBytes = new byte[]
            {
                //Header count: 1
                0x01,
                // Block version: 2
                0x02, 0x00, 0x00, 0x00,
                // Hash of previous block's header
                0xB6, 0xFF, 0x0B, 0x1B, 0x16, 0x80, 0xA2, 0x86, 0x2A, 0x30, 0xCA, 0x44, 0xD3, 0x46, 0xD9, 0xE8,
                0x91, 0x0D, 0x33, 0x4B, 0xEB, 0x48, 0xCA, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                // Merkle root
                0x9D, 0x10, 0xAA, 0x52, 0xEE, 0x94, 0x93, 0x86, 0xCA, 0x93, 0x85, 0x69, 0x5F, 0x04, 0xED, 0xE2,
                0x70, 0xDD, 0xA2, 0x08, 0x10, 0xDE, 0xCD, 0x12, 0xBC, 0x9B, 0x04, 0x8A, 0xAA, 0xB3, 0x14, 0x71,
                // Unix time: 1415239972
                0x24, 0xD9, 0x5A, 0x54,
                // Target (nBits)
                0x30, 0xC3, 0x1B, 0x18,
                // Nonce
                0xFE, 0x9F, 0x08, 0x64,
                // Transaction count (0x00)
                0x00
            };

            HeadersMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = HeadersMessage.Read(reader);
            }

            Assert.That(message.Headers.Length, Is.EqualTo(1));
            Assert.That(message.Headers[0].Version, Is.EqualTo(2));
            Assert.That(message.Headers[0].Timestamp, Is.EqualTo(1415239972));
            Assert.That(message.Headers[0].NBits, Is.EqualTo(0x181BC330));

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
        public void Test()
        {
            byte[] inBytes = new byte[]
                             {
                                 0x71, 0x11, 0x01, 0x00,
                                 0x01,
                                 0x6F, 0xE2, 0x8C, 0x0A, 0xB6, 0xF1, 0xB3, 0x72, 0xC1, 0xA6, 0xA2, 0x46, 0xAE, 0x63, 0xF7, 0x4F,
                                 0x93, 0x1E, 0x83, 0x65, 0xE1, 0x5A, 0x08, 0x9C, 0x68, 0xD6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                             };

            GetHeadersMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = GetHeadersMessage.Read(reader);
            }

            Assert.That(message.ProtocolVersion, Is.EqualTo(70001));
            Assert.That(message.LocatorHashes.Count, Is.EqualTo(1));
            Assert.That(message.LocatorHashes[0], Is.EqualTo(new byte[]
                                                             {
                                                                 0x6F, 0xE2, 0x8C, 0x0A, 0xB6, 0xF1, 0xB3, 0x72, 0xC1, 0xA6, 0xA2, 0x46, 0xAE, 0x63, 0xF7, 0x4F,
                                                                 0x93, 0x1E, 0x83, 0x65, 0xE1, 0x5A, 0x08, 0x9C, 0x68, 0xD6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00
                                                             }));
            Assert.That(message.HashStop, Is.EqualTo(new byte[]
                                                     {
                                                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                                                     }));
            Assert.That(message.HashStop, Is.Not.Null);

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
        public void Test()
        {
            byte[] inBytes = new byte[]
            {
                // 1 address in this message
                0x01,
                // Mon Dec 20 21:50:10 EST 2010 (Tue, 21 Dec 2010 02:50:10 GMT)
                0xE2, 0x15, 0x10, 0x4D,
                // 1 (NODE_NETWORK service - see version message)
                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                // IPv4: 10.0.0.1, IPv6: ::ffff:10.0.0.1(IPv4 - mapped IPv6 address)
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x0A, 0x00, 0x00, 0x01,
                // port 8333
                0x20, 0x8D
            };

            AddrMessage message;

            MemoryStream inStream = new MemoryStream(inBytes);
            using (BitcoinStreamReader reader = new BitcoinStreamReader(inStream))
            {
                message = AddrMessage.Read(reader);
            }

            Assert.That(message.AddressList.Length, Is.EqualTo(1));

            NetAddr address = message.AddressList[0];

            Assert.That(UnixTime.ToDateTime(address.Timestamp), Is.EqualTo(new DateTime(2010, 12, 21, 02, 50, 10, DateTimeKind.Utc)));
            Assert.That(address.Services, Is.EqualTo(1));
            Assert.That(address.Address, Is.EqualTo(IpUtils.MapToIPv6(IPAddress.Parse("10.0.0.1"))));
            Assert.That(address.Port, Is.EqualTo(8333));

            byte[] outBytes = BitcoinStreamWriter.GetBytes(message.Write);
            Assert.That(outBytes, Is.EqualTo(inBytes));
        }
        private void Start()
        {
            VersionMessage outVersionMessage = CreateVersionMessage();
            WriteMessage(outVersionMessage);

            BitcoinMessage incVersionMessage = conn.ReadMessage();
            if (incVersionMessage.Command != VersionMessage.Command)
            {
                throw new BitcoinNetworkException("Remote endpoint did not send Version message.");
            }

            VersionMessage incVersionMessageParsed;
            using (BitcoinStreamReader reader = new BitcoinStreamReader(new MemoryStream(incVersionMessage.Payload)))
            {
                //todo: review and handle exceptions
                incVersionMessageParsed = VersionMessage.Read(reader);
            }

            //todo: check minimal peer protocol version

            WriteMessage(new VerAckMessage());

            BitcoinMessage incVerAckMessage = conn.ReadMessage();
            if (incVerAckMessage.Command != VerAckMessage.Command)
            {
                //todo: handle correctly
                throw new BitcoinNetworkException("Remote endpoint did not send VerAck message.");
            }

            peerInfo = new BitcoinPeerInfo(conn.RemoteEndPoint, incVersionMessageParsed);

            running = true;

            threadPool = new BlockingThreadPool(MessageProcessingThreadsCount, MessageProcessingTasksCount);

            listenerThread = new Thread(Listen);
            listenerThread.Name = "Endpoint Listener";
            listenerThread.IsBackground = true; //todo: ??? should it be background?
            listenerThread.Start();
        }
        public static MerkleBlockMessage Read(BitcoinStreamReader reader)
        {
            BlockHeader blockHeader = BlockHeader.Read(reader);
            uint totalTransactions = reader.ReadUInt32();

            ulong hashesCount = reader.ReadUInt64Compact();
            if (hashesCount > 1024*1024) //todo: see if there is actual limitation for this field
            {
                //todo: handle correctly
                throw new Exception("Too many hashes.");
            }

            byte[][] hashes = new byte[hashesCount][];
            for (ulong i = 0; i < hashesCount; i++)
            {
                hashes[i] = reader.ReadBytes(32);
            }

            ulong flagBytesCount = reader.ReadUInt64Compact();
            if (flagBytesCount > 1024*1024) //todo: see if there is actual limitation for this field
            {
                //todo: handle correctly
                throw new Exception("Too many flags.");
            }
            byte[] flags = reader.ReadBytes((int) flagBytesCount);

            return new MerkleBlockMessage(blockHeader, totalTransactions, hashes, flags);
        }
예제 #24
0
 public static AddrMessage Read(BitcoinStreamReader reader)
 {
     NetAddr[] addressList = reader.ReadArray(MaxAddressesPerMessage, NetAddr.Read);
     return new AddrMessage(addressList);
 }
예제 #25
0
 public static PongMessage Read(BitcoinStreamReader reader)
 {
     ulong nonce = reader.ReadUInt64();
     return new PongMessage(nonce);
 }
 public static InventoryVector Read(BitcoinStreamReader reader)
 {
     InventoryVectorType type = (InventoryVectorType) reader.ReadInt32();
     byte[] hash = reader.ReadBytes(32);
     return new InventoryVector(type, hash);
 }
예제 #27
0
        public static RejectMessage Read(BitcoinStreamReader reader)
        {
            string rejectedCommand = reader.ReadText(MaxTextLength);
            byte reasonByte = reader.ReadByte();
            string reasonText = reader.ReadText(MaxTextLength);
            //todo: parse data? length should be provided as parameter?

            RejectReason reason = (RejectReason) reasonByte;

            return new RejectMessage(rejectedCommand, reason, reasonText);
        }
예제 #28
0
 public static BlockHeader GetHeader()
 {
     MemoryStream mem = new MemoryStream(GenesisBlock.Raw);
     using (BitcoinStreamReader reader = new BitcoinStreamReader(mem))
     {
         BlockMessage blockMessage = BlockMessage.Read(reader);
         return blockMessage.BlockHeader;
     }
 }
예제 #29
0
 public static GetAddrMessage Read(BitcoinStreamReader reader)
 {
     return new GetAddrMessage();
 }
예제 #30
0
 public static TxOutPoint Read(BitcoinStreamReader reader)
 {
     byte[] hash = reader.ReadBytes(32);
     int index = reader.ReadInt32();
     return new TxOutPoint(hash, index);
 }