How to transfer ownership of LightAccount
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:
function transferOwnership(address newOwner) public virtual onlyOwner
There a number of ways you can call this method using Account Kit.
1. Using transferOwnership
client action
import { lightAccountClient } from "./client";
import { createLightAccountClient } from "@account-kit/smart-contracts";
// this will return the signer of the smart account you want to transfer ownerhip to
const newOwner = LocalAccountSigner.mnemonicToAccountSigner(NEW_OWNER_MNEMONIC);
const accountAddress = lightAccountClient.getAddress();
const hash = lightAccountClient.transferOwnership({
newOwner,
waitForTxn: true,
});
// after transaction is mined on the network,
// create a new light account client for the transferred Light Account
const transferredClient = await createLightAccountClient({
transport: custom(smartAccountClient),
chain: smartAccountClient.chain,
signer: newOwner,
accountAddress, // NOTE: you MUST specify the original smart account address to connect using the new owner/signer
version: "v2.0.0", // NOTE: if the version of the light account is not v2.0.0, it must be specified here
});
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
import { encodeFunctionData } from "viem";
import { lightAccountClient } from "./client";
// this will return the address of the smart account you want to transfer ownerhip of
const accountAddress = lightAccountClient.getAddress();
const newOwner = "0x..."; // the address of the new owner
const result = await lightAccountClient.sendUserOperation({
to: accountAddress,
data: lightAccountClient.encodeTransferOwnership(newOwner),
});
// wait for txn with UO to be mined
await lightAccountClient.waitForUserOperationTransaction(result);
See the LightAccount
docs for more details about our `LightAccount implementation.