Skip to main content

Safe Account Transactions and Signing

Every AbstractionKit Safe account class inherits these methods from the base SafeAccount class: Safe Unified Account (SafeMultiChainSigAccountV1 and SafeMultiChainSigAccountV2, the recommended classes), Safe Account V3 (SafeAccountV0_3_0) and Safe Account V2 (SafeAccountV0_2_0). Building, estimating and signing with private keys are class-specific and documented on each class page. Owner, module and address methods are on Safe Account Setup, Owners and Modules.

The examples import SafeMultiChainSigAccountV1 as SafeAccount, the recommended class for new Safes. Each method is called the same way on SafeMultiChainSigAccountV2, SafeAccountV0_3_0 and SafeAccountV0_2_0: change the import to your class. Where a class differs (UserOperation type, default addresses, example output), the section says so.

Methods at a Glance​

MethodSignatureReturns
sendUserOperationsendUserOperation(userOperation: UserOperationV6 | UserOperationV7 | UserOperationV9, bundlerRpc: string | Transport | Bundler)Promise<SendUseroperationResponse>
signUserOperationWithSignerssignUserOperationWithSigners(userOperation, signers: ReadonlyArray<ExternalSigner>, chainId: bigint, options?: SafeSignatureOptions)Promise<string>
createAccountCallDataSingleTransaction (static)createAccountCallDataSingleTransaction(metaTransaction: MetaTransaction, overrides?: { safeModuleExecutorFunctionSelector? })string
createAccountCallDataBatchTransactions (static)createAccountCallDataBatchTransactions(metaTransactions: MetaTransaction[], overrides?: { safeModuleExecutorFunctionSelector?; multisendContractAddress? })string
getUserOperationEip712Hash (static)getUserOperationEip712Hash(useroperation, chainId: bigint, overrides?: { validAfter?; validUntil?; entrypointAddress?; safe4337ModuleAddress? })string
getUserOperationEip712Data (static)getUserOperationEip712Data(useroperation, chainId: bigint, overrides?: { validAfter?; validUntil?; entrypointAddress?; safe4337ModuleAddress? }){ domain; types; messageValue }
formatEip712SignaturesToUseroperationSignature (static)formatEip712SignaturesToUseroperationSignature(signersAddresses: string[], signatures: string[], overrides?: { validAfter?; validUntil?; isMultiChainSignature?; merkleProof? })string

useroperation is UserOperationV9 on the Safe Unified Account, UserOperationV7 on SafeAccountV0_3_0 and UserOperationV6 on SafeAccountV0_2_0.

Methods​

sendUserOperation​

This method sends the UserOperation to the bundler to be executed on-chain. It returns a promise SendUseroperationResponse object to confirm the on-chain inclusion of the UserOperation.

example.ts
const sendUserOperationResponse = await smartAccount.sendUserOperation(userOperation, bundlerUrl)

console.log("sendUserOperationResponse: ", sendUserOperationResponse);
console.log("UserOperation sent. Waiting to be included...");

const receipt = await sendUserOperationResponse.included()

console.log("receipt: ", receipt);
Example Response
sendUserOperationResponse: SendUseroperationResponse {
userOperationHash: '0xbd2cf2185876fbc6eccfdcb9e7a6d606f1032fd0f38b3a3c7ddf7d59d605f0e4',
bundler: Bundler {
transport: HttpTransport {
nextId: 2,
url: 'https://api.candide.dev/public/v3/11155111',
options: {}
},
outbound: { request: [Function: request] }
},
entrypointAddress: '0x0000000071727De22E5E9d8BAf0edAc6f37da032'
}
UserOperation sent. Waiting to be included...
receipt: {
userOpHash: '0xbd2cf2185876fbc6eccfdcb9e7a6d606f1032fd0f38b3a3c7ddf7d59d605f0e4',
entryPoint: '0x0000000071727De22E5E9d8BAf0edAc6f37da032',
sender: '0x4604a50478f900dbbb6dc5b3f2fd4d498bc8939d',
nonce: 0n,
paymaster: '0x8b1f6cb5d062aa2ce8d581942bbb960420d875ba',
actualGasCost: 161556241n,
actualGasUsed: 2098133n,
success: true,
logs: [
{
address: '0x0000000071727de22e5e9d8baf0edac6f37da032',
topics: [Array],
data: '0x000000000000000000000000000000000000000000000000000000000000000000000000...',
blockHash: '0x634d29185efba9d6ef6f98a6c547a50b71c021c3a78c389828970ca772181478',
blockNumber: '0xb8da33',
blockTimestamp: '0x6af4e230',
transactionHash: '0x3a69747ddf810c6f908036782319b75e24bd535bb4b9f880feecdedd301a075b',
transactionIndex: '0xb',
logIndex: '0x17',
removed: false
}
],
receipt: {
blockHash: '0x634d29185efba9d6ef6f98a6c547a50b71c021c3a78c389828970ca772181478',
blockNumber: 12114483n,
from: '0x3cfdc212769c890907bce93d3d8c2c53de6a7a89',
cumulativeGasUsed: 7840335n,
gasUsed: 1974796n,
logs: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
],
logsBloom: '0x000004000018100000000001000000008000001000000000000000000200000000080000...',
transactionHash: '0x3a69747ddf810c6f908036782319b75e24bd535bb4b9f880feecdedd301a075b',
transactionIndex: 11n,
effectiveGasPrice: 171n
}
}

