Skip to content
LogoLogo

Watch Access Key Witnesses

accessKey.watchWitness

Watches KeyAuthorizationWitness events on the account keychain.

Usage

import {  } from './viem.config'
 
const  = ..()
 
.(() => {
  for (const  of ) .(.)
{ account: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb', witness: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef' }
})
 
// Later, tear down the watcher.
.()

Also callable standalone: Actions.accessKey.watchWitness(client, options), with Actions imported from 'viem/tempo'.

Recipes

Activate Checkout Sessions When Their Witness Lands Onchain

Filter args.witness to the witnesses you issued via accessKey.signAuthorization to mark each checkout session active once its authorization lands.

import {  } from './viem.config'
 
// Witnesses issued with pending checkout sessions.
const  = new <`0x${string}`, string>([
  [
    '0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658',
    'order_1024',
  ],
])
 
const  = ..({
  : { : [....()] },
})
 
.(() => {
  for (const  of ) {
    const  = .(..)
    if (!) continue
    .(..)
    .('Checkout active for:', )
Checkout active for: order_1024
  }
})

Backfill Missed Authorizations After a Restart

Pass fromBlock so a restarted service replays witness events emitted while it was offline.

import {  } from './viem.config'
 
// Last block the service processed before restarting.
const  = 1_204_320n
 
const  = ..({
  :  + 1n,
})
 
.(() => {
  for (const  of ) {
    .(., ..)
  }
})

Return Value

Returns a watcher handle. Watching starts when the first listener (or async iterator) attaches, and stops when off is called.

type ReturnType = {
  /** Registers a log listener. Starts the watcher on first registration. Returns an unregister function. */
  onLogs: (fn: (logs: readonly Log[]) => void) => () => void
  /** Registers an error listener. Returns an unregister function. */
  onError: (fn: (error: Error) => void) => () => void
  /** Tears down the watcher: removes listeners, ends iterators, stops the poll or subscription. */
  off: () => void
  /** Async-iterates emitted log batches (latest-only stream). */
  [Symbol.asyncIterator]: () => AsyncIterableIterator<{ logs: readonly Log[] }>
}

Each log is decoded, with the event arguments on args:

type Log = {
  /** Decoded `KeyAuthorizationWitness` event arguments. */
  args: {
    account: Address
    witness: Hex
  }
  /** Name of the emitted event. */
  eventName: 'KeyAuthorizationWitness'
  // ...standard log fields (address, blockHash, blockNumber, logIndex, transactionHash, ...)
}

Parameters

args

  • Type: object
type Args = {
  account?: Address | Address[] | null
  witness?: Hex | Hex[] | null
}

Indexed argument values to filter logs by.

batch

  • Type: boolean
  • Default: true

Whether to batch the logs found within a poll interval into a single emission. When false, each log is emitted on its own.

fromBlock

  • Type: bigint

Block number from which to start watching for logs.

poll

  • Type: boolean

Whether to poll for new logs instead of using a subscription. Defaults to true when the transport cannot subscribe or fromBlock is provided.

pollingInterval

  • Type: number
  • Default: client.pollingInterval

Polling frequency (in ms).