Skip to content

Transfer Account Ownership

With Openfort, you can change the ownership of an account from one address to another. Your users can take ownership of their account without ever having to go through exposing a private key. Secure, frictionless, and easy.

There are 2 steps involved in transferring account ownership:

  • transferOwnership: starts the ownership transfer of the contract to a new account. Called through the API and performed by Openfort.
  • acceptOwnership: the new owner accepts the ownership transfer. Performed in the client side by the new owner.

Openfort accounts implement Ownable2Step from Openzeppelin to create a secure way of transferring account ownership. You can check out the code that allows for this behavior in their GitHub repository.

Quickstart

This guide will go though all the necessary steps to transfer account ownership.

1. Set up Openfort (Server side)

Use our official libraries to access the Openfort API from your application:

Install Openfort Node.js library:

npm
npm install @openfort/openfort-node --save

Initialize '@openfort/openfort-node' with your secret key:

const Openfort = require("@openfort/openfort-node").default;
const openfort = new Openfort(YOUR_SECRET_KEY);

2. Request transfer ownership (Server side)

Openfort will perform a transferOwnership operation to transfer the ownership of the account from the current owner to the new owner.

The policy parameter is a policy that will be used to sponsor the transaction. You can find more information about policies in our API documentation. Bear in mind this policy needs to have a account_functions policy rule to allow the sponsorship of this operation.

curl
curl https://api.openfort.io/v1/accounts/acc_.../request_transfer_ownership \
  -H "Authorization: Bearer $YOUR_SECRET_KEY" \
  -d newOwnerAddress="0x416c...354D" \
  -d "policy=pol_..."

3. Accept account ownership (Client side)

Using Wagmi React hooks, you can accept the account ownership by performing an acceptOwnership operation to the account address. Find a working example of how to accept account ownership in our GitHub repository component sample.

Accept account ownership from client side:

import {
  usePrepareContractWrite,
  useContractWrite,
  useWaitForTransaction,
} from "wagmi";
 
const { config } = usePrepareContractWrite({
  address: accountAddress,
  abi: [
    {
      inputs: [],
      name: "acceptOwnership",
      outputs: [],
      stateMutability: "nonpayable",
      type: "function",
    },
  ],
  functionName: "acceptOwnership",
});
 
const { data, write } = useContractWrite(config);
 
const { isLoading, isSuccess } = useWaitForTransaction({
  hash: data?.hash,
});

Video Tutorial