Smart ContractsOther AccountsModular Account V1Multisig

Modular Account with Multisig Plugin • Getting started

1. Create a Multisig Account Client

Initialize a Multisig Modular Account client and set the n accounts as signers.

It is recommended to use the createMultisigAccountAlchemyClient directly to create new accounts with multi-sig ownership, rather than extending the Modular Account client.

If you have an existing Modular Account (which has multi-owner plugin by default), please see details here for installing the plugin before proceeding.

client.ts
1import { LocalAccountSigner } from "@aa-sdk/core";
2import { alchemy, sepolia } from "@account-kit/infra";
3import { createMultisigAccountAlchemyClient } from "@account-kit/smart-contracts";
4
5const MODULAR_MULTISIG_ACCOUNT_OWNER_MNEMONIC = "YOUR MNEMONIC";
6
7// Creating a 3/3 multisig account
8export const signers = [
9 LocalAccountSigner.mnemonicToAccountSigner(
10 MODULAR_MULTISIG_ACCOUNT_OWNER_MNEMONIC,
11 { accountIndex: 0 }
12 ),
13 LocalAccountSigner.mnemonicToAccountSigner(
14 MODULAR_MULTISIG_ACCOUNT_OWNER_MNEMONIC,
15 { accountIndex: 1 }
16 ),
17 LocalAccountSigner.mnemonicToAccountSigner(
18 MODULAR_MULTISIG_ACCOUNT_OWNER_MNEMONIC,
19 { accountIndex: 2 }
20 ),
21];
22
23export const threshold = 3n;
24
25export const owners = await Promise.all(signers.map((s) => s.getAddress()));
26
27export const multisigAccountClient = await createMultisigAccountAlchemyClient({
28 chain: sepolia,
29 signer: signers[0],
30 owners,
31 threshold,
32 transport: alchemy({
33 apiKey: "YOUR_API_KEY",
34 }),
35});

3. Propose a User Operation

To propose a new user operation for review by the multisig signers, you will use the proposeUserOperation method. This estimates gas, constructs the user operation struct, and if gasManagerConfig is used then it attempts to use a paymaster. Lastly, a signature is generated with the pre-provided signer.

1import { multisigAccountClient } from "./client";
2import { createMultisigAccountAlchemyClient } from "@account-kit/smart-contracts";
3
4const {
5 request,
6 aggregatedSignature,
7 signatureObj: firstSig,
8} = await multisigAccountClient.proposeUserOperation({
9 uo: {
10 target: targetAddress,
11 data: "0x",
12 },
13});

4. Get the threshold signatures

Next, you have to collect the next k-2 signatures, excluding the first signature which you already provided and the last signature which we’ll deal with in step 5 when we send the user operation. Each member of the multisig can sign with the signMultisigUserOperation method.

1import { createMultisigAccountAlchemyClient } from "@account-kit/smart-contracts";
2import { signers, owners, threshold } from "./client";
3
4const multisigAccountClient = await createMultisigAccountAlchemyClient({
5 chain,
6 signer: signers[1], // using the second signer
7 owners,
8 threshold,
9 apiKey: "YOUR_API_KEY",
10});
11
12const { aggregatedSignature, signatureObj: secondSig } =
13 await multisigAccountClient.signMultisigUserOperation({
14 account: multisigAccountClient.account,
15 // output from step 1, and from this step if k-2 > 1
16 signatures: [previousAggregatedSignature],
17 userOperationRequest: request,
18 });

5. Send the User Operation

After collecting k-1 signatures, you’re ready to collect the last signature and send the user operation. This is done with the sendUserOperation method. sendUserOperation also formats this aggregated signature, sorting its parts in ascending order by owner address as expected by the Multisig Plugin smart contract.

1import { createMultisigAccountAlchemyClient } from "@account-kit/smart-contracts";
2import { signers, owners, threshold } from "./client";
3
4const multisigAccountClient = await createMultisigAccountAlchemyClient({
5 chain,
6 // using the last signer
7 signer: signers[2],
8 owners,
9 threshold,
10 apiKey: "YOUR_API_KEY",
11});
12
13const result = await multisigAccountClient.sendUserOperation({
14 uo: request.callData,
15 context: {
16 aggregatedSignature,
17 signatures: [firstSig, secondSig],
18 userOpSignatureType: "ACTUAL",
19 },
20});

By default, we use the variable gas feature in the Multisig Plugin smart contract. For this, we need userOpSignatureType to be set to “ACTUAL”. If you do not wish to use this feature, gas overrides should be passed in sendUserOperation, and userOpSignatureType should be set to “UPPERLIMIT”.

1import { multisigAccountClient } from "./client";
2
3const result = await multisigAccountClient.sendUserOperation({
4 uo: request.callData,
5 overrides: {
6 callGasLimit: request.callGasLimit,
7 verificationGasLimit: request.verificationGasLimit,
8 preVerificationGas: request.preVerificationGas,
9 maxFeePerGas: request.maxFeePerGas,
10 maxPriorityFeePerGas: request.maxPriorityFeePerGas,
11 },
12 context: {
13 aggregatedSignature,
14 signatures: [firstSig, secondSig],
15 userOpSignatureType: "UPPERLIMIT",
16 },
17});

Conclusion

That’s it! You’ve initialized a modular account with three multisig members, proposed a user operation, collected the necessary signatures, and sent the user operation to the bundler.

For more info, check out the technical details of the multisig plugin.