import type { InnerWalletApiClient } from "../../types.ts"; import { toHex, type Address, type IsUndefined } from "viem"; import { AccountNotFoundError } from "@aa-sdk/core"; import type { WalletServerRpcSchemaType } from "@alchemy/wallet-api-types/rpc"; import { metrics } from "../../metrics.js"; type RpcSchema = Extract< WalletServerRpcSchemaType, { Request: { method: "wallet_prepareSign"; }; } >; export type PrepareSignParams< TAccount extends Address | undefined = Address | undefined, > = Omit & (IsUndefined extends true ? { from: Address } : { from?: never }); export type PrepareSignResult = RpcSchema["ReturnType"]; /** * Prepares a signature request for signing messages or transactions. * * @param {InnerWalletApiClient} client - The wallet API client to use for the request * @param {PrepareSignParams} params - Parameters for preparing the signature request * @returns {Promise} A Promise that resolves to the prepare sign result containing a signature request * * @example * ```ts * // Prepare a message to be signed * const result = await client.prepareSign({ * from: "0x1234...", * type: "personal_sign", * data: "Hello, world!", * }); * ``` */ export async function prepareSign< TAccount extends Address | undefined = Address | undefined, >( client: InnerWalletApiClient, params: PrepareSignParams, ): Promise { metrics.trackEvent({ name: "prepare_sign", }); const from = params.from ?? client.account?.address; if (!from) { throw new AccountNotFoundError(); } return client.request({ method: "wallet_prepareSign", params: [{ ...params, from, chainId: toHex(client.chain.id) }], }); }