Home

Transfer the owner of an account

Learn how to transfer ownership of a smart account.

With Openfort, you can change the ownership of an account from one address to another. Your players 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.

note

For more information about how to use the sesion key endpoints, you can visit our API documentation. Check out our sample with a transfer ownership example: GitHub source and video walkthrough.

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:

command-line

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

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

server.ts

_10
const Openfort = require('@openfort/openfort-node').default;
_10
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 documentation. Bear in mind this policy needs to have a account_functions policy rule to allow the sponsorship of this operation.

server.ts

_10
const accountId = 'acc_...';
_10
const policy = 'pol_...';
_10
const newOwnerAddress = '0x416c...354D';
_10
const playerTransferOwnership = await openfort.accounts.requestTransferOwnership(
_10
{
_10
id: accountId,
_10
policy: policy,
_10
newOwnerAddress: newOwnerAddress,
_10
}
_10
);

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:

accept-ownership.tsx

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