UNPKG

@zerodev/sdk

Version:

A utility library for working with ERC-4337

76 lines (71 loc) 2.7 kB
// Copied from: https://github.com/pimlicolabs/permissionless.js/blob/main/packages/permissionless/actions/smartAccount/signMessage.ts import type { Chain, Client, SignMessageParameters, SignMessageReturnType, Transport } from "viem" import type { SmartAccount } from "viem/account-abstraction" import { parseAccount } from "viem/utils" import { AccountNotFoundError } from "../../errors/index.js" /** * Calculates an Ethereum-specific signature in [EIP-191 format](https://eips.ethereum.org/EIPS/eip-191): `keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))`. * * - Docs: https://viem.sh/docs/actions/wallet/signMessage.html * - JSON-RPC Methods: * - JSON-RPC Accounts: [`personal_sign`](https://docs.metamask.io/guide/signing-data.html#personal-sign) * - Local Accounts: Signs locally. No JSON-RPC request. * * With the calculated signature, you can: * - use [`verifyMessage`](https://viem.sh/docs/utilities/verifyMessage.html) to verify the signature, * - use [`recoverMessageAddress`](https://viem.sh/docs/utilities/recoverMessageAddress.html) to recover the signing address from a signature. * * @param client - Client to use * @param parameters - {@link SignMessageParameters} * @returns The signed message. {@link SignMessageReturnType} * * @example * import { createWalletClient, custom } from 'viem' * import { mainnet } from 'viem/chains' * import { signMessage } from 'viem/wallet' * * const client = createWalletClient({ * chain: mainnet, * transport: custom(window.ethereum), * }) * const signature = await signMessage(client, { * account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', * message: 'hello world', * }) * * @example * // Account Hoisting * import { createWalletClient, custom } from 'viem' * import { privateKeyToAccount } from 'viem/accounts' * import { mainnet } from 'viem/chains' * import { signMessage } from 'viem/wallet' * * const client = createWalletClient({ * account: privateKeyToAccount('0x…'), * chain: mainnet, * transport: custom(window.ethereum), * }) * const signature = await signMessage(client, { * message: 'hello world', * }) */ export async function signMessage<TAccount extends SmartAccount | undefined>( client: Client<Transport, Chain | undefined, TAccount>, { account: account_ = client.account, message }: SignMessageParameters<TAccount> ): Promise<SignMessageReturnType> { if (!account_) throw new AccountNotFoundError({ docsPath: "/docs/actions/wallet/signMessage" }) const account = parseAccount(account_) as SmartAccount return account.signMessage({ message }) }