Docs
chromia.com

Automate cross-chain asset registration

On this page

Automating cross-chain FT4 asset registration can streamline workflows in controlled environments, but it also introduces potential risks. Proper validation of assets and careful handling of admin credentials are essential.


Set up a client

Instantiate a chromiaClient for your chain:

TypeScript
import { createClient } from "postchain-client";

const chromiaClient = await createClient({
  directoryNodeUrlPool: ["http://localhost:7740"],//Replace with your node URL(s)
  blockchainRid: Buffer.from("your-blockchain-rid", "hex"),//Replace with your chain's RID
});

Set up the admin signature provider

Instantiate the adminSignatureProvider using the private key:

TypeScript
import { encryption, newSignatureProvider } from "postchain-client";

const adminPrivateKey = process.env.ADMIN_PRIVATE_KEY;
if (!adminPrivateKey) {
  throw new Error("ADMIN_PRIVATE_KEY is not set.");
}

const adminKeyPair = encryption.makeKeyPair(adminPrivateKey);
const adminSignatureProvider = newSignatureProvider(adminKeyPair);

Register a cross-chain asset

TypeScript
import {
  createClientToBlockchain,
  registerCrosschainAsset,
  getAssetDetailsForCrosschainRegistration,
} from "@chromia/ft4";
import type { BufferId, TransactionReceipt } from "postchain-client";

export async function registerAssetWithValidation(
  assetId: BufferId,
  originBlockchainRid: BufferId
): Promise<{ receipt: TransactionReceipt }> {
  // Create a client for the origin blockchain
  const originBlockchainClient = await createClientToBlockchain(chromiaClient, originBlockchainRid);

  // Fetch asset details from the origin blockchain
  const asset = await getAssetDetailsForCrosschainRegistration(originBlockchainClient, assetId);

  // Ensure the issuing blockchain matches the origin chain
  if (Buffer.compare(asset.blockchainRid, originBlockchainRid) !== 0) {
    throw new Error(
      `Validation error: Issuing blockchain (${asset.blockchainRid.toString(
        "hex"
      )}) does not match the origin (${originBlockchainRid.toString("hex")}).`
    );
  }

  // Register the validated asset on the destination chain
  return await registerCrosschainAsset(chromiaClient, adminSignatureProvider, assetId, originBlockchainRid);
}