Skip to main content

Delegation Toolkit documentation

Embed MetaMask Smart Accounts into your dapp, enabling new user experiences.

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:

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 },
});

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:

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 },
});

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:

Installation required

To use WebAuthn, install the Ox SDK.

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) },
});

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:

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,
});
note

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.

note

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:

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 },
});

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:

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 },
});

Next steps

With a MetaMask smart account, you can perform the following functions: