Docs
chromia.com

Register an account

On this page

This section demonstrates how to register a new account on the blockchain using either Chromia-native keys or MetaMask (EVM-compatible keys). By now, you should have a client connection established and be familiar with auth descriptors.

FT4 account registration flowClient (your app)Your chain (Rell + FT4)Key or walletFT key · EVM wallet1Pay (transfer strategies)transfer to address2RegisterregisterAccount()3Log indisposable keylogin()5Pending depositheld until claimedregister_accountstrategy checks rules4Accountmain descriptor ["A","T"]session
Registering an FT4 account. With the open strategy, skip step 2. With a transfer strategy (open, fee or subscription), the user first sends tokens to their future account address, and register_account claims them.

Chromia-native account registration

JavaScript
import { registerAccount, registrationStrategy } from "@chromia/ft4";
import { createSingleSigAuthDescriptorRegistration } from "@chromia/ft4";
import { createInMemoryFtKeyStore } from "@chromia/ft4";

/**
 * Registers an account on the blockchain.
 *
 * @param {object} connection - A valid FT4 connection instance.
 * @param {object} keyPair - The key pair to register the account with (format: { pubKey, privKey }).
 */
async function registerChromiaNativeAccount(connection, keyPair) {
  const keyStore = createInMemoryFtKeyStore(keyPair); // Create a keystore using the provided key pair
  const authDescriptor = createSingleSigAuthDescriptorRegistration(["A", "T"], keyStore.id); // Create the auth descriptor

  const { session } = await registerAccount(
    connection.client,
    keyStore,
    registrationStrategy.open(authDescriptor)//Use the open strategy for registration
  );

  console.log(`Account registered successfully! Account ID: ${session.account.id.toString("hex")}`);
}

An available key pair can be passed directly to registerChromiaNativeAccount, or one can be generated as follows:

JavaScript
const keyPair = encryption.makeKeyPair(); // Generates a key pair with pubKey and privKey
console.log("Save your private key securely:", keyPair.privKey.toString("hex"));

Call registerChromiaNativeAccount, passing the connection and the key pair:

JavaScript
await registerChromiaNativeAccount(connection, keyPair);

MetaMask account registration

JavaScript
import { createWeb3ProviderEvmKeyStore } from "@chromia/ft4";
import { registerAccount, registrationStrategy } from "@chromia/ft4";
import { createSingleSigAuthDescriptorRegistration } from "@chromia/ft4";

/**
 * Registers an account on the blockchain using MetaMask.
 *
 * @param {object} connection - A valid FT4 connection instance.
 */
async function registerMetaMaskAccount(connection) {
  if (!window.ethereum) {
    console.error("MetaMask not found. Please install MetaMask.");
    return;
  }

  console.log("Connecting to MetaMask...");
  const keyStore = await createWeb3ProviderEvmKeyStore(window.ethereum); // Use MetaMask to create a keystore

  const authDescriptor = createSingleSigAuthDescriptorRegistration(
    ["A", "T"],//Permissions for the account
    keyStore.id
  );

  const { session } = await registerAccount(connection.client, keyStore, registrationStrategy.open(authDescriptor));

  console.log(`Account registered successfully! Account ID: ${session.account.id.toString("hex")}`);
}

To register an account with MetaMask, make sure the browser plugin is available, and call the function with a valid connection:

JavaScript
await registerMetaMaskAccount(connection);

In this repository, you can find additional code examples for account registration.