Skip to content

Commit

Permalink
fix: show first 4 bytes of transaction data in signing string (instea…
Browse files Browse the repository at this point in the history
…d of 3) (#2522)
  • Loading branch information
antazoey authored Feb 19, 2025
1 parent 32623e3 commit a32e621
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/ape/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ def __str__(self) -> str:
if isinstance(data["data"], str):
data["data"] = (
"0x"
+ bytes(data["data"][:3], encoding="utf8").hex()
+ bytes(data["data"][:4], encoding="utf8").hex()
+ "..."
+ bytes(data["data"][-3:], encoding="utf8").hex()
+ bytes(data["data"][-4:], encoding="utf8").hex()
)
else:
data["data"] = (
to_hex(bytes(data["data"][:3])) + "..." + to_hex(bytes(data["data"][-3:]))
to_hex(bytes(data["data"][:4])) + "..." + to_hex(bytes(data["data"][-4:]))
)
else:
if isinstance(data["data"], str):
Expand Down
17 changes: 17 additions & 0 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ def test_sign_message(signer, message):
assert signer.check_signature(message, signature)


def test_sign_transaction(signer, message, ethereum):
transaction = ethereum.create_transaction(nonce=0, max_fee=0, max_priority_fee=0)
signed_transaction = signer.sign_transaction(transaction)
assert signed_transaction.signature is not None


def test_sign_transaction_using_keyfile_account(keyfile_account, message, ethereum, runner):
transaction = ethereum.create_transaction(
nonce=0, max_fee=0, max_priority_fee=0, data="0x21314135413451"
)

with runner.isolation(f"y\n{PASSPHRASE}\ny"):
signed_transaction = keyfile_account.sign_transaction(transaction)

assert signed_transaction.signature is not None


def test_sign_string(signer):
message = "Hello Apes!"
signature = signer.sign_message(message)
Expand Down
11 changes: 11 additions & 0 deletions tests/functional/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ def test_str_when_data_is_bytes(ethereum):
assert isinstance(actual, str)


def test_str_when_data_is_long_shows_first_4_bytes(vyper_contract_instance):
"""
Tests against a condition that would cause transactions to
fail with string-encoding errors.
"""
txn = vyper_contract_instance.setNumber.as_transaction(123)
actual = str(txn)
assert isinstance(actual, str)
assert "data: 0x30783366..." in actual


def test_receipt_when_none(ethereum):
txn = ethereum.create_transaction(data=HexBytes("0x123"))
assert txn.receipt is None
Expand Down

0 comments on commit a32e621

Please sign in to comment.