Skip to content

Commit

Permalink
Fix non-0x-prefixed raw tx for signing middleware due to hexbytes cha…
Browse files Browse the repository at this point in the history
…nge.
  • Loading branch information
fselmo committed Aug 29, 2024
1 parent 86827d6 commit c189e3c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions newsfragments/3471.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug with newer ``hexbytes`` versions that yield non-0x-prefixed hex for ``HexBytes``: ``raw_transaction.hex()`` -> ``raw_transaction.to_0x_hex()``.
40 changes: 40 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
)
from web3.middleware import (
ExtraDataToPOAMiddleware,
SignAndSendRawMiddlewareBuilder,
)
from web3.types import (
ENS,
Expand Down Expand Up @@ -725,6 +726,26 @@ async def test_async_eth_send_raw_transaction(
txn_hash = await async_w3.eth.send_raw_transaction(signed.raw_transaction)
assert txn_hash == HexBytes(signed.hash)

@pytest.mark.asyncio
async def test_async_sign_and_send_raw_middleware(
self, async_w3: "AsyncWeb3", keyfile_account_pkey: HexStr
) -> None:
keyfile_account = async_w3.eth.account.from_key(keyfile_account_pkey)
txn: TxParams = {
"from": keyfile_account.address,
"to": keyfile_account.address,
"value": Wei(0),
"gas": 21000,
}
async_w3.middleware_onion.add(
SignAndSendRawMiddlewareBuilder.build(keyfile_account), "signing"
)
txn_hash = await async_w3.eth.send_transaction(txn)
assert isinstance(txn_hash, HexBytes)

# clean up
async_w3.middleware_onion.remove("signing")

@pytest.mark.asyncio
async def test_GasPriceStrategyMiddleware(
self,
Expand Down Expand Up @@ -3716,6 +3737,25 @@ def test_eth_send_raw_transaction(
txn_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
assert txn_hash == HexBytes(signed.hash)

def test_sign_and_send_raw_middleware(
self, w3: "Web3", keyfile_account_pkey: HexStr
) -> None:
keyfile_account = w3.eth.account.from_key(keyfile_account_pkey)
txn: TxParams = {
"from": keyfile_account.address,
"to": keyfile_account.address,
"value": Wei(0),
"gas": 21000,
}
w3.middleware_onion.add(
SignAndSendRawMiddlewareBuilder.build(keyfile_account), "signing"
)
txn_hash = w3.eth.send_transaction(txn)
assert isinstance(txn_hash, HexBytes)

# cleanup
w3.middleware_onion.remove("signing")

def test_eth_call(self, w3: "Web3", math_contract: "Contract") -> None:
txn_params = math_contract._prepare_transaction(
abi_element_identifier="add",
Expand Down
4 changes: 2 additions & 2 deletions web3/middleware/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def request_processor(self, method: "RPCEndpoint", params: Any) -> Any:

return (
RPCEndpoint("eth_sendRawTransaction"),
[raw_tx.hex()],
[raw_tx.to_0x_hex()],
)

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

return (
RPCEndpoint("eth_sendRawTransaction"),
[raw_tx.hex()],
[raw_tx.to_0x_hex()],
)

0 comments on commit c189e3c

Please sign in to comment.