toSmartContractAccount

Converts an account to a smart contract account and sets up various account-related methods using the provided parameters like transport, chain, entry point, and other utilities.

Import

1import { toSmartContractAccount } from "@aa-sdk/core";

Usage

1import { http, type SignableMessage } from "viem";
2import { sepolia } from "viem/chains";
3
4const myAccount = await toSmartContractAccount({
5 /// REQUIRED PARAMS ///
6 source: "MyAccount",
7 transport: http("RPC_URL"),
8 chain: sepolia,
9 // The EntryPointDef that your account is com"patible with
10 entryPoint: getEntryPoint(sepolia, { version: "0.6.0" }),
11 // This should return a concatenation of your `factoryAddress` and the `callData` for your factory's create account method
12 getAccountInitCode: async () => "0x{factoryAddress}{callData}",
13 // an invalid signature that doesn't cause your account to revert during validation
14 getDummySignature: () => "0x1234...",
15 // given a UO in the form of {target, data, value} should output the calldata for calling your contract's execution method
16 encodeExecute: async (uo) => "0xcalldata",
17 signMessage: async ({ message }: { message: SignableMessage }) => "0x...",
18 signTypedData: async (typedData) => "0x000",
19
20 /// OPTIONAL PARAMS ///
21 // if you already know your account's address, pass that in here to avoid generating a new counterfactual
22 accountAddress: "0xaddressoverride",
23 // if your account supports batching, this should take an array of UOs and return the calldata for calling your contract's batchExecute method
24 encodeBatchExecute: async (uos) => "0x...",
25 // if your contract expects a different signing scheme than the default signMessage scheme, you can override that here
26 signUserOperationHash: async (hash) => "0x...",
27 // allows you to define the calldata for upgrading your account
28 encodeUpgradeToAndCall: async (params) => "0x...",
29});

Parameters

params

ToSmartContractAccountParams the parameters required for converting to a smart contract account

params.transport

Transport the transport mechanism used for communication

params.chain

Chain the blockchain chain used in the account

params.entryPoint

EntryPoint the entry point of the smart contract

params.source

string the source identifier for the account

params.accountAddress

Address the address of the account

params.getAccountInitCode

() => Promise<Hex> a function to get the initial state code of the account

params.signMessage

(message: { message: SignableMessage }) => Promise<Hex> a function to sign a message

params.signTypedData

(typedDataDefinition: TypedDataDefinition<typedData, primaryType>) => Promise<Hex> a function to sign typed data

params.encodeBatchExecute

(transactions: Transaction[]) => Hex a function to encode batch transactions

params.encodeExecute

(tx: Transaction) => Hex a function to encode a single transaction

params.getDummySignature

() => Promise<Hex> a function to get a dummy signature

params.signUserOperationHash

(uoHash: Hex) => Promise<Hex> a function to sign user operations

params.encodeUpgradeToAndCall

(implementationAddress: Address, implementationCallData: Hex) => Hex a function to encode upgrade call

Returns

Promise<SmartContractAccount> a promise that resolves to a SmartContractAccount object with methods and properties for interacting with the smart contract account