import type { CreateKeyParams, PauliKey, RevokeKeyResult } from "./types"; /** * PauliKeySDK — create and manage scoped agent keys. * * Pauli Keys are capability-scoped sub-keys derived from a parent `fwag_` key. * They follow the child-scope ⊆ parent-scope rule: * - A parent with scopes ["read","pay","swap"] can spawn children with any subset * - A parent with scope ["read"] cannot spawn a child with scope ["pay"] * * Key format: fwag__ * * @example * const workerKey = await agent.keys.create({ * label: "worker-payment-agent", * scopes: ["pay"], * spendLimit: "100", // $100 max per transaction * dailyLimit: "500", // $500 max per day * }); * // workerKey.keyId === "fwag_pay_a3f9c2b1..." */ export class PauliKeySDK { constructor(private fetch: (path: string, init?: RequestInit) => Promise) {} /** * Create a new scoped Pauli key. * Scopes must be a subset of the parent key's scopes. * The full key value is returned once — store it securely. */ async create(params: CreateKeyParams): Promise<{ key: string; meta: PauliKey }> { const r = await this.fetch("/v1/agents/keys/create", { method: "POST", body: JSON.stringify(params), }); const data = await r.json() as any; if (!r.ok) throw new Error(data.error ?? "key creation failed"); return { key: data.key, meta: data.meta as PauliKey }; } /** * List all active Pauli keys under the current parent key. * The full key value is NOT returned — only metadata. */ async list(): Promise { const r = await this.fetch("/v1/agents/keys"); const data = await r.json() as any; if (!r.ok) throw new Error(data.error ?? "key list failed"); return (data.keys ?? []) as PauliKey[]; } /** * Revoke (deactivate) a Pauli key by its keyId. * Revocation is immediate — any in-flight requests using the key will fail. */ async revoke(keyId: string): Promise { const r = await this.fetch("/v1/agents/keys/revoke", { method: "POST", body: JSON.stringify({ keyId }), }); const data = await r.json() as any; if (!r.ok) throw new Error(data.error ?? "key revocation failed"); return data as RevokeKeyResult; } /** * Get metadata for a specific Pauli key by its keyId. */ async get(keyId: string): Promise { const r = await this.fetch(`/v1/agents/keys/${encodeURIComponent(keyId)}`); const data = await r.json() as any; if (!r.ok) throw new Error(data.error ?? "key fetch failed"); return data as PauliKey; } }