Sign Access Key Authorization
accessKey.signAuthorization
Signs a key authorization for an access key.
Usage
import { } from 'viem/utils'
import { } from 'viem/tempo'
import { } from './viem.config'
const = .('0x...')
const = .(.(), {
: ,
})
const = await ..({
,
: .((.() + 30_000) / 1000),
})
.('Key authorization:', )
Key authorization: { address: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb', chainId: 1337n, type: 'p256', ... }import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Recipes
Provision the Key with the User's First Payment
Attach the signed authorization to a token.transfer via keyAuthorization, so provisioning lands with the first payment instead of a standalone transaction.
import { } from 'viem/utils'
import { } from 'viem/tempo'
import { } from './viem.config'
const = .('0x...')
const = .(.(), {
: ,
})
// Sign the authorization offchain: no transaction is sent yet.
const = await ..({
,
})
// The authorization is provisioned with the first payment the key signs.
const { } = await ..({
: ,
: { : '49.99' },
,
: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
: '0x20c0000000000000000000000000000000000000',
})import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Bind the Authorization to a Server Challenge
Pass a server-issued challenge as witness to tie the authorization to a single session, and cancel it with accessKey.burnWitness before it lands.
import { } from 'viem/utils'
import { } from 'viem/tempo'
import { } from './viem.config'
const = .('0x...')
const = .(.(), {
: ,
})
// Single-use challenge issued by your server for this session.
const =
'0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658'
const = await ..({
,
: ,
})import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Return Value
type ReturnType = KeyAuthorization.SignedA signed key authorization object that can be passed as keyAuthorization to any write action.
Parameters
accessKey
- Type:
Address | AccessKeyAccount | { address: Address; type: SignatureEnvelope.Type } | { publicKey: Hex; type: SignatureEnvelope.Type }
Access key to authorize.
account
- Type:
Account | Hex
Account to sign with.
admin
- Type:
boolean
Whether to authorize the key as an admin key. Admin keys are unrestricted and can manage the account's other access keys; expiry, limits, and scopes are ignored. Requires the T6 hardfork.
chainId
- Type:
number | bigint
Chain ID for replay protection.
expiry
- Type:
number
Unix timestamp when the key expires.
limits
- Type:
readonly { token: Address; limit: bigint; period?: number }[]
Spending limits per token.
scopes
- Type:
readonly { address: Address; selector?: Hex | string; recipients?: readonly Address[] }[]
Call scopes restricting which contracts, selectors, and recipients the key can call.
witness
- Type:
Hex
Optional 32-byte witness bound into the authorization's signing hash.