Пример #1
0
def convert_transaction_result(data: dict):
    """
    Convert transaction result data into the right format.
    It supports data about a transaction made not only from JSON RPC V3 but also from V2.

    1. Fields such as status, blockHeight, txIndex, stepUsed, stepPrice, cumulativeStepUsed have to be converted to an integer.
    2. The field 'logsBloom' has to be converted to bytes.

    :param data: data about the transaction result
    """
    # Only for the transaction made with JSON RPC V2 successfully does not have the property 'status'
    if "status" not in data and "code" in data and data["code"] == 0:
        data["status"] = 1
        del data["code"]
        return

    # List of Fields which have to be converted to int
    int_fields = [
        "status", "blockHeight", "txIndex", "stepUsed", "stepPrice",
        "cumulativeStepUsed"
    ]

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

    if "logsBloom" in data:
        data["logsBloom"] = convert_hex_str_to_bytes(data["logsBloom"])
Пример #2
0
 def test_convert_hex_str_to_bytes(self):
     """
     Given: Input data is hex str.
     When : Converts hex string to bytes.
     THen : Gets return value correctly.
     """
     hex_str = "0x0000"
     self.assertEqual(convert_hex_str_to_bytes(hex_str), b'\x00\x00')
Пример #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()
Пример #4
0
    def test_make_key_store_content(self):
        # make keystore file
        content = KeyWallet.create()
        content.store(self.keystore_path, self.keystore_password)

        # get private key from keystore file
        #        written_key = key_from_key_store(file_path=self.keystore_path, password=self.keystore_password)

        written_key = convert_hex_str_to_bytes(
            KeyWallet.load(self.keystore_path,
                           self.keystore_password).get_private_key())

        self.assertTrue(isinstance(written_key, bytes))

        os.remove(self.keystore_path)
Пример #5
0
def key_from_key_store(file_path, password):
    wallet = KeyWallet.load(file_path, password)
    return convert_hex_str_to_bytes(wallet.get_private_key())
Пример #6
0
def convert_data_content_to_bytes(key, value, new_obj):
    if new_obj["dataType"] == "deploy":
        new_obj[key]["content"] = convert_hex_str_to_bytes(value["content"])