Takes a stream and creates a NetworkEnvelope
(cls, s, testnet=False)
| 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''' |
nothing calls this directly
no test coverage detected