Ejemplo n.º 1
0
    def from_parser(
        cls: Type[SerializedDict],
        parser: BinaryParser,
        _length_hint: Optional[None] = None,
    ) -> SerializedDict:
        """
        Construct a SerializedDict from a BinaryParser.

        Args:
            parser: The parser to construct a SerializedDict from.

        Returns:
            The SerializedDict constructed from parser.
        """
        from xrpl.core.binarycodec.binary_wrappers.binary_serializer import (
            BinarySerializer,
        )

        serializer = BinarySerializer()

        while not parser.is_end():
            field = parser.read_field()
            if field.name == _OBJECT_END_MARKER:
                break

            associated_value = parser.read_field_value(field)
            serializer.write_field_and_value(field, associated_value)
            if field.type == _SERIALIZED_DICT:
                serializer.append(_OBJECT_END_MARKER_BYTE)

        return SerializedDict(bytes(serializer))
Ejemplo n.º 2
0
    def from_parser(
        cls: Type[SerializedList],
        parser: BinaryParser,
        _length_hint: Optional[None] = None,
    ) -> SerializedList:
        """
        Construct a SerializedList from a BinaryParser.

        Args:
            parser: The parser to construct a SerializedList from.

        Returns:
            The SerializedList constructed from parser.
        """
        bytestring = b""

        while not parser.is_end():
            field = parser.read_field()
            if field.name == _ARRAY_END_MARKER_NAME:
                break
            bytestring += bytes(field.header)
            bytestring += bytes(parser.read_field_value(field))
            bytestring += _OBJECT_END_MARKER

        bytestring += _ARRAY_END_MARKER
        return SerializedList(bytestring)
Ejemplo n.º 3
0
    def from_parser(
        cls: Type[PathStep], parser: BinaryParser, _length_hint: Optional[None] = None
    ) -> PathStep:
        """
        Construct a PathStep object from an existing BinaryParser.

        Args:
            parser: The parser to construct a PathStep from.

        Returns:
            The PathStep constructed from parser.
        """
        data_type = parser.read_uint8()
        buffer = b""

        if data_type & _TYPE_ACCOUNT:
            account_id = parser.read(AccountID.LENGTH)
            buffer += account_id
        if data_type & _TYPE_CURRENCY:
            currency = parser.read(Currency.LENGTH)
            buffer += currency
        if data_type & _TYPE_ISSUER:
            issuer = parser.read(AccountID.LENGTH)
            buffer += issuer

        return PathStep(bytes([data_type]) + buffer)
Ejemplo n.º 4
0
    def test_write_length_encoded(self):
        # length ranges: 0 - 192, 193 - 12480, 12481 - 918744
        for case in [100, 1000, 20_000]:
            bytestring = "A2" * case
            blob = Blob.from_value(bytestring)
            self.assertEqual(len(blob), case)  # sanity check
            binary_serializer = BinarySerializer()
            binary_serializer.write_length_encoded(blob)

            binary_parser = BinaryParser(bytes(binary_serializer).hex())
            decoded_length = binary_parser._read_length_prefix()
            self.assertEqual(case, decoded_length)
Ejemplo n.º 5
0
def decode(buffer: str) -> Dict[str, Any]:
    """
    Decode a transaction.

    Args:
        buffer: a hex-string of the encoded transaction.

    Returns:
        The JSON representation of the transaction.
    """
    parser = BinaryParser(buffer)
    parsed_type = cast(SerializedDict, parser.read_type(SerializedDict))
    return parsed_type.to_json()
Ejemplo n.º 6
0
def decode(buffer: str) -> Dict[str, Any]:
    """
    Decode a transaction from binary format to a JSON-like dictionary
    representation.

    Args:
        buffer: The encoded transaction binary, as a hexadecimal string.

    Returns:
        A JSON-like dictionary representation of the transaction.
    """
    parser = BinaryParser(buffer)
    parsed_type = cast(SerializedDict, parser.read_type(SerializedDict))
    return parsed_type.to_json()
Ejemplo n.º 7
0
    def to_json(self: Path) -> List[Dict[str, str]]:
        """
        Returns the JSON representation of a Path.

        Returns:
            The JSON representation of a Path.
        """
        json = []
        path_parser = BinaryParser(str(self))

        while not path_parser.is_end():
            pathstep = PathStep.from_parser(path_parser)
            json.append(pathstep.to_json())

        return json
Ejemplo n.º 8
0
    def to_json(self: PathSet) -> List[List[Dict[str, str]]]:
        """
        Returns the JSON representation of a PathSet.

        Returns:
            The JSON representation of a PathSet.
        """
        json = []
        pathset_parser = BinaryParser(str(self))

        while not pathset_parser.is_end():
            path = Path.from_parser(pathset_parser)
            json.append(path.to_json())
            pathset_parser.skip(1)

        return json
Ejemplo n.º 9
0
    def to_json(self: SerializedDict) -> Dict[str, Any]:
        """
        Returns the JSON representation of a SerializedDict.

        Returns:
            The JSON representation of a SerializedDict.
        """
        parser = BinaryParser(str(self))
        accumulator = {}

        while not parser.is_end():
            field = parser.read_field()
            if field.name == _OBJECT_END_MARKER:
                break
            json_value = parser.read_field_value(field).to_json()
            accumulator[field.name] = _enum_to_str(field.name, json_value)

        return accumulator
