Skip to content

Commit 9a564bb

Browse files
committed
Use HexBytes.hex() for sign-and-send-raw middlewares
- Don't assume some conversion is going to happen later down the line. The JSON-RPC asks for hexstr value, give it the hexstr value if we can guarantee that it is a hexstr. We already have the type as HexBytes so call ``.hex()`` on the value and send that as the param. - closes #2936
1 parent 0a8ca6d commit 9a564bb

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

tests/core/middleware/test_transaction_signing.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313
from eth_utils import (
1414
ValidationError as EthUtilsValidationError,
15+
is_hexstr,
1516
to_bytes,
1617
to_hex,
1718
)
@@ -310,7 +311,7 @@ def assert_method_and_txn_signed(actual, expected):
310311
raw_txn = actual[1][0]
311312
actual_method = actual[0]
312313
assert actual_method == expected
313-
assert isinstance(raw_txn, bytes)
314+
assert is_hexstr(raw_txn)
314315

315316

316317
@pytest.fixture()
@@ -417,7 +418,7 @@ def test_sign_and_send_raw_middleware_with_byte_addresses(
417418
raw_txn = actual[1][0]
418419
actual_method = actual[0]
419420
assert actual_method == "eth_sendRawTransaction"
420-
assert isinstance(raw_txn, bytes)
421+
assert is_hexstr(raw_txn)
421422

422423

423424
# -- async -- #
@@ -613,4 +614,4 @@ async def test_async_sign_and_send_raw_middleware_with_byte_addresses(
613614
raw_txn = actual[1][0]
614615
actual_method = actual[0]
615616
assert actual_method == "eth_sendRawTransaction"
616-
assert isinstance(raw_txn, bytes)
617+
assert is_hexstr(raw_txn)

web3/middleware/signing.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
178178
account = accounts[transaction["from"]]
179179
raw_tx = account.sign_transaction(transaction).rawTransaction
180180

181-
return make_request(RPCEndpoint("eth_sendRawTransaction"), [raw_tx])
181+
return make_request(RPCEndpoint("eth_sendRawTransaction"), [raw_tx.hex()])
182182

183183
return middleware
184184

@@ -229,7 +229,10 @@ async def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
229229
account = accounts[to_checksum_address(tx_from)]
230230
raw_tx = account.sign_transaction(filled_transaction).rawTransaction
231231

232-
return await make_request(RPCEndpoint("eth_sendRawTransaction"), [raw_tx])
232+
return await make_request(
233+
RPCEndpoint("eth_sendRawTransaction"),
234+
[raw_tx.hex()],
235+
)
233236

234237
return middleware
235238

0 commit comments

Comments
 (0)