def decode_single(typ, data): if is_hex_encoded_value(data): warnings.warn( DeprecationWarning( "Automatic inference of hex encoded data has been deprecated. " "Please adjust your code to ensure that the data argument for " "`decode_single` is a byte string")) data = decode_hex(remove_0x_prefix(data)) if is_text(data): warnings.warn( DeprecationWarning( "Automatic conversion of encoded data to bytes has been deprecated. " "Please adjust your code to ensure that the data argument for " "`decode_single` is a byte string")) data = force_bytes(data) try: base, sub, arrlist = typ except ValueError: base, sub, arrlist = process_type(typ) decoder = get_single_decoder(base, sub, arrlist) stream = BytesIO(data) return decoder(stream)
def encode_single(typ, arg): try: base, sub, arrlist = typ except ValueError: base, sub, arrlist = process_type(typ) encoder = get_single_encoder(base, sub, arrlist) return encoder(arg)
def test_multi_decoder(types, data, expected): decoders = tuple(( get_single_decoder(*process_type(t)) for t in types )) decoder = MultiDecoder.as_decoder(decoders=decoders) stream = BytesIO(decode_hex(data)) actual = decoder(stream) assert actual == expected
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))) processed_types = tuple(process_type(_type) for _type in types) decoder = get_multi_decoder(processed_types) stream = BytesIO(data) return decoder(stream)
def encode_single(typ, arg): try: base, sub, arrlist = typ except ValueError: base, sub, arrlist = process_type(typ) if is_text(arg): arg = force_bytes(arg) encoder = get_single_encoder(base, sub, arrlist) return encoder(arg)
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))) try: base, sub, arrlist = typ except ValueError: base, sub, arrlist = process_type(typ) decoder = get_single_decoder(base, sub, arrlist) stream = BytesIO(data) return decoder(stream)
def decode_abi(types, data): if is_hex_encoded_value(data): warnings.warn( DeprecationWarning( "Automatic inference of hex encoded data has been deprecated. " "Please adjust your code to ensure that the data argument for " "`decode_single` is a byte string")) data = decode_hex(remove_0x_prefix(data)) if is_text(data): warnings.warn( DeprecationWarning( "Automatic conversion of encoded data to bytes has been deprecated. " "Please adjust your code to ensure that the data argument for " "`decode_abi` is a byte string")) data = force_bytes(data) processed_types = tuple(process_type(_type) for _type in types) decoder = get_multi_decoder(processed_types) stream = BytesIO(data) return decoder(stream)
def encode_abi(types, args): processed_types = [process_type(typ) for typ in types] encoder = get_multi_encoder(processed_types) return encoder(args)
def test_collapse_type(original, expected): assert collapse_type(*process_type(original)) == expected
def test_process_parsing_errors(typestr): with pytest.raises(ParseError): process_type(typestr)
def test_process_validation_errors(typestr): with pytest.raises(ValueError): process_type(typestr)
def test_process_type_requires_basic_type(): with pytest.raises(ValueError): process_type('(int,int)')
def test_process_type_requires_string_type(): with pytest.raises(TypeError): process_type(b'uint256')
def test_process_type(typestr, expected_parse): assert process_type(typestr) == expected_parse
def test_process_exceptions(typestr): with pytest.raises(ValueError): process_type(typestr)