Esempio n. 1
0
def is_basic_transaction(params: dict) -> bool:
    """
    Checks an instance of `Transaction` has right format.
    Every types of `Transaction` like icx transaction, deploy transaction, call transaction and message transaction
    is checked by this method.
    """
    inner_key_of_params = ['version', 'from', 'to', 'stepLimit', 'timestamp', 'nid']

    return has_keys(params, inner_key_of_params) \
           and is_wallet_address(params['from']) \
           and (is_wallet_address(params['to']) or is_score_address(params['to'])) \
           and is_0x_prefixed(params['stepLimit']) \
           and is_0x_prefixed(params['timestamp']) is not None
Esempio n. 2
0
def is_message_transaction(params: dict) -> bool:
    """Checks an instance of `MessageTransaction` has right format."""
    inner_key_of_params = ['dataType', 'data']
    return is_basic_transaction(params) \
           and has_keys(params, inner_key_of_params) \
           and is_0x_prefixed(params["data"]) \
           and params["dataType"] == "message"
    def __init__(self, from_: str, to: str, value: int, step_limit: int, nid: int, nonce: int, version: int,
                 timestamp: int, data: str):
        Transaction.__init__(self, from_, to, value, step_limit, nid, nonce, version, timestamp)

        if is_0x_prefixed(data) and is_lowercase_hex_string(remove_0x_prefix(data)):
            self.__data = data
        else:
            raise DataTypeException("Message data should be hex string prefixed with '0x'.")
Esempio n. 4
0
def is_T_HASH(value):
    """T_HASH is data type which is 64-digit hexadecimal string prefixed with `0x`."""
    try:
        if is_0x_prefixed(value) and len(remove_0x_prefix(value)) == 64:
            return True
        else:
            raise DataTypeException("This hash value is unrecognized.")
    except ValueError:
        raise DataTypeException("This hash value is unrecognized.")
Esempio n. 5
0
def is_hex_block_hash(value) -> bool:
    """
    Checks the value - a parameter is valid.
    Hash value of a block starts with '0x' and 64 digits hex string

    :param value: hash value of a block, hexadecimal digits. type(str)
    :return: type(bool)
    """
    return is_str(value) and is_0x_prefixed(value) and len(remove_0x_prefix(value)) == 64
Esempio n. 6
0
def is_deploy_transaction(params: dict) -> bool:
    """Checks an instance of `DeployTransaction` has right format."""
    inner_key_of_params = ['dataType', 'data']
    inner_key_of_data = ['contentType', 'content']

    return is_basic_transaction(params) \
           and has_keys(params, inner_key_of_params) \
           and has_keys(params['data'], inner_key_of_data) \
           and params['dataType'] == 'deploy' \
           and is_0x_prefixed(params['data']['content']) \
           and 'value' not in params
Esempio n. 7
0
def is_T_BIN_DATA(value):
    """
    T_BIN_DATA is data type which is hexadeciamal string prefixed with `0x`
    and length is even.
    """
    try:
        if is_0x_prefixed(value) and len(remove_0x_prefix(value)) % 2 == 0:
            return True
        else:
            raise DataTypeException("This value is not T_BIN_DATA data type.")
    except ValueError:
        raise DataTypeException("This value is not T_BIN_DATA data type.")
Esempio n. 8
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 validate_block(data: dict):
    """Validate the block data has right returning format."""
    if data["height"] == 0:
        return True

    if "tx_hash" in data:
        return False

    for transaction in data["confirmed_transaction_list"]:
        validate_common_data_on_transaction(transaction)
        if "txHash" not in transaction and not is_0x_prefixed(transaction["txHash"]):
            return False

    return True
Esempio n. 10
0
def is_deposit_transaction(params: dict, deposit_type: str) -> bool:
    """Checks an instance of `DepositTransaction` has right format.

    :param params:
    :param deposit_type: add or withdraw
    :return: bool
    """
    inner_key_of_params = ['dataType', 'data']
    inner_key_of_data_for_add_action = ["action"]
    inner_key_of_data_for_withdraw_action = ["action", "id"]

    checked: bool = has_keys(params, inner_key_of_params) and params["dataType"] == "deposit"

    if deposit_type == "withdraw":
        return has_keys(params["data"], inner_key_of_data_for_withdraw_action) \
               and is_0x_prefixed(params["data"]["id"]) and params["data"]["action"] == "withdraw" and checked
    else:
        return has_keys(params["data"], inner_key_of_data_for_add_action) \
               and params["data"]["action"] == "add" and checked
Esempio n. 11
0
def is_icx_transaction(params: dict) -> bool:
    """Checks an instance of `Transaction` for transfer icx has right format."""
    return is_basic_transaction(params) \
           and 'value' in params \
           and is_0x_prefixed(params['value'])
Esempio n. 12
0
 def is_hex_string_prefixed_with_0x(value: str):
     if is_0x_prefixed(value) and is_lowercase_hex_string(
             remove_0x_prefix(value)):
         return True
     else:
         return False