@ckbfs/api 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,91 @@
1
+ import { Script, Signer, Transaction } from "@ckb-ccc/core";
2
+ import { calculateChecksum, verifyChecksum, updateChecksum, verifyWitnessChecksum } from './utils/checksum';
3
+ import { createCKBFSCell, createPublishTransaction, createAppendTransaction, publishCKBFS, appendCKBFS, CKBFSCellOptions, PublishOptions, AppendOptions } from './utils/transaction';
4
+ import { readFile, readFileAsText, readFileAsUint8Array, writeFile, getContentType, splitFileIntoChunks, combineChunksToFile } from './utils/file';
5
+ import { createCKBFSWitness, createTextCKBFSWitness, extractCKBFSWitnessContent, isCKBFSWitness, createChunkedCKBFSWitnesses } from './utils/witness';
6
+ import { CKBFSData, BackLinkV1, BackLinkV2, CKBFSDataType, BackLinkType, CKBFS_HEADER, CKBFS_HEADER_STRING } from './utils/molecule';
7
+ import { NetworkType, ProtocolVersion, 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';
8
+ /**
9
+ * Custom options for file publishing and appending
10
+ */
11
+ export interface FileOptions {
12
+ contentType?: string;
13
+ filename?: string;
14
+ capacity?: bigint;
15
+ feeRate?: number;
16
+ network?: NetworkType;
17
+ version?: string;
18
+ useTypeID?: boolean;
19
+ }
20
+ /**
21
+ * Configuration options for the CKBFS SDK
22
+ */
23
+ export interface CKBFSOptions {
24
+ chunkSize?: number;
25
+ version?: string;
26
+ useTypeID?: boolean;
27
+ network?: NetworkType;
28
+ }
29
+ /**
30
+ * Main CKBFS SDK class
31
+ */
32
+ export declare class CKBFS {
33
+ private signer;
34
+ private chunkSize;
35
+ private network;
36
+ private version;
37
+ private useTypeID;
38
+ /**
39
+ * Creates a new CKBFS SDK instance
40
+ * @param signerOrPrivateKey The signer instance or CKB private key to use for signing transactions
41
+ * @param networkOrOptions The network type or configuration options
42
+ * @param options Additional configuration options when using privateKey
43
+ */
44
+ constructor(signerOrPrivateKey: Signer | string, networkOrOptions?: NetworkType | CKBFSOptions, options?: CKBFSOptions);
45
+ /**
46
+ * Gets the recommended address object for the signer
47
+ * @returns Promise resolving to the address object
48
+ */
49
+ getAddress(): Promise<import("@ckb-ccc/core").Address>;
50
+ /**
51
+ * Gets the lock script for the signer
52
+ * @returns Promise resolving to the lock script
53
+ */
54
+ getLock(): Promise<Script>;
55
+ /**
56
+ * Gets the CKBFS script configuration for the current settings
57
+ * @returns The CKBFS script configuration
58
+ */
59
+ getCKBFSConfig(): CKBFSScriptConfig;
60
+ /**
61
+ * Publishes a file to CKBFS
62
+ * @param filePath The path to the file to publish
63
+ * @param options Options for publishing the file
64
+ * @returns Promise resolving to the transaction hash
65
+ */
66
+ publishFile(filePath: string, options?: FileOptions): Promise<string>;
67
+ /**
68
+ * Appends content to an existing CKBFS file
69
+ * @param filePath The path to the file containing the content to append
70
+ * @param ckbfsCell The CKBFS cell to append to
71
+ * @param options Additional options for the append operation
72
+ * @returns Promise resolving to the transaction hash
73
+ */
74
+ appendFile(filePath: string, ckbfsCell: AppendOptions['ckbfsCell'], options?: Omit<FileOptions, 'contentType' | 'filename'>): Promise<string>;
75
+ /**
76
+ * Creates a new transaction for publishing a file but doesn't sign or send it
77
+ * @param filePath The path to the file to publish
78
+ * @param options Options for publishing the file
79
+ * @returns Promise resolving to the unsigned transaction
80
+ */
81
+ createPublishTransaction(filePath: string, options?: FileOptions): Promise<Transaction>;
82
+ /**
83
+ * Creates a new transaction for appending content but doesn't sign or send it
84
+ * @param filePath The path to the file containing the content to append
85
+ * @param ckbfsCell The CKBFS cell to append to
86
+ * @param options Additional options for the append operation
87
+ * @returns Promise resolving to the unsigned transaction
88
+ */
89
+ createAppendTransaction(filePath: string, ckbfsCell: AppendOptions['ckbfsCell'], options?: Omit<FileOptions, 'contentType' | 'filename'>): Promise<Transaction>;
90
+ }
91
+ export { calculateChecksum, verifyChecksum, updateChecksum, verifyWitnessChecksum, createCKBFSCell, createPublishTransaction, createAppendTransaction, publishCKBFS, appendCKBFS, readFile, readFileAsText, readFileAsUint8Array, writeFile, getContentType, splitFileIntoChunks, combineChunksToFile, createCKBFSWitness, createTextCKBFSWitness, extractCKBFSWitnessContent, isCKBFSWitness, createChunkedCKBFSWitnesses, CKBFSData, BackLinkV1, BackLinkV2, CKBFSDataType, BackLinkType, CKBFSCellOptions, PublishOptions, AppendOptions, CKBFS_HEADER, CKBFS_HEADER_STRING, NetworkType, ProtocolVersion, 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 };
package/dist/index.js ADDED
@@ -0,0 +1,224 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCKBFSScriptConfig = exports.DEPLOY_TX_HASH = exports.DEP_GROUP_TX_HASH = exports.ADLER32_TYPE_ID = exports.ADLER32_CODE_HASH = exports.CKBFS_TYPE_ID = exports.CKBFS_CODE_HASH = exports.DEFAULT_VERSION = exports.DEFAULT_NETWORK = exports.ProtocolVersion = exports.NetworkType = exports.CKBFS_HEADER_STRING = exports.CKBFS_HEADER = exports.BackLinkV2 = exports.BackLinkV1 = exports.CKBFSData = exports.createChunkedCKBFSWitnesses = exports.isCKBFSWitness = exports.extractCKBFSWitnessContent = exports.createTextCKBFSWitness = exports.createCKBFSWitness = exports.combineChunksToFile = exports.splitFileIntoChunks = exports.getContentType = exports.writeFile = exports.readFileAsUint8Array = exports.readFileAsText = exports.readFile = exports.appendCKBFS = exports.publishCKBFS = exports.createAppendTransaction = exports.createPublishTransaction = exports.createCKBFSCell = exports.verifyWitnessChecksum = exports.updateChecksum = exports.verifyChecksum = exports.calculateChecksum = exports.CKBFS = void 0;
4
+ const core_1 = require("@ckb-ccc/core");
5
+ const checksum_1 = require("./utils/checksum");
6
+ Object.defineProperty(exports, "calculateChecksum", { enumerable: true, get: function () { return checksum_1.calculateChecksum; } });
7
+ Object.defineProperty(exports, "verifyChecksum", { enumerable: true, get: function () { return checksum_1.verifyChecksum; } });
8
+ Object.defineProperty(exports, "updateChecksum", { enumerable: true, get: function () { return checksum_1.updateChecksum; } });
9
+ Object.defineProperty(exports, "verifyWitnessChecksum", { enumerable: true, get: function () { return checksum_1.verifyWitnessChecksum; } });
10
+ const transaction_1 = require("./utils/transaction");
11
+ Object.defineProperty(exports, "createCKBFSCell", { enumerable: true, get: function () { return transaction_1.createCKBFSCell; } });
12
+ Object.defineProperty(exports, "createPublishTransaction", { enumerable: true, get: function () { return transaction_1.createPublishTransaction; } });
13
+ Object.defineProperty(exports, "createAppendTransaction", { enumerable: true, get: function () { return transaction_1.createAppendTransaction; } });
14
+ Object.defineProperty(exports, "publishCKBFS", { enumerable: true, get: function () { return transaction_1.publishCKBFS; } });
15
+ Object.defineProperty(exports, "appendCKBFS", { enumerable: true, get: function () { return transaction_1.appendCKBFS; } });
16
+ const file_1 = require("./utils/file");
17
+ Object.defineProperty(exports, "readFile", { enumerable: true, get: function () { return file_1.readFile; } });
18
+ Object.defineProperty(exports, "readFileAsText", { enumerable: true, get: function () { return file_1.readFileAsText; } });
19
+ Object.defineProperty(exports, "readFileAsUint8Array", { enumerable: true, get: function () { return file_1.readFileAsUint8Array; } });
20
+ Object.defineProperty(exports, "writeFile", { enumerable: true, get: function () { return file_1.writeFile; } });
21
+ Object.defineProperty(exports, "getContentType", { enumerable: true, get: function () { return file_1.getContentType; } });
22
+ Object.defineProperty(exports, "splitFileIntoChunks", { enumerable: true, get: function () { return file_1.splitFileIntoChunks; } });
23
+ Object.defineProperty(exports, "combineChunksToFile", { enumerable: true, get: function () { return file_1.combineChunksToFile; } });
24
+ const witness_1 = require("./utils/witness");
25
+ Object.defineProperty(exports, "createCKBFSWitness", { enumerable: true, get: function () { return witness_1.createCKBFSWitness; } });
26
+ Object.defineProperty(exports, "createTextCKBFSWitness", { enumerable: true, get: function () { return witness_1.createTextCKBFSWitness; } });
27
+ Object.defineProperty(exports, "extractCKBFSWitnessContent", { enumerable: true, get: function () { return witness_1.extractCKBFSWitnessContent; } });
28
+ Object.defineProperty(exports, "isCKBFSWitness", { enumerable: true, get: function () { return witness_1.isCKBFSWitness; } });
29
+ Object.defineProperty(exports, "createChunkedCKBFSWitnesses", { enumerable: true, get: function () { return witness_1.createChunkedCKBFSWitnesses; } });
30
+ const molecule_1 = require("./utils/molecule");
31
+ Object.defineProperty(exports, "CKBFSData", { enumerable: true, get: function () { return molecule_1.CKBFSData; } });
32
+ Object.defineProperty(exports, "BackLinkV1", { enumerable: true, get: function () { return molecule_1.BackLinkV1; } });
33
+ Object.defineProperty(exports, "BackLinkV2", { enumerable: true, get: function () { return molecule_1.BackLinkV2; } });
34
+ Object.defineProperty(exports, "CKBFS_HEADER", { enumerable: true, get: function () { return molecule_1.CKBFS_HEADER; } });
35
+ Object.defineProperty(exports, "CKBFS_HEADER_STRING", { enumerable: true, get: function () { return molecule_1.CKBFS_HEADER_STRING; } });
36
+ const constants_1 = require("./utils/constants");
37
+ Object.defineProperty(exports, "NetworkType", { enumerable: true, get: function () { return constants_1.NetworkType; } });
38
+ Object.defineProperty(exports, "ProtocolVersion", { enumerable: true, get: function () { return constants_1.ProtocolVersion; } });
39
+ Object.defineProperty(exports, "DEFAULT_NETWORK", { enumerable: true, get: function () { return constants_1.DEFAULT_NETWORK; } });
40
+ Object.defineProperty(exports, "DEFAULT_VERSION", { enumerable: true, get: function () { return constants_1.DEFAULT_VERSION; } });
41
+ Object.defineProperty(exports, "CKBFS_CODE_HASH", { enumerable: true, get: function () { return constants_1.CKBFS_CODE_HASH; } });
42
+ Object.defineProperty(exports, "CKBFS_TYPE_ID", { enumerable: true, get: function () { return constants_1.CKBFS_TYPE_ID; } });
43
+ Object.defineProperty(exports, "ADLER32_CODE_HASH", { enumerable: true, get: function () { return constants_1.ADLER32_CODE_HASH; } });
44
+ Object.defineProperty(exports, "ADLER32_TYPE_ID", { enumerable: true, get: function () { return constants_1.ADLER32_TYPE_ID; } });
45
+ Object.defineProperty(exports, "DEP_GROUP_TX_HASH", { enumerable: true, get: function () { return constants_1.DEP_GROUP_TX_HASH; } });
46
+ Object.defineProperty(exports, "DEPLOY_TX_HASH", { enumerable: true, get: function () { return constants_1.DEPLOY_TX_HASH; } });
47
+ Object.defineProperty(exports, "getCKBFSScriptConfig", { enumerable: true, get: function () { return constants_1.getCKBFSScriptConfig; } });
48
+ /**
49
+ * Main CKBFS SDK class
50
+ */
51
+ class CKBFS {
52
+ /**
53
+ * Creates a new CKBFS SDK instance
54
+ * @param signerOrPrivateKey The signer instance or CKB private key to use for signing transactions
55
+ * @param networkOrOptions The network type or configuration options
56
+ * @param options Additional configuration options when using privateKey
57
+ */
58
+ constructor(signerOrPrivateKey, networkOrOptions = constants_1.DEFAULT_NETWORK, options) {
59
+ // Determine if first parameter is a Signer or privateKey
60
+ if (typeof signerOrPrivateKey === 'string') {
61
+ // Initialize with private key
62
+ const privateKey = signerOrPrivateKey;
63
+ const network = typeof networkOrOptions === 'string' ? networkOrOptions : constants_1.DEFAULT_NETWORK;
64
+ const opts = options || (typeof networkOrOptions === 'object' ? networkOrOptions : {});
65
+ const client = new core_1.ClientPublicTestnet();
66
+ this.signer = new core_1.SignerCkbPrivateKey(client, privateKey);
67
+ this.network = network;
68
+ this.chunkSize = opts.chunkSize || 30 * 1024;
69
+ this.version = opts.version || constants_1.DEFAULT_VERSION;
70
+ this.useTypeID = opts.useTypeID || false;
71
+ }
72
+ else {
73
+ // Initialize with signer
74
+ this.signer = signerOrPrivateKey;
75
+ const opts = typeof networkOrOptions === 'object' ? networkOrOptions : {};
76
+ this.network = opts.network || constants_1.DEFAULT_NETWORK;
77
+ this.chunkSize = opts.chunkSize || 30 * 1024;
78
+ this.version = opts.version || constants_1.DEFAULT_VERSION;
79
+ this.useTypeID = opts.useTypeID || false;
80
+ }
81
+ }
82
+ /**
83
+ * Gets the recommended address object for the signer
84
+ * @returns Promise resolving to the address object
85
+ */
86
+ async getAddress() {
87
+ return this.signer.getRecommendedAddressObj();
88
+ }
89
+ /**
90
+ * Gets the lock script for the signer
91
+ * @returns Promise resolving to the lock script
92
+ */
93
+ async getLock() {
94
+ const address = await this.getAddress();
95
+ return address.script;
96
+ }
97
+ /**
98
+ * Gets the CKBFS script configuration for the current settings
99
+ * @returns The CKBFS script configuration
100
+ */
101
+ getCKBFSConfig() {
102
+ return (0, constants_1.getCKBFSScriptConfig)(this.network, this.version, this.useTypeID);
103
+ }
104
+ /**
105
+ * Publishes a file to CKBFS
106
+ * @param filePath The path to the file to publish
107
+ * @param options Options for publishing the file
108
+ * @returns Promise resolving to the transaction hash
109
+ */
110
+ async publishFile(filePath, options = {}) {
111
+ // Read the file and split into chunks
112
+ const fileContent = (0, file_1.readFileAsUint8Array)(filePath);
113
+ const contentChunks = [];
114
+ for (let i = 0; i < fileContent.length; i += this.chunkSize) {
115
+ contentChunks.push(fileContent.slice(i, i + this.chunkSize));
116
+ }
117
+ // Get the lock script
118
+ const lock = await this.getLock();
119
+ // Determine content type if not provided
120
+ const contentType = options.contentType || (0, file_1.getContentType)(filePath);
121
+ // Use the filename from the path if not provided
122
+ const pathParts = filePath.split(/[\\\/]/);
123
+ const filename = options.filename || pathParts[pathParts.length - 1];
124
+ // Create and sign the transaction
125
+ const tx = await (0, transaction_1.publishCKBFS)(this.signer, {
126
+ contentChunks,
127
+ contentType,
128
+ filename,
129
+ lock,
130
+ capacity: options.capacity,
131
+ feeRate: options.feeRate,
132
+ network: options.network || this.network,
133
+ version: options.version || this.version,
134
+ useTypeID: options.useTypeID !== undefined ? options.useTypeID : this.useTypeID
135
+ });
136
+ console.log('tx', tx.stringify());
137
+ // Send the transaction
138
+ const txHash = await this.signer.sendTransaction(tx);
139
+ return txHash;
140
+ }
141
+ /**
142
+ * Appends content to an existing CKBFS file
143
+ * @param filePath The path to the file containing the content to append
144
+ * @param ckbfsCell The CKBFS cell to append to
145
+ * @param options Additional options for the append operation
146
+ * @returns Promise resolving to the transaction hash
147
+ */
148
+ async appendFile(filePath, ckbfsCell, options = {}) {
149
+ // Read the file and split into chunks
150
+ const fileContent = (0, file_1.readFileAsUint8Array)(filePath);
151
+ const contentChunks = [];
152
+ for (let i = 0; i < fileContent.length; i += this.chunkSize) {
153
+ contentChunks.push(fileContent.slice(i, i + this.chunkSize));
154
+ }
155
+ // Create and sign the transaction
156
+ const tx = await (0, transaction_1.appendCKBFS)(this.signer, {
157
+ ckbfsCell,
158
+ contentChunks,
159
+ feeRate: options.feeRate,
160
+ network: options.network || this.network,
161
+ version: options.version || this.version
162
+ });
163
+ // Send the transaction
164
+ const txHash = await this.signer.sendTransaction(tx);
165
+ return txHash;
166
+ }
167
+ /**
168
+ * Creates a new transaction for publishing a file but doesn't sign or send it
169
+ * @param filePath The path to the file to publish
170
+ * @param options Options for publishing the file
171
+ * @returns Promise resolving to the unsigned transaction
172
+ */
173
+ async createPublishTransaction(filePath, options = {}) {
174
+ // Read the file and split into chunks
175
+ const fileContent = (0, file_1.readFileAsUint8Array)(filePath);
176
+ const contentChunks = [];
177
+ for (let i = 0; i < fileContent.length; i += this.chunkSize) {
178
+ contentChunks.push(fileContent.slice(i, i + this.chunkSize));
179
+ }
180
+ // Get the lock script
181
+ const lock = await this.getLock();
182
+ // Determine content type if not provided
183
+ const contentType = options.contentType || (0, file_1.getContentType)(filePath);
184
+ // Use the filename from the path if not provided
185
+ const pathParts = filePath.split(/[\\\/]/);
186
+ const filename = options.filename || pathParts[pathParts.length - 1];
187
+ // Create the transaction
188
+ return (0, transaction_1.createPublishTransaction)(this.signer, {
189
+ contentChunks,
190
+ contentType,
191
+ filename,
192
+ lock,
193
+ capacity: options.capacity,
194
+ feeRate: options.feeRate,
195
+ network: options.network || this.network,
196
+ version: options.version || this.version,
197
+ useTypeID: options.useTypeID !== undefined ? options.useTypeID : this.useTypeID
198
+ });
199
+ }
200
+ /**
201
+ * Creates a new transaction for appending content but doesn't sign or send it
202
+ * @param filePath The path to the file containing the content to append
203
+ * @param ckbfsCell The CKBFS cell to append to
204
+ * @param options Additional options for the append operation
205
+ * @returns Promise resolving to the unsigned transaction
206
+ */
207
+ async createAppendTransaction(filePath, ckbfsCell, options = {}) {
208
+ // Read the file and split into chunks
209
+ const fileContent = (0, file_1.readFileAsUint8Array)(filePath);
210
+ const contentChunks = [];
211
+ for (let i = 0; i < fileContent.length; i += this.chunkSize) {
212
+ contentChunks.push(fileContent.slice(i, i + this.chunkSize));
213
+ }
214
+ // Create the transaction
215
+ return (0, transaction_1.createAppendTransaction)(this.signer, {
216
+ ckbfsCell,
217
+ contentChunks,
218
+ feeRate: options.feeRate,
219
+ network: options.network || this.network,
220
+ version: options.version || this.version
221
+ });
222
+ }
223
+ }
224
+ exports.CKBFS = CKBFS;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Utility functions for Adler32 checksum generation and verification
3
+ */
4
+ /**
5
+ * Calculates Adler32 checksum for the provided data
6
+ * @param data The data to calculate checksum for
7
+ * @returns Promise resolving to the calculated checksum as a number
8
+ */
9
+ export declare function calculateChecksum(data: Uint8Array): Promise<number>;
10
+ /**
11
+ * Updates an existing checksum with new data
12
+ * @param previousChecksum The existing checksum to update
13
+ * @param newData The new data to add to the checksum
14
+ * @returns Promise resolving to the updated checksum as a number
15
+ */
16
+ export declare function updateChecksum(previousChecksum: number, newData: Uint8Array): Promise<number>;
17
+ /**
18
+ * Verifies if a given checksum matches the expected checksum for the data
19
+ * @param data The data to verify
20
+ * @param expectedChecksum The expected checksum
21
+ * @returns Promise resolving to a boolean indicating whether the checksum is valid
22
+ */
23
+ export declare function verifyChecksum(data: Uint8Array, expectedChecksum: number): Promise<boolean>;
24
+ /**
25
+ * Verifies the checksum of a CKBFS witness
26
+ * @param witness The witness bytes
27
+ * @param expectedChecksum The expected checksum
28
+ * @param backlinks Optional backlinks to use for checksum verification
29
+ * @returns Promise resolving to a boolean indicating whether the checksum is valid
30
+ */
31
+ export declare function verifyWitnessChecksum(witness: Uint8Array, expectedChecksum: number, backlinks?: {
32
+ checksum: number;
33
+ }[]): Promise<boolean>;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateChecksum = calculateChecksum;
4
+ exports.updateChecksum = updateChecksum;
5
+ exports.verifyChecksum = verifyChecksum;
6
+ exports.verifyWitnessChecksum = verifyWitnessChecksum;
7
+ const hash_wasm_1 = require("hash-wasm");
8
+ /**
9
+ * Utility functions for Adler32 checksum generation and verification
10
+ */
11
+ /**
12
+ * Calculates Adler32 checksum for the provided data
13
+ * @param data The data to calculate checksum for
14
+ * @returns Promise resolving to the calculated checksum as a number
15
+ */
16
+ async function calculateChecksum(data) {
17
+ const checksumString = await (0, hash_wasm_1.adler32)(data);
18
+ const checksumBuffer = Buffer.from(checksumString, 'hex');
19
+ return checksumBuffer.readUInt32BE();
20
+ }
21
+ /**
22
+ * Updates an existing checksum with new data
23
+ * @param previousChecksum The existing checksum to update
24
+ * @param newData The new data to add to the checksum
25
+ * @returns Promise resolving to the updated checksum as a number
26
+ */
27
+ async function updateChecksum(previousChecksum, newData) {
28
+ // In a real implementation, this would require the actual Adler32 state recovery
29
+ // For now, we're simply concatenating the previousChecksum as a hex string with the new data
30
+ // and calculating a new checksum
31
+ const checksumBytes = Buffer.alloc(4);
32
+ checksumBytes.writeUInt32BE(previousChecksum);
33
+ // Concatenate the previous checksum bytes with the new data
34
+ const combinedData = Buffer.concat([checksumBytes, Buffer.from(newData)]);
35
+ // Calculate the new checksum
36
+ return calculateChecksum(combinedData);
37
+ }
38
+ /**
39
+ * Verifies if a given checksum matches the expected checksum for the data
40
+ * @param data The data to verify
41
+ * @param expectedChecksum The expected checksum
42
+ * @returns Promise resolving to a boolean indicating whether the checksum is valid
43
+ */
44
+ async function verifyChecksum(data, expectedChecksum) {
45
+ const calculatedChecksum = await calculateChecksum(data);
46
+ return calculatedChecksum === expectedChecksum;
47
+ }
48
+ /**
49
+ * Verifies the checksum of a CKBFS witness
50
+ * @param witness The witness bytes
51
+ * @param expectedChecksum The expected checksum
52
+ * @param backlinks Optional backlinks to use for checksum verification
53
+ * @returns Promise resolving to a boolean indicating whether the checksum is valid
54
+ */
55
+ async function verifyWitnessChecksum(witness, expectedChecksum, backlinks = []) {
56
+ // Extract the content bytes from the witness (skip the CKBFS header and version)
57
+ const contentBytes = witness.slice(6);
58
+ // If backlinks are provided, use the last backlink's checksum
59
+ if (backlinks.length > 0) {
60
+ const lastBacklink = backlinks[backlinks.length - 1];
61
+ const updatedChecksum = await updateChecksum(lastBacklink.checksum, contentBytes);
62
+ return updatedChecksum === expectedChecksum;
63
+ }
64
+ // Otherwise, calculate checksum from scratch
65
+ return verifyChecksum(contentBytes, expectedChecksum);
66
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * CKBFS protocol deployment constants
3
+ */
4
+ export declare enum NetworkType {
5
+ Mainnet = "mainnet",
6
+ Testnet = "testnet"
7
+ }
8
+ export declare const ProtocolVersion: {
9
+ readonly V1: "20240906.ce6724722cf6";
10
+ readonly V2: "20241025.db973a8e8032";
11
+ };
12
+ export type ProtocolVersionType = typeof ProtocolVersion[keyof typeof ProtocolVersion];
13
+ export declare const CKBFS_CODE_HASH: Record<NetworkType, Record<string, string>>;
14
+ export declare const CKBFS_TYPE_ID: Record<NetworkType, Record<string, string>>;
15
+ export declare const ADLER32_CODE_HASH: Record<NetworkType, Record<string, string>>;
16
+ export declare const ADLER32_TYPE_ID: Record<NetworkType, Record<string, string>>;
17
+ export declare const DEP_GROUP_TX_HASH: Record<NetworkType, Record<string, string>>;
18
+ export declare const DEPLOY_TX_HASH: Record<NetworkType, Record<string, {
19
+ ckbfs: string;
20
+ adler32: string;
21
+ }>>;
22
+ export declare const DEFAULT_VERSION: "20241025.db973a8e8032";
23
+ export declare const DEFAULT_NETWORK = NetworkType.Testnet;
24
+ export interface CKBFSScriptConfig {
25
+ codeHash: string;
26
+ hashType: 'data1' | 'type';
27
+ depTxHash: string;
28
+ depIndex?: number;
29
+ }
30
+ /**
31
+ * Get CKBFS script configuration for a specific network and version
32
+ * @param network Network type (mainnet or testnet)
33
+ * @param version Protocol version (default: latest version)
34
+ * @param useTypeID Whether to use type ID instead of code hash (default: false)
35
+ * @returns CKBFS script configuration
36
+ */
37
+ export declare function getCKBFSScriptConfig(network?: NetworkType, version?: string, useTypeID?: boolean): CKBFSScriptConfig;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ /**
3
+ * CKBFS protocol deployment constants
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DEFAULT_NETWORK = exports.DEFAULT_VERSION = exports.DEPLOY_TX_HASH = exports.DEP_GROUP_TX_HASH = exports.ADLER32_TYPE_ID = exports.ADLER32_CODE_HASH = exports.CKBFS_TYPE_ID = exports.CKBFS_CODE_HASH = exports.ProtocolVersion = exports.NetworkType = void 0;
7
+ exports.getCKBFSScriptConfig = getCKBFSScriptConfig;
8
+ var NetworkType;
9
+ (function (NetworkType) {
10
+ NetworkType["Mainnet"] = "mainnet";
11
+ NetworkType["Testnet"] = "testnet";
12
+ })(NetworkType || (exports.NetworkType = NetworkType = {}));
13
+ // Use string literals for version values to avoid TypeScript indexing issues
14
+ exports.ProtocolVersion = {
15
+ V1: '20240906.ce6724722cf6', // Original version, compact and simple, suitable for small files
16
+ V2: '20241025.db973a8e8032' // New version, more features and can do complex operations
17
+ };
18
+ // CKBFS Type Script Constants
19
+ exports.CKBFS_CODE_HASH = {
20
+ [NetworkType.Mainnet]: {
21
+ [exports.ProtocolVersion.V2]: '0x31e6376287d223b8c0410d562fb422f04d1d617b2947596a14c3d2efb7218d3a'
22
+ },
23
+ [NetworkType.Testnet]: {
24
+ [exports.ProtocolVersion.V1]: '0xe8905ad29a02cf8befa9c258f4f941773839a618d75a64afc22059de9413f712',
25
+ [exports.ProtocolVersion.V2]: '0x31e6376287d223b8c0410d562fb422f04d1d617b2947596a14c3d2efb7218d3a'
26
+ }
27
+ };
28
+ exports.CKBFS_TYPE_ID = {
29
+ [NetworkType.Mainnet]: {
30
+ [exports.ProtocolVersion.V2]: '0xfd2058c9a0c0183354cf637e25d2707ffa9bb6fa2ba9b29f4ebc6be3e54ad7eb'
31
+ },
32
+ [NetworkType.Testnet]: {
33
+ [exports.ProtocolVersion.V1]: '0x88ef4d436af35684a27edda0d44dd8771318330285f90f02d13606e095aea86f',
34
+ [exports.ProtocolVersion.V2]: '0x7c6dcab8268201f064dc8676b5eafa60ca2569e5c6209dcbab0eb64a9cb3aaa3'
35
+ }
36
+ };
37
+ // Adler32 Hasher Constants
38
+ exports.ADLER32_CODE_HASH = {
39
+ [NetworkType.Mainnet]: {
40
+ [exports.ProtocolVersion.V2]: '0x2138683f76944437c0c643664120d620bdb5858dd6c9d1d156805e279c2c536f'
41
+ },
42
+ [NetworkType.Testnet]: {
43
+ [exports.ProtocolVersion.V1]: '0x8af42cd329cf1bcffb4c73b48252e99cb32346fdbc1cdaa5ae1d000232d47e84',
44
+ [exports.ProtocolVersion.V2]: '0x2138683f76944437c0c643664120d620bdb5858dd6c9d1d156805e279c2c536f'
45
+ }
46
+ };
47
+ exports.ADLER32_TYPE_ID = {
48
+ [NetworkType.Mainnet]: {
49
+ [exports.ProtocolVersion.V2]: '0x641c01d590833a3f5471bd441651d9f2a8a200141949cdfeef2d68d8094c5876'
50
+ },
51
+ [NetworkType.Testnet]: {
52
+ [exports.ProtocolVersion.V1]: '0xccf29a0d8e860044a3d2f6a6e709f6572f77e4fe245fadd212fc342337048d60',
53
+ [exports.ProtocolVersion.V2]: '0x5f73f128be76e397f5a3b56c94ca16883a8ee91b498bc0ee80473818318c05ac'
54
+ }
55
+ };
56
+ // Dep Group Transaction Constants
57
+ exports.DEP_GROUP_TX_HASH = {
58
+ [NetworkType.Mainnet]: {
59
+ [exports.ProtocolVersion.V2]: '0xfab07962ed7178ed88d450774e2a6ecd50bae856bdb9b692980be8c5147d1bfa'
60
+ },
61
+ [NetworkType.Testnet]: {
62
+ [exports.ProtocolVersion.V1]: '0xc8fd44aba36f0c4b37536b6c7ea3b88df65fa97e02f77cd33b9bf20bf241a09b',
63
+ [exports.ProtocolVersion.V2]: '0x469af0d961dcaaedd872968a9388b546717a6ccfa47b3165b3f9c981e9d66aaa'
64
+ }
65
+ };
66
+ // Deploy Transaction Constants
67
+ exports.DEPLOY_TX_HASH = {
68
+ [NetworkType.Mainnet]: {
69
+ [exports.ProtocolVersion.V2]: {
70
+ ckbfs: '0xc9b6698f44c3b80e7e1c48823b2714e432b93f0206ffaf9df885d23267ed2ebc',
71
+ adler32: '0xc9b6698f44c3b80e7e1c48823b2714e432b93f0206ffaf9df885d23267ed2ebc'
72
+ }
73
+ },
74
+ [NetworkType.Testnet]: {
75
+ [exports.ProtocolVersion.V1]: {
76
+ ckbfs: '0xde8eb09151fbcdcba398423159ce348cc89a38a736de3fd0960b18b084465382',
77
+ adler32: '0x042f264d7397a181437b51ff9981cf536f252ab5740b61ce52ce31ada04ed54b'
78
+ },
79
+ [exports.ProtocolVersion.V2]: {
80
+ ckbfs: '0x2c8c9ad3134743368b5a79977648f96c5bd0aba187021a72fb624301064d3616',
81
+ adler32: '0x2c8c9ad3134743368b5a79977648f96c5bd0aba187021a72fb624301064d3616'
82
+ }
83
+ }
84
+ };
85
+ // Default values - V2 is now the default
86
+ exports.DEFAULT_VERSION = exports.ProtocolVersion.V2;
87
+ exports.DEFAULT_NETWORK = NetworkType.Testnet;
88
+ /**
89
+ * Get CKBFS script configuration for a specific network and version
90
+ * @param network Network type (mainnet or testnet)
91
+ * @param version Protocol version (default: latest version)
92
+ * @param useTypeID Whether to use type ID instead of code hash (default: false)
93
+ * @returns CKBFS script configuration
94
+ */
95
+ function getCKBFSScriptConfig(network = exports.DEFAULT_NETWORK, version = exports.DEFAULT_VERSION, useTypeID = false) {
96
+ return {
97
+ codeHash: useTypeID
98
+ ? exports.CKBFS_TYPE_ID[network][version]
99
+ : exports.CKBFS_CODE_HASH[network][version],
100
+ hashType: useTypeID ? 'type' : 'data1',
101
+ depTxHash: exports.DEP_GROUP_TX_HASH[network][version],
102
+ depIndex: 0
103
+ };
104
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Utility functions for file operations
3
+ */
4
+ /**
5
+ * Reads a file from the file system
6
+ * @param filePath The path to the file to read
7
+ * @returns Buffer containing the file contents
8
+ */
9
+ export declare function readFile(filePath: string): Buffer;
10
+ /**
11
+ * Reads a file as text from the file system
12
+ * @param filePath The path to the file to read
13
+ * @returns String containing the file contents
14
+ */
15
+ export declare function readFileAsText(filePath: string): string;
16
+ /**
17
+ * Reads a file as Uint8Array from the file system
18
+ * @param filePath The path to the file to read
19
+ * @returns Uint8Array containing the file contents
20
+ */
21
+ export declare function readFileAsUint8Array(filePath: string): Uint8Array;
22
+ /**
23
+ * Writes data to a file in the file system
24
+ * @param filePath The path to write the file to
25
+ * @param data The data to write to the file
26
+ */
27
+ export declare function writeFile(filePath: string, data: Buffer | string): void;
28
+ /**
29
+ * Gets the MIME content type based on file extension
30
+ * @param filePath The path to the file
31
+ * @returns The MIME content type for the file
32
+ */
33
+ export declare function getContentType(filePath: string): string;
34
+ /**
35
+ * Splits a file into chunks of a specific size
36
+ * @param filePath The path to the file to split
37
+ * @param chunkSize The maximum size of each chunk in bytes
38
+ * @returns Array of Uint8Array chunks
39
+ */
40
+ export declare function splitFileIntoChunks(filePath: string, chunkSize: number): Uint8Array[];
41
+ /**
42
+ * Combines chunks into a single file
43
+ * @param chunks Array of chunks to combine
44
+ * @param outputPath The path to write the combined file to
45
+ */
46
+ export declare function combineChunksToFile(chunks: Uint8Array[], outputPath: string): void;