arcium-frontend

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Arcium Frontend Development

Arcium前端开发

Guide for building encrypted Solana applications using Arcium.
本指南介绍如何使用Arcium构建加密的Solana应用。

When to Use This Skill

适用场景

Use this skill when:
  • Building a new Arcium-powered frontend
  • Adding encrypted computations to existing apps
  • Integrating Solana wallet adapters
  • Deriving Arcium PDAs
  • Encrypting user inputs
  • Building and sending encrypted instructions
  • Troubleshooting Arcium-related errors
在以下场景中可使用本技能:
  • 构建全新的Arcium驱动前端
  • 为现有应用添加加密计算功能
  • 集成Solana钱包适配器
  • 派生Arcium PDA
  • 加密用户输入
  • 构建并发送加密指令
  • 排查Arcium相关错误

Required Dependencies

所需依赖

bash
npm install @arcium-hq/client @coral-xyz/anchor @solana/web3.js
bash
npm install @arcium-hq/client @coral-xyz/anchor @solana/web3.js

Environment Variables

环境变量

VariableRequiredDefault
ARCIUM_CLUSTER_OFFSET
Yes
NEXT_PUBLIC_MXE_PROGRAM_ID
Yes
NEXT_PUBLIC_RPC_URL
Nodevnet
变量名是否必填默认值
ARCIUM_CLUSTER_OFFSET
NEXT_PUBLIC_MXE_PROGRAM_ID
NEXT_PUBLIC_RPC_URL
devnet

Core Flow

核心流程

  1. Setup provider → Create Anchor provider from wallet
  2. Fetch MXE key
    getMXEPublicKey(provider, programId)
  3. Generate offset → Random 8-byte computation offset
  4. Encrypt values → X25519 key exchange + RescueCipher
  5. Derive PDAs → MXE, cluster, computation, comp def accounts
  6. Build instruction → Encode discriminator + encrypted data
  7. Send transaction → Sign and submit with compute budget
  1. 设置Provider → 从钱包创建Anchor Provider
  2. 获取MXE公钥
    getMXEPublicKey(provider, programId)
  3. 生成偏移量 → 随机8字节计算偏移量
  4. 加密数值 → X25519密钥交换 + RescueCipher
  5. 派生PDA → MXE、集群、计算、计算定义账户
  6. 构建指令 → 编码鉴别器 + 加密数据
  7. 发送交易 → 签名并提交,附带计算预算

Key Imports

关键导入

typescript
import * as anchor from '@coral-xyz/anchor';
import { PublicKey, SystemProgram } from '@solana/web3.js';
import {
  getMXEPublicKey,
  getClusterAccAddress,
  getMXEAccAddress,
  getMempoolAccAddress,
  getExecutingPoolAccAddress,
  getComputationAccAddress,
  getCompDefAccAddress,
  getArciumAccountBaseSeed,
  getCompDefAccOffset,
  getArciumProgramId,
  x25519,
  RescueCipher,
  deserializeLE,
} from '@arcium-hq/client';
typescript
import * as anchor from '@coral-xyz/anchor';
import { PublicKey, SystemProgram } from '@solana/web3.js';
import {
  getMXEPublicKey,
  getClusterAccAddress,
  getMXEAccAddress,
  getMempoolAccAddress,
  getExecutingPoolAccAddress,
  getComputationAccAddress,
  getCompDefAccAddress,
  getArciumAccountBaseSeed,
  getCompDefAccOffset,
  getArciumProgramId,
  x25519,
  RescueCipher,
  deserializeLE,
} from '@arcium-hq/client';

Encryption Pattern

加密模式

typescript
// 1. Fetch MXE public key
const mxePublicKey = await getMXEPublicKey(provider, programId);

// 2. Generate ephemeral keypair
const privateKey = x25519.utils.randomPrivateKey();
const publicKey = x25519.getPublicKey(privateKey);

// 3. Derive shared secret
const sharedSecret = x25519.getSharedSecret(privateKey, mxePublicKey);

// 4. Encrypt values
const cipher = new RescueCipher(sharedSecret);
const nonce = nacl.randomBytes(16);
const values = [BigInt(10), BigInt(7), BigInt(5)];
const ciphertexts = cipher.encrypt(values, nonce);
typescript
// 1. 获取MXE公钥
const mxePublicKey = await getMXEPublicKey(provider, programId);

// 2. 生成临时密钥对
const privateKey = x25519.utils.randomPrivateKey();
const publicKey = x25519.getPublicKey(privateKey);