On SafeAccountV0_2_0 (EntryPoint v0.6), userOperation is a UserOperationV6 (see Safe Account V2 for its fields), and entrypointAddress in the response and entryPoint in the receipt are the EntryPoint v0.6 address 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789. On the Safe Unified Account (SafeMultiChainSigAccountV1 and SafeMultiChainSigAccountV2, EntryPoint v0.9), userOperation is a UserOperationV9 (see Safe Unified Account).

To send from a fresh instance, attach the account by its address:

example.ts
const account = new SafeAccount(userOperation.sender);
const response = await account.sendUserOperation(userOperation, bundlerUrl);
const receipt = await response.included();

Source code​

sendUserOperation

signUserOperationWithSigners​

Signs a UserOperation using one or more ExternalSigners instead of raw private keys. Integrates viem, ethers, hardware wallets, HSMs, MPC services, and WebAuthn through the same API.

example.ts
import { fromViem } from "abstractionkit";
import { privateKeyToAccount } from "viem/accounts";

const viemAccount = privateKeyToAccount("0x...");

userOperation.signature = await smartAccount.signUserOperationWithSigners(
userOperation,
[fromViem(viemAccount)],
chainId,
);

See External Signers for the full list of adapters and custom-signer integrations.

On SafeAccountV0_2_0 (EntryPoint v0.6), userOperation is a UserOperationV6. On the Safe Unified Account (SafeMultiChainSigAccountV1 and SafeMultiChainSigAccountV2, EntryPoint v0.9), it is a UserOperationV9; the signature is always encoded with the multichain scheme (isMultiChainSignature: true), and a multiChainMerkleProof option is rejected. To sign several UserOperations under one signature, use signUserOperationsWithSigners.

Source code​

createAccountCallDataSingleTransaction​

Encode calldata for a single MetaTransaction to be executed by Safe account

Usage​

In this example, we make a transfer of 1 wei to a random address.

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

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

const callData = SafeAccount.createAccountCallDataSingleTransaction({
to: "0x1a02592A3484c2077d2E5D24482497F85e1980C6",
value: 1n,
data: "0x",
});

console.log("callData: " + callData);
Example Response
callData: 0x541d63c80000000000000000000000001a02592a3484c2077d2e5d24482497f85e1980c60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Source code​

createAccountCallDataSingleTransaction

createAccountCallDataBatchTransactions​

Encode calldata for a list of MetaTransactions to be executed by Safe account

Usage​

In this example, we make a transfer to 2 different random addresses, 1 wei each.

example.ts
import {
SafeMultiChainSigAccountV1 as SafeAccount,
MetaTransaction,
} from "abstractionkit";

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

const tx1: MetaTransaction = {
to: "0x1a02592A3484c2077d2E5D24482497F85e1980C6",
value: 1n,
data: "0x",
};
const tx2: MetaTransaction = {
to: "0x3fe285dcd76bcce4ac92d38a6f2f8e964041e020",
value: 1n,
data: "0x",
};

const callData = SafeAccount.createAccountCallDataBatchTransactions([tx1, tx2]);

console.log("callData: " + callData);
Example Response
callData: 0x541d63c800000000000000000000000038869bf66a61cf6bdb996a6ae40d5853fd43b52600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001048d80ff0a000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000aa001a02592a3484c2077d2e5d24482497f85e1980c600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000003fe285dcd76bcce4ac92d38a6f2f8e964041e020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Source code​

createAccountCallDataBatchTransactions

getUserOperationEip712Hash​

Create a userOperation eip712 hash

Usage​

example
import {
SafeMultiChainSigAccountV1 as SafeAccount,
UserOperationV9
} from "abstractionkit";

