Guest checkout
A visitor pays from a guest account, whose key pair the app keeps in its store, with no Tempo ID user.
Overview
A guest session signs a visitor in with no Tempo ID user. The instance keeps a P256 key pair in its store, and the key pair owns a root Tempo Account, the guest account.
Tempo ID issues a guest session no tokens and no access key. The key pair signs each payment and transaction of the visitor in the app. Guest sessions describes the side of Tempo ID.
The demo signs a visitor in as a guest, and pays 1 OUSD from the guest account. A new guest account holds no funds, so Deposit on the payment page offers the testnet faucet.
Walkthrough
These steps build a React app that signs a visitor in as a guest, and takes a payment from the guest account.
Create the instance
With guest: true, each sign-in offers Continue as guest, beside the passkey and the emailed code. An instance without it never holds a guest session.
In a browser, the default store is IndexedDB. It keeps the key pair as a non-extractable CryptoKey.
import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
guest: true,
plugins: [react(), viem()],
})Sign the visitor in
dialog.signIn opens the sign-in of Tempo ID. If the visitor selects Continue as guest, the instance loads or creates the key pair, and resolves with a guest session.
A guest session holds the address of the guest account, and its kind is 'guest'. The store keeps the key pair under its own entry, so the session holds no private key.
import { Checkout } from './Checkout.js'
import { accounts } from './tempo.config.js'
export function App() {
const { session } = accounts.useSession()
if (!session)
return (
<button onClick={() => accounts.dialog.signIn()} type="button">
Sign in with Tempo
</button>
)
const label =
session.kind === 'guest'
? `Guest account ${session.address}`
: `Signed in as ${session.user.email}`
return (
<main>
<p>{label}</p>
<Checkout />
</main>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
guest: true,
plugins: [react(), viem()],
})A call that reads Tempo ID, such as a read of the access token, throws Accounts.GuestSessionError with a guest session.
Pay
With a guest session, accounts.pay opens the payment page of Tempo ID for the guest account, in a dialog. The visitor selects Pay, and the key pair signs the transfer in the page of the app.
The token is a symbol that the chain lists, such as ousd, or the address of a TIP-20 token. The key pair signs one transaction for each call, which makes exactly the requested transfer.
import { useState } from 'react'
import { accounts } from './tempo.config.js'
export function Checkout() {
const [transaction, setTransaction] = useState<string>()
return (
<>
<button
onClick={async () => {
const { transaction } = await accounts.pay({
amount: '5',
to: '0x000000000000000000000000000000000000dEaD',
token: 'ousd',
})
setTransaction(transaction)
}}
type="button"
>
Pay 5 OUSD
</button>
{transaction && <p>Paid in {transaction}.</p>}
</>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
guest: true,
plugins: [react(), viem()],
})A new guest account holds no funds. If it holds less than the payment, Deposit on the payment page opens the deposit pages of Tempo ID for the guest account.
| Deposit method | For a guest |
|---|---|
| Tempo transfer | Offered on every chain, to the guest account's address |
| EVM bridge | Offered on Tempo mainnet, for an OUSD payment |
| Buy MACH | Not offered, since its cards and receipts need a user |
| Swap from the balance | Not offered, since the key pair signs only the payment |
Done returns to the same checkout, and Pay enables once the funds arrive. On a test chain, the deposit pages also offer the faucet of the token.
Use cases
Pay without a session
With guest: true and no session, accounts.pay opens the signed-out payment page, which offers Continue as guest. That selection starts the guest session, and the same page pays from the guest account.
If the visitor signs in instead, the page pays from the account of the user. A call with challenge, or an instance with the siwe plugin, offers no guest.
import { accounts } from './tempo.config.js'
export function Checkout() {
return (
<button
onClick={async () => {
// With no session, the payment page signs the visitor in, or starts a guest session.
const { transaction } = await accounts.pay({
amount: '5',
to: '0x000000000000000000000000000000000000dEaD',
token: 'ousd',
})
console.log(transaction)
}}
type="button"
>
Pay 5 OUSD
</button>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
guest: true,
plugins: [react(), viem()],
})Offer a guest in each sign-in flow
With guest: true, each sign-in plugin offers Continue as guest, and resolves with a guest session after that selection.
| Sign-in | Returns the selection |
|---|---|
dialog.signIn, in a frame or a popup | The frame's or the popup's message |
redirect.signIn | The redirect URI with guest=1, which the next load of the page completes |
device.signIn | The refusal guest_selected of the next poll, which the plugin turns into the session |
Outside a browser, Store.file keeps the key pair in a file that only the user reads. The expo preset keeps it in the secure store of the device.
A store that other parties read, such as Store.postgres, refuses the key pair. So does Store.memory in a page, since a reload loses the funds.
import { Accounts, Store } from 'accounts'
import { device } from 'accounts/plugins'
export const accounts = Accounts.create({
chain: 'testnet',
clientId: 'cli:acme-example',
guest: true,
plugins: [
device({
onCode: (code) =>
console.log(
`Open ${code.verificationUriComplete} and confirm the code ${code.userCode}.`,
),
}),
],
// The key pair of the guest account stays in this file.
store: Store.file('~/.acme/accounts.json'),
})Sign a guest in to the app
With the siwe plugin on the page, dialog.signIn offers Continue as guest too. After that selection, the guest key pair signs the plugin's message, and the call posts the signature to the verify route before it sets the guest session.
The call returns the verify route's JSON object as auth, as for a Tempo ID sign-in. The guest account is a root Tempo Account, so the route verifies its P256 signature as the account's own. Sign in with the plugin on the page describes the routes.
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<button
onClick={async () => {
// A guest or a Tempo ID user, signed in to the app's routes either way.
const { auth } = await accounts.dialog.signIn()
console.log(auth)
}}
type="button"
>
Sign in
</button>
)
}import { Accounts } from 'accounts'
import { siwe, viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
guest: true,
plugins: [react(), siwe({ auth: '/api/auth' }), viem()],
})Pay from a CLI
Outside a page, accounts.pay pays from the guest account through the routes of Tempo ID, with no page. Actions.guest.pay describes each request and check.
| Guest account | What pay does |
|---|---|
| Holds the payment | Pays at once |
| Short, on a test chain | Requests the chain's faucet, then pays |
| Short, on another chain | Passes a funding code to the device plugin's onCode, waits for the funds until the code expires, then pays |
Short, without device | Rejects with Accounts.RequestError and the reason insufficient_balance |
If Tempo ID and the chain confirm no outcome, the call rejects with Actions.guest.PendingError, which names the checkout. The store keeps no record of the checkout, so the app confirms its outcome before it pays again.
import { accounts } from './tempo.config.js'
const session = (await accounts.session.get()) ?? (await accounts.device.signIn())
if (session.kind === 'guest') console.log(`Paying as guest ${session.address}.`)
const { transaction } = await accounts.pay({
amount: '5',
to: '0x000000000000000000000000000000000000dEaD',
token: 'ousd',
})
console.log(`Paid in ${transaction}.`)import { Accounts, Store } from 'accounts'
import { device } from 'accounts/plugins'
export const accounts = Accounts.create({
chain: 'testnet',
clientId: 'cli:acme-example',
guest: true,
plugins: [
device({
// Shows the sign-in code, and the funding code of a short guest account.
onCode: (code) =>
console.log(
`Open ${code.verificationUriComplete} and confirm the code ${code.userCode}.`,
),
}),
],
store: Store.file('~/.acme/accounts.json'),
})Send a transaction with Viem
The viem plugin's client signs each transaction of a guest session with the key pair, as the root account. Its relay pays fees as for any session. Viem covers the client.
import { parseUnits } from 'viem'
import { Addresses } from 'viem/tempo'
import { accounts } from './tempo.config.js'
export function Send() {
return (
<button
onClick={async () => {
const { receipt } = await accounts.viem.token.transferSync({
amount: parseUnits('1', 6),
to: '0x000000000000000000000000000000000000dEaD',
token: Addresses.ousd,
})
console.log(receipt.transactionHash)
}}
type="button"
>
Send 1 OUSD
</button>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
guest: true,
plugins: [react(), viem()],
})Pay MPP charges
With a guest session, the mpp plugin pays each charge and voucher with the key pair. No access key limits the guest account, so a charge needs maxAmount, and a payment session maxDeposit.
| Without the cap | Error |
|---|---|
| A charge above 0 | mpp.MaxAmountRequiredError, before the key pair signs |
| A payment session | mpp.MaxDepositRequiredError, before any request |
A malformed maxDeposit | mpp.MaxDepositInvalidError, for a value other than a decimal above 0 with at most 6 decimals |
import { accounts } from './tempo.config.js'
export function Fortune() {
return (
<button
onClick={async () => {
const response = await accounts
.mpp({ maxAmount: '0.05' })
.fetch('https://api.example.com/fortune')
console.log(await response.json())
}}
type="button"
>
Fetch a fortune
</button>
)
}import { Accounts } from 'accounts'
import { mpp } from 'accounts/mpp'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
guest: true,
// Each payment session holds at most 1 OUSD.
plugins: [mpp({ maxDeposit: '1' }), react()],
})Forget the guest account
accounts.signOut ends the guest session and keeps the key pair, so the next guest session holds the same guest account. accounts.guest.get returns its address.
accounts.guest.disconnect removes the key pair from the store, and signs out a guest session. The funds stay on the address, and no key signs for it again.
import { accounts } from './tempo.config.js'
export function Forget() {
return (
<button
onClick={async () => {
const address = await accounts.guest.get()
if (!address) return
// The funds of `address` stay there, with no signer.
await accounts.guest.disconnect()
}}
type="button"
>
Forget the guest account
</button>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
guest: true,
plugins: [react(), viem()],
})Limitations
- A guest pays only in the token of the payment. A payment that needs a swap shows a shortfall.
- The deposit pages offer a guest no Buy MACH and no swap. The EVM bridge pays only an OUSD payment on Tempo mainnet.
- A guest session holds no access key. It takes no
accessKeyorchallengesign-in, and pays no MPP subscription. - The guest key pair signs the
siweplugin's message only indialog.signIn. A payment with the plugin and without a session offers no guest. - A sign-in through the
backendplugin never offers a guest. - A guest account has no passkey and no Tempo ID user. No sign-in turns it into a configurable account.
- Outside a page, a short guest account on a chain without a faucet needs the
deviceplugin'sonCodeto show its funding code.