Skip to main content

Safe Account V2

Safe Account V2 uses the original Safe Singleton and adds ERC-4337 functionality using a module/fallback handler.

The V2 contracts, known as the SafeAccountV0_2_0 class in AbstractionKit, support EntryPoint v0.6.

Import​

import { SafeAccountV0_2_0 as SafeAccount } from "abstractionkit";

How to Use​

Initialize a new Safe Account and calculate its address:

const ownerPublicAddress = "0xBdbc5FBC9cA8C3F514D073eC3de840Ac84FC6D31"; // Safe owner pub address
const smartAccount = SafeAccount.initializeNewAccount([ownerPublicAddress]);

const accountAddress = smartAccount.accountAddress;

Methods at a Glance​

MethodSignatureReturns
constructornew SafeAccountV0_2_0(accountAddress: string, overrides?: { safe4337ModuleAddress?; entrypointAddress?; onChainIdentifierParams?; onChainIdentifier?; safeAccountSingleton? })SafeAccountV0_2_0
initializeNewAccount (static)initializeNewAccount(owners: Signer[], overrides?: InitCodeOverrides)SafeAccountV0_2_0
createUserOperationcreateUserOperation(transactions: MetaTransaction[], providerRpc?: string | Transport | JsonRpcNode, bundlerRpc?: string | Transport | Bundler, overrides?: CreateUserOperationV6Overrides)Promise<UserOperationV6>
signUserOperationsignUserOperation(useroperation: UserOperationV6, privateKeys: string[], chainId: bigint, options?: SafeSignatureOptions)string
createInitCode (static)createInitCode(owners: Signer[], overrides?: InitCodeOverrides)string
createAccountAddressAndInitCode (static)createAccountAddressAndInitCode(owners: Signer[], overrides?: InitCodeOverrides)[string, string]
estimateUserOperationGasestimateUserOperationGas(userOperation: UserOperationV6, bundlerRpc: string | Transport | Bundler, overrides?)Promise<[bigint, bigint, bigint]>
createMigrateToSafeAccountV0_3_0MetaTransactionscreateMigrateToSafeAccountV0_3_0MetaTransactions(nodeRpcUrl: string | Transport | JsonRpcNode, overrides?: { safeV06ModuleAddress?; safeV07ModuleAddress?; prevModuleAddress?; pageSize?; modulesStart?; skipPreflight? })Promise<MetaTransaction[]>

SafeAccountV0_2_0 also inherits the shared Safe methods, documented once for every Safe class: sending, external-signer signing, calldata encoding and EIP-712 helpers on Safe Account Transactions and Signing, and address, owner, module and account-state methods on Safe Account Setup, Owners and Modules.

Constructor​

Creates a SafeAccountV0_2_0 instance for an existing deployed Safe account. For new (undeployed) accounts, use the static initializeNewAccount method instead.

Usage​

import { SafeAccountV0_2_0 as SafeAccount } from "abstractionkit";

const accountAddress = "0x1a02592A3484c2077d2E5D24482497F85e1980C6";

const smartAccount = new SafeAccount(accountAddress);
keytypedescription
accountAddressstringThe on-chain address of the deployed Safe account.
overrides?objectOptional overrides for module and EntryPoint addresses.
overrides?.safe4337ModuleAddress?stringOverride the default Safe 4337 module address.
overrides?.entrypointAddress?stringOverride the default EntryPoint address.
overrides?.onChainIdentifierParams?OnChainIdentifierParamsTypeParameters for on-chain identifier tracking.
overrides?.onChainIdentifier?stringPre-computed on-chain identifier string.

Source code​

constructor

Methods​

The Essentials methods provide all necessary functionalities with support for overrides, offering a streamlined approach.

initializeNewAccount​

Initializes a new SafeAccount class given a list of owners' public addresses. Only needs to be called on the first transaction when the account has not been deployed yet.

Usage​

In this example, we initiate a single owner account.

example.ts
import { SafeAccountV0_2_0 as SafeAccount } from "abstractionkit";

const ownerPublicAddress = "0xBdbc5FBC9cA8C3F514D073eC3de840Ac84FC6D31";
const smartAccount = SafeAccount.initializeNewAccount([ownerPublicAddress]);

console.log("Account address (sender): " + smartAccount.accountAddress);
Example Response
Account address (sender): 0xb8741a449D50ed0Dcfe395287F85BE152884c8d9

Source code​

initializeNewAccount

createUserOperation​

This method determines the nonce, fetches the gas prices, estimates gas limits, and returns a UserOperation to be signed. You can override any of these values using the overrides parameter.

Usage​

This example mints the same NFT twice in a single UserOperation.

example.ts
import { MetaTransaction } from "abstractionkit";

const jsonRpcNodeProvider = "https://ethereum-sepolia-rpc.publicnode.com";
const bundlerUrl = "https://api.candide.dev/public/v3/11155111";

const transaction: MetaTransaction = {
to: "0xD9de104e3386d9A45a61BcE269c43E48B534e4E7", // NFT contract address
value: 0n,
data: "0x1249c58b", // mint()
}

let userOperation = await smartAccount.createUserOperation(
[transaction, transaction], // batch transactions to mint 2 NFTs
jsonRpcNodeProvider,
bundlerUrl,
)

