Passkey Recovery for Circle Smart Accounts
Reference implementation for setting up and executing wallet recovery using a BIP-39 mnemonic phrase to register a new passkey when the original is lost.
Source: passkey-recovery example
Environment Variables
Scaffolding (Next.js vs Vite pairs and fixed Client URL) lives in SKILL.md. This example uses Vite:
VITE_CLIENT_KEY=
VITE_CLIENT_URL=https://modular-sdk.circle.com/v1/rpc/w3s/buidlSetup
import { createPublicClient, type Hex } from 'viem'
import { polygonAmoy } from 'viem/chains'
import { english, generateMnemonic, mnemonicToAccount } from 'viem/accounts'
import {
type P256Credential,
type SmartAccount,
type WebAuthnAccount,
createBundlerClient,
toWebAuthnAccount,
} from 'viem/account-abstraction'
import {
recoveryActions,
toCircleSmartAccount,
toModularTransport,
toPasskeyTransport,
toWebAuthnCredential,
WebAuthnMode,
} from '@circle-fin/modular-wallets-core'
import { validateMnemonic } from 'bip39'
const clientKey = import.meta.env.VITE_CLIENT_KEY as string
const clientUrl = import.meta.env.VITE_CLIENT_URL as string
const passkeyTransport = toPasskeyTransport(clientUrl, clientKey)
const modularTransport = toModularTransport(`${clientUrl}/polygonAmoy`, clientKey)
const client = createPublicClient({
chain: polygonAmoy,
transport: modularTransport,
})
// Extend bundler client with recovery actions for managing account recovery
const bundlerClient = createBundlerClient({
chain: polygonAmoy,
transport: modularTransport,
}).extend(recoveryActions)Recovery Setup (while user has access)
Step 1: Generate Recovery Key
Generate a BIP-39 mnemonic and derive the recovery EOA address. The user must save this phrase securely -- it is the only way to recover the account if the passkey is lost.
const mnemonic = generateMnemonic(english)
const recoveryEoa = mnemonicToAccount(mnemonic)
const recoveryAddress: Hex = recoveryEoa.address
// Display mnemonic to user and instruct them to save it securelyStep 2: Register Recovery Address On-Chain
Register the recovery EOA address with the smart account. This is an on-chain transaction that links the recovery address to the account.
await bundlerClient.registerRecoveryAddress({
account, // the user's existing Circle Smart Account
recoveryAddress, // EOA address derived from the mnemonic
paymaster: true,
})Recovery Execution (when passkey is lost)
Step 3: Validate Mnemonic and Create New Passkey
Prompt the user for their saved mnemonic, validate it, and register a new passkey credential.
if (!validateMnemonic(userEnteredMnemonic.trim())) {
throw new Error('Invalid recovery phrase')
}
const newCredential = await toWebAuthnCredential({
transport: passkeyTransport,
mode: WebAuthnMode.Register,
username: `${originalUsername}-recovered-${Date.now()}`,
})Step 4: Execute Recovery
Use the mnemonic EOA to add the new passkey as an owner. This does NOT remove the old passkey; if it may have been stolen rather than lost, remove it with a separate owner-removal flow (Set up passkey recovery).
const localAccount = mnemonicToAccount(userEnteredMnemonic.trim())
// Create a temporary smart account using the recovery EOA as owner
const tempAccount = await toCircleSmartAccount({
client,
owner: localAccount,
})
// Adds newCredential as an owner
await bundlerClient.executeRecovery({
account: tempAccount,
credential: newCredential,
paymaster: true,
})
// Persist the new credential for future sessions
localStorage.setItem('credential', JSON.stringify(newCredential))Expected Flow Summary
- Setup: User registers passkey -> generates mnemonic -> registers recovery address on-chain
- Loss: Original passkey becomes inaccessible (lost device, deleted credential)
- Recovery: User enters mnemonic -> creates new passkey ->
executeRecoveryadds it as an owner on-chain - Result: The new passkey can sign; the old passkey is NOT removed (see Step 4)