Interactions with the TDK API typically require an authenticated user to make the requests from the client side. However, the nature of Treasure Connect’s on-chain session creation also allows games to act on a Treasure Account’s behalf through the backend only for the duration and scope of the session.

This feature is currently in alpha availability and is subject to change. The method of signing via private key will be replaced by AWS or Google KMS in the next iteration.

Prerequisites

  • TDK Core library installed in your project.
  • Access to a backend wallet’s private key. Please coordinate with Treasure to configure.
  • The Treasure Account you want to act on behalf of must have already created a session through the client-side TDK with the same backendWallet address that will be used to sign the backend-to-backend request. See here for more information on starting sessions from a React-based application.

Usage

import {
  createTreasureConnectClient,
  generateAccountSignature,
  TDKAPI,
} from "@treasure-dev/tdk-core";

const client = createTreasureConnectClient({ clientId: "..." });

const tdk = new TDKAPI({
  baseUri: "https://tdk-api.spellcaster.lol",
  chainId: 421614,
  backendWallet: "...",
});

const accountAddress = "..."; // The Treasure Account address you want to act on behalf of

await tdk.transaction.create(
  {
    address: "0x55d0cf68a1afe0932aff6f36c87efa703508191c",
    abi: [
      {
        type: "function",
        name: "transfer",
        stateMutability: "nonpayable",
        inputs: [
          {
            name: "recipient",
            type: "address",
          },
          {
            name: "amount",
            type: "uint256",
          },
        ],
        outputs: [
          {
            type: "bool",
          },
        ],
      },
    ] as const,
    functionName: "transfer",
    args: [
      "...", // recipient address
      1000000000000000000000n, // 1,000
    ],
  },
  {
    accountAddress,
    accountSignature: await generateAccountSignature({
      client,
      accountAddress,
      backendWalletPrivateKey: "...",
    }),
  },
);

The key change in the above flow is that the TDKAPI client doesn’t need to be created with a user’s auth token. Instead, the account address is specified in the transaction options, along with a payload signed by the backend wallet that will be verified by the TDK API.