Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show first 4 bytes of transaction data in signing string (instead of 3) #2522

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ape/api/transactions.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we make this configurable? like displaying the full data vs. only displaying partial?

accounts:
  show_full_calldata: true  # defaults `false`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am always done for using config to change behavior like that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ill make another PR for this feature

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Loading