EIP-7702 authorization.
An authorization is a signed permit by which an externally-owned account (the authority)
designates contract code to run in its place: once included in a type-4 transaction
(Ethers.Transaction.Eip7702), the authority's account code is set to the delegation
designator 0xef0100 ++ address, making every call to the EOA execute the delegate
contract's code.
The signed payload is not the tuple itself but its EIP-7702 hash:
keccak256(0x05 ++ rlp([chain_id, address, nonce]))To produce a signature use Ethers.sign_authorization/2, which routes through the
configured Ethers.Signer. The signed counterpart is Ethers.Authorization.Signed.
Fields
chain_id- chain where the authorization is valid.0makes it valid on every chain — one signature delegates the authority everywhere (the nonce must still match on each chain). Only use0deliberately.address- the delegate contract whose code the authority's account will run. The zero address clears an existing delegation (seeclear/1).nonce- the authority's account nonce at the time the authorization is applied on chain. When the authority itself sends the type-4 transaction (self-sponsoring), the transaction increments the account nonce first, so the authorization nonce must be the current nonce plus one — see the:executoroption ofEthers.sign_authorization/2.
A mismatched nonce (or a signature with a high s value) does not fail the transaction;
the authorization is silently skipped on chain, so getting these right matters. To
confirm that an authorization took effect, check the authority's account afterwards with
Ethers.get_delegation/2 (or Ethers.get_code/2 and parse_delegation/1).
Summary
Functions
Creates an authorization that clears the authority's delegation.
Same as clear/1 but raises on error.
Calculates the EIP-7702 signing hash of an authorization.
Creates a new authorization struct with the given parameters.
Same as new/1 but raises on error.
Parses the delegate address from an EIP-7702 delegation designator.
Types
@type t() :: %Ethers.Authorization{ address: Ethers.Types.t_address(), chain_id: non_neg_integer(), nonce: non_neg_integer() }
An unsigned EIP-7702 authorization incorporating the following fields:
chain_id- chain ID the authorization is valid on, or0for every chainaddress- the delegate contract addressnonce- the authority's account nonce at the time the authorization applies
Functions
Creates an authorization that clears the authority's delegation.
Same as new/1 with the zero address: applying it resets the authority's account code
to empty instead of writing a delegation designator. This is the only way to remove an
EIP-7702 delegation. The authority's nonce is still consumed.
Examples
iex> Ethers.Authorization.clear(chain_id: 1, nonce: 8)
{:ok, %Ethers.Authorization{chain_id: 1, address: "0x0000000000000000000000000000000000000000", nonce: 8}}
Same as clear/1 but raises on error.
@spec hash(t()) :: <<_::256>>
Calculates the EIP-7702 signing hash of an authorization.
Returns the 32-byte digest of keccak256(0x05 ++ rlp([chain_id, address, nonce])).
Creates a new authorization struct with the given parameters.
Accepts a map or a keyword list with the :chain_id, :address and :nonce keys, all
required. See the module documentation for the field semantics. As required by EIP-7702,
chain_id must be below 2**256 and nonce below 2**64.
Examples
iex> Ethers.Authorization.new(chain_id: 1, address: "0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1", nonce: 7)
{:ok, %Ethers.Authorization{chain_id: 1, address: "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1", nonce: 7}}
Same as new/1 but raises on error.
@spec parse_delegation(binary()) :: {:ok, Ethers.Types.t_address()} | :error
Parses the delegate address from an EIP-7702 delegation designator.
Takes account code as a binary (as returned by Ethers.get_code/2) and returns
{:ok, delegate_address} (checksummed) if it is exactly a delegation designator:
0xef0100 ++ address, 23 bytes in total. Returns :error for anything else: empty code,
regular contract code or code of any other length. Ethers.get_delegation/2 fetches and
parses the code of an account in one step.
Applying an authorization for the zero address (see clear/1) does not write a designator:
it resets the account code to empty, for which this function returns :error.
Examples
iex> code = Ethers.Utils.hex_decode!("0xef010090f8bf6a479f320ead074411a4b0e7944ea8c9c1")
iex> Ethers.Authorization.parse_delegation(code)
{:ok, "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"}
iex> Ethers.Authorization.parse_delegation("")
:error