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

Async sign and send raw middleware #3025

Merged
merged 4 commits into from Jul 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/middleware.rst
Expand Up @@ -537,6 +537,7 @@ Signing
~~~~~~~

.. py:method:: web3.middleware.construct_sign_and_send_raw_middleware(private_key_or_account)
web3.middleware.async_construct_sign_and_send_raw_middleware(private_key_or_account)

This middleware automatically captures transactions, signs them, and sends them as raw transactions.
The ``from`` field on the transaction, or ``w3.eth.default_account`` must be set to the address of the private key for
Expand Down Expand Up @@ -573,6 +574,27 @@ this middleware to have any effect.
>>> w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct))
>>> w3.eth.default_account = acct.address

>>> # use `eth_sendTransaction` to automatically sign and send the raw transaction
>>> w3.eth.send_transaction(tx_dict)
HexBytes('0x09511acf75918fd03de58141d2fd409af4fd6d3dce48eb3aa1656c8f3c2c5c21')

Similarly, with AsyncWeb3:

.. code-block:: python

>>> from web3 import AsyncWeb3
>>> async_w3 = AsyncWeb3(AsyncHTTPProvider('HTTP_ENDPOINT'))
>>> from web3.middleware import async_construct_sign_and_send_raw_middleware
>>> from eth_account import Account
>>> import os
>>> acct = async_w3.eth.account.from_key(os.environ.get('PRIVATE_KEY'))
>>> async_w3.middleware_onion.add(await async_construct_sign_and_send_raw_middleware(acct))
>>> async_w3.eth.default_account = acct.address

>>> # use `eth_sendTransaction` to automatically sign and send the raw transaction
>>> await async_w3.eth.send_transaction(tx_dict)
HexBytes('0x09511acf75918fd03de58141d2fd409af4fd6d3dce48eb3aa1656c8f3c2c5c21')

Now you can send a transaction from acct.address without having to build and sign each raw transaction.

When making use of this signing middleware, when sending dynamic fee transactions (recommended over legacy transactions),
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2936.bugfix.rst
@@ -0,0 +1 @@
Fix for a possible bug in ``construct_sign_and_send_raw_middleware`` where the signed transaction was sent as bytes and expected to be converted to hex by formatting later on. It is now explicitly sent as the hex string hash within the middleware.
1 change: 1 addition & 0 deletions newsfragments/3025.feature.rst
@@ -0,0 +1 @@
Add async support for the sign-and-send raw transaction middleware via ``construct_async_sign_and_send_raw_middleware()``.