Changing 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.
Overview#
The never ending battle between security and usability is a constant in the blockchain space. Specially when it comes to account ownership and custody.
Openfort solves this problem by offering a flexible custody model that allows for a secure and frictionless way of transferring account ownership.
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.
How Transactions Work#
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 behaviour 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
_10npm install @openfort/openfort-node --save
Initialize '@openfort/openfort-node
_10const Openfort = require('@openfort/openfort-node').default_10const openfort = new Openfort(YOUR_SECRET_KEY)
2. Request transfer ownership - Server side#
Openfort will performa 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.
Request transfer ownership:
_12const playerId = 'pla_...'_12const policy = 'pol_...'_12const chainId = 80001_12const newOwnerAddress = '0x416c...354D'_12const playerTransferOwnership = await openfort.players.transferAccountOwnership(_12 {_12 playerId: playerId,_12 policy: policy,_12 chainId: chainId,_12 newOwnerAddress: newOwnerAddress,_12 }_12)
3. Accept account ownership - Client side#
Using Wagmi React hooks, you can accept the account ownership by performing an acceptOwnership
operation.
Find a working example of how to accept account ownership in our GitHub repository component sample.
Accept account ownership from client side
_25import {_25 usePrepareContractWrite,_25 useContractWrite,_25 useWaitForTransaction,_25} from 'wagmi'_25_25const { 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_25const { data, write } = useContractWrite(config)_25_25const { isLoading, isSuccess } = useWaitForTransaction({_25 hash: data?.hash,_25})
4. Conclusion#
For more information about how to use the sesion key endpoints, you can visit our API documentation.
Check out our working working examples of this quickstart:
- Sample registering a session key with an account with a self-custodied signer: GitHub source and video walkthrough.