UNPKG

@gorbchain-xyz/chaindecode

Version:

GorbchainSDK V1.3+ - Complete Solana development toolkit with advanced cryptography, messaging, and collaboration features. Build secure applications with blockchain, DeFi, and end-to-end encryption.

127 lines (126 loc) 4.13 kB
/** * Central registry for managing blockchain program instruction decoders * * This module provides the core functionality for registering, managing, * and executing instruction decoders for various blockchain programs. */ /** * Represents a decoded blockchain instruction with structured data */ export interface DecodedInstruction { /** The type/name of the decoded instruction */ type: string; /** The program ID that owns this instruction */ programId: string; /** Parsed instruction data specific to the instruction type */ data: Record<string, unknown>; /** Array of account information used by this instruction */ accounts: string[]; /** Optional raw instruction data for debugging/reference */ raw?: Record<string, unknown>; } /** * Raw instruction interface for type safety */ export interface RawInstruction { programId: string; data?: Uint8Array | number[]; accounts?: string[]; programAddress?: { toString(): string; }; } /** * Function signature for instruction decoder implementations * * @param instruction - Raw instruction object to decode * @returns Decoded instruction with structured data */ export type DecoderFunction = (instruction: RawInstruction) => DecodedInstruction; /** * Registry for managing instruction decoders across multiple programs * * The DecoderRegistry maintains a mapping between program IDs and their * corresponding decoder functions, enabling automatic instruction decoding * based on the program that created the instruction. * * @example * ```typescript * const registry = new DecoderRegistry(); * * // Register a decoder for SPL Token instructions * registry.register('spl-token', 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', * (instruction) => ({ * type: 'spl-token-transfer', * programId: instruction.programId, * data: { amount: instruction.amount }, * accounts: instruction.accounts * }) * ); * * // Decode an instruction * const decoded = registry.decode(instruction); * ``` */ export declare class DecoderRegistry { /** Internal map of program names to decoder functions */ private decoders; /** Internal map of program IDs to program names for lookup */ private programIdToName; /** * Register a decoder function for a specific blockchain program * * @param programName - Human-readable name for the program * @param programId - The program's public key address * @param decoder - Function that decodes instructions for this program * * @example * ```typescript * registry.register( * 'my-custom-program', * 'ProgramId12345...', * (instruction) => ({ * type: 'custom-action', * programId: instruction.programId, * data: parseCustomData(instruction.data), * accounts: instruction.accounts * }) * ); * ``` */ register(programName: string, programId: string, decoder: DecoderFunction): void; /** * Decode an instruction using the appropriate decoder */ decode(instruction: RawInstruction): DecodedInstruction; /** * Check if a decoder is registered for a specific program ID * * @param programId - The program's public key address to check * @returns True if a decoder is registered for this program ID * * @example * ```typescript * if (registry.hasDecoder('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA')) { * console.log('SPL Token decoder is available'); * } * ``` */ hasDecoder(programId: string): boolean; /** * Get a list of all registered program names * * @returns Array of program names that have registered decoders * * @example * ```typescript * const programs = registry.getRegisteredPrograms(); * console.log('Supported programs:', programs); * // Output: ['spl-token', 'gorba-token', 'custom-program'] * ``` */ getRegisteredPrograms(): string[]; private createRawResult; private createErrorResult; private toHexString; }