import { Script, Signer, Transaction, ClientPublicTestnet, SignerCkbPrivateKey, ClientPublicMainnet, } from "@ckb-ccc/core"; import { calculateChecksum, verifyChecksum, updateChecksum, verifyWitnessChecksum, verifyV3WitnessChecksum, verifyV3WitnessChain, } from "./utils/checksum"; import { createCKBFSCell, createPublishTransaction as utilCreatePublishTransaction, preparePublishTransaction, createAppendTransaction as utilCreateAppendTransaction, prepareAppendTransaction, createAppendTransactionDry, publishCKBFS as utilPublishCKBFS, appendCKBFS as utilAppendCKBFS, CKBFSCellOptions, PublishOptions, AppendOptions, // V3 functions PublishV3Options, AppendV3Options, TransferV3Options, publishCKBFSV3, appendCKBFSV3, transferCKBFSV3, createPublishV3Transaction, prepareAppendV3Transaction, preparePublishV3Transaction, createAppendV3Transaction, createAppendV3TransactionDry, createTransferV3Transaction, } from "./utils/transaction"; import { readFile, readFileAsText, readFileAsUint8Array, writeFile, getContentType, splitFileIntoChunks, combineChunksToFile, getFileContentFromChain, saveFileFromChain, getFileContentFromChainByTypeId, saveFileFromChainByTypeId, decodeFileFromChainByTypeId, getFileContentFromChainByIdentifier, saveFileFromChainByIdentifier, decodeFileFromChainByIdentifier, parseIdentifier, IdentifierType, decodeWitnessContent, decodeMultipleWitnessContents, extractFileFromWitnesses, decodeFileFromWitnessData, saveFileFromWitnessData, // V3 file retrieval functions getFileContentFromChainV3, getFileContentFromChainByIdentifierV3, saveFileFromChainByIdentifierV3, resolveCKBFSCell } from "./utils/file"; import { createCKBFSWitness, createTextCKBFSWitness, extractCKBFSWitnessContent, isCKBFSWitness, createChunkedCKBFSWitnesses, createCKBFSV3Witness, createChunkedCKBFSV3Witnesses, extractCKBFSV3WitnessContent, isCKBFSV3Witness, CKBFSV3WitnessOptions, } from "./utils/witness"; import { CKBFSData, BackLinkV1, BackLinkV2, CKBFSDataType, BackLinkType, CKBFS_HEADER, CKBFS_HEADER_STRING, } from "./utils/molecule"; import { NetworkType, ProtocolVersion, ProtocolVersionType, DEFAULT_NETWORK, DEFAULT_VERSION, CKBFS_CODE_HASH, CKBFS_TYPE_ID, ADLER32_CODE_HASH, ADLER32_TYPE_ID, DEP_GROUP_TX_HASH, DEPLOY_TX_HASH, getCKBFSScriptConfig, CKBFSScriptConfig, } from "./utils/constants"; import { ensureHexPrefix } from "./utils/transaction"; // Helper to encode string to Uint8Array const textEncoder = new TextEncoder(); /** * Custom options for file publishing and appending */ export interface FileOptions { contentType?: string; filename?: string; capacity?: bigint; feeRate?: number; network?: NetworkType; version?: ProtocolVersionType; useTypeID?: boolean; } /** * Options required when publishing content directly (string or Uint8Array) */ export type PublishContentOptions = Omit< FileOptions, "capacity" | "contentType" | "filename" > & Required> & { capacity?: bigint; }; /** * Options required when appending content directly (string or Uint8Array) */ export type AppendContentOptions = Omit< FileOptions, "contentType" | "filename" | "capacity" > & { capacity?: bigint; witnessStartIndex?: number; }; /** * Configuration options for the CKBFS SDK */ export interface CKBFSOptions { chunkSize?: number; version?: ProtocolVersionType; useTypeID?: boolean; network?: NetworkType; rpcUrl?: string; } /** * Main CKBFS SDK class */ export class CKBFS { private signer: Signer; private chunkSize: number; private network: NetworkType; private version: ProtocolVersionType; private useTypeID: boolean; private rpcUrl: string; /** * Creates a new CKBFS SDK instance * @param signerOrPrivateKey The signer instance or CKB private key to use for signing transactions * @param networkOrOptions The network type or configuration options * @param options Additional configuration options when using privateKey */ constructor( signerOrPrivateKey: Signer | string, networkOrOptions: NetworkType | CKBFSOptions = DEFAULT_NETWORK, options?: CKBFSOptions, ) { // Determine if first parameter is a Signer or privateKey if (typeof signerOrPrivateKey === "string") { // Initialize with private key const privateKey = signerOrPrivateKey; const network = typeof networkOrOptions === "string" ? networkOrOptions : DEFAULT_NETWORK; const opts = options || (typeof networkOrOptions === "object" ? networkOrOptions : {}); const client = network === "mainnet" ? new ClientPublicMainnet({ url: opts.rpcUrl, }) : new ClientPublicTestnet({ url: opts.rpcUrl, }); this.signer = new SignerCkbPrivateKey(client, privateKey); this.network = network; this.chunkSize = opts.chunkSize || 30 * 1024; this.version = opts.version || DEFAULT_VERSION; this.useTypeID = opts.useTypeID || false; this.rpcUrl = opts.rpcUrl || client.url; } else { // Initialize with signer this.signer = signerOrPrivateKey; const opts = typeof networkOrOptions === "object" ? networkOrOptions : {}; this.network = opts.network || DEFAULT_NETWORK; this.chunkSize = opts.chunkSize || 30 * 1024; this.version = opts.version || DEFAULT_VERSION; this.useTypeID = opts.useTypeID || false; this.rpcUrl = opts.rpcUrl || this.signer.client.url; } } /** * Gets the recommended address object for the signer * @returns Promise resolving to the address object */ async getAddress() { return this.signer.getRecommendedAddressObj(); } /** * Gets the lock script for the signer * @returns Promise resolving to the lock script */ async getLock(): Promise