Skip to content

web3.eth.account signature hypothesis tests #107

Description

@carver

What was wrong?

Want to catch/prevent more issues like ApeWorX/web3.py#466 earlier.

How can it be fixed?

More hypothesis tests. Piper's suggestions (on ApeWorX/web3.py#477):

  • A signed transaction's signature recovers to the expected sender address
  • The raw transaction can be rlp decoded into a transaction object
  • An rlp decoded transaction object's v,r,s match the v,r,s from the signature.
  • The signature bytes are 65 bytes in length

The downside is that they can be pretty time-intensive. Just a test for the length of signature bytes added 7 seconds to a 53-second test suite.

Example:

--- a/tests/core/eth-module/test_accounts.py
+++ b/tests/core/eth-module/test_accounts.py
@@ -2,10 +2,19 @@
 
 import pytest
 
+from eth_keys.constants import (
+    SECPK1_N,
+)
 from eth_utils import (
     is_checksum_address,
 )
 
+from hypothesis import (
+    example,
+    given,
+    strategies as st,
+)
+
 from web3 import (
     Account,
     Web3,
@@ -268,6 +277,16 @@ def test_eth_account_sign(acct, message, key, expected_bytes, expected_hash, v,
     assert account.sign(message_text=message) == signed
 
 
+@given(st.binary(), st.integers(
+    min_value=Web3.toInt(hexstr='0x1' + '00' * 31),
+    max_value=SECPK1_N - 1
+    )
+)
+def test_message_signature_length(acct, message, key):
+    signed = acct.sign(message, key)
+    assert len(signed.signature) == 65
+
+
 @pytest.mark.parametrize(
     'txn, private_key, expected_raw_tx, tx_hash, r, s, v',
     (

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3normal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions