def decode_single(typ: TypeStr, data: Decodable) -> Any: """ Decodes the binary value ``data`` of the ABI type ``typ`` into its equivalent python value. :param typ: The string representation of the ABI type that will be used for decoding e.g. ``'uint256'``, ``'bytes[]'``, ``'(int,int)'``, etc. :param data: The binary value to be decoded. :returns: The equivalent python value of the ABI value represented in ``data``. """ if not is_bytes(data): raise TypeError( "The `data` value must be of bytes type. Got {0}".format( type(data))) if isinstance(typ, str): type_str = typ else: type_str = collapse_type(*typ) decoder = registry.get_decoder(type_str) stream = ContextFramesBytesIO(data) return decoder(stream)
def decode_abi(types, data): if not is_bytes(data): raise TypeError( "The `data` value must be of bytes type. Got {0}".format( type(data))) decoders = [registry.get_decoder(type_str) for type_str in types] decoder = TupleDecoder(decoders=decoders) stream = ContextFramesBytesIO(data) return decoder(stream)
def decode_single(typ, data): if not is_bytes(data): raise TypeError( "The `data` value must be of bytes type. Got {0}".format( type(data))) if isinstance(typ, str): type_str = typ else: type_str = collapse_type(*typ) decoder = registry.get_decoder(type_str) stream = ContextFramesBytesIO(data) return decoder(stream)
def decode_abi(types: Iterable[TypeStr], data: Decodable) -> Tuple[Any, ...]: """ Decodes the binary value ``data`` as a sequence of values of the ABI types in ``types`` via the head-tail mechanism into a tuple of equivalent python values. :param types: An iterable of string representations of the ABI types that will be used for decoding e.g. ``('uint256', 'bytes[]', '(int,int)')`` :param data: The binary value to be decoded. :returns: A tuple of equivalent python values for the ABI values represented in ``data``. """ if not is_bytes(data): raise TypeError( "The `data` value must be of bytes type. Got {0}".format( type(data))) decoders = [registry.get_decoder(type_str) for type_str in types] decoder = TupleDecoder(decoders=decoders) stream = ContextFramesBytesIO(data) return decoder(stream)
def test_default_registry_gives_correct_basic_coders(type_str, encoder_class, decoder_class): assert isinstance(default_registry.get_encoder(type_str), encoder_class) assert isinstance(default_registry.get_decoder(type_str), decoder_class)
def test_tuple_decoder(types, data, expected): decoders = [registry.get_decoder(t) for t in types] decoder = TupleDecoder(decoders=decoders) stream = ContextFramesBytesIO(decode_hex(data)) actual = decoder(stream) assert actual == expected
def test_tuple_decoder(types, data, expected): decoders = [registry.get_decoder(t) for t in types] decoder = TupleDecoder(decoders=decoders) stream = ContextFramesBytesIO(decode_hex(data)) actual = decoder(stream) assert actual == expected