import type { WalletServerRpcSchemaType } from "@alchemy/wallet-api-types/rpc"; import type { InnerWalletApiClient, WithoutChainId } from "../../types.ts"; import { toHex, type Address, type IsUndefined } from "viem"; import { AccountNotFoundError } from "@aa-sdk/core"; import { metrics } from "../../metrics.js"; type RpcSchema = Extract< WalletServerRpcSchemaType, { Request: { method: "wallet_formatSign"; }; } >; export type FormatSignParams< TAccount extends Address | undefined = Address | undefined, > = Omit, "from"> & (IsUndefined extends true ? { from: Address } : { from?: never }); export type FormatSignResult = RpcSchema["ReturnType"]; /** * Formats a signature request for signing messages or transactions. * * @param {InnerWalletApiClient} client - The wallet API client to use for the request * @param {FormatSignParams} params - Parameters for formatting the signature * @returns {Promise} A Promise that resolves to the formatSign result containing the formatted signature. * * @example * ```ts * // Formats a signature * const result = await client.formatSign({ * from: "0x1234...", * signature: { * type: "ecdsa", * data: "0xabcd..." * }, * }); * ``` */ export async function formatSign< TAccount extends Address | undefined = Address | undefined, >( client: InnerWalletApiClient, params: FormatSignParams, ): Promise { metrics.trackEvent({ name: "format_sign", }); const from = params.from ?? client.account?.address; if (!from) { throw new AccountNotFoundError(); } return client.request({ method: "wallet_formatSign", params: [{ ...params, from, chainId: toHex(client.chain.id) }], }); }