Create a MetaMask smart account
The MetaMask Delegation Toolkit is embedded, meaning that the end user can instantly interact with a dapp without wallet authorization, confirmations, or corporate logos. You can enable users to create a MetaMask smart account directly in your dapp. The toolkit supports different smart account types, each with its own configuration and support for different signing mechanisms.
This page provides examples of using toMetaMaskSmartAccount
with Viem Core SDK to create different types of smart accounts with different types of signatories.
Prerequisites
Create a Hybrid smart account
A Hybrid smart account supports both an externally owned account (EOA) owner and any number of P256 (passkey) signers. You can create a Hybrid smart account with the following types of signatories.
Create a Hybrid smart account with an account signatory
Use toMetaMaskSmartAccount
and Viem's privateKeyToAccount
to create a Hybrid smart account with a signatory from a private key:
- example.ts
- client.ts
- signatory.ts
import { publicClient } from "./client.ts"
import { account } from "./signatory.ts";
import {
Implementation,
toMetaMaskSmartAccount,
} from "@metamask/delegation-toolkit";
const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Hybrid,
deployParams: [account.address, [], [], []],
deploySalt: "0x",
signatory: { account },
});
import { http, createPublicClient } from "viem";
import { sepolia as chain } from "viem/chains";
const transport = http();
export const publicClient = createPublicClient({
transport,
chain,
});
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";
const privateKey = generatePrivateKey();
export const account = privateKeyToAccount(privateKey);
Create a Hybrid smart account with a Wallet Client signatory
Use toMetaMaskSmartAccount
and Viem's createWalletClient
to create a Hybrid smart account with a Wallet Client signatory:
- example.ts
- client.ts
- signatory.ts
import { publicClient } from "./client.ts"
import { walletClient } from "./signatory.ts";
import {
Implementation,
toMetaMaskSmartAccount,
} from "@metamask/delegation-toolkit";
const addresses = await walletClient.getAddresses();
const owner = addresses[0];
const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Hybrid,
deployParams: [owner, [], [], []],
deploySalt: "0x",
signatory: { walletClient },
});
import { http, createPublicClient } from "viem";
import { sepolia as chain } from "viem/chains";
const transport = http();
export const publicClient = createPublicClient({
transport,
chain,
});
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";
import { sepolia as chain } from "viem/chains";
import { http, createWalletClient } from "viem";
const privateKey = generatePrivateKey();
const account = privateKeyToAccount(privateKey);
export const walletClient = createWalletClient({
account,
chain,
transport: http()
})
Create a Hybrid smart account with a WebAuthn (passkey) signatory
Use toMetaMaskSmartAccount
and Viem's toWebAuthnAccount
to create a Hybrid smart account with a WebAuthn Account signatory:
To use WebAuthn, install the Ox SDK.
- example.ts
- client.ts
- signatory.ts
import { publicClient } from "./client.ts"
import { webAuthnAccount, credential } from "./signatory.ts";
import {
Implementation,
toMetaMaskSmartAccount,
} from "@metamask/delegation-toolkit";
import { Address, PublicKey } from "ox";
import { toHex } from "viem";
// Deserialize compressed public key
const publicKey = PublicKey.fromHex(credential.publicKey);
// Convert public key to address
const owner = Address.fromPublicKey(publicKey);
const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Hybrid,
deployParams: [owner, [credential.id], [publicKey.x], [publicKey.y]],
deploySalt: "0x",
signatory: { webAuthnAccount, keyId: toHex(credential.id) },
});
import { http, createPublicClient } from "viem";
import { sepolia as chain } from "viem/chains";
const transport = http();
export const publicClient = createPublicClient({
transport,
chain,
});
import {
createWebAuthnCredential,
toWebAuthnAccount,
} from "viem/account-abstraction";
export const credential = await createWebAuthnCredential({
name: "MetaMask smart account",
});
export const webAuthnAccount = toWebAuthnAccount({ credential });
Create a Multisig smart account
A Multisig smart account supports multiple EOA signers with a configurable threshold for execution.
Use toMetaMaskSmartAccount
to create a Multsig smart account with a combination of account signatories and Wallet Client signatories:
- example.ts
- client.ts
- signers.ts
import { publicClient } from "./client.ts";
import { account, walletClient } from "./signers.ts";
import {
Implementation,
toMetaMaskSmartAccount,
} from "@metamask/delegation-toolkit";
const signers = [ account.address, walletClient.address ];
const signatory = [ { account }, { walletClient } ];
const threshold = 2n
const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.MultiSig,
deployParams: [signers, threshold],
deploySalt: "0x",
signatory,
});
import { http, createPublicClient } from "viem";
import { sepolia as chain } from "viem/chains";
const transport = http();
export const publicClient = createPublicClient({
transport,
chain,
});
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";
import { sepolia as chain } from "viem/chains";
import { http, createWalletClient } from "viem";
// This private key will be used to generate the first signer.
const privateKey = generatePrivateKey();
export const account = privateKeyToAccount(privateKey);
// This private key will be used to generate the second signer.
const walletClientPrivatekey = generatePrivateKey();
const walletClientAccount = privateKeyToAccount(walletClientPrivatekey);
export const walletClient = createWalletClient({
account: walletClientAccount,
chain,
transport: http()
});
The number of signers in the signatories must be at least equal to the threshold for valid signature generation.
Create a Stateless 7702 smart account
A Stateless 7702 smart account represents an EOA that has been upgraded to support MetaMask Smart Accounts functionality as defined by EIP-7702.
This implementation does not handle the upgrade process; see the EIP-7702 quickstart to learn how to upgrade.
You can create a Stateless 7702 smart account with the following types of signatories.
Create a Stateless 7702 smart account with an account signatory
Use toMetaMaskSmartAccount
and Viem's privateKeyToAccount
to create a Stateless 7702 smart account with a signatory from a private key:
- example.ts
- client.ts
- signatory.ts
import { publicClient } from "./client.ts";
import { account } from "./signatory.ts";
import {
Implementation,
toMetaMaskSmartAccount,
} from "@metamask/delegation-toolkit";
const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Stateless7702,
address: account.address // Address of the upgraded EOA
signatory: { account },
});
import { http, createPublicClient } from "viem";
import { sepolia as chain } from "viem/chains";
const transport = http();
export const publicClient = createPublicClient({
transport,
chain,
});
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";
const privateKey = generatePrivateKey();
export const account = privateKeyToAccount(privateKey);
Create a Stateless 7702 smart account with a Wallet Client signatory
Use toMetaMaskSmartAccount
and Viem's createWalletClient
to create a Stateless 7702 smart account with a Wallet Client signatory:
- example.ts
- client.ts
- signatory.ts
import { publicClient } from "./client.ts";
import { walletClient } from "./signatory.ts";
import {
Implementation,
toMetaMaskSmartAccount,
} from "@metamask/delegation-toolkit";
const addresses = await walletClient.getAddresses();
const address = addresses[0];
const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Stateless7702,
address, // Address of the upgraded EOA
signatory: { walletClient },
});
import { http, createPublicClient } from "viem";
import { sepolia as chain } from "viem/chains";
const transport = http();
export const publicClient = createPublicClient({
transport,
chain,
});
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";
import { sepolia as chain } from "viem/chains";
import { http, createWalletClient } from "viem";
const privateKey = generatePrivateKey();
const account = privateKeyToAccount(privateKey);
export const walletClient = createWalletClient({
account,
chain,
transport: http(),
})
Next steps
With a MetaMask smart account, you can perform the following functions:
- In conjunction with Viem Account Abstraction clients, deploy the smart account and send user operations.
- Sign delegations that can be used to grant specific rights and permissions to other accounts. Smart accounts that sign delegations are called delegator accounts.