Skip to Content
butr
Guides

Custom storage

Back persistence with memory, React Native AsyncStorage, cookies, or a WalletPersistence of your own.

The manager persists through config.storage, a WalletPersistence. There are two levels to customise it at:

  • A storage driver keeps butr's key layout and swaps where the strings go: memory, AsyncStorage. Wrap it with createWalletStorage. For cookies, use createCookieWalletStorage, which also reads the state on the server.
  • A WalletPersistence replaces the layout entirely: one key, a server, IndexedDB. Implement load and save.

See persistence for what is stored and when.

Drivers

A driver is a three-method get / set / remove interface that may be async:

type StorageDriver = {
  getItem: (key: string) => MaybePromise<string | null>;
  removeItem: (key: string) => MaybePromise<void>;
  setItem: (key: string, value: string) => MaybePromise<void>;
};

createWalletStorage takes a persistent driver (pool, selection, active id) and a session driver (the disconnect intent), defaulting to localStorage and sessionStorage:

import type { WalletManagerConfig } from "@usebutr/core";
import { createMemoryStorageDriver, createWalletStorage } from "@usebutr/core";
import { autoDiscovery } from "@usebutr/wallets";

const config: WalletManagerConfig = {
  sources: [autoDiscovery()],
  storage: createWalletStorage({
    keyPrefix: "myapp",
    session: createMemoryStorageDriver(), // in-memory, reset on every start
  }),
};

The key prefix belongs to createWalletStorage.

Cookies

createCookieWalletStorage stores the pool, selection, and active id in cookies and returns, with the persistence, the readInitialState reader a server uses to seed initialState. Both use one key layout, so the prefix is set once:

import { createCookieWalletStorage } from "@usebutr/core";

const walletStorage = createCookieWalletStorage({
  domain: "example.com", // share across subdomains; omit for the current host
  keyPrefix: "myapp",
  maxAgeSeconds: 604_800, // defaults to 30 days
  path: "/",
  sameSite: "lax",
  secure: true, // false for local development on http://
});

The disconnect intent stays in sessionStorage. See SSR without a flash for the server half.

React Native / Expo (AsyncStorage)

Drivers may be async, so any AsyncStorage-like API works. This is the exact demo-expo-web driver:

async-storage-driver.ts
import AsyncStorage from "@react-native-async-storage/async-storage";
import type { StorageDriver } from "@usebutr/core";

const asyncStorageDriver: StorageDriver = {
  getItem: (key) => AsyncStorage.getItem(key),
  setItem: (key, value) => AsyncStorage.setItem(key, value),
  removeItem: (key) => AsyncStorage.removeItem(key),
};

export { asyncStorageDriver };

Wire it into the config:

wallet-provider.tsx
import type { WalletManagerConfig } from "@usebutr/core";
import { createWalletStorage } from "@usebutr/core";
import { WalletManagerProvider } from "@usebutr/react";
import { autoDiscovery } from "@usebutr/wallets";
import type { ReactNode } from "react";

import { asyncStorageDriver } from "./async-storage-driver";

const config: WalletManagerConfig = {
  sources: [autoDiscovery()],
  storage: createWalletStorage({
    keyPrefix: "butr-demo",
    persistent: asyncStorageDriver,
    session: asyncStorageDriver,
  }),
};

const WalletProvider = ({ children }: { children: ReactNode }) => (
  <WalletManagerProvider config={config}>{children}</WalletManagerProvider>
);

AsyncStorage has no true session-storage equivalent. The demo backs both drivers with the same store and accepts that session entries outlive the session. butr's session slot holds only the isUserDisconnected flag, which the next connect clears, and butr itself never reads it to decide anything. If your own auto-connect logic depends on it, back session with createMemoryStorageDriver() instead, which resets on every app start.

Your own WalletPersistence

For anything the driver layout doesn't fit, implement the interface directly. The manager hands save the complete state after every change, so an implementation never merges, diffs, or queues:

import type { PersistedWalletState, WalletPersistence } from "@usebutr/core";

const EMPTY: PersistedWalletState = {
  activeConnectorId: null,
  isUserDisconnected: false,
  pool: {},
  selection: {},
};

const storage: WalletPersistence = {
  load: async () => (await db.get<PersistedWalletState>("wallets")) ?? EMPTY,
  save: async (state) => {
    await db.put("wallets", state);
  },
};
  • load runs once, when the manager starts. A rejection is reported through onStorageError and treated as empty storage, so validate what you read rather than trusting it.
  • save never runs before load has resolved, and saves never overlap: while one is in flight, later changes collapse into one follow-up call with the latest state.
  • A rejected save is reported through onStorageError and does not affect the in-memory state.
  • initialState takes any WalletSnapshot, so server rendering from your own layout means building that snapshot yourself.

Source: apps/demo-expo-web/src/async-storage-driver.ts, apps/demo-expo-web/src/wallet-provider.tsx, and packages/core/src/storage in the butr repository. See also persistence concepts.