text: from io import BytesIO
  from unittest import TestCase
  
  from helper import (
      double_sha256,
      int_to_little_endian,
      little_endian_to_int,
  )
  
  
  NETWORK_MAGIC = b'\xf9\xbe\xb4\xd9'
  
  
  class NetworkEnvelope:
  
      def __init__(self, command, payload):
          self.command = command
          self.payload = payload
  
      def __repr__(self):
          return '{}: {}'.format(
              self.command.decode('ascii'),
              self.payload.hex(),
          )
  
      @classmethod
      def parse(cls, s):
          '''Takes a stream and creates a NetworkEnvelope'''
          # check the network magic NETWORK_MAGIC
          # command 12 bytes
          # payload length 4 bytes, little endian
          # checksum 4 bytes, first four of double-sha256 of payload
          # payload is of length payload_length
          # verify checksum
          raise NotImplementedError
  
      def serialize(self):
          '''Returns the byte serialization of the entire network message'''
          # add the network magic NETWORK_MAGIC
          # command 12 bytes
          # payload length 4 bytes, little endian
          # checksum 4 bytes, first four of double-sha256 of payload
          # payload
          raise NotImplementedError
  
  
  class NetworkEnvelopeTest(TestCase):
  
      def test_parse(self):
          msg = bytes.fromhex('f9beb4d976657261636b000000000000000000005df6e0e2')
          stream = BytesIO(msg)
          envelope = NetworkEnvelope.parse(stream)
          self.assertEqual(envelope.command[:6], b'verack')
          self.assertEqual(envelope.payload, b'')
          msg = bytes.fromhex('f9beb4d976657273696f6e0000000000650000005f1a69d2721101000100000000000000bc8f5e5400000000010000000000000000000000000000000000ffffc61b6409208d010000000000000000000000000000000000ffffcb0071c0208d128035cbc97953f80f2f5361746f7368693a302e392e332fcf05050001')
          stream = BytesIO(msg)
          envelope = NetworkEnvelope.parse(stream)
          self.assertEqual(envelope.command[:7], b'version')
          self.assertEqual(envelope.payload, msg[24:])
  
      def test_serialize(self):
          msg = bytes.fromhex('f9beb4d976657261636b000000000000000000005df6e0e2')
          stream = BytesIO(msg)
          envelope = NetworkEnvelope.parse(stream)
          self.assertEqual(envelope.serialize(), msg)
          msg = bytes.fromhex('f9beb4d976657273696f6e0000000000650000005f1a69d2721101000100000000000000bc8f5e5400000000010000000000000000000000000000000000ffffc61b6409208d010000000000000000000000000000000000ffffcb0071c0208d128035cbc97953f80f2f5361746f7368693a302e392e332fcf05050001')
          stream = BytesIO(msg)
          envelope = NetworkEnvelope.parse(stream)
          self.assertEqual(envelope.serialize(), msg)
  