> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://accounts.tempo.xyz/api/mcp` to find what you need.

# `Keystore`

Pluggable backends for key material. A keystore owns a key end to end: `createKey()` provisions it and returns an opaque handle, and `toAccount()` turns a persisted record back into a signing account. The [`Provider`](/docs/api/provider#accesskeykeystores) uses keystores to back access keys, one keystore per key type.

Keystores hold key material; [`Storage`](/docs/api/storage) persists provider state. A keystore can be local (WebCrypto, Secure Enclave / Android Keystore) or remote (an HSM or key-management service).

## Keystores

[Keystore.p256](/docs/api/keystore.p256): Pure-JS P-256 keys

[Keystore.secp256k1](/docs/api/keystore.secp256k1): Pure-JS secp256k1 keys

[Keystore.webCryptoP256](/docs/api/keystore.webCryptoP256): WebCrypto P-256 keys — the built-in default

## Interface

Apps with custom needs (own native module, HSM, remote signer) implement the two-function interface:

```ts
type Handle = { kind: string; [key: string]: unknown }

type Keystore = {
  /**
   * Whether handles hold live objects (e.g. a CryptoKey) that persist only
   * through structured-clone storage — on other storage the handle is
   * stripped at persist time, making the key session-only. When false
   * (default), handles must survive JSON serialization, portable across
   * all storage adapters.
   */
  requiresStructuredClone?: boolean
  /**
   * Creates access-key material. `handle` is opaque and persisted verbatim
   * by the SDK. Must fail loudly when runtime prerequisites are missing
   * (e.g. no Secure Enclave, no `crypto.subtle`) so provisioning errors
   * surface at authorization time, not at first sign.
   */
  createKey(): Promise<{ publicKey: Hex; handle: Handle }>
  /**
   * Turns a persisted key record back into a signing account.
   * Called once per launch; the SDK caches the result per record.
   */
  toAccount(
    record: { keyType: string; publicKey: Hex; handle: Handle },
    context: { access: Address; keyAuthorizationManager: KeyAuthorizationManager },
  ): Promise<Account> | Account
}
```

### Handles

The handle is opaque to the SDK: it is persisted verbatim alongside the access-key record (so material and record share one lifecycle — one write, one backup, one removal) and handed back to `toAccount` on hydration. The `kind` discriminant marks which keystore wrote it; the remaining fields are owned by that keystore and must survive its declared persistence format.

### Errors

`toAccount` errors drive the record lifecycle:

* Throw `Keystore.KeyUnavailableError` when the key behind the handle is **permanently gone** (e.g. hardware key deleted, app keychain wiped). The SDK evicts the record so callers fall back to authorizing a fresh key.
* Throw any other error for handles the keystore does not recognize or **transient** failures (e.g. device locked): the record is kept and retried on next use.

A keystore composing multiple backends — say, hardware keys with a software fallback — should treat `KeyUnavailableError` as an ownership claim and not route the handle to another backend, and should gate provisioning fallback on a capability check rather than on errors, so a transient failure never silently downgrades to a weaker backend.
