import type { HttpAgent } from "@icp-sdk/core/agent"; import type { CandidAdapterParameters, CandidDefinition, CandidClientManager, ReactorParser } from "./types.js"; import { CanisterId } from "@ic-reactor/core"; /** * CandidAdapter provides functionality to fetch and parse Candid definitions * from Internet Computer canisters. * * It supports multiple methods for retrieving Candid definitions: * 1. From canister metadata (preferred) * 2. From the `__get_candid_interface_tmp_hack` method (fallback) * * It also supports parsing Candid to JavaScript using: * 1. Local WASM parser (@ic-reactor/parser) - faster, no network request * 2. Remote didjs canister - always available fallback * * @example * ```typescript * import { CandidAdapter } from "@ic-reactor/candid" * import { ClientManager } from "@ic-reactor/core" * import { QueryClient } from "@tanstack/query-core" * * const queryClient = new QueryClient() * const clientManager = new ClientManager({ queryClient }) * await clientManager.initialize() * * const adapter = new CandidAdapter({ clientManager }) * * // Optionally load the local parser for faster processing * await adapter.loadParser() * * // Get the Candid definition for a canister * const { idlFactory } = await adapter.getCandidDefinition("ryjl3-tyaaa-aaaaa-aaaba-cai") * ``` */ export declare class CandidAdapter { /** The client manager providing agent and identity access. */ clientManager: CandidClientManager; /** The canister ID of the didjs canister for remote Candid compilation. */ didjsCanisterId: CanisterId; /** The optional local parser module. */ private parserModule?; /** Whether parser auto-loading has been attempted. */ private parserLoadAttempted; /** Function to unsubscribe from identity updates. */ unsubscribe: () => void; /** * Creates a new CandidAdapter instance. * * @param params - The adapter parameters. */ constructor({ clientManager, didjsCanisterId }: CandidAdapterParameters); /** * The HTTP agent from the client manager. */ get agent(): HttpAgent; /** * Whether the local parser is available. */ get hasParser(): boolean; /** * Loads the local parser module for converting Candid to JavaScript. * If no module is provided, attempts to dynamically load @ic-reactor/parser. * * @param module - Optional parser module to use. * @throws Error if the parser loading fails. * * @example * ```typescript * // Load the default parser * await adapter.loadParser() * * // Or provide a custom parser * import * as parser from "@ic-reactor/parser" * await adapter.loadParser(parser) * ``` */ loadParser(module?: ReactorParser): Promise; /** * Attempts to load the parser silently (no error if not available). * Useful for optional parser initialization. */ private tryLoadParser; /** * Gets the default didjs canister ID based on whether the agent is local or not. */ private getDefaultDidJsId; /** * Gets the parsed Candid definition for a canister, ready for use with Actor.createActor. * This is the main entry point for fetching a canister's interface. * * @param canisterId - The canister ID to get the Candid definition for. * @returns The parsed Candid definition with idlFactory and optional init. * @throws Error if fetching or parsing fails. * * @example * ```typescript * const { idlFactory } = await adapter.getCandidDefinition("ryjl3-tyaaa-aaaaa-aaaba-cai") * ``` */ getCandidDefinition(canisterId: CanisterId): Promise; /** * Fetches the raw Candid source string for a canister. * First attempts to get it from metadata, then falls back to the tmp hack method. * * @param canisterId - The canister ID to fetch the Candid source for. * @returns The raw Candid source string (.did file contents). * @throws Error if both methods fail. * * @example * ```typescript * const candidSource = await adapter.fetchCandidSource("ryjl3-tyaaa-aaaaa-aaaba-cai") * console.log(candidSource) // service { greet: (text) -> (text) query; } * ``` */ fetchCandidSource(canisterId: CanisterId): Promise; /** * Parses Candid source string and returns the definition with idlFactory. * First attempts to use the local parser, then falls back to the remote didjs canister. * * @param candidSource - The raw Candid source string. * @returns The parsed Candid definition. * @throws Error if parsing fails. */ parseCandidSource(candidSource: string): Promise; /** * Fetches Candid source from the canister's metadata. * * @param canisterId - The canister ID to query. * @returns The Candid source string, or undefined if not available. */ fetchFromMetadata(canisterId: CanisterId): Promise; /** * Fetches Candid source using the temporary hack method. * This calls the `__get_candid_interface_tmp_hack` query method on the canister. * * @param canisterId - The canister ID to query. * @returns The Candid source string. */ fetchFromTmpHack(canisterId: CanisterId): Promise; /** * Compiles Candid source to JavaScript using the local WASM parser. * * @param candidSource - The Candid source to compile. * @returns The compiled JavaScript code. * @throws Error if the parser is not loaded. */ compileLocal(candidSource: string): string; /** * Compiles Candid source to JavaScript using the remote didjs canister. * * @param candidSource - The Candid source to compile. * @param didjsCanisterId - Optional custom didjs canister ID. * @returns The compiled JavaScript code, or undefined if compilation fails. */ compileRemote(candidSource: string, didjsCanisterId?: string): Promise; /** * Validates Candid source using the local parser. * * @param candidSource - The Candid source to validate. * @returns True if the source is valid, false otherwise. * @throws Error if the parser is not loaded. */ validateCandid(candidSource: string): boolean; /** * @deprecated Use `loadParser()` instead. */ initializeParser(module?: ReactorParser): Promise; /** * @deprecated Use `fetchCandidSource()` instead. */ fetchCandidDefinition(canisterId: CanisterId): Promise; /** * @deprecated Use `fetchFromMetadata()` instead. */ getFromMetadata(canisterId: CanisterId): Promise; /** * @deprecated Use `fetchFromTmpHack()` instead. */ getFromTmpHack(canisterId: CanisterId): Promise; /** * @deprecated Use `parseCandidSource()` instead. */ evaluateCandidDefinition(data: string): Promise; /** * @deprecated Use `compileRemote()` instead. */ fetchDidTojs(candidSource: string, didjsCanisterId?: string): Promise; /** * @deprecated Use `compileLocal()` instead. */ parseDidToJs(candidSource: string): string; /** * @deprecated Use `validateCandid()` instead. */ validateIDL(candidSource: string): boolean; } //# sourceMappingURL=adapter.d.ts.map