Skip to content
LogoLogo

signAuthorization

Signs an EIP-7702 Authorization. The signed Authorization can be used in Transaction APIs like sendTransaction and writeContract to delegate an authorized Contract onto an Account.

Usage

A Contract can be authorized by supplying a contractAddress. By default, it will be signed over the Account's next available Nonce and the current Chain ID. You can also explicitly set the nonce and chainId.

import {  } from 'viem/accounts'
import {  } from './client'
 
const  = ('0x...')
 
const  = await .({ 
  : , 
  : '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
}) 
}
const = await .({ : [], : '0xdeadbeef', : ., })

Explicit Scoping

We can explicitly sign over a provided nonce and/or chainId by supplying them as parameters:

// @filename: client.ts
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
 
const relay = privateKeyToAccount('0x...')
 
export const walletClient = createWalletClient({
  account: relay,
  chain: mainnet,
  transport: http(),
})
// @filename: example.ts
// ---cut---
import { walletClient } from './client'
 
const eoa = privateKeyToAccount('0x...')
 
const authorization = await walletClient.signAuthorization({
  account: eoa,
  contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  chainId: 10, 
  nonce: 420, 
})
}
yParity: 0,
s: "0x1b2687608968ecb67230bbf7944199560fa2b3cffe9cc2b1c024e1c8f86a9e08",
r: "0xf507fb8fa33ffd05a7f26c980bbb8271aa113affc8f192feba87abe26549bda1",
nonce: 420,
contractAddress: "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2",
chainId: 10,
{
 
const hash = await walletClient.sendTransaction({
  authorizationList: [authorization],
  data: '0xdeadbeef',
  to: eoa.address,
})

Returns

SignedAuthorization

A signed Authorization object.

Parameters

account

  • Type: Account

Account to use for delegation.

Accepts a Local Account (Private Key, etc).

import {  } from 'viem/accounts'
import {  } from './client'
 
const  = await .({
  : ('0x...'), 
  : '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2'
}) 

chainId (optional)

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

The Chain ID to scope the Authorization to.

import {  } from 'viem/accounts'
import {  } from './client'
 
const  = await .({
  : ('0x...'),
  : '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  : 1, 
}) 

contractAddress

  • Type: Address

The target Contract to delegate to the Account.

import {  } from 'viem/accounts'
import {  } from './client'
 
const  = await .({
  : ('0x...'),
  : '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2'
}) 

executor (optional)

  • Type: 'self' | undefined

Whether the EIP-7702 Transaction will be executed by the Account that signed the Authorization.

If not specified, it will be assumed that the EIP-7702 Transaction will be executed by another Account (ie. a relayer Account).

import {  } from 'viem/accounts'
import {  } from './client'
 
const  = await .({
  : ('0x...'),
  : '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  : 'self', 
}) 

nonce (optional)

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

The nonce to scope the Authorization to.

import {  } from 'viem/accounts'
import {  } from './client'
 
const  = await .({
  : ('0x...'),
  : '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  : 69, 
})