@ckbfs/api
Version:
SDK for CKBFS protocol on CKB
245 lines (244 loc) • 9.61 kB
TypeScript
/**
* Utility functions for file operations
*/
/**
* Reads a file from the file system
* @param filePath The path to the file to read
* @returns Buffer containing the file contents
*/
export declare function readFile(filePath: string): Buffer;
/**
* Reads a file as text from the file system
* @param filePath The path to the file to read
* @returns String containing the file contents
*/
export declare function readFileAsText(filePath: string): string;
/**
* Reads a file as Uint8Array from the file system
* @param filePath The path to the file to read
* @returns Uint8Array containing the file contents
*/
export declare function readFileAsUint8Array(filePath: string): Uint8Array;
/**
* Writes data to a file in the file system
* @param filePath The path to write the file to
* @param data The data to write to the file
*/
export declare function writeFile(filePath: string, data: Buffer | string): void;
/**
* Gets the MIME content type based on file extension
* @param filePath The path to the file
* @returns The MIME content type for the file
*/
export declare function getContentType(filePath: string): string;
/**
* Splits a file into chunks of a specific size
* @param filePath The path to the file to split
* @param chunkSize The maximum size of each chunk in bytes
* @returns Array of Uint8Array chunks
*/
export declare function splitFileIntoChunks(filePath: string, chunkSize: number): Uint8Array[];
/**
* Combines chunks into a single file
* @param chunks Array of chunks to combine
* @param outputPath The path to write the combined file to
*/
export declare function combineChunksToFile(chunks: Uint8Array[], outputPath: string): void;
/**
* Retrieves complete file content from the blockchain by following backlinks
* @param client The CKB client to use for blockchain queries
* @param outPoint The output point of the latest CKBFS cell
* @param ckbfsData The data from the latest CKBFS cell
* @returns Promise resolving to the complete file content
*/
export declare function getFileContentFromChain(client: any, outPoint: {
txHash: string;
index: number;
}, ckbfsData: any): Promise<Uint8Array>;
/**
* Saves file content retrieved from blockchain to disk
* @param content The file content to save
* @param ckbfsData The CKBFS cell data containing file metadata
* @param outputPath Optional path to save the file (defaults to filename in current directory)
* @returns The path where the file was saved
*/
export declare function saveFileFromChain(content: Uint8Array, ckbfsData: any, outputPath?: string): string;
/**
* Decodes content from a single CKBFS witness
* @param witnessHex The witness data in hex format (with or without 0x prefix)
* @returns Object containing the decoded content and metadata, or null if not a valid CKBFS witness
*/
export declare function decodeWitnessContent(witnessHex: string): {
content: Uint8Array;
isValid: boolean;
} | null;
/**
* Decodes and combines content from multiple CKBFS witnesses
* @param witnessHexArray Array of witness data in hex format
* @param preserveOrder Whether to preserve the order of witnesses (default: true)
* @returns Combined content from all valid CKBFS witnesses
*/
export declare function decodeMultipleWitnessContents(witnessHexArray: string[], preserveOrder?: boolean): Uint8Array;
/**
* Extracts complete file content from witnesses using specified indexes
* @param witnesses Array of all witnesses from a transaction
* @param indexes Array of witness indexes that contain CKBFS content
* @returns Combined content from the specified witness indexes
*/
export declare function extractFileFromWitnesses(witnesses: string[], indexes: number[]): Uint8Array;
/**
* Decodes file content directly from witness data without blockchain queries
* @param witnessData Object containing witness information
* @returns Object containing the decoded file content and metadata
*/
export declare function decodeFileFromWitnessData(witnessData: {
witnesses: string[];
indexes: number[] | number;
filename?: string;
contentType?: string;
}): {
content: Uint8Array;
filename?: string;
contentType?: string;
size: number;
};
/**
* Saves decoded file content directly from witness data
* @param witnessData Object containing witness information
* @param outputPath Optional path to save the file
* @returns The path where the file was saved
*/
export declare function saveFileFromWitnessData(witnessData: {
witnesses: string[];
indexes: number[] | number;
filename?: string;
contentType?: string;
}, outputPath?: string): string;
/**
* Identifier types for CKBFS cells
*/
export declare enum IdentifierType {
TypeID = "typeId",
OutPoint = "outPoint",
Unknown = "unknown"
}
/**
* Parsed identifier information
*/
export interface ParsedIdentifier {
type: IdentifierType;
typeId?: string;
txHash?: string;
index?: number;
original: string;
}
/**
* Detects and parses different CKBFS identifier formats
* @param identifier The identifier string to parse
* @returns Parsed identifier information
*/
export declare function parseIdentifier(identifier: string): ParsedIdentifier;
/**
* Retrieves complete file content from the blockchain using any supported identifier
* @param client The CKB client to use for blockchain queries
* @param identifier The identifier (TypeID hex, CKBFS TypeID URI, or CKBFS outPoint URI)
* @param options Optional configuration for network, version, and useTypeID
* @returns Promise resolving to the complete file content and metadata
*/
export declare function getFileContentFromChainByIdentifier(client: any, identifier: string, options?: {
network?: "mainnet" | "testnet";
version?: string;
useTypeID?: boolean;
}): Promise<{
content: Uint8Array;
filename: string;
contentType: string;
checksum: number;
size: number;
backLinks: any[];
parsedId: ParsedIdentifier;
} | null>;
/**
* Retrieves complete file content from the blockchain using TypeID (legacy function)
* @param client The CKB client to use for blockchain queries
* @param typeId The TypeID (args) of the CKBFS cell
* @param options Optional configuration for network, version, and useTypeID
* @returns Promise resolving to the complete file content and metadata
*/
export declare function getFileContentFromChainByTypeId(client: any, typeId: string, options?: {
network?: "mainnet" | "testnet";
version?: string;
useTypeID?: boolean;
}): Promise<{
content: Uint8Array;
filename: string;
contentType: string;
checksum: number;
size: number;
backLinks: any[];
} | null>;
/**
* Saves file content retrieved from blockchain by identifier to disk
* @param client The CKB client to use for blockchain queries
* @param identifier The identifier (TypeID hex, CKBFS TypeID URI, or CKBFS outPoint URI)
* @param outputPath Optional path to save the file (defaults to filename from CKBFS data)
* @param options Optional configuration for network, version, and useTypeID
* @returns Promise resolving to the path where the file was saved, or null if file not found
*/
export declare function saveFileFromChainByIdentifier(client: any, identifier: string, outputPath?: string, options?: {
network?: "mainnet" | "testnet";
version?: string;
useTypeID?: boolean;
}): Promise<string | null>;
/**
* Saves file content retrieved from blockchain by TypeID to disk (legacy function)
* @param client The CKB client to use for blockchain queries
* @param typeId The TypeID (args) of the CKBFS cell
* @param outputPath Optional path to save the file (defaults to filename from CKBFS data)
* @param options Optional configuration for network, version, and useTypeID
* @returns Promise resolving to the path where the file was saved, or null if file not found
*/
export declare function saveFileFromChainByTypeId(client: any, typeId: string, outputPath?: string, options?: {
network?: "mainnet" | "testnet";
version?: string;
useTypeID?: boolean;
}): Promise<string | null>;
/**
* Decodes file content directly from identifier using witness decoding (new method)
* @param client The CKB client to use for blockchain queries
* @param identifier The identifier (TypeID hex, CKBFS TypeID URI, or CKBFS outPoint URI)
* @param options Optional configuration for network, version, and useTypeID
* @returns Promise resolving to the decoded file content and metadata, or null if not found
*/
export declare function decodeFileFromChainByIdentifier(client: any, identifier: string, options?: {
network?: "mainnet" | "testnet";
version?: string;
useTypeID?: boolean;
}): Promise<{
content: Uint8Array;
filename: string;
contentType: string;
checksum: number;
size: number;
backLinks: any[];
parsedId: ParsedIdentifier;
} | null>;
/**
* Decodes file content directly from TypeID using witness decoding (legacy function)
* @param client The CKB client to use for blockchain queries
* @param typeId The TypeID (args) of the CKBFS cell
* @param options Optional configuration for network, version, and useTypeID
* @returns Promise resolving to the decoded file content and metadata, or null if not found
*/
export declare function decodeFileFromChainByTypeId(client: any, typeId: string, options?: {
network?: "mainnet" | "testnet";
version?: string;
useTypeID?: boolean;
}): Promise<{
content: Uint8Array;
filename: string;
contentType: string;
checksum: number;
size: number;
backLinks: any[];
} | null>;