console.log(userOperation);
Example Response
{
sender: '0x1e4Cd9D1DBf1D4DF6A79E1D11f5ff45Cc7F0458b',
nonce: 0n,
callData: '0x541d63c800000000000000000000000038869bf66a61cf6bdb996a6ae40d5853fd43b526...',
callGasLimit: 429347n,
verificationGasLimit: 1502311n,
preVerificationGas: 149456n,
maxFeePerGas: 81n,
maxPriorityFeePerGas: 60n,
signature: '0xffffffffffffffffffffffff47003599ffa7e9198f321afa774e34a12a959844efd6363b...',
initCode: '0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec671688f0b9000000000000000000000000...',
paymasterAndData: '0x'
}

Source code​

createUserOperation

signUserOperation​

This method takes a userOperation, the private keys of the owner of the account, and the chainId and returns the signature field.

example.ts
const chainId = BigInt("11155111"); // sepolia chain ID
const privateKey = "0x4cad764980d84fc6684ca839cae2c78be5432e292fa98416e11687ceb9096a03";
const userOperation = {..}

const signature = smartAccount.signUserOperation(
userOperation,
[privateKey],
chainId,
);

console.log(signature);
Example Response
0x00000000000000000000000041c6297bd9573e8d979a272db4f6576a98f639a7e6874055a627769401dc46d01143551ccaa473364ace4340ec395c546dccb725e1eac2639ecef443d229f0071b

Source code​

signUserOperation

Advanced Methods​

The Advanced methods offer fine control and customization, catering to developers who require detailed configurations for their specific requirements.

createInitCode​

Calculates the initCode needed to deploy the account on-chain.

Usage​

In this example, we initiate a single owner account.

example.ts
import { SafeAccountV0_2_0 as SafeAccount } from "abstractionkit";

const owner1PublicAddress = "0xBdbc5FBC9cA8C3F514D073eC3de840Ac84FC6D31";
const owner2PublicAddress = "0x4991A5360e5da9BAF62fF644d89F46268e5159eA";
const initCode = SafeAccount.createInitCode(
[owner1PublicAddress, owner2PublicAddress],
{ threshold: 2 },
);

console.log("initCode: ", initCode);
Example Response
initCode: 0x...

Source code​

createInitCode

createAccountAddressAndInitCode​

Calculates the Safe address and the initCode needed to deploy the account on-chain.

Usage​

In this example, we initiate a single owner account.

example.ts
import { SafeAccountV0_2_0 as SafeAccount } from "abstractionkit";

const ownerPublicAddress = "0xBdbc5FBC9cA8C3F514D073eC3de840Ac84FC6D31";
let [accountAddress, initCode] = SafeAccount.createAccountAddressAndInitCode(
[ownerPublicAddress],
);

console.log("Account address (sender): " + accountAddress);
console.log("initCode: ", initCode);
Example Response
Account address (sender): 0xb8741a449D50ed0Dcfe395287F85BE152884c8d9
initCode: 0x...

Source code​

createAccountAddressAndInitCode

estimateUserOperationGas​

Estimates gas limits for a UserOperation.

Usage​

import {
SafeAccountV0_2_0 as SafeAccount,
UserOperationV6
} from "abstractionkit";

const bundlerRPC = "https://api.candide.dev/public/v3/11155111";

const ownerPublicAddress = "0xBdbc5FBC9cA8C3F514D073eC3de840Ac84FC6D31";
const smartAccount = SafeAccount.initializeNewAccount([ownerPublicAddress])

// Use createUserOperation() to help you construct the userOp below
let userOperation: UserOperationV6 = await smartAccount.createUserOperation(..);

const [preVerificationGas, verificationGasLimit, callGasLimit] = await smartAccount.estimateUserOperationGas(userOperation, bundlerRPC);
Example Response
[ 149456n, 1502311n, 429347n ]

Source code​

estimateUserOperationGas

createMigrateToSafeAccountV0_3_0MetaTransactions​

Creates an array of meta-transactions to migrate a Safe account from EntryPoint v0.6 (module v0.2.0) to EntryPoint v0.7 (module v0.3.0). The migration disables the v0.6 module, enables the v0.7 module, and updates the fallback handler.

Usage​

import { SafeAccountV0_2_0 as SafeAccount } from "abstractionkit";

const nodeRpcUrl = "https://ethereum-sepolia-rpc.publicnode.com";

const smartAccount = new SafeAccount(accountAddress);

const migrationMetaTransactions =
await smartAccount.createMigrateToSafeAccountV0_3_0MetaTransactions(nodeRpcUrl);

// Include these meta-transactions in a UserOperation to execute the migration
const userOperation = await smartAccount.createUserOperation(
migrationMetaTransactions,
nodeRpcUrl,
bundlerUrl,
);
keytypedescription
nodeRpcUrlstring | Transport | JsonRpcNodeThe JSON-RPC API URL for the target chain.
overrides?objectOptional overrides for contract addresses, pagination, and the preflight check.
overrides?.safeV06ModuleAddress?stringOverride the Safe v0.6 module address to disable.
overrides?.safeV07ModuleAddress?stringOverride the Safe v0.7 module address to enable.
overrides?.prevModuleAddress?stringOverride the linked-list predecessor of the module being disabled. Defaults to an on-chain lookup.
overrides?.pageSize?bigintPage size for the module pagination query.
overrides?.modulesStart?stringStarting address for the module pagination query.
overrides?.skipPreflight?booleanSkip the on-chain check that verifies the account is a Safe (>= 1.4.1) running the old module before building the batch. Defaults to false.

Source code​

createMigrateToSafeAccountV0_3_0MetaTransactions

Audits​