How to transfer ownership of a Light Account

Not all smart account implementations support transferring the ownership (e.g. SimpleAccount). However, a number of the accounts in this guide and in Account Kit do, including our LightAccount! Let’s see a few different ways we can transfer ownership of an Account (using LightAccount as an example).

Usage

LightAccount exposes the following method which allows the existing owner to transfer ownership to a new owner address:

1function transferOwnership(address newOwner) public virtual onlyOwner

There a number of ways you can call this method using Account Kit.

1. Using transferOwnership client action

1import { lightAccountClient } from "./client";
2import { createLightAccountClient } from "@account-kit/smart-contracts";
3
4// this will return the signer of the smart account you want to transfer ownerhip to
5const newOwner = LocalAccountSigner.mnemonicToAccountSigner(NEW_OWNER_MNEMONIC);
6const accountAddress = lightAccountClient.getAddress();
7
8// [!code focus:99]
9const hash = lightAccountClient.transferOwnership({
10 newOwner,
11 waitForTxn: true,
12});
13
14// after transaction is mined on the network,
15// create a new light account client for the transferred Light Account
16const transferredClient = await createLightAccountClient({
17 transport: custom(smartAccountClient),
18 chain: smartAccountClient.chain,
19 signer: newOwner,
20 accountAddress, // NOTE: you MUST specify the original smart account address to connect using the new owner/signer
21 version: "v2.0.0", // NOTE: if the version of the light account is not v2.0.0, it must be specified here
22});

Since @alchemy/aa-accounts exports a LightAccount ABI, the above approach makes it easy to transfer ownership. That said, you can also directly call sendUserOperation to execute the ownership transfer. As you will see below, however, it is a bit verbose:

2. Using sendUserOperation

1import { encodeFunctionData } from "viem";
2import { lightAccountClient } from "./client";
3
4// this will return the address of the smart account you want to transfer ownerhip of
5const accountAddress = lightAccountClient.getAddress();
6const newOwner = "0x..."; // the address of the new owner
7
8// [!code focus:99]
9const result = await lightAccountClient.sendUserOperation({
10 to: accountAddress,
11 data: lightAccountClient.encodeTransferOwnership(newOwner),
12});
13// wait for txn with UO to be mined
14await lightAccountClient.waitForUserOperationTransaction(result);

See the LightAccount docs for more details about our `LightAccount implementation.