import type { IdentityResult, UAIResolveResult, UAIReverseResult, UAIRegisterResult, PriceResult, } from "./types"; export class IdentitySDK { constructor(private fetch: (path: string, init?: RequestInit) => Promise) {} // ── Email identity ───────────────────────────────────────────────────────── /** Resolve a Fluid Wallet email address to its on-chain wallet address */ async resolve(email: string): Promise { const r = await this.fetch(`/v1/agents/identity/resolve?email=${encodeURIComponent(email)}`); const data = await r.json() as any; if (!r.ok) throw new Error(data.error ?? "identity lookup failed"); return data as IdentityResult; } /** Swap transaction history for the current agent key */ async history(limit = 20): Promise { const r = await this.fetch(`/v1/agents/history?limit=${limit}`); const data = await r.json() as any; if (!r.ok) throw new Error(data.error ?? "history fetch failed"); return data.history ?? []; } // ── UAI / Fluid ID ───────────────────────────────────────────────────────── /** * Resolve a Fluid ID username to a wallet address. * @param username e.g. "alice" (without .fluidbase.eth suffix) * @param networkId Optional chain filter: "base" | "ethereum" | "solana" */ async resolveFluidId(username: string, networkId?: string): Promise { const qs = networkId ? `?networkId=${encodeURIComponent(networkId)}` : ""; const r = await this.fetch(`/api/fw-names/resolve/${encodeURIComponent(username)}${qs}`); const data = await r.json() as any; if (!r.ok) throw new Error(data.error ?? "Fluid ID lookup failed"); return { username: data.username ?? username, address: data.address ?? null, displayName: data.displayName ?? null, avatarUrl: data.avatarUrl ?? null, networkId: data.networkId ?? networkId, }; } /** * Reverse-resolve a wallet address to its registered Fluid ID. * @param address EVM (0x…) or Solana address */ async reverseFluidId(address: string): Promise { const r = await this.fetch(`/api/fw-names/reverse/${encodeURIComponent(address)}`); const data = await r.json() as any; if (!r.ok) throw new Error(data.error ?? "Fluid ID reverse lookup failed"); return { address, username: data.username ?? null, fluidId: data.username ? `${data.username}.fluidbase.eth` : null, }; } /** * Register a Fluid ID for the current agent's wallet address. * @param username Desired Fluid ID (e.g. "myapp-agent") */ async registerFluidId(username: string): Promise { const r = await this.fetch("/api/fw-names/register", { method: "POST", body: JSON.stringify({ username }), }); const data = await r.json() as any; if (!r.ok) throw new Error(data.error ?? "Fluid ID registration failed"); return data as UAIRegisterResult; } // ── Prices ──────────────────────────────────────────────────────────────── /** * Get the current USD spot price for any supported token. * @param token e.g. "ETH", "BTC", "SOL", "USDC" */ async getPrice(token: string): Promise { const r = await this.fetch(`/api/prices/simple?ids=${encodeURIComponent(token.toLowerCase())}&vs_currencies=usd`); const data = await r.json() as any; if (!r.ok) throw new Error(data.error ?? "price fetch failed"); const usd = data?.[token.toLowerCase()]?.usd ?? data?.price ?? data?.usd ?? null; if (usd == null) throw new Error(`Price not found for ${token}`); return { token: token.toUpperCase(), usd: Number(usd), source: data.source }; } }