@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,108 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.readFile = readFile;
7
+ exports.readFileAsText = readFileAsText;
8
+ exports.readFileAsUint8Array = readFileAsUint8Array;
9
+ exports.writeFile = writeFile;
10
+ exports.getContentType = getContentType;
11
+ exports.splitFileIntoChunks = splitFileIntoChunks;
12
+ exports.combineChunksToFile = combineChunksToFile;
13
+ const fs_1 = __importDefault(require("fs"));
14
+ const path_1 = __importDefault(require("path"));
15
+ /**
16
+ * Utility functions for file operations
17
+ */
18
+ /**
19
+ * Reads a file from the file system
20
+ * @param filePath The path to the file to read
21
+ * @returns Buffer containing the file contents
22
+ */
23
+ function readFile(filePath) {
24
+ return fs_1.default.readFileSync(filePath);
25
+ }
26
+ /**
27
+ * Reads a file as text from the file system
28
+ * @param filePath The path to the file to read
29
+ * @returns String containing the file contents
30
+ */
31
+ function readFileAsText(filePath) {
32
+ return fs_1.default.readFileSync(filePath, 'utf-8');
33
+ }
34
+ /**
35
+ * Reads a file as Uint8Array from the file system
36
+ * @param filePath The path to the file to read
37
+ * @returns Uint8Array containing the file contents
38
+ */
39
+ function readFileAsUint8Array(filePath) {
40
+ const buffer = fs_1.default.readFileSync(filePath);
41
+ return new Uint8Array(buffer);
42
+ }
43
+ /**
44
+ * Writes data to a file in the file system
45
+ * @param filePath The path to write the file to
46
+ * @param data The data to write to the file
47
+ */
48
+ function writeFile(filePath, data) {
49
+ // Ensure the directory exists
50
+ const dirPath = path_1.default.dirname(filePath);
51
+ if (!fs_1.default.existsSync(dirPath)) {
52
+ fs_1.default.mkdirSync(dirPath, { recursive: true });
53
+ }
54
+ fs_1.default.writeFileSync(filePath, data);
55
+ }
56
+ /**
57
+ * Gets the MIME content type based on file extension
58
+ * @param filePath The path to the file
59
+ * @returns The MIME content type for the file
60
+ */
61
+ function getContentType(filePath) {
62
+ const extension = path_1.default.extname(filePath).toLowerCase();
63
+ const mimeTypes = {
64
+ '.txt': 'text/plain',
65
+ '.html': 'text/html',
66
+ '.htm': 'text/html',
67
+ '.css': 'text/css',
68
+ '.js': 'application/javascript',
69
+ '.json': 'application/json',
70
+ '.jpg': 'image/jpeg',
71
+ '.jpeg': 'image/jpeg',
72
+ '.png': 'image/png',
73
+ '.gif': 'image/gif',
74
+ '.svg': 'image/svg+xml',
75
+ '.pdf': 'application/pdf',
76
+ '.mp3': 'audio/mpeg',
77
+ '.mp4': 'video/mp4',
78
+ '.wav': 'audio/wav',
79
+ '.xml': 'application/xml',
80
+ '.zip': 'application/zip',
81
+ '.md': 'text/markdown',
82
+ '.markdown': 'text/markdown',
83
+ };
84
+ return mimeTypes[extension] || 'application/octet-stream';
85
+ }
86
+ /**
87
+ * Splits a file into chunks of a specific size
88
+ * @param filePath The path to the file to split
89
+ * @param chunkSize The maximum size of each chunk in bytes
90
+ * @returns Array of Uint8Array chunks
91
+ */
92
+ function splitFileIntoChunks(filePath, chunkSize) {
93
+ const fileBuffer = fs_1.default.readFileSync(filePath);
94
+ const chunks = [];
95
+ for (let i = 0; i < fileBuffer.length; i += chunkSize) {
96
+ chunks.push(new Uint8Array(fileBuffer.slice(i, i + chunkSize)));
97
+ }
98
+ return chunks;
99
+ }
100
+ /**
101
+ * Combines chunks into a single file
102
+ * @param chunks Array of chunks to combine
103
+ * @param outputPath The path to write the combined file to
104
+ */
105
+ function combineChunksToFile(chunks, outputPath) {
106
+ const combinedBuffer = Buffer.concat(chunks.map(chunk => Buffer.from(chunk)));
107
+ writeFile(outputPath, combinedBuffer);
108
+ }
@@ -0,0 +1,77 @@
1
+ import { molecule, number } from "@ckb-lumos/codec";
2
+ /**
3
+ * Molecule definitions for CKBFS data structures.
4
+ */
5
+ export declare const Indexes: molecule.ArrayLayoutCodec<import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>>;
6
+ export declare const BackLinkV1: molecule.ObjectLayoutCodec<{
7
+ txHash: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
8
+ index: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
9
+ checksum: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
10
+ }>;
11
+ export declare const BackLinkV2: molecule.ObjectLayoutCodec<{
12
+ txHash: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
13
+ indexes: molecule.ArrayLayoutCodec<import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>>;
14
+ checksum: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
15
+ }>;
16
+ export declare const BackLinksV1: molecule.ArrayLayoutCodec<molecule.ObjectLayoutCodec<{
17
+ txHash: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
18
+ index: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
19
+ checksum: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
20
+ }>>;
21
+ export declare const BackLinksV2: molecule.ArrayLayoutCodec<molecule.ObjectLayoutCodec<{
22
+ txHash: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
23
+ indexes: molecule.ArrayLayoutCodec<import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>>;
24
+ checksum: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
25
+ }>>;
26
+ export declare const CKBFSDataV1: molecule.ObjectLayoutCodec<{
27
+ index: molecule.ArrayLayoutCodec<import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>>;
28
+ checksum: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
29
+ contentType: import("@ckb-lumos/codec/lib/base").BytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
30
+ filename: import("@ckb-lumos/codec/lib/base").BytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
31
+ backLinks: molecule.ArrayLayoutCodec<molecule.ObjectLayoutCodec<{
32
+ txHash: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
33
+ index: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
34
+ checksum: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
35
+ }>>;
36
+ }>;
37
+ export declare const CKBFSDataV2: molecule.ObjectLayoutCodec<{
38
+ indexes: molecule.ArrayLayoutCodec<import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>>;
39
+ checksum: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
40
+ contentType: import("@ckb-lumos/codec/lib/base").BytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
41
+ filename: import("@ckb-lumos/codec/lib/base").BytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
42
+ backLinks: molecule.ArrayLayoutCodec<molecule.ObjectLayoutCodec<{
43
+ txHash: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
44
+ indexes: molecule.ArrayLayoutCodec<import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>>;
45
+ checksum: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
46
+ }>>;
47
+ }>;
48
+ export type BackLinkTypeV1 = {
49
+ txHash: string;
50
+ index: number;
51
+ checksum: number;
52
+ };
53
+ export type BackLinkTypeV2 = {
54
+ txHash: string;
55
+ indexes: number[];
56
+ checksum: number;
57
+ };
58
+ export type BackLinkType = {
59
+ txHash: string;
60
+ index?: number;
61
+ indexes?: number[];
62
+ checksum: number;
63
+ };
64
+ export type CKBFSDataType = {
65
+ index?: number[];
66
+ indexes?: number[];
67
+ checksum: number;
68
+ contentType: Uint8Array;
69
+ filename: Uint8Array;
70
+ backLinks: BackLinkType[];
71
+ };
72
+ export declare const CKBFSData: {
73
+ pack: (data: CKBFSDataType, version?: string) => Uint8Array;
74
+ unpack: (buf: Uint8Array, version?: string) => CKBFSDataType;
75
+ };
76
+ export declare const CKBFS_HEADER: Uint8Array<ArrayBuffer>;
77
+ export declare const CKBFS_HEADER_STRING = "CKBFS";
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CKBFS_HEADER_STRING = exports.CKBFS_HEADER = exports.CKBFSData = exports.CKBFSDataV2 = exports.CKBFSDataV1 = exports.BackLinksV2 = exports.BackLinksV1 = exports.BackLinkV2 = exports.BackLinkV1 = exports.Indexes = void 0;
4
+ const codec_1 = require("@ckb-lumos/codec");
5
+ const base_1 = require("@ckb-lumos/base");
6
+ const constants_1 = require("./constants");
7
+ /**
8
+ * Molecule definitions for CKBFS data structures.
9
+ */
10
+ // Define the Indexes vector
11
+ exports.Indexes = codec_1.molecule.vector(codec_1.number.Uint32);
12
+ // Define the BackLink table structure for V1
13
+ exports.BackLinkV1 = codec_1.molecule.table({
14
+ txHash: base_1.blockchain.Byte32,
15
+ index: codec_1.number.Uint32,
16
+ checksum: codec_1.number.Uint32,
17
+ }, ["txHash", "index", "checksum"]);
18
+ // Define the BackLink table structure for V2
19
+ exports.BackLinkV2 = codec_1.molecule.table({
20
+ txHash: base_1.blockchain.Byte32,
21
+ indexes: exports.Indexes,
22
+ checksum: codec_1.number.Uint32,
23
+ }, ["txHash", "indexes", "checksum"]);
24
+ // Define the BackLinks vector for V1
25
+ exports.BackLinksV1 = codec_1.molecule.vector(exports.BackLinkV1);
26
+ // Define the BackLinks vector for V2
27
+ exports.BackLinksV2 = codec_1.molecule.vector(exports.BackLinkV2);
28
+ // Define the CKBFSData table structure for V1
29
+ exports.CKBFSDataV1 = codec_1.molecule.table({
30
+ index: exports.Indexes,
31
+ checksum: codec_1.number.Uint32,
32
+ contentType: base_1.blockchain.Bytes,
33
+ filename: base_1.blockchain.Bytes,
34
+ backLinks: exports.BackLinksV1,
35
+ }, ["index", "checksum", "contentType", "filename", "backLinks"]);
36
+ // Define the CKBFSData table structure for V2
37
+ exports.CKBFSDataV2 = codec_1.molecule.table({
38
+ indexes: exports.Indexes,
39
+ checksum: codec_1.number.Uint32,
40
+ contentType: base_1.blockchain.Bytes,
41
+ filename: base_1.blockchain.Bytes,
42
+ backLinks: exports.BackLinksV2,
43
+ }, ["indexes", "checksum", "contentType", "filename", "backLinks"]);
44
+ // Helper function to safely get either index or indexes
45
+ function getIndexes(data) {
46
+ return data.indexes || data.index || [];
47
+ }
48
+ // Helper function to safely get either index or indexes from BackLinkType
49
+ function getBackLinkIndex(bl) {
50
+ if (typeof bl.index === 'number') {
51
+ return bl.index;
52
+ }
53
+ if (Array.isArray(bl.indexes) && bl.indexes.length > 0) {
54
+ return bl.indexes[0];
55
+ }
56
+ return 0;
57
+ }
58
+ // Helper function to safely get indexes array from BackLinkType
59
+ function getBackLinkIndexes(bl) {
60
+ if (Array.isArray(bl.indexes)) {
61
+ return bl.indexes;
62
+ }
63
+ if (typeof bl.index === 'number') {
64
+ return [bl.index];
65
+ }
66
+ return [0];
67
+ }
68
+ // Helper function to get the right CKBFSData based on version
69
+ exports.CKBFSData = {
70
+ pack: (data, version = constants_1.ProtocolVersion.V2) => {
71
+ if (version === constants_1.ProtocolVersion.V1) {
72
+ // V1 formatting
73
+ return exports.CKBFSDataV1.pack({
74
+ index: getIndexes(data),
75
+ checksum: data.checksum,
76
+ contentType: data.contentType,
77
+ filename: data.filename,
78
+ backLinks: data.backLinks.map(bl => ({
79
+ txHash: bl.txHash,
80
+ index: getBackLinkIndex(bl),
81
+ checksum: bl.checksum,
82
+ })),
83
+ });
84
+ }
85
+ else {
86
+ // V2 formatting
87
+ return exports.CKBFSDataV2.pack({
88
+ indexes: getIndexes(data),
89
+ checksum: data.checksum,
90
+ contentType: data.contentType,
91
+ filename: data.filename,
92
+ backLinks: data.backLinks.map(bl => ({
93
+ txHash: bl.txHash,
94
+ indexes: getBackLinkIndexes(bl),
95
+ checksum: bl.checksum,
96
+ })),
97
+ });
98
+ }
99
+ },
100
+ unpack: (buf, version = constants_1.ProtocolVersion.V2) => {
101
+ try {
102
+ if (version === constants_1.ProtocolVersion.V1) {
103
+ const unpacked = exports.CKBFSDataV1.unpack(buf);
104
+ return {
105
+ index: unpacked.index,
106
+ checksum: unpacked.checksum,
107
+ contentType: new Uint8Array(Buffer.from(unpacked.contentType)),
108
+ filename: new Uint8Array(Buffer.from(unpacked.filename)),
109
+ backLinks: unpacked.backLinks.map(bl => ({
110
+ txHash: bl.txHash,
111
+ index: bl.index,
112
+ checksum: bl.checksum,
113
+ })),
114
+ };
115
+ }
116
+ else {
117
+ // V2 format
118
+ const unpacked = exports.CKBFSDataV2.unpack(buf);
119
+ return {
120
+ indexes: unpacked.indexes,
121
+ checksum: unpacked.checksum,
122
+ contentType: new Uint8Array(Buffer.from(unpacked.contentType)),
123
+ filename: new Uint8Array(Buffer.from(unpacked.filename)),
124
+ backLinks: unpacked.backLinks.map(bl => ({
125
+ txHash: bl.txHash,
126
+ indexes: bl.indexes,
127
+ checksum: bl.checksum,
128
+ })),
129
+ };
130
+ }
131
+ }
132
+ catch (error) {
133
+ console.error('Error unpacking CKBFSData:', error);
134
+ throw new Error('Failed to unpack CKBFSData: ' + error);
135
+ }
136
+ }
137
+ };
138
+ // Constants for CKBFS protocol
139
+ exports.CKBFS_HEADER = new Uint8Array([0x43, 0x4B, 0x42, 0x46, 0x53]); // "CKBFS" in ASCII
140
+ exports.CKBFS_HEADER_STRING = "CKBFS";
@@ -0,0 +1,82 @@
1
+ import { ccc, Transaction, Script, Signer } from "@ckb-ccc/core";
2
+ import { CKBFSDataType } from './molecule';
3
+ import { NetworkType } from './constants';
4
+ /**
5
+ * Utility functions for CKB transaction creation and handling
6
+ */
7
+ /**
8
+ * Options for creating a CKBFS cell
9
+ */
10
+ export interface CKBFSCellOptions {
11
+ contentType: string;
12
+ filename: string;
13
+ capacity?: bigint;
14
+ lock: Script;
15
+ network?: NetworkType;
16
+ version?: string;
17
+ useTypeID?: boolean;
18
+ }
19
+ /**
20
+ * Options for publishing a file to CKBFS
21
+ */
22
+ export interface PublishOptions extends CKBFSCellOptions {
23
+ contentChunks: Uint8Array[];
24
+ feeRate?: number;
25
+ }
26
+ /**
27
+ * Options for appending content to a CKBFS file
28
+ */
29
+ export interface AppendOptions {
30
+ ckbfsCell: {
31
+ outPoint: {
32
+ txHash: string;
33
+ index: number;
34
+ };
35
+ data: CKBFSDataType;
36
+ type: Script;
37
+ lock: Script;
38
+ capacity: bigint;
39
+ };
40
+ contentChunks: Uint8Array[];
41
+ feeRate?: number;
42
+ network?: NetworkType;
43
+ version?: string;
44
+ }
45
+ /**
46
+ * Creates a CKBFS cell
47
+ * @param options Options for creating the CKBFS cell
48
+ * @returns The created cell output
49
+ */
50
+ export declare function createCKBFSCell(options: CKBFSCellOptions): {
51
+ lock: ccc.Script;
52
+ type: ccc.Script;
53
+ capacity: bigint;
54
+ };
55
+ /**
56
+ * Creates a transaction for publishing a file to CKBFS
57
+ * @param signer The signer to use for the transaction
58
+ * @param options Options for publishing the file
59
+ * @returns Promise resolving to the created transaction
60
+ */
61
+ export declare function createPublishTransaction(signer: Signer, options: PublishOptions): Promise<Transaction>;
62
+ /**
63
+ * Creates a transaction for appending content to a CKBFS file
64
+ * @param signer The signer to use for the transaction
65
+ * @param options Options for appending content
66
+ * @returns Promise resolving to the created transaction
67
+ */
68
+ export declare function createAppendTransaction(signer: Signer, options: AppendOptions): Promise<Transaction>;
69
+ /**
70
+ * Creates a complete transaction for publishing a file to CKBFS
71
+ * @param signer The signer to use for the transaction
72
+ * @param options Options for publishing the file
73
+ * @returns Promise resolving to the signed transaction
74
+ */
75
+ export declare function publishCKBFS(signer: Signer, options: PublishOptions): Promise<Transaction>;
76
+ /**
77
+ * Creates a complete transaction for appending content to a CKBFS file
78
+ * @param signer The signer to use for the transaction
79
+ * @param options Options for appending content
80
+ * @returns Promise resolving to the signed transaction
81
+ */
82
+ export declare function appendCKBFS(signer: Signer, options: AppendOptions): Promise<Transaction>;