Skip to content

feat(eth): add w3.eth.sign_authorization() EIP-7702 convenience helper - #3865

Open
kushdab wants to merge 3 commits into
ApeWorX:mainfrom
kushdab:feat/eth-sign-authorization-helper
Open

feat(eth): add w3.eth.sign_authorization() EIP-7702 convenience helper#3865
kushdab wants to merge 3 commits into
ApeWorX:mainfrom
kushdab:feat/eth-sign-authorization-helper

Conversation

@kushdab

@kushdab kushdab commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Addresses #3655 — auto-nonce/chainId bare-metal improvement.

Motivation

The current EIP-7702 workflow forces callers to manually fetch chain
state before every authorization:

signed_auth = acct.sign_authorization({
    "chainId": w3.eth.chain_id,                          # manual
    "address": contract.address,
    "nonce": w3.eth.get_transaction_count(acct.address), # manual
}, private_key)

As @wolovim noted, web3.py can provide "minor niceties, like auto nonce
management in the bare metal versions." This PR is that niceity.

API

# Minimal — chainId and nonce auto-filled from chain state
auth = w3.eth.sign_authorization(contract.address, my_private_key)

# Opt out of auto-fill for either field
auth = w3.eth.sign_authorization(
    contract.address,
    my_private_key,
    chain_id=0,    # chain-agnostic (replayable anywhere)
    nonce=nonce+1, # explicit nonce, e.g. when EOA submits its own type-4 tx first
)

The result is a SignedSetCodeAuthorization ready for authorizationList:

tx = {
    "from": acct.address,
    "to": acct.address,
    "authorizationList": [auth],
    "data": contract.encode_abi("myFunction", args=[arg1, arg2]),
}
signed = acct.sign_transaction(tx)
w3.eth.send_raw_transaction(signed.raw_transaction)

Changes

File Change
web3/eth/eth.py Add Eth.sign_authorization(address, private_key, *, chain_id, nonce)
web3/eth/async_eth.py Add AsyncEth.sign_authorization(...) (awaitable)
tests/core/eth-module/test_eth_sign_authorization.py 15 tests covering auto-fill, explicit overrides, nonce/chainId short-circuit (RPC not called when explicit), and structural fitness as an authorizationList item

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant