| 24 | |
| 25 | |
| 26 | class NetworkEnvelope: |
| 27 | |
| 28 | def __init__(self, command, payload, testnet=False): |
| 29 | self.command = command |
| 30 | self.payload = payload |
| 31 | if testnet: |
| 32 | self.magic = TESTNET_NETWORK_MAGIC |
| 33 | else: |
| 34 | self.magic = NETWORK_MAGIC |
| 35 | |
| 36 | def __repr__(self): |
| 37 | return '{}: {}'.format( |
| 38 | self.command.decode('ascii'), |
| 39 | self.payload.hex(), |
| 40 | ) |
| 41 | |
| 42 | @classmethod |
| 43 | def parse(cls, s, testnet=False): |
| 44 | '''Takes a stream and creates a NetworkEnvelope''' |
| 45 | # check the network magic |
| 46 | magic = s.read(4) |
| 47 | if magic == b'': |
| 48 | raise RuntimeError('Connection reset!') |
| 49 | if testnet: |
| 50 | expected_magic = TESTNET_NETWORK_MAGIC |
| 51 | else: |
| 52 | expected_magic = NETWORK_MAGIC |
| 53 | if magic != expected_magic: |
| 54 | raise RuntimeError('magic is not right {} vs {}'.format(magic.hex(), expected_magic.hex())) |
| 55 | # command 12 bytes |
| 56 | command = s.read(12) |
| 57 | # strip the trailing 0's |
| 58 | command = command.strip(b'\x00') |
| 59 | # payload length 4 bytes, little endian |
| 60 | payload_length = little_endian_to_int(s.read(4)) |
| 61 | # checksum 4 bytes, first four of hash256 of payload |
| 62 | checksum = s.read(4) |
| 63 | # payload is of length payload_length |
| 64 | payload = s.read(payload_length) |
| 65 | # verify checksum |
| 66 | calculated_checksum = hash256(payload)[:4] |
| 67 | if calculated_checksum != checksum: |
| 68 | raise RuntimeError('checksum does not match') |
| 69 | # return an instance of the class |
| 70 | return cls(command, payload, testnet=testnet) |
| 71 | |
| 72 | def serialize(self): |
| 73 | '''Returns the byte serialization of the entire network message''' |
| 74 | # add the network magic |
| 75 | result = self.magic |
| 76 | # command 12 bytes |
| 77 | # fill with 0's |
| 78 | result += self.command + b'\x00' * (12 - len(self.command)) |
| 79 | # payload length 4 bytes, little endian |
| 80 | result += int_to_little_endian(len(self.payload), 4) |
| 81 | # checksum 4 bytes, first four of hash256 of payload |
| 82 | result += hash256(self.payload)[:4] |
| 83 | # payload |