const userOperation: UserOperationV9 = await smartAccount.createUserOperation(..)

const safeUserOpHash = SafeAccount.getUserOperationEip712Hash(userOperation, chainId);

console.log(safeUserOpHash);
Example Response
0xec030c825b12b398c10f1b552004e43ec753fdf001e1c1daa1ceffe4f7ff5056

On SafeAccountV0_2_0 (EntryPoint v0.6), userOperation is a UserOperationV6 and entrypointAddress defaults to EntryPoint v0.6. On the Safe Unified Account (SafeMultiChainSigAccountV1 and SafeMultiChainSigAccountV2, EntryPoint v0.9), it is a UserOperationV9 and entrypointAddress defaults to EntryPoint v0.9. Each class defaults safe4337ModuleAddress to its own Safe 4337 module.

Source code​

getUserOperationEip712Data​

A static method that returns the EIP-712 domain data for a userOp

keytypedescription
userOperationobjectUserOp to hash
userOperation.senderstringThe account making the operation
userOperation.noncebigintAnti-replay parameter (see “Semi-abstracted Nonce Support” )
userOperation.factorystring | nullaccount factory, only for new accounts (null if the account already exists)
userOperation.factoryDatastring | nulldata for account factory (null if the account already exists)
userOperation.callDatastringThe data to pass to the sender during the main execution call
userOperation.callGasLimitbigintThe amount of gas to allocate the main execution call
userOperation.verificationGasLimitbigintThe amount of gas to allocate for the verification step
userOperation.preVerificationGasbigintExtra gas to pay the bundler
userOperation.maxFeePerGasbigintMaximum fee per gas (similar to EIP-1559 max_fee_per_gas)
userOperation.maxPriorityFeePerGasbigintMaximum priority fee per gas (similar to EIP-1559 max_priority_fee_per_gas)
userOperation.paymasterstring | nullAddress of paymaster contract (null if account pays for itself)
userOperation.paymasterVerificationGasLimitbigint | nullThe amount of gas to allocate for the paymaster verification step
userOperation.paymasterPostOpGasLimitbigint | nullThe amount of gas to allocate for the paymaster post-operation code
userOperation.paymasterDatastring | nullData for paymaster (null if no paymaster)
userOperation.signaturestringData passed into the account to verify authorization
chainIdbiginttarget chain id
overrides?objectoptional overrides
overrides?.validAfter?bigintTimestamp the signature will be valid after
overrides?.validUntil?bigintTimestamp the signature will be valid until
overrides?.entrypointAddress?stringAddress of the entrypoint contract
overrides?.safe4337ModuleAddress?stringAddress of the Safe 4337 module.

On SafeAccountV0_2_0 (EntryPoint v0.6), userOperation is a UserOperationV6. On the Safe Unified Account (SafeMultiChainSigAccountV1 and SafeMultiChainSigAccountV2, EntryPoint v0.9), it is a UserOperationV9. Each class defaults entrypointAddress and safe4337ModuleAddress to its own EntryPoint and Safe 4337 module.

Source code​

formatEip712SignaturesToUseroperationSignature​

A static method to format a list of eip712 signatures to a userOperation signature.

Usage​

import { SafeMultiChainSigAccountV1 as SafeAccount } from "abstractionkit";
import { Wallet } from "ethers";

const ownerPrivateKey = process.env.PRIVATE_KEY as string;
const signer = new Wallet(ownerPrivateKey);
const ownerPublicAddress = signer.address;

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

let userOperation = ... // smartAccount.createUserOperation(..)

const safeUserOpHash = SafeAccount.getUserOperationEip712Hash(userOperation, chainId);
const signature = signer.signingKey.sign(safeUserOpHash).serialized;

const formatedSig = SafeAccount.formatEip712SignaturesToUseroperationSignature(
[ownerPublicAddress],
[signature],
{ isMultiChainSignature: true }, // required for SafeMultiChainSigAccountV1 and V2; omit for SafeAccountV0_3_0 and V0_2_0
);
userOperation.signature = formatedSig;
Example Response
0x0000000000000000000000006da39f6f7b0d2c0035084d3c313350697b3167ff591a84bf0b4bb4741224b5d226682ec306544c091e2b6535042c900b459282edfe98e393d552963ca8db11731c

On the Safe Unified Account, signatures always use the multichain encoding: pass { isMultiChainSignature: true } as overrides. See Manual EIP-712 signing for a single UserOperation.

Source code​

formatEip712SignaturesToUseroperationSignature