Exemplo n.º 1
0
def is_encodable(typ: TypeStr, arg: Any) -> bool:
    """
    Determines if the python value ``arg`` is encodable as a value of the ABI
    type ``typ``.

    :param typ: A string representation for the ABI type against which the
        python value ``arg`` will be checked e.g. ``'uint256'``, ``'bytes[]'``,
        ``'(int,int)'``, etc.
    :param arg: The python value whose encodability should be checked.

    :returns: ``True`` if ``arg`` is encodable as a value of the ABI type
        ``typ``.  Otherwise, ``False``.
    """
    if isinstance(typ, str):
        type_str = typ
    else:
        type_str = collapse_type(*typ)

    encoder = registry.get_encoder(type_str)

    try:
        encoder.validate_value(arg)
    except EncodingError:
        return False
    except AttributeError:
        try:
            encoder(arg)
        except EncodingError:
            return False

    return True
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
def encode_single(typ, arg):
    if isinstance(typ, str):
        type_str = typ
    else:
        type_str = collapse_type(*typ)

    encoder = registry.get_encoder(type_str)

    return encoder(arg)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
def is_encodable(typ, arg):
    """
    Determines if the given python value ``arg`` can be encoded as a value of
    abi type ``typ``.
    """
    if isinstance(typ, str):
        type_str = typ
    else:
        type_str = collapse_type(*typ)

    encoder = registry.get_encoder(type_str)

    try:
        encoder.validate_value(arg)
    except EncodingError:
        return False
    else:
        return True
Exemplo n.º 6
0
def encode_single(typ: TypeStr, arg: Any) -> bytes:
    """
    Encodes the python value ``arg`` as a binary value of the ABI type ``typ``.

    :param typ: The string representation of the ABI type that will be used for
        encoding e.g. ``'uint256'``, ``'bytes[]'``, ``'(int,int)'``, etc.
    :param arg: The python value to be encoded.

    :returns: The binary representation of the python value ``arg`` as a value
        of the ABI type ``typ``.
    """
    if isinstance(typ, str):
        type_str = typ
    else:
        type_str = collapse_type(*typ)

    encoder = registry.get_encoder(type_str)

    return encoder(arg)
Exemplo n.º 7
0
def test_collapse_type(original, expected):
    assert collapse_type(*process_type(original)) == expected