Skip to content

prepareAuthorization

Prepares an EIP-7702 Authorization for signing. This Action will fill the required fields of the Authorization object if they are not provided (e.g. nonce and chainId).

With the prepared Authorization object, you can use signAuthorization to sign over it.

Usage

example.ts
import { walletClient } from './client'
 
const authorization = await walletClient.prepareAuthorization({ 
  contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
}) 
{
chainId: 1,
contractAddress: "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2",
nonce: 1,
}
const signedAuthorization = await walletClient.signAuthorization(authorization)

Explicit Scoping

We can explicitly set a nonce and/or chainId by supplying them as parameters:

example.ts
import { walletClient } from './client'
 
const authorization = await walletClient.prepareAuthorization({
  contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  chainId: 10, 
})
{
chainId: 10,
contractAddress: "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2",
nonce: 420,
}
const signedAuthorization = await walletClient.signAuthorization(authorization)

Returns

Authorization

A prepared & unsigned Authorization object.

Parameters

account

  • Type: Account

Account to use to prepare the Authorization object.

Accepts a Local Account (Private Key, etc).

import { privateKeyToAccount } from 'viem/accounts'
import { walletClient } from './client'
 
const authorization = await walletClient.prepareAuthorization({
  account: privateKeyToAccount('0x...'), 
  contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2'
}) 

chainId (optional)

  • Type: Address
  • Default: client.chain.id or Network chain ID

The Chain ID to scope the Authorization to. If set to zero (0), then the Authorization will be valid on all chains.

import { privateKeyToAccount } from 'viem/accounts'
import { walletClient } from './client'
 
const authorization = await walletClient.prepareAuthorization({
  account: privateKeyToAccount('0x...'),
  contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  chainId: 1, 
}) 

contractAddress

  • Type: Address

The target Contract to designate onto the Account.

import { privateKeyToAccount } from 'viem/accounts'
import { walletClient } from './client'
 
const authorization = await walletClient.prepareAuthorization({
  account: privateKeyToAccount('0x...'),
  contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2'
}) 

delegate (optional)

  • Type: true | Address | Account

Whether the EIP-7702 Transaction will be executed by another Account.

If not specified, it will be assumed that the EIP-7702 Transaction will be executed by the Account that signed the Authorization.

import { privateKeyToAccount } from 'viem/accounts'
import { walletClient } from './client'
 
const authorization = await walletClient.prepareAuthorization({
  account: privateKeyToAccount('0x...'),
  contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  delegate: true, 
}) 

nonce (optional)

  • Type: Address
  • Default: Account's next available nonce.

The nonce to scope the Authorization to.

import { privateKeyToAccount } from 'viem/accounts'
import { walletClient } from './client'
 
const authorization = await walletClient.prepareAuthorization({
  account: privateKeyToAccount('0x...'),
  contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  nonce: 69, 
})