Skip to content

Commit c189e3c

Browse files
committed
Fix non-0x-prefixed raw tx for signing middleware due to hexbytes change.
1 parent 86827d6 commit c189e3c

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

newsfragments/3471.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug with newer ``hexbytes`` versions that yield non-0x-prefixed hex for ``HexBytes``: ``raw_transaction.hex()`` -> ``raw_transaction.to_0x_hex()``.

web3/_utils/module_testing/eth_module.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
)
8989
from web3.middleware import (
9090
ExtraDataToPOAMiddleware,
91+
SignAndSendRawMiddlewareBuilder,
9192
)
9293
from web3.types import (
9394
ENS,
@@ -725,6 +726,26 @@ async def test_async_eth_send_raw_transaction(
725726
txn_hash = await async_w3.eth.send_raw_transaction(signed.raw_transaction)
726727
assert txn_hash == HexBytes(signed.hash)
727728

729+
@pytest.mark.asyncio
730+
async def test_async_sign_and_send_raw_middleware(
731+
self, async_w3: "AsyncWeb3", keyfile_account_pkey: HexStr
732+
) -> None:
733+
keyfile_account = async_w3.eth.account.from_key(keyfile_account_pkey)
734+
txn: TxParams = {
735+
"from": keyfile_account.address,
736+
"to": keyfile_account.address,
737+
"value": Wei(0),
738+
"gas": 21000,
739+
}
740+
async_w3.middleware_onion.add(
741+
SignAndSendRawMiddlewareBuilder.build(keyfile_account), "signing"
742+
)
743+
txn_hash = await async_w3.eth.send_transaction(txn)
744+
assert isinstance(txn_hash, HexBytes)
745+
746+
# clean up
747+
async_w3.middleware_onion.remove("signing")
748+
728749
@pytest.mark.asyncio
729750
async def test_GasPriceStrategyMiddleware(
730751
self,
@@ -3716,6 +3737,25 @@ def test_eth_send_raw_transaction(
37163737
txn_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
37173738
assert txn_hash == HexBytes(signed.hash)
37183739

3740+
def test_sign_and_send_raw_middleware(
3741+
self, w3: "Web3", keyfile_account_pkey: HexStr
3742+
) -> None:
3743+
keyfile_account = w3.eth.account.from_key(keyfile_account_pkey)
3744+
txn: TxParams = {
3745+
"from": keyfile_account.address,
3746+
"to": keyfile_account.address,
3747+
"value": Wei(0),
3748+
"gas": 21000,
3749+
}
3750+
w3.middleware_onion.add(
3751+
SignAndSendRawMiddlewareBuilder.build(keyfile_account), "signing"
3752+
)
3753+
txn_hash = w3.eth.send_transaction(txn)
3754+
assert isinstance(txn_hash, HexBytes)
3755+
3756+
# cleanup
3757+
w3.middleware_onion.remove("signing")
3758+
37193759
def test_eth_call(self, w3: "Web3", math_contract: "Contract") -> None:
37203760
txn_params = math_contract._prepare_transaction(
37213761
abi_element_identifier="add",

web3/middleware/signing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def request_processor(self, method: "RPCEndpoint", params: Any) -> Any:
189189

190190
return (
191191
RPCEndpoint("eth_sendRawTransaction"),
192-
[raw_tx.hex()],
192+
[raw_tx.to_0x_hex()],
193193
)
194194

195195
# -- async -- #
@@ -220,5 +220,5 @@ async def async_request_processor(self, method: "RPCEndpoint", params: Any) -> A
220220

221221
return (
222222
RPCEndpoint("eth_sendRawTransaction"),
223-
[raw_tx.hex()],
223+
[raw_tx.to_0x_hex()],
224224
)

0 commit comments

Comments
 (0)