accounts
Version:
Tempo Accounts SDK
163 lines • 7.37 kB
TypeScript
import type { Address } from 'viem/accounts';
import * as Adapter from '../Adapter.js';
/**
* Creates a Privy adapter backed by Privy sessions and embedded Ethereum wallets.
*
* The adapter owns silent reconnect, session-expiry cleanup, and signing. Apps supply
* the UI-bearing login flow via `loadAccounts` (and optionally a distinct `createAccount`
* for registration). Callbacks fire only on user-initiated `wallet_connect`/registration —
* never during silent restore on page reload.
*
* Silent restore on page reload pulls wallets directly from the Privy SDK
* (`client.user.get` + `client.embeddedWallet.getEthereumProvider`), so apps don't
* need to re-run the login UI when the user returns with a still-valid Privy session.
*
* React apps can use `accounts/react/privy` to supply the UI-bearing login flow.
* Core JS integrations may still return a subset of embedded wallet addresses; if
* omitted, the adapter exposes every embedded wallet on the resulting Privy user.
*
* @example
* ```ts
* const provider = Provider.create({
* adapter: privy({
* client,
* loadAccounts: async ({ client }) => {
* await signInWithPrivyEmail(client.auth)
* },
* }),
* })
* ```
*/
export declare function privy<const client extends privy.Client>(options: privy.Options<client>): Adapter.Adapter;
export declare namespace privy {
/** Options for {@link privy}. */
type Options<client extends Client = Client> = {
/** Existing structural Privy client, such as `Privy` from Core JS or a React hook-backed shim. */
client: client;
/**
* Runs the Privy registration UI. May optionally return a subset of the user's
* embedded wallet addresses to expose to the provider; if omitted, the adapter
* exposes every embedded wallet on the resulting Privy user.
*
* The adapter materializes EIP-1193 providers internally via
* `client.embeddedWallet.getEthereumProvider`, so callbacks don't need to.
*
* Defaults to `loadAccounts` — apps that don't distinguish register vs login
* can omit this.
*/
createAccount?: ((parameters: {
/** Initialized Privy client. */
client: client;
/** Provider create-account parameters. */
parameters: Adapter.createAccount.Parameters;
}) => Promise<AccountSelection>) | undefined;
/** Data URI of the provider icon. @default Black 1×1 SVG. */
icon?: `data:image/${string}` | undefined;
/**
* Runs the Privy login UI in response to a user-initiated `wallet_connect`.
* May optionally return a subset of the user's embedded wallet addresses to expose
* to the provider; if omitted, the adapter exposes every embedded wallet on the
* Privy user.
*
* Silent restore on page reload pulls wallets directly from the Privy SDK
* (`client.user.get` + `client.embeddedWallet.getEthereumProvider`) and does
* NOT call this function.
*/
loadAccounts: (parameters: {
/** Initialized Privy client. */
client: client;
/** Provider load-accounts parameters. */
parameters?: Adapter.loadAccounts.Parameters | undefined;
}) => Promise<AccountSelection>;
/** Display name of the provider. @default "Privy" */
name?: string | undefined;
/** Reverse DNS identifier. @default "io.privy" */
rdns?: string | undefined;
};
/**
* Optional subset of embedded wallet addresses returned from `createAccount` /
* `loadAccounts`. `void`/`undefined` means "expose every embedded wallet".
*/
type AccountSelection = readonly Address[] | void;
/**
* Minimal structural Privy client surface used by the adapter for session checks,
* silent restore, and disconnect. User-initiated `wallet_connect`/registration
* is delegated to the app's `loadAccounts` / `createAccount` callbacks.
*
* Satisfied by `Privy` from `@privy-io/js-sdk-core` and by hook-backed shims around
* `@privy-io/react-auth`. The adapter never imports either Privy SDK itself; the
* structural shape keeps the dependency one-way.
*/
type Client = {
/** Auth API; the adapter only needs `logout`. */
auth: {
/**
* Clears the current Privy session. The adapter passes the current user id
* (when available) so multi-tab/multi-user setups scope the logout correctly.
*/
logout: (parameters?: {
userId: string;
} | undefined) => Promise<void> | void;
};
/** Embedded wallet API used by the adapter to materialize EIP-1193 providers. */
embeddedWallet: {
/** Returns an EIP-1193 provider for a Privy embedded Ethereum wallet. */
getEthereumProvider(parameters: {
wallet: LinkedAccount;
entropyId: string;
entropyIdVerifier: string;
}): Promise<EthereumProvider> | EthereumProvider;
};
/** Returns the current Privy access token, or `null` if no session. */
getAccessToken: () => Promise<string | null>;
/** Initializes the client. Called once by the adapter, before any other method. */
initialize?: (() => Promise<void> | void) | undefined;
/** User API used by the adapter to scope `auth.logout` and to silently restore wallets. */
user: {
/** Returns the currently authenticated Privy user. */
get: () => Promise<{
user: User;
}>;
};
};
/** Minimal Privy user shape used by the adapter for silent restore. */
type User = {
id: string;
linked_accounts?: readonly LinkedAccount[] | undefined;
};
/** Minimal Privy linked account shape used by the adapter for silent restore. */
type LinkedAccount = {
/** Embedded wallet address. */
address?: string | undefined;
/** Privy wallet chain type. */
chain_type?: string | undefined;
/** Privy wallet connector type. */
connector_type?: string | undefined;
/** Privy linked account type. */
type?: string | undefined;
/** Privy wallet client type. */
wallet_client_type?: string | undefined;
/** Privy embedded wallet index. */
wallet_index?: number | undefined;
};
/** Minimal EIP-1193 provider surface used by the adapter for `secp256k1_sign`. */
type EthereumProvider = {
request(parameters: {
method: string;
params?: readonly unknown[] | undefined;
}): Promise<unknown>;
};
/**
* Materialized Privy embedded wallet — the `{ address, provider }` shape the
* adapter caches internally after calling
* `client.embeddedWallet.getEthereumProvider`. The adapter calls
* `provider.request({ method: 'secp256k1_sign', params: [hash] })` for signing.
*/
type EmbeddedWallet = {
/** Embedded Ethereum wallet address. */
address: string;
/** EIP-1193 provider used for `secp256k1_sign` fallback signing. */
provider: EthereumProvider;
};
}
//# sourceMappingURL=privy.d.ts.map