// 3. 派生共享密钥
const sharedSecret = x25519.getSharedSecret(privateKey, mxePublicKey);

// 4. 加密数值
const cipher = new RescueCipher(sharedSecret);
const nonce = nacl.randomBytes(16);
const values = [BigInt(10), BigInt(7), BigInt(5)];
const ciphertexts = cipher.encrypt(values, nonce);

PDA Derivation Pattern

PDA派生模式

typescript
// Cluster-based PDAs
const clusterOffset = 768109697; // from ARCIUM_CLUSTER_OFFSET
const clusterAccount = getClusterAccAddress(clusterOffset);
const mempoolAccount = getMempoolAccAddress(clusterOffset);
const executingPool = getExecutingPoolAccAddress(clusterOffset);

// Program-based PDAs
const mxeAccount = getMXEAccAddress(programId);
const computationOffset = new anchor.BN(nacl.randomBytes(8), 'le');
const computationAccount = getComputationAccAddress(clusterOffset, computationOffset);

// Comp def account
const baseSeed = getArciumAccountBaseSeed('ComputationDefinitionAccount');
const offsetBytes = getCompDefAccOffset('my_computation');
const [compDefAccount] = PublicKey.findProgramAddressSync(
  [baseSeed, programId.toBuffer(), offsetBytes],
  getArciumProgramId()
);
typescript
// 基于集群的PDA
const clusterOffset = 768109697; // 来自ARCIUM_CLUSTER_OFFSET
const clusterAccount = getClusterAccAddress(clusterOffset);
const mempoolAccount = getMempoolAccAddress(clusterOffset);
const executingPool = getExecutingPoolAccAddress(clusterOffset);

// 基于程序的PDA
const mxeAccount = getMXEAccAddress(programId);
const computationOffset = new anchor.BN(nacl.randomBytes(8), 'le');
const computationAccount = getComputationAccAddress(clusterOffset, computationOffset);

// 计算定义账户
const baseSeed = getArciumAccountBaseSeed('ComputationDefinitionAccount');
const offsetBytes = getCompDefAccOffset('my_computation');
const [compDefAccount] = PublicKey.findProgramAddressSync(
  [baseSeed, programId.toBuffer(), offsetBytes],
  getArciumProgramId()
);

Account Ordering

账户顺序

Standard order for encrypted instructions:
  1. payer
    (signer, writable)
  2. mxeAccount
    (not signer, not writable)
  3. mempoolAccount
    (not signer, writable)
  4. executingPool
    (not signer, writable)
  5. computationAccount
    (not signer, writable)
  6. compDefAccount
    (not signer, not writable)
  7. clusterAccount
    (not signer, writable)
  8. Game-specific accounts
  9. SystemProgram.programId
  10. arciumProgramId
加密指令的标准顺序:
  1. payer
    (签名者,可写)
  2. mxeAccount
    (非签名者,不可写)
  3. mempoolAccount
    (非签名者,可写)
  4. executingPool
    (非签名者,可写)
  5. computationAccount
    (非签名者,可写)
  6. compDefAccount
    (非签名者,不可写)
  7. clusterAccount
    (非签名者,可写)
  8. 游戏专属账户
  9. SystemProgram.programId
  10. arciumProgramId

Common Errors

常见错误

ErrorCauseSolution
ARCIUM_CLUSTER_OFFSET is missing
Missing env varAdd to
.env.local
MXE Public Key not found
MXE not initializedInitialize MXE account first
Account not found
Wrong PDA derivationCheck cluster offset matches network
错误信息原因解决方案
ARCIUM_CLUSTER_OFFSET is missing
缺少环境变量添加至
.env.local
MXE Public Key not found
MXE未初始化先初始化MXE账户
Account not found
PDA派生错误检查集群偏移量是否与网络匹配

Detailed References

详细参考资料

For in-depth explanations, see:
  • references/encryption-flow.md — X25519 + RescueCipher details
  • references/pda-derivation.md — Account derivation patterns
  • references/wallet-integration.md — Wallet adapter setup
  • references/nextjs-patterns.md — Next.js App Router integration
  • references/env-configuration.md — Environment setup
  • references/troubleshooting.md — Common errors and fixes
如需深入了解,请查看:
  • references/encryption-flow.md — X25519 + RescueCipher 细节说明
  • references/pda-derivation.md — 账户派生模式
  • references/wallet-integration.md — 钱包适配器设置
  • references/nextjs-patterns.md — Next.js App Router 集成
  • references/env-configuration.md — 环境设置
  • references/troubleshooting.md — 常见错误与修复方案