viem-goviem-go

prepareAuthorization

Prepares an EIP-7702 authorization for signing

prepareAuthorization

Prepares an EIP-7702 Authorization object 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 the Authorization object.

Import

Import the wallet actions package so you can call this action.

import "github.com/ChefBingbong/viem-go/actions/wallet"

Usage

See how to construct a wallet client and call this action.

import (
"context"
"log"
"github.com/ChefBingbong/viem-go/actions/wallet"
"github.com/ChefBingbong/viem-go/client"
"github.com/ChefBingbong/viem-go/client/transport"
"github.com/ChefBingbong/viem-go/chain/definitions"
)
ctx := context.Background()
walletClient, err := client.CreateWalletClient(client.WalletClientConfig{
Chain: definitions.Mainnet,
Transport: transport.HTTP("https://eth.llamarpc.com"),
})
if err != nil {
log.Fatal(err)
}
defer func() { _ = walletClient.Close() }()
auth, err := wallet.PrepareAuthorization(ctx, walletClient, wallet.PrepareAuthorizationParameters{
ContractAddress: "0xA0Cf798816D4b9b9866b5330EEa46a18382f251e",
})
if err != nil {
log.Fatal(err)
}
log.Printf("Prepared authorization: %+v", auth)

Returns

authorization.AuthorizationRequest

The prepared authorization request with all required fields populated.

Parameters

Account

Control which account is used to prepare the authorization.

  • Type: Account

  • Optional

The account to prepare authorization for. If nil, uses the client's account.

import "github.com/ChefBingbong/viem-go/accounts"
account, err := accounts.PrivateKeyToAccount("0x...")
if err != nil {
log.Fatal(err)
}
auth, err := wallet.PrepareAuthorization(ctx, walletClient, wallet.PrepareAuthorizationParameters{
Account: account,
ContractAddress: "0xA0Cf798816D4b9b9866b5330EEa46a18382f251e",
})

ContractAddress

Specify the contract address that the authorization applies to.

  • Type: string

-- Required

The contract address being authorized. Either ContractAddress or Address must be provided.

auth, err := wallet.PrepareAuthorization(ctx, walletClient, wallet.PrepareAuthorizationParameters{
ContractAddress: "0xA0Cf798816D4b9b9866b5330EEa46a18382f251e",
})

Address

Optionally provide an alias for the contract address.

  • Type: string

  • Optional

An alias for ContractAddress (for compatibility).

ChainID

Override the chain ID used for the authorization.

  • Type: *int

  • Optional

The chain ID for the authorization. If nil, it will be fetched from the client's chain or via eth_chainId.

chainID := 1
auth, err := wallet.PrepareAuthorization(ctx, walletClient, wallet.PrepareAuthorizationParameters{
ContractAddress: "0xA0Cf798816D4b9b9866b5330EEa46a18382f251e",
ChainID: &chainID,
})

Nonce

Override the nonce used for the authorization.

  • Type: *int

  • Optional

The nonce for the authorization. If nil, it will be fetched via eth_getTransactionCount with "pending" block tag.

nonce := 0
auth, err := wallet.PrepareAuthorization(ctx, walletClient, wallet.PrepareAuthorizationParameters{
ContractAddress: "0xA0Cf798816D4b9b9866b5330EEa46a18382f251e",
Nonce: &nonce,
})

Executor

Configure who will execute the EIP-7702 transaction.

  • Type: any

  • Optional

Specifies who will execute the EIP-7702 transaction:

  • nil: assumes another account will execute
  • "self": the signing account will execute (nonce += 1)
  • Account: a specific account will execute (nonce += 1 if same as signing account)
auth, err := wallet.PrepareAuthorization(ctx, walletClient, wallet.PrepareAuthorizationParameters{
ContractAddress: "0xA0Cf798816D4b9b9866b5330EEa46a18382f251e",
Executor: "self",
})