def convert_int_to_hex_str(value: int):
    try:
        if is_integer(value):
            return add_0x_prefix(hex(value))
        else:
            raise DataTypeException("Data's type should be integer.")
    except KeyError:
        raise DataTypeException("Data type is wrong.")
Beispiel #2
0
def is_block_height(value: int) -> bool:
    """Checks the value - a parameter is valid.

    :param value: height of a block, hexadecimal digits. type(str).
    :return: type(bool)
    """
    try:
        if not is_integer(value):
            return False
    except ValueError:
        return False
    return 0 <= value < 2**256
Beispiel #3
0
def convert_common_data_on_transaction(transaction: dict):
    """
    Convert common fields in the transaction such as value, fee, nid, stepLimit, timestamp, nonce, method, version, data.
    Used in validating a transaction list in a block or validating a single transaction.

    1. Fields such as value, fee, nid, stepLimit, timestamp, and nonce have to be converted to an integer.
    2. Fields such as timestamp and nonce have a different type of the value by the version as V2 or V3.
        - If the version V3, the data type is hex string prefixed with '0x'.
        - If the version V2, the data type is a string but the meaning is an integer.
    3. The field 'method' has to be removed.
    4. The field 'version' has to be added number 2 if the version is 2 or if 3, has to be converted to an integer.
    5. If the field 'dataType' is 'deploy', the field 'content' has to be converted to bytes.
       Or if 'message', the field 'data' has to be converted to an integer.

    :param transaction: data about the single transaction
    """

    # List of Fields which have to be converted to int
    int_fields = ["value", "fee", "nid", "stepLimit"]

    for int_field in int_fields:
        if int_field in transaction:
            transaction[int_field] = convert_hex_str_to_int(
                transaction[int_field])

    odd_fields = ["timestamp", "nonce"]

    for odd_field in odd_fields:
        if odd_field in transaction:
            if is_integer(transaction[odd_field]):
                pass
            elif is_0x_prefixed(transaction[odd_field]):
                transaction[odd_field] = convert_hex_str_to_int(
                    transaction[odd_field])
            else:
                transaction[odd_field] = int(transaction[odd_field])

    if "method" in transaction:
        del transaction["method"]

    if "version" in transaction and convert_hex_str_to_int(
            transaction["version"]) >= 3:
        transaction["version"] = convert_hex_str_to_int(transaction["version"])
    else:
        transaction["version"] = 2

    if "dataType" in transaction:
        if transaction["dataType"] == "deploy":
            transaction["data"]["content"] = convert_hex_str_to_bytes(
                transaction["data"]["content"])
        elif transaction["dataType"] == "message":
            transaction["data"] = bytearray.fromhex(
                remove_0x_prefix(transaction["data"])).decode()
def convert_int_to_hex_str(value: int) -> str:
    """Converts an integer to hex string prefixed with '0x'."""
    if is_integer(value):
        return hex(value)
    else:
        raise DataTypeException("Data type should be integer.")