예제 #1
0
def test_sign_wrong_v(get_private_key: Callable,
                      get_accounts: Callable) -> None:
    """ sign() raises when the private key is not a string """
    A = get_accounts(1)[0]
    privatekey = get_private_key(A)
    with pytest.raises(ValueError):
        sign(privatekey, bytes("a" * 32, "ascii"), v=22)
예제 #2
0
def test_sign_not_bytes(get_private_key: Callable,
                        get_accounts: Callable) -> None:
    """ sign() raises when message is not bytes """
    A = get_accounts(1)[0]
    privatekey = get_private_key(A)
    with pytest.raises(TypeError):
        sign(privatekey, "a" * 32, v=27)  # type: ignore
예제 #3
0
def test_sign_not_32_bytes(get_private_key: Callable,
                           get_accounts: Callable) -> None:
    """ sign() raises when message is not exactly 32 bytes """
    A = get_accounts(1)[0]
    privatekey = get_private_key(A)
    with pytest.raises(ValueError):
        sign(privatekey, bytes("a" * 31, "ascii"), v=27)
예제 #4
0
def test_sign_privatekey_not_string(get_private_key: Callable,
                                    get_accounts: Callable) -> None:
    """ sign() raises when the private key is not a string """
    A = get_accounts(1)[0]
    privatekey = get_private_key(A)
    with pytest.raises(TypeError):
        sign(bytes(privatekey, "ascii"), bytes("a" * 32, "ascii"), v=27)
예제 #5
0
def test_ecrecover_output_zero(signature_test_contract, get_accounts,
                               get_private_key):
    """ ecrecover returns 0 for an incorrect value of the v parameter """
    A = get_accounts(1)[0]
    privatekey = get_private_key(A)
    message_hash = Web3.soliditySha3(["string", "uint256"], ["hello", 5])
    signature = sign(privatekey, message_hash, v=27)

    assert (signature_test_contract.functions.verifyEcrecoverOutput(
        message_hash, signature[:32], signature[32:64],
        2).call() == EMPTY_ADDRESS)
def test_verify_fail(signature_test_contract, get_accounts, get_private_key):
    """ ECVerify.ecverify on failure cases

    the signature test contract returns the correct address on a correct
    message hash, returns a different address on a wrong message hash, and
    fails on a too long signature """
    A = get_accounts(1)[0]
    message_hash = Web3.soliditySha3(['string', 'uint256'], ['hello', 5])
    signature = sign(get_private_key(A), message_hash, v=27)

    assert signature_test_contract.functions.verify(message_hash, signature).call() == A

    message_hash = Web3.soliditySha3(['string', 'uint256'], ['hello', 6])
    assert signature_test_contract.functions.verify(message_hash, signature).call() != A

    signature2 = signature[:65] + bytes([2])
    with pytest.raises(TransactionFailed):
        signature_test_contract.functions.verify(message_hash, signature2).call()
def test_ecrecover_output_fail(signature_test_contract, get_accounts, get_private_key):
    """ ecrecover detects a wrong message content and returns zero """
    A = get_accounts(1)[0]
    privatekey = get_private_key(A)
    message_hash = Web3.soliditySha3(['string', 'uint256'], ['hello', 5])
    signature = sign(privatekey, message_hash, v=27)

    assert signature_test_contract.functions.verifyEcrecoverOutput(
        message_hash,
        signature[:32],
        signature[32:64],
        int.from_bytes(signature[64:], byteorder='big'),
    ).call() == A

    message_hash2 = Web3.soliditySha3(['string', 'uint256'], ['hello', 6])
    assert signature_test_contract.functions.verifyEcrecoverOutput(
        message_hash2,
        signature[:32],
        signature[32:64],
        int.from_bytes(signature[64:], byteorder='big'),
    ).call() != A
예제 #8
0
def test_ecrecover_output_fail(signature_test_contract: Contract,
                               get_accounts: Callable,
                               get_private_key: Callable) -> None:
    """ ecrecover detects a wrong message content and returns zero """
    A = get_accounts(1)[0]
    privatekey = get_private_key(A)
    message_hash = Web3.solidityKeccak(["string", "uint256"], ["hello", 5])
    signature = sign(privatekey, message_hash, v=27)

    assert (signature_test_contract.functions.verifyEcrecoverOutput(
        message_hash,
        signature[:32],
        signature[32:64],
        int.from_bytes(signature[64:], byteorder="big"),
    ).call() == A)

    message_hash2 = Web3.solidityKeccak(["string", "uint256"], ["hello", 6])
    assert (signature_test_contract.functions.verifyEcrecoverOutput(
        message_hash2,
        signature[:32],
        signature[32:64],
        int.from_bytes(signature[64:], byteorder="big"),
    ).call() != A)