Create a delegator 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. Enable users to create a delegator account directly in your dapp.
Prerequisites
- Install and set up the MetaMask Delegation Toolkit.
- Configure the toolkit.
Create a MetaMaskSmartAccount
The following is an example of creating a delegator account using Viem Core SDK. Viem Core SDK provides low-level interfaces to offer flexibility and control over the delegator account creation lifecycle.
In the example, the Viem privateKeyToAccount
function creates an externally owned account as the owner of the delegator account.
import { http, createPublicClient } from "viem";
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";
import { lineaSepolia as chain } from "viem/chains";
import {
Implementation,
toMetaMaskSmartAccount,
} from "@codefi/delegator-core-viem";
const transport = http();
const publicClient = createClient({ transport, chain });
const privateKey = generatePrivateKey();
const owner = privateKeyToAccount(privateKey);
const deploySalt = "0x";
const account = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Hybrid,
deployParams: [owner.address, [], [], []],
deploySalt,
signatory: { account: owner },
});
This example creates the MetaMaskSmartAccount
, which can perform several functions:
- In conjunction with Viem Account Abstraction clients, deploy the smart contract account, and send user operations.
- Sign delegations that can be used to grant specific rights and permissions to other accounts.
The example above uses the Hybrid Delegator smart contract account, which is configurable to have an EOA "owner" and any number of P256 (passkey) signers. You can also configure other delegator account types.