Skip to main content

Create a smart account

You can enable users to create a MetaMask smart account directly in your dapp. Use toMetaMaskSmartAccount to create different types of smart accounts with different signature schemes.

Prerequisites

Install and set up the Smart Accounts Kit.

Hybrid smart account

A Hybrid smart account supports both an externally owned account (EOA) owner and any number of passkey (WebAuthn) signers.

This example uses toMetaMaskSmartAccount and Viem's Wallet Client to create a Hybrid smart account. The signer parameter also accepts Viem's Local Account and WebAuthnAccount.

See the toMetaMaskSmartAccount API reference for more information.

import { publicClient } from "./client.ts"
import { walletClient } from "./signer.ts";
import {
Implementation,
toMetaMaskSmartAccount,
} from "@metamask/smart-accounts-kit";

// Some wallets like MetaMask may require you to request access to
// account addresses using walletClient.requestAddresses() first.
const [address] = await walletClient.getAddresses();

const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Hybrid,
deployParams: [address, [], [], []],
deploySalt: "0x",
signer: { walletClient },
});

Multisig smart account

A Multisig smart account supports multiple EOA signers with a configurable threshold for execution.

This example uses toMetaMaskSmartAccount to create a Multisig smart account with a combination of account signers and Wallet Client signers.

import { publicClient } from "./client.ts";
import { account, walletClient } from "./signers.ts";
import {
Implementation,
toMetaMaskSmartAccount,
} from "@metamask/smart-accounts-kit";

const owners = [ account.address, walletClient.address ];
const signer = [ { account }, { walletClient } ];
const threshold = 2n

const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.MultiSig,
deployParams: [owners, threshold],
deploySalt: "0x",
signer,
});
note

The number of signers must be at least equal to the threshold to generate a valid signature.

EIP-7702 smart account

An EIP-7702 smart account represents an EOA that has been upgraded to support MetaMask Smart Accounts functionality as defined by EIP-7702.

This example uses toMetaMaskSmartAccount and Viem's privateKeyToAccount to create an EIP-7702 smart account. This example doesn't handle the upgrade process; see the EIP-7702 quickstart to learn how to upgrade.

Important

The EIP-7702 implementation only works with Viem's Local Accounts. It doesn't work with a JSON-RPC Account like MetaMask.

See the Upgrade a MetaMask EOA to a smart account tutorial.

import { publicClient } from "./client.ts";
import { account } from "./signer.ts";
import {
Implementation,
toMetaMaskSmartAccount,
} from "@metamask/smart-accounts-kit";

const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Stateless7702,
address: account.address,
signer: { account },
});

Next steps