Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

3.4 KB
Raw Blame
/**
 * x402 "exact" scheme client — SPL TransferChecked payment.
 *
 * Builds a TransferChecked transaction, signs + sends it, and returns the
 * tx signature as the proof payload for the X-PAYMENT header.
 *
 * Compatible with the x402 v2 spec and the PaymentPayload/PaymentRequirements
 * types from @pump-fun/agent-payments-sdk/x402.
 */
import {
  PublicKey,
  type Connection,
} from "@solana/web3.js";
import {
  getAssociatedTokenAddressSync,
  createAssociatedTokenAccountInstruction,
  createTransferCheckedInstruction,
  getMint,
  TOKEN_PROGRAM_ID,
  ASSOCIATED_TOKEN_PROGRAM_ID,
} from "@solana/spl-token";
import type { WalletProvider } from "../wallet/types.js";
import { buildAndSend } from "../tx/build.js";
import { memoInstruction } from "../utils/memo.js";
import type { ExactPaymentRequirements } from "./types.js";
import { x402NonceMemo } from "./types.js";

export interface ExactPaymentProof {
  signature: string;
  network: string;
}

/**
 * Pay using the "exact" x402 scheme.
 * Returns the proof to include in the X-PAYMENT header payload.
 */
export async function payExact(
  wallet: WalletProvider,
  connection: Connection,
  requirements: ExactPaymentRequirements,
): Promise<ExactPaymentProof> {
  const mint = new PublicKey(requirements.asset);
  const payTo = new PublicKey(requirements.payTo);
  const amount = BigInt(requirements.amount);

  const mintInfo = await getMint(connection, mint);
  const senderAta = getAssociatedTokenAddressSync(mint, wallet.publicKey, false, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID);
  const receiverAta = getAssociatedTokenAddressSync(mint, payTo, false, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID);

  const ixs = [];

  const receiverInfo = await connection.getAccountInfo(receiverAta);
  if (!receiverInfo) {
    ixs.push(
      createAssociatedTokenAccountInstruction(
        wallet.publicKey,
        receiverAta,
        payTo,
        mint,
        TOKEN_PROGRAM_ID,
        ASSOCIATED_TOKEN_PROGRAM_ID,
      ),
    );
  }

  ixs.push(
    createTransferCheckedInstruction(
      senderAta,
      mint,
      receiverAta,
      wallet.publicKey,
      amount,
      mintInfo.decimals,
      [],
      TOKEN_PROGRAM_ID,
    ),
  );

  // Bind the payment to the server-issued single-use challenge nonce by carrying
  // it as a memo. The facilitator matches this exact memo before settling, so
  // the payment cannot be replayed against any other request.
  const nonce = requirements.extra?.nonce;
  if (!nonce) {
    throw new Error("Exact payment requirements are missing the server-issued challenge nonce");
  }
  ixs.push(memoInstruction(x402NonceMemo(nonce)));

  const signature = await buildAndSend(wallet, connection, ixs);

  return { signature, network: requirements.network };
}

/**
 * Build an x402 PaymentPayload for the "exact" scheme.
 *
 * Pass the result to `encodePaymentPayload()` before attaching to the
 * `X-PAYMENT` request header on the retry. Top-level `scheme` and `network`
 * mirror the chosen `accepts[]` entry so the server's facilitator can route
 * the proof without re-walking the nested `accepted` object.
 */
export function buildExactPaymentPayload(
  requirements: ExactPaymentRequirements,
  proof: ExactPaymentProof,
  resourceUrl?: string,
) {
  return {
    x402Version: 2 as const,
    scheme: requirements.scheme,
    network: requirements.network,
    resource: resourceUrl,
    accepted: requirements,
    payload: proof,
  };
}