MetaMask Smart Accounts API reference
The following API methods are related to creating, managing, and signing with MetaMask Smart Accounts.
aggregateSignature
Aggregates multiple partial signatures into a single combined multisig signature.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
signatures | PartialSignature[] | Yes | Collection of partial signatures provided by signers, to be merged into an aggregated signature. |
Example
- example.ts
- config.ts
import {
bundlerClient,
aliceSmartAccount,
bobSmartAccount,
aliceAccount,
bobAccount,
} from './config.ts'
import { aggregateSignature } from '@metamask/smart-accounts-kit'
const userOperation = await bundlerClient.prepareUserOperation({
account: aliceSmartAccount,
calls: [
{
target: zeroAddress,
value: 0n,
data: '0x',
},
],
})
const aliceSignature = await aliceSmartAccount.signUserOperation(userOperation)
const bobSignature = await bobSmartAccount.signUserOperation(userOperation)
const aggregatedSignature = aggregateSignature({
signatures: [
{
signer: aliceAccount.address,
signature: aliceSignature,
type: 'ECDSA',
},
{
signer: bobAccount.address,
signature: bobSignature,
type: 'ECDSA',
},
],
})
import { createPublicClient, http } from 'viem'
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'
import { createBundlerClient } from 'viem/account-abstraction'
import { sepolia as chain } from 'viem/chains'
import { Implementation, toMetaMaskSmartAccount } from '@metamask/smart-accounts-kit'
const publicClient = createPublicClient({
chain,
transport: http(),
})
const alicePrivateKey = generatePrivateKey()
const aliceAccount = privateKeyToAccount(alicePrivateKey)
const bobPrivateKey = generatePrivateKey()
const bobAccount = privateKeyToAccount(bobPrivateKey)
const signers = [aliceAccount.address, bobAccount.address]
const threshold = 2n
export const aliceSmartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.MultiSig,
deployParams: [signers, threshold],
deploySalt: '0x',
signer: [{ account: aliceAccount }],
})
export const bobSmartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.MultiSig,
deployParams: [signers, threshold],
deploySalt: '0x',
signer: [{ account: bobAccount }],
})
export const bundlerClient = createBundlerClient({
client: publicClient,
transport: http('https://public.pimlico.io/v2/rpc'),
})
encodeCalls
Encodes calls for execution by a MetaMask smart account. If there's a single call directly to the smart account, it returns the call data directly. For multiple calls or calls to other addresses, it creates executions and encodes them for the smart account's execute function.
The execution mode is set to SingleDefault for a single call to other address, or BatchDefault for multiple calls.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
calls | Call[] | Yes | List of calls to be encoded. |
Example
- example.ts
- config.ts
import { smartAccount } from './config.ts'
const calls = [
{
to: zeroAddress,
data: '0x',
value: 0n,
},
]
const executeCallData = await smartAccount.encodeCalls(calls)
import { createPublicClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { sepolia as chain } from 'viem/chains'
import { Implementation, toMetaMaskSmartAccount } from '@metamask/smart-accounts-kit'
const publicClient = createPublicClient({
chain,
transport: http(),
})
const delegatorAccount = privateKeyToAccount('0x...')
export const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Hybrid,
deployParams: [delegatorAccount.address, [], [], []],
deploySalt: '0x',
signer: { account: delegatorAccount },
})
getFactoryArgs
Returns the factory address and factory data that can be used to deploy a smart account.
Example
- example.ts
- config.ts
import { smartAccount } from './config.ts'
const { factory, factoryData } = await smartAccount.getFactoryArgs()
import { createPublicClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { sepolia as chain } from 'viem/chains'
import { Implementation, toMetaMaskSmartAccount } from '@metamask/smart-accounts-kit'
const publicClient = createPublicClient({
chain,
transport: http(),
})
const delegatorAccount = privateKeyToAccount('0x...')
export const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Hybrid,
deployParams: [delegatorAccount.address, [], [], []],
deploySalt: '0x',
signer: { account: delegatorAccount },
})
getNonce
Returns the nonce for a smart account.