예제 #1
0
 def _validate_key_order(cls, keys: Sequence[bytes],
                         serialized_enr: Sequence[bytes]) -> None:
     if keys != tuple(sorted(keys)):
         raise DeserializationError(
             f"ENR keys are not sorted: {b', '.join(keys).decode()}",
             serialized_enr,
         )
예제 #2
0
    def deserialize(self, serial: bytes) -> str:
        if (len(serial) > self.byte_length):
            raise DeserializationError(
                "Bytes too long, only need {}".format(self.byte_length),
                serial
            )

        if len(serial) == 0 or serial[0] == 0:
            raise DeserializationError(
                "No leading zeros. And byte sequence length should be > 0",
                serial
            )

        missing = self.byte_length - len(serial)
        b_list = [0] * missing + [x for x in serial]
        return super().deserialize(bytes(b_list))
예제 #3
0
    def deserialize(self, serial: bytes) -> str:
        if len(serial) != self.byte_length:
            raise DeserializationError(
                "Bytes should be {} long.".format(self.byte_length),
                serial
            )

        return super().deserialize(serial)
예제 #4
0
파일: sedes.py 프로젝트: ethereum/eth-enr
    def _validate_serialized_length(cls,
                                    serialized_enr: Sequence[bytes]) -> None:
        if len(serialized_enr) < 2:
            raise DeserializationError(
                "ENR must contain at least a signature and a sequence number",
                serialized_enr,
            )

        num_keys_and_values = len(serialized_enr) - 2
        if num_keys_and_values % 2 != 0:
            raise DeserializationError(
                "ENR must have exactly one value for each key", serialized_enr)

        byte_size = sum(len(element) for element in serialized_enr)
        if byte_size > MAX_ENR_SIZE:
            raise DeserializationError(
                f"ENRs must not be larger than {MAX_ENR_SIZE} bytes",
                serialized_enr)
예제 #5
0
 def deserialize(self, serial):
     if serial == b'\x00':
         return False
     elif serial == b'\x01':
         return True
     else:
         raise DeserializationError(
             'Invalid serialized boolean.  Must be either 0x01 or 0x00',
             serial)
예제 #6
0
 def _validate_key_uniqueness(cls, keys: Sequence[bytes],
                              serialized_enr: Sequence[bytes]) -> None:
     duplicates = {
         key
         for key, num in collections.Counter(keys).items() if num > 1
     }
     if duplicates:
         raise DeserializationError(
             f"ENR contains the following duplicate keys: {b', '.join(duplicates).decode()}",
             serialized_enr,
         )
예제 #7
0
    def deserialize(cls, encoded: bytes) -> SignedTransactionAPI:
        if len(encoded) == 0:
            raise DeserializationError(
                "Encoded transaction was empty, which makes it invalid",
                encoded,
            )

        if isinstance(encoded, bytes):
            transaction_type = to_int(encoded[0])
            if transaction_type == 1:
                raise UnrecognizedTransactionType(transaction_type,
                                                  "TODO: Implement EIP-2930")
            elif transaction_type in range(0, 0x80):
                raise UnrecognizedTransactionType(transaction_type,
                                                  "Unknown transaction type")
            else:
                raise DeserializationError(
                    f"Typed Transaction must start with 0-0x7f, but got {hex(transaction_type)}",
                    encoded,
                )
        else:
            return cls.legacy_signed.deserialize(encoded)
예제 #8
0
    def deserialize(self, serial: bytes) -> bytes:
        '''
        De-serialize a RLP "item" back to bytes.

        Parameters
        ----------
        serial : bytes
            The input.

        Returns
        -------
        bytes
            Original bytes.

        Raises
        ------
        DeserializationError
            raise if input is not bytes.
        '''
        if not self.is_valid_type(serial):
            raise DeserializationError(
                'type of "serial" param is not right, bytes required.', serial)

        return serial
예제 #9
0
    def deserialize(self, serial) -> int:
        '''
        Deserialize bytes to int.

        Parameters
        ----------
        serial : [type]
            bytes

        Returns
        -------
        int
            integer

        Raises
        ------
        DeserializationError
            If bytes contain leading 0.
        '''
        if len(serial) > 0 and serial[0] == 0:
            raise DeserializationError(
                "Leading 0 should be removed from bytes",
                serial
            )

        # add leading 0 to bytes sequence if width is set.
        if self.max_bytes:
            byte_list = [x for x in serial]
            length = len(byte_list)
            missed = self.max_bytes - length
            if missed:
                byte_list = [0] * missed + byte_list
            serial2 = bytes(byte_list)
        else:
            serial2 = serial
        return super().deserialize(serial2)
예제 #10
0
 def deserialize(obj):
     raise DeserializationError("not yet implemented", obj)