Skip to content
/ ape Public
forked from ApeWorX/ape

Commit a32e621

Browse files
authored
fix: show first 4 bytes of transaction data in signing string (instead of 3) (ApeWorX#2522)
1 parent 32623e3 commit a32e621

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/ape/api/transactions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ def __str__(self) -> str:
190190
if isinstance(data["data"], str):
191191
data["data"] = (
192192
"0x"
193-
+ bytes(data["data"][:3], encoding="utf8").hex()
193+
+ bytes(data["data"][:4], encoding="utf8").hex()
194194
+ "..."
195-
+ bytes(data["data"][-3:], encoding="utf8").hex()
195+
+ bytes(data["data"][-4:], encoding="utf8").hex()
196196
)
197197
else:
198198
data["data"] = (
199-
to_hex(bytes(data["data"][:3])) + "..." + to_hex(bytes(data["data"][-3:]))
199+
to_hex(bytes(data["data"][:4])) + "..." + to_hex(bytes(data["data"][-4:]))
200200
)
201201
else:
202202
if isinstance(data["data"], str):

tests/functional/test_accounts.py

+17
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ def test_sign_message(signer, message):
7171
assert signer.check_signature(message, signature)
7272

7373

74+
def test_sign_transaction(signer, message, ethereum):
75+
transaction = ethereum.create_transaction(nonce=0, max_fee=0, max_priority_fee=0)
76+
signed_transaction = signer.sign_transaction(transaction)
77+
assert signed_transaction.signature is not None
78+
79+
80+
def test_sign_transaction_using_keyfile_account(keyfile_account, message, ethereum, runner):
81+
transaction = ethereum.create_transaction(
82+
nonce=0, max_fee=0, max_priority_fee=0, data="0x21314135413451"
83+
)
84+
85+
with runner.isolation(f"y\n{PASSPHRASE}\ny"):
86+
signed_transaction = keyfile_account.sign_transaction(transaction)
87+
88+
assert signed_transaction.signature is not None
89+
90+
7491
def test_sign_string(signer):
7592
message = "Hello Apes!"
7693
signature = signer.sign_message(message)

tests/functional/test_transaction.py

+11
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,17 @@ def test_str_when_data_is_bytes(ethereum):
307307
assert isinstance(actual, str)
308308

309309

310+
def test_str_when_data_is_long_shows_first_4_bytes(vyper_contract_instance):
311+
"""
312+
Tests against a condition that would cause transactions to
313+
fail with string-encoding errors.
314+
"""
315+
txn = vyper_contract_instance.setNumber.as_transaction(123)
316+
actual = str(txn)
317+
assert isinstance(actual, str)
318+
assert "data: 0x30783366..." in actual
319+
320+
310321
def test_receipt_when_none(ethereum):
311322
txn = ethereum.create_transaction(data=HexBytes("0x123"))
312323
assert txn.receipt is None

0 commit comments

Comments
 (0)