Players and Accounts

Learn how and when to create a player and their respective accounts using Openfort SDKs and APIs.

Overview

This guide explains how to use Openfort to interact and manage your players and their accounts inside your game ecosystem. When using Openfort, you must create a player for each individual that wants to use your game. Each player can have accounts on different chains; they all will have the same public address.

player-account-relation

What are Players?

You create players every time a user signs up in your platform. Players are used to identify users and enable them to interact with your game. Ideally, you want to create a player as soon as a new user signs up to your game and assing the newly created user with the player_id that Openfort returns.

player-sign-flow

Openfort provides a backend SDK you can use. Otherwise, you can use the API directly.

What are Accounts?

Openfort accounts represent the Ethereum account and they are used to manage the assets of a player. Players in Openfort don't have any account assigned to them when created.

Whenever you want to create an account, you'll simply need to create it using a player_id and a chain_id. In most circumstances it's not necessary to create an account manually. Whenever you use that player_id to perform an on-chain action, Openfort will automatically create an account for you in the specified chain_id.

AccountDecentralization.svg

The custodial sheme of that newly created account will depend on wether you provide the external_owner_address or not. When not provided, a custodial signer will be created for the new account.

Quickstart

In this guide you'll learn how to create a player, create an account for it and then mint an NFT.

1. Set up Openfort - Server side

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

Install Openfort

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

Initialize '@openfort/openfort-node' wih your secret key

index.js
const openfort = require('@openfort/openfort-node')('sk_test_...');

2. Create a Player - Server side

Whenever a new user signs up to your game, you'll need to create a player in Openfort.

Create a player

POST
/v1/players
curl https://api.openfort.xyz/v1/players \
  -H "Authorization: Bearer $YOUR_SECRET_KEY" \
  -d name="First player" \
  -d description="My First Test Player (created for API docs at https://www.openfort.xyz/docs/api)"

After creating a player, you'll receive a player_id that you can use from this point on to interact with the Openfort API.

3. Add the contract you want to interact with

Add Asset Contract to your game project. In this example, we'll use a simple ERC721 contract on the Mumbai network deployed at 0x38090d1636069c0ff1Af6bc1737Fb996B7f63AC0.

Add an asset contract

POST
/v1/contracts
curl https://api.openfort.xyz/v1/contracts \
  -H "Authorization: Bearer $YOUR_SECRET_KEY" \
  -d chain_id=80001 \
  -d address="0x38090d1636069c0ff1Af6bc1737Fb996B7f63AC0" \
  -d name="Simple NFT" \
  -d description="tutorial NFT asset"

This will return a contract_id that you can use from this point on to interact with the Openfort API. You can always identified by checking it's prefix con_.

4. (Custodial) Mint an asset with a Player - Server side

Create a transaction

POST
/v1/transaction_intents
curl https://api.openfort.xyz/v1/transaction_intents \
  -H "Authorization: Bearer $YOUR_SECRET_KEY" \
  -d player=pla_... \
  -d optimistic=true \
  -d chain_id=80001 \
  -d "interactions[0][contract]=con_..." \
  -d "interactions[0][function_name]=mint" \
  -d "interactions[0][function_args][0]=pla_..."

If you want to learn more about how transaction intents work, visit our Execute Intents guide.

4. (Self-custodial) Mint an asset with a Player - Server side Client side

To create a self-custodial account, you'll need to provide the external_owner_address when creating the transaction intent. If you don't provide it, a custodial account will be created instead.

Create a transaction

POST
/v1/transaction_intents
curl https://api.openfort.xyz/v1/transaction_intents \
  -H "Authorization: Bearer $YOUR_SECRET_KEY" \
  -d player=pla_... \
  -d optimistic=true \
  -d chain_id=80001 \
  -d external_owner_address="0x3809...3ac0" \
  -d "interactions[0][contract]=con_..." \
  -d "interactions[0][function_name]=mint" \
  -d "interactions[0][function_args][0]=pla_..."

The response of this request will contain a next_action object.

next_action response object from transaction_intents

"next_action": {
    "type": "sign_with_wallet",
    "payload": {
        "user_op": {
            "sender": "0x32a03030Cf534F299492a91cdeef5eD6A98558e2",
            ...},
        "user_op_hash": "0x91b4efe3648c79467f7b50aa9bb1b4eae383a52dd6d741d39ece29ed2ef8362d"
    }
},

Where you will need to sign the user_op_hash with the account you specified in the external_owner_address field. You have to then send the signed transaction back to Openfort.

To do so, you can either use our client SDK or use our API directly. In this demo, let's we'll be using the client SDK in a JavaScript environment.

Install Openfort.js

Command line
npm install @openfort/openfort-js --save

Initialize Openfort

index.js
import { Openfort } from '@openfort/openfort-js';
const openfort = new Openfort('pk_test_...');

await openfort.sendSignatureSessionRequest(
  transactionIntent.id,
  SIGNED_USED_OP_HASH,
);

The flow will look like this:

transaction_intent-sign

5. Conclusion

We now have a fully working implementation that allows us to mint assets for a player. Nevertheless, there are a two main disadvantages of this current implementation:

  • The newly created account will need to have native tokens to pay for the transaction fees:

    We can use Openfort policies to sponsor transactions for our users. This way, we can pay for the transaction fees of our users. Learn how to use them at Sponsoring Transactions.

  • When non-custodial, the owner will need to sign each transaction introducing friction (e.g. pop-ups) to the user experience):

    We can use Openfort session keys to create scoped session keys in client side. This way, we can sign transactions without UX interruptions. Learn how to use them at Session Keys.

Check out our working working examples of this quickstart: