def test_invalid_structured_data_invalid_abi_type():
    # Given type/types are invalid
    invalid_structured_data_string = open(
        "tests/fixtures/invalid_message_invalid_abi_type.json"
    ).read()
    invalid_structured_data = json.loads(invalid_structured_data_string)
    with pytest.raises(TypeError, match="Received Invalid type `uint25689` in the struct `Person`"):
        hash_message(invalid_structured_data)
Beispiel #2
0
def test_invalid_structured_data_invalid_abi_type():
    # Given type/types are invalid
    invalid_structured_data_string = open(
        "tests/fixtures/invalid_message_invalid_abi_type.json").read()
    invalid_structured_data = json.loads(invalid_structured_data_string)
    with pytest.raises(ABITypeError) as e:
        hash_message(invalid_structured_data)
    assert "'uint25689': integer size out of bounds" in str(e.value)
Beispiel #3
0
def test_invalid_structured_data_value_type_mismatch_in_primary_type():
    # Given type is valid (string), but the value (int) is not of the mentioned type
    invalid_structured_data_string = open(
        "tests/fixtures/invalid_message_value_type_mismatch_primary_type.json"
    ).read()
    invalid_structured_data = json.loads(invalid_structured_data_string)
    with pytest.raises(TypeError) as e:
        hash_message(invalid_structured_data)
    assert (str(e.value) ==
            "Value of `contents` (12345) in the struct `Mail` is of the "
            "type `<class 'int'>`, but expected string value")
Beispiel #4
0
def test_unequal_array_dimension_between_schema_and_data():
    invalid_structured_data_string = open(
        "tests/fixtures/invalid_message_unequal_array_dimensions.json").read()
    invalid_structured_data = json.loads(invalid_structured_data_string)
    with pytest.raises(TypeError) as e:
        hash_message(invalid_structured_data)
    assert (
        str(e.value) == "Array data "
        "`[{'name': 'Bob', 'wallet': '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB'}]` has "
        "dimensions `(1,)` whereas the schema has dimensions `(2, 3, 4)`"
        or str(e.value) == "Array data "
        "`[{'wallet': '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', 'name': 'Bob'}]` has "
        "dimensions `(1,)` whereas the schema has dimensions `(2, 3, 4)`")
def test_structured_data_invalid_identifier_filtered_by_abi_encodable_function():
    # Given valid abi type, but the value is not of the specified type
    # (Error is found by the ``is_encodable`` ABI function)
    invalid_structured_data_string = open(
        "tests/fixtures/invalid_message_valid_abi_type_invalid_value.json"
    ).read()
    invalid_structured_data = json.loads(invalid_structured_data_string)
    with pytest.raises(TypeError) as e:
        hash_message(invalid_structured_data)
    assert (
        str(e.value) == "Value of `balance` (how do you do?) in the struct `Person` is of the "
        "type `<class 'str'>`, but expected uint256 value"
    )
Beispiel #6
0
def signature_wrapper(message, signature_version, version_specific_data):
    if not isinstance(message, bytes):
        raise TypeError("Message is of the type {}, expected bytes".format(
            type(message)))
    if not isinstance(signature_version, bytes):
        raise TypeError(
            "Signature Version is of the type {}, expected bytes".format(
                type(signature_version)))

    if signature_version == PERSONAL_SIGN_VERSION:
        preamble = b'\x19Ethereum Signed Message:\n'
        size = str(len(message)).encode('utf-8')
        return preamble + size + message
    elif signature_version == INTENDED_VALIDATOR_SIGN_VERSION:
        wallet_address = to_bytes(hexstr=version_specific_data)
        if len(wallet_address) != 20:
            raise TypeError(
                "Invalid Wallet Address: {}".format(version_specific_data))
        wrapped_message = b'\x19' + signature_version + wallet_address + message
        return wrapped_message
    elif signature_version == STRUCTURED_DATA_SIGN_VERSION:
        message_string = to_text(primitive=message)
        structured_data = load_and_validate_structured_message(message_string)
        domainSeparator = hash_domain(structured_data)
        wrapped_message = (b'\x19' + signature_version + domainSeparator +
                           hash_message(structured_data))
        return wrapped_message
    else:
        raise NotImplementedError(
            "Currently supported signature versions are: {0}, {1}, {2}. ".
            format(
                '0x' + INTENDED_VALIDATOR_SIGN_VERSION.hex(),
                '0x' + PERSONAL_SIGN_VERSION.hex(),
                '0x' + STRUCTURED_DATA_SIGN_VERSION.hex(),
            ) + "But received signature version {}".format(
                '0x' + signature_version.hex()))
Beispiel #7
0
def test_hash_struct_main_message(structured_valid_data_json_string):
    structured_data = json.loads(structured_valid_data_json_string)
    expected_hex_value = "76649452daa3e4101beef5b01669fd0de45ece35188d529703c73526a2454521"
    assert hash_message(structured_data).hex() == expected_hex_value
Beispiel #8
0
def test_hash_struct_main_message(structured_valid_data_json_string):
    structured_data = json.loads(structured_valid_data_json_string)
    expected_hex_value = "c52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e"
    assert hash_message(structured_data).hex() == expected_hex_value