Revoke access keys
Change the limits of an access key, or revoke it, on Tempo ID's approval page.
Overview
The chain accepts a change to an access key only from the account owner. An app thus changes or revokes its own access key on Tempo ID's approval page, in the dialog, where the owner signs.
The demo signs a user in with an access key that sends up to 10 OUSD, then lowers the limit to 5 OUSD or revokes the access key.
Walkthrough
Sign the user in with an access key
The viem plugin adds the transaction actions of the last step.
import { accounts } from './tempo.config.js'
export function App() {
const { session } = accounts.useSession()
if (!session?.accessKey)
return (
<button onClick={() => accounts.dialog.signIn()} type="button">
Sign in with Tempo
</button>
)
return <p>Tempo Account {session.accessKey.account}</p>
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '10', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})Change the limits
dialog.updateAccessKey returns the hash of the change's transaction. Each limit sets the amount that the access key can still spend, in base units, and keeps its period. A token that the list leaves out keeps its limit. A negative limit, or two limits on one token, throws Accounts.RequestError with invalid_field before the dialog opens.
import { useState } from 'react'
import { parseUnits } from 'viem'
import { Addresses } from 'viem/tempo'
import { accounts } from './tempo.config.js'
export function AccessKey() {
const [status, setStatus] = useState<string>()
async function change() {
const { transaction } = await accounts.dialog.updateAccessKey({
limits: [{ limit: parseUnits('5', 6), token: Addresses.ousd }],
})
setStatus(`Changed in ${transaction}.`)
}
return (
<>
<button onClick={change} type="button">
Lower the limit to 5 OUSD
</button>
<p role="status">{status}</p>
</>
)
}import { AccessKey } from './AccessKey.js'
import { accounts } from './tempo.config.js'
export function App() {
const { session } = accounts.useSession()
if (!session?.accessKey)
return (
<button onClick={() => accounts.dialog.signIn()} type="button">
Sign in with Tempo
</button>
)
return <AccessKey />
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '10', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})The app renders the access key component once the user signs in. The later steps grow that component.
Revoke the access key
A second button of the same component calls dialog.revokeAccessKey. The returned transaction is null if the chain already held the access key revoked.
import { useState } from 'react'
import { parseUnits } from 'viem'
import { Addresses } from 'viem/tempo'
import { accounts } from './tempo.config.js'
export function AccessKey() {
const [status, setStatus] = useState<string>()
async function change() {
const { transaction } = await accounts.dialog.updateAccessKey({
limits: [{ limit: parseUnits('5', 6), token: Addresses.ousd }],
})
setStatus(`Changed in ${transaction}.`)
}
async function revoke() {
const { transaction } = await accounts.dialog.revokeAccessKey()
setStatus(transaction ? `Revoked in ${transaction}.` : 'Revoked before.')
}
return (
<>
<button onClick={change} type="button">
Lower the limit to 5 OUSD
</button>
<button onClick={revoke} type="button">
Revoke the access key
</button>
<p role="status">{status}</p>
</>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '10', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})The session stays, but its access key signs no further transaction.
Handle a refused change
Cancel rejects each call with Accounts.RequestError, whose code is access_denied. A closed dialog rejects with an AbortError.
import { Accounts } from 'accounts'
import { useState } from 'react'
import { parseUnits } from 'viem'
import { Addresses } from 'viem/tempo'
import { accounts } from './tempo.config.js'
export function AccessKey() {
const [status, setStatus] = useState<string>()
async function change() {
const { transaction } = await accounts.dialog.updateAccessKey({
limits: [{ limit: parseUnits('5', 6), token: Addresses.ousd }],
})
setStatus(`Changed in ${transaction}.`)
}
async function revoke() {
try {
const { transaction } = await accounts.dialog.revokeAccessKey()
setStatus(transaction ? `Revoked in ${transaction}.` : 'Revoked before.')
} catch (error) {
if (
error instanceof Accounts.RequestError &&
error.code === 'access_denied'
)
return setStatus('The access key stays.')
throw error
}
}
return (
<>
<button onClick={change} type="button">
Lower the limit to 5 OUSD
</button>
<button onClick={revoke} type="button">
Revoke the access key
</button>
<p role="status">{status}</p>
</>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '10', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})Handle a revoked access key
If the node refuses a revoked access key's transaction, the viem plugin signs in for a new access key with the same limits and scopes, and sends again. If the user closes that sign-in, the call rejects with Plugins.viem.RetryClosedError, matched by its name.
import { useState } from 'react'
import { parseUnits } from 'viem'
import { Addresses } from 'viem/tempo'
import { accounts } from './tempo.config.js'
export function Send() {
const [status, setStatus] = useState<string>()
async function send() {
try {
await accounts.token.transferSync({
amount: parseUnits('1', 6),
to: '0x000000000000000000000000000000000000dEaD',
token: Addresses.ousd,
})
} catch (error) {
if (
error instanceof Error &&
error.name === 'Plugins.viem.RetryClosedError'
)
return setStatus('The transaction did not send.')
throw error
}
}
return (
<>
<button onClick={send} type="button">
Send 1 OUSD
</button>
<p role="status">{status}</p>
</>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '10', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})Use cases
Each use case sets one option of dialog.updateAccessKey or dialog.revokeAccessKey, or of the viem plugin.
Change the scopes
scopes replaces the calls that the access key may make. Without it, the scopes stay.
import { Addresses, Scopes } from 'viem/tempo'
import { accounts } from './tempo.config.js'
export function ChangeScopes() {
async function change() {
await accounts.dialog.updateAccessKey({
scopes: [
Scopes.tip20(Addresses.ousd).transfer({
recipients: ['0x000000000000000000000000000000000000dEaD'],
}),
],
})
}
return (
<button onClick={change} type="button">
Send to one recipient only
</button>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '10', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})Show the approval page in a popup
mode: 'popup' shows the approval page in a popup window, with no frame. A popup opens only within a click. Without it, the call takes the plugin's mode.
import { accounts } from './tempo.config.js'
export function Revoke() {
return (
<button
onClick={() => accounts.dialog.revokeAccessKey({ mode: 'popup' })}
type="button"
>
Revoke the access key
</button>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '10', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})Cancel a revocation
signal ends a change or a revocation from the page, such as after a time limit. Its abort closes the dialog or the popup, and rejects the call with the signal's reason.
import { accounts } from './tempo.config.js'
export function Revoke() {
return (
<button
onClick={() =>
accounts.dialog.revokeAccessKey({
// The dialog closes after two minutes without an approval.
signal: AbortSignal.timeout(120_000),
})
}
type="button"
>
Revoke the access key
</button>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '10', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})Reject in place of a new sign-in
The viem plugin's retry, with accessKey set to false, turns off the sign-in for a new access key. The transaction then rejects with the node's refusal, KeyAlreadyRevoked.
import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '10', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem({ retry: { accessKey: false } })],
})Revoke an access key that a server holds
Each call takes the accessKey and account of another of the app's access keys. An access key on several other chains also needs chainId.
import { accounts } from './tempo.config.js'
export function Revoke() {
async function revoke() {
await accounts.dialog.revokeAccessKey({
accessKey: '0x9f10aF4d1a3F1a2bD3C6f1B8e7a5D2c4E6b80f08',
account: '0xe8Eb65558c6c437bC38F07cF0D66CF74E87fD946',
})
}
return (
<button onClick={revoke} type="button">
Revoke the server's access key
</button>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '10', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})If Tempo ID did not sign that access key for the app, the call rejects with unknown_key.