Ejemplo n.º 10
0
    def to_json(self: SerializedList) -> List[Any]:
        """
        Returns the JSON representation of a SerializedList.

        Returns:
            The JSON representation of a SerializedList.
        """
        result = []
        parser = BinaryParser(str(self))

        while not parser.is_end():
            field = parser.read_field()
            if field.name == _ARRAY_END_MARKER_NAME:
                break

            outer = {}
            outer[field.name] = SerializedDict.from_parser(parser).to_json()
            result.append(outer)
        return result
Ejemplo n.º 11
0
    def from_parser(
        cls: Type[PathSet], parser: BinaryParser, _length_hint: Optional[None] = None
    ) -> PathSet:
        """
        Construct a PathSet object from an existing BinaryParser.

        Args:
            parser: The parser to construct a PathSet from.

        Returns:
            The PathSet constructed from parser.
        """
        buffer: List[bytes] = []
        while not parser.is_end():
            path = Path.from_parser(parser)
            buffer.append(bytes(path))
            buffer.append(parser.read(1))

            if buffer[-1][0] == _PATHSET_END_BYTE:
                break
        return PathSet(b"".join(buffer))
Ejemplo n.º 12
0
    def from_parser(cls: Type[UInt16],
                    parser: BinaryParser,
                    _length_hint: Optional[int] = None) -> UInt16:
        """
        Construct a new UInt16 type from a BinaryParser.

        Args:
            parser: The BinaryParser to construct a UInt16 from.

        Returns:
            The UInt16 constructed from parser.
        """
        return cls(parser.read(_WIDTH))
Ejemplo n.º 13
0
    def from_parser(
        cls: Type[Path], parser: BinaryParser, _length_hint: Optional[None] = None
    ) -> Path:
        """
        Construct a Path object from an existing BinaryParser.

        Args:
            parser: The parser to construct a Path from.

        Returns:
            The Path constructed from parser.
        """
        buffer: List[bytes] = []
        while not parser.is_end():
            pathstep = PathStep.from_parser(parser)
            buffer.append(bytes(pathstep))

            if parser.peek() == cast(bytes, _PATHSET_END_BYTE) or parser.peek() == cast(
                bytes, _PATH_SEPARATOR_BYTE
            ):
                break
        return Path(b"".join(buffer))
Ejemplo n.º 14
0
    def to_json(self: PathStep) -> Dict[str, str]:
        """
        Returns the JSON representation of a PathStep.

        Returns:
            The JSON representation of a PathStep.
        """
        parser = BinaryParser(str(self))
        data_type = parser.read_uint8()
        json = {}

        if data_type & _TYPE_ACCOUNT:
            account_id = AccountID.from_parser(parser).to_json()
            json["account"] = account_id
        if data_type & _TYPE_CURRENCY:
            currency = Currency.from_parser(parser).to_json()
            json["currency"] = currency
        if data_type & _TYPE_ISSUER:
            issuer = AccountID.from_parser(parser).to_json()
            json["issuer"] = issuer

        return json
Ejemplo n.º 15
0
    def from_parser(cls: Type[Blob], parser: BinaryParser,
                    length_hint: int) -> Blob:
        """
        Defines how to read a Blob from a BinaryParser.

        Args:
            parser: The parser to construct a Blob from.
            length_hint: The number of bytes to consume from the parser.

        Returns:
            The Blob constructed from parser.
        """
        return cls(parser.read(length_hint))
Ejemplo n.º 16
0
    def from_parser(cls: Type[UInt8],
                    parser: BinaryParser,
                    _length_hint: Optional[int] = None) -> UInt8:
        """
        Construct a new UInt8 type from a BinaryParser.

        Args:
            parser: The parser to construct a UInt8 from.

        Returns:
            A new UInt8.
        """
        return cls(parser.read(_WIDTH))
Ejemplo n.º 17
0
    def from_parser(
        cls: Type[Hash], parser: BinaryParser, length_hint: Optional[int] = None
    ) -> Hash:
        """
        Construct a Hash object from an existing BinaryParser.

        Args:
            parser: The parser to construct the Hash object from.
            length_hint: The number of bytes to consume from the parser.

        Returns:
            The Hash object constructed from a parser.
        """
        num_bytes = length_hint if length_hint is not None else cls._get_length()
        return cls(parser.read(num_bytes))
Ejemplo n.º 18
0
 def test_from_parser_to_json(self):
     parser = BinaryParser(BUFFER)
     serialized_list = SerializedList.from_parser(parser)
     self.assertEqual(serialized_list.to_json(), EXPECTED_JSON)
Ejemplo n.º 19
0
 def test_from_parser(self):
     parser = BinaryParser(BUFFER)
     serialized_list = SerializedList.from_parser(parser)
     self.assertEqual(BUFFER, str(serialized_list))
Ejemplo n.º 20
0
 def test_from_parser_to_json(self):
     parser = BinaryParser(buffer)
     pathset = PathSet.from_parser(parser)
     self.assertEqual(pathset.to_json(), expected_json)
Ejemplo n.º 21
0
 def test_from_parser_to_json(self):
     parser = BinaryParser(buffer)
     transaction = SerializedDict.from_parser(parser)
     self.assertEqual(transaction.to_json(), expected_json)