@ckbfs/api 1.3.0 → 1.4.0
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.
- package/README.md +570 -0
- package/code.png +0 -0
- package/demo-output.txt +1 -0
- package/direct_direct_content_example.txt +1 -0
- package/dist/index.d.ts +15 -15
- package/dist/index.js +41 -22
- package/dist/utils/constants.d.ts +7 -3
- package/dist/utils/constants.js +41 -34
- package/dist/utils/file.d.ts +179 -0
- package/dist/utils/file.js +599 -31
- package/dist/utils/molecule.d.ts +3 -2
- package/dist/utils/molecule.js +22 -24
- package/dist/utils/transaction.d.ts +4 -4
- package/dist/utils/transaction.js +28 -27
- package/examples/append.ts +2 -39
- package/examples/example.txt +1 -0
- package/examples/identifier-test.ts +178 -0
- package/examples/index.ts +36 -24
- package/examples/publish.ts +5 -5
- package/examples/retrieve.ts +580 -0
- package/examples/witness-decode-demo.ts +190 -0
- package/identifier_direct_content_example.txt +1 -0
- package/package-lock.json +4978 -0
- package/package.json +3 -2
- package/src/index.ts +168 -99
- package/src/utils/constants.ts +77 -43
- package/src/utils/file.ts +864 -59
- package/src/utils/molecule.ts +41 -36
- package/src/utils/transaction.ts +171 -145
- package/traditional_direct_content_example.txt +1 -0
- package/typeid_direct_content_example.txt +1 -0
- package/example.txt +0 -1
- package/src/utils/createPublishTransaction +0 -24
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ckbfs/api",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "SDK for CKBFS protocol on CKB",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Code Monad<code@lab-11.org>",
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
"example": "ts-node examples/index.ts",
|
|
16
16
|
"example:publish": "ts-node examples/publish.ts",
|
|
17
17
|
"example:append": "ts-node examples/append.ts",
|
|
18
|
-
"example:retrieve": "ts-node examples/retrieve.ts"
|
|
18
|
+
"example:retrieve": "ts-node examples/retrieve.ts",
|
|
19
|
+
"example:witness-decode-demo": "ts-node examples/witness-decode-demo.ts"
|
|
19
20
|
},
|
|
20
21
|
"keywords": [
|
|
21
22
|
"ckb",
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import {
|
|
2
|
+
Script,
|
|
3
|
+
Signer,
|
|
4
|
+
Transaction,
|
|
5
|
+
ClientPublicTestnet,
|
|
6
|
+
SignerCkbPrivateKey,
|
|
7
|
+
ClientPublicMainnet,
|
|
8
|
+
} from "@ckb-ccc/core";
|
|
9
|
+
import {
|
|
10
|
+
calculateChecksum,
|
|
11
|
+
verifyChecksum,
|
|
12
|
+
updateChecksum,
|
|
13
|
+
verifyWitnessChecksum,
|
|
14
|
+
} from "./utils/checksum";
|
|
8
15
|
import {
|
|
9
16
|
createCKBFSCell,
|
|
10
17
|
createPublishTransaction as utilCreatePublishTransaction,
|
|
@@ -13,8 +20,8 @@ import {
|
|
|
13
20
|
appendCKBFS as utilAppendCKBFS,
|
|
14
21
|
CKBFSCellOptions,
|
|
15
22
|
PublishOptions,
|
|
16
|
-
AppendOptions
|
|
17
|
-
} from
|
|
23
|
+
AppendOptions,
|
|
24
|
+
} from "./utils/transaction";
|
|
18
25
|
import {
|
|
19
26
|
readFile,
|
|
20
27
|
readFileAsText,
|
|
@@ -24,15 +31,28 @@ import {
|
|
|
24
31
|
splitFileIntoChunks,
|
|
25
32
|
combineChunksToFile,
|
|
26
33
|
getFileContentFromChain,
|
|
27
|
-
saveFileFromChain
|
|
28
|
-
|
|
34
|
+
saveFileFromChain,
|
|
35
|
+
getFileContentFromChainByTypeId,
|
|
36
|
+
saveFileFromChainByTypeId,
|
|
37
|
+
decodeFileFromChainByTypeId,
|
|
38
|
+
getFileContentFromChainByIdentifier,
|
|
39
|
+
saveFileFromChainByIdentifier,
|
|
40
|
+
decodeFileFromChainByIdentifier,
|
|
41
|
+
parseIdentifier,
|
|
42
|
+
IdentifierType,
|
|
43
|
+
decodeWitnessContent,
|
|
44
|
+
decodeMultipleWitnessContents,
|
|
45
|
+
extractFileFromWitnesses,
|
|
46
|
+
decodeFileFromWitnessData,
|
|
47
|
+
saveFileFromWitnessData,
|
|
48
|
+
} from "./utils/file";
|
|
29
49
|
import {
|
|
30
50
|
createCKBFSWitness,
|
|
31
51
|
createTextCKBFSWitness,
|
|
32
52
|
extractCKBFSWitnessContent,
|
|
33
53
|
isCKBFSWitness,
|
|
34
|
-
createChunkedCKBFSWitnesses
|
|
35
|
-
} from
|
|
54
|
+
createChunkedCKBFSWitnesses,
|
|
55
|
+
} from "./utils/witness";
|
|
36
56
|
import {
|
|
37
57
|
CKBFSData,
|
|
38
58
|
BackLinkV1,
|
|
@@ -40,11 +60,12 @@ import {
|
|
|
40
60
|
CKBFSDataType,
|
|
41
61
|
BackLinkType,
|
|
42
62
|
CKBFS_HEADER,
|
|
43
|
-
CKBFS_HEADER_STRING
|
|
44
|
-
} from
|
|
63
|
+
CKBFS_HEADER_STRING,
|
|
64
|
+
} from "./utils/molecule";
|
|
45
65
|
import {
|
|
46
66
|
NetworkType,
|
|
47
67
|
ProtocolVersion,
|
|
68
|
+
ProtocolVersionType,
|
|
48
69
|
DEFAULT_NETWORK,
|
|
49
70
|
DEFAULT_VERSION,
|
|
50
71
|
CKBFS_CODE_HASH,
|
|
@@ -54,9 +75,9 @@ import {
|
|
|
54
75
|
DEP_GROUP_TX_HASH,
|
|
55
76
|
DEPLOY_TX_HASH,
|
|
56
77
|
getCKBFSScriptConfig,
|
|
57
|
-
CKBFSScriptConfig
|
|
58
|
-
} from
|
|
59
|
-
import { ensureHexPrefix } from
|
|
78
|
+
CKBFSScriptConfig,
|
|
79
|
+
} from "./utils/constants";
|
|
80
|
+
import { ensureHexPrefix } from "./utils/transaction";
|
|
60
81
|
|
|
61
82
|
// Helper to encode string to Uint8Array
|
|
62
83
|
const textEncoder = new TextEncoder();
|
|
@@ -70,29 +91,35 @@ export interface FileOptions {
|
|
|
70
91
|
capacity?: bigint;
|
|
71
92
|
feeRate?: number;
|
|
72
93
|
network?: NetworkType;
|
|
73
|
-
version?:
|
|
94
|
+
version?: ProtocolVersionType;
|
|
74
95
|
useTypeID?: boolean;
|
|
75
96
|
}
|
|
76
97
|
|
|
77
98
|
/**
|
|
78
99
|
* Options required when publishing content directly (string or Uint8Array)
|
|
79
100
|
*/
|
|
80
|
-
export type PublishContentOptions = Omit<
|
|
81
|
-
|
|
82
|
-
|
|
101
|
+
export type PublishContentOptions = Omit<
|
|
102
|
+
FileOptions,
|
|
103
|
+
"capacity" | "contentType" | "filename"
|
|
104
|
+
> &
|
|
105
|
+
Required<Pick<FileOptions, "contentType" | "filename">> & {
|
|
106
|
+
capacity?: bigint;
|
|
107
|
+
};
|
|
83
108
|
|
|
84
109
|
/**
|
|
85
110
|
* Options required when appending content directly (string or Uint8Array)
|
|
86
111
|
*/
|
|
87
|
-
export type AppendContentOptions = Omit<
|
|
88
|
-
|
|
112
|
+
export type AppendContentOptions = Omit<
|
|
113
|
+
FileOptions,
|
|
114
|
+
"contentType" | "filename" | "capacity"
|
|
115
|
+
> & { capacity?: bigint };
|
|
89
116
|
|
|
90
117
|
/**
|
|
91
118
|
* Configuration options for the CKBFS SDK
|
|
92
119
|
*/
|
|
93
120
|
export interface CKBFSOptions {
|
|
94
121
|
chunkSize?: number;
|
|
95
|
-
version?:
|
|
122
|
+
version?: ProtocolVersionType;
|
|
96
123
|
useTypeID?: boolean;
|
|
97
124
|
network?: NetworkType;
|
|
98
125
|
}
|
|
@@ -104,9 +131,9 @@ export class CKBFS {
|
|
|
104
131
|
private signer: Signer;
|
|
105
132
|
private chunkSize: number;
|
|
106
133
|
private network: NetworkType;
|
|
107
|
-
private version:
|
|
134
|
+
private version: ProtocolVersionType;
|
|
108
135
|
private useTypeID: boolean;
|
|
109
|
-
|
|
136
|
+
|
|
110
137
|
/**
|
|
111
138
|
* Creates a new CKBFS SDK instance
|
|
112
139
|
* @param signerOrPrivateKey The signer instance or CKB private key to use for signing transactions
|
|
@@ -116,16 +143,24 @@ export class CKBFS {
|
|
|
116
143
|
constructor(
|
|
117
144
|
signerOrPrivateKey: Signer | string,
|
|
118
145
|
networkOrOptions: NetworkType | CKBFSOptions = DEFAULT_NETWORK,
|
|
119
|
-
options?: CKBFSOptions
|
|
146
|
+
options?: CKBFSOptions,
|
|
120
147
|
) {
|
|
121
148
|
// Determine if first parameter is a Signer or privateKey
|
|
122
|
-
if (typeof signerOrPrivateKey ===
|
|
149
|
+
if (typeof signerOrPrivateKey === "string") {
|
|
123
150
|
// Initialize with private key
|
|
124
151
|
const privateKey = signerOrPrivateKey;
|
|
125
|
-
const network =
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
152
|
+
const network =
|
|
153
|
+
typeof networkOrOptions === "string"
|
|
154
|
+
? networkOrOptions
|
|
155
|
+
: DEFAULT_NETWORK;
|
|
156
|
+
const opts =
|
|
157
|
+
options ||
|
|
158
|
+
(typeof networkOrOptions === "object" ? networkOrOptions : {});
|
|
159
|
+
|
|
160
|
+
const client =
|
|
161
|
+
network === "mainnet"
|
|
162
|
+
? new ClientPublicMainnet()
|
|
163
|
+
: new ClientPublicTestnet();
|
|
129
164
|
this.signer = new SignerCkbPrivateKey(client, privateKey);
|
|
130
165
|
this.network = network;
|
|
131
166
|
this.chunkSize = opts.chunkSize || 30 * 1024;
|
|
@@ -134,15 +169,15 @@ export class CKBFS {
|
|
|
134
169
|
} else {
|
|
135
170
|
// Initialize with signer
|
|
136
171
|
this.signer = signerOrPrivateKey;
|
|
137
|
-
const opts = typeof networkOrOptions ===
|
|
138
|
-
|
|
172
|
+
const opts = typeof networkOrOptions === "object" ? networkOrOptions : {};
|
|
173
|
+
|
|
139
174
|
this.network = opts.network || DEFAULT_NETWORK;
|
|
140
175
|
this.chunkSize = opts.chunkSize || 30 * 1024;
|
|
141
176
|
this.version = opts.version || DEFAULT_VERSION;
|
|
142
177
|
this.useTypeID = opts.useTypeID || false;
|
|
143
178
|
}
|
|
144
179
|
}
|
|
145
|
-
|
|
180
|
+
|
|
146
181
|
/**
|
|
147
182
|
* Gets the recommended address object for the signer
|
|
148
183
|
* @returns Promise resolving to the address object
|
|
@@ -150,7 +185,7 @@ export class CKBFS {
|
|
|
150
185
|
async getAddress() {
|
|
151
186
|
return this.signer.getRecommendedAddressObj();
|
|
152
187
|
}
|
|
153
|
-
|
|
188
|
+
|
|
154
189
|
/**
|
|
155
190
|
* Gets the lock script for the signer
|
|
156
191
|
* @returns Promise resolving to the lock script
|
|
@@ -159,7 +194,7 @@ export class CKBFS {
|
|
|
159
194
|
const address = await this.getAddress();
|
|
160
195
|
return address.script;
|
|
161
196
|
}
|
|
162
|
-
|
|
197
|
+
|
|
163
198
|
/**
|
|
164
199
|
* Gets the CKBFS script configuration for the current settings
|
|
165
200
|
* @returns The CKBFS script configuration
|
|
@@ -167,32 +202,35 @@ export class CKBFS {
|
|
|
167
202
|
getCKBFSConfig(): CKBFSScriptConfig {
|
|
168
203
|
return getCKBFSScriptConfig(this.network, this.version, this.useTypeID);
|
|
169
204
|
}
|
|
170
|
-
|
|
205
|
+
|
|
171
206
|
/**
|
|
172
207
|
* Publishes a file to CKBFS
|
|
173
208
|
* @param filePath The path to the file to publish
|
|
174
209
|
* @param options Options for publishing the file
|
|
175
210
|
* @returns Promise resolving to the transaction hash
|
|
176
211
|
*/
|
|
177
|
-
async publishFile(
|
|
212
|
+
async publishFile(
|
|
213
|
+
filePath: string,
|
|
214
|
+
options: FileOptions = {},
|
|
215
|
+
): Promise<string> {
|
|
178
216
|
// Read the file and split into chunks
|
|
179
217
|
const fileContent = readFileAsUint8Array(filePath);
|
|
180
218
|
const contentChunks = [];
|
|
181
|
-
|
|
219
|
+
|
|
182
220
|
for (let i = 0; i < fileContent.length; i += this.chunkSize) {
|
|
183
221
|
contentChunks.push(fileContent.slice(i, i + this.chunkSize));
|
|
184
222
|
}
|
|
185
|
-
|
|
223
|
+
|
|
186
224
|
// Get the lock script
|
|
187
225
|
const lock = await this.getLock();
|
|
188
|
-
|
|
226
|
+
|
|
189
227
|
// Determine content type if not provided
|
|
190
228
|
const contentType = options.contentType || getContentType(filePath);
|
|
191
|
-
|
|
229
|
+
|
|
192
230
|
// Use the filename from the path if not provided
|
|
193
231
|
const pathParts = filePath.split(/[\\\/]/);
|
|
194
232
|
const filename = options.filename || pathParts[pathParts.length - 1];
|
|
195
|
-
|
|
233
|
+
|
|
196
234
|
// Create and sign the transaction using the utility function
|
|
197
235
|
const tx = await utilPublishCKBFS(this.signer, {
|
|
198
236
|
contentChunks,
|
|
@@ -203,14 +241,15 @@ export class CKBFS {
|
|
|
203
241
|
feeRate: options.feeRate,
|
|
204
242
|
network: options.network || this.network,
|
|
205
243
|
version: options.version || this.version,
|
|
206
|
-
useTypeID:
|
|
244
|
+
useTypeID:
|
|
245
|
+
options.useTypeID !== undefined ? options.useTypeID : this.useTypeID,
|
|
207
246
|
});
|
|
208
247
|
|
|
209
|
-
console.log(
|
|
210
|
-
|
|
248
|
+
console.log("Publish file tx:", tx.stringify());
|
|
249
|
+
|
|
211
250
|
// Send the transaction
|
|
212
251
|
const txHash = await this.signer.sendTransaction(tx);
|
|
213
|
-
|
|
252
|
+
|
|
214
253
|
return ensureHexPrefix(txHash);
|
|
215
254
|
}
|
|
216
255
|
|
|
@@ -220,8 +259,12 @@ export class CKBFS {
|
|
|
220
259
|
* @param options Options for publishing the content (contentType and filename are required)
|
|
221
260
|
* @returns Promise resolving to the transaction hash
|
|
222
261
|
*/
|
|
223
|
-
async publishContent(
|
|
224
|
-
|
|
262
|
+
async publishContent(
|
|
263
|
+
content: string | Uint8Array,
|
|
264
|
+
options: PublishContentOptions,
|
|
265
|
+
): Promise<string> {
|
|
266
|
+
const contentBytes =
|
|
267
|
+
typeof content === "string" ? textEncoder.encode(content) : content;
|
|
225
268
|
const contentChunks = [];
|
|
226
269
|
|
|
227
270
|
for (let i = 0; i < contentBytes.length; i += this.chunkSize) {
|
|
@@ -242,15 +285,16 @@ export class CKBFS {
|
|
|
242
285
|
feeRate: options.feeRate,
|
|
243
286
|
network: options.network || this.network,
|
|
244
287
|
version: options.version || this.version,
|
|
245
|
-
useTypeID:
|
|
288
|
+
useTypeID:
|
|
289
|
+
options.useTypeID !== undefined ? options.useTypeID : this.useTypeID,
|
|
246
290
|
});
|
|
247
291
|
|
|
248
|
-
console.log(
|
|
292
|
+
console.log("Publish content tx:", tx.stringify());
|
|
249
293
|
|
|
250
294
|
const txHash = await this.signer.sendTransaction(tx);
|
|
251
295
|
return ensureHexPrefix(txHash);
|
|
252
296
|
}
|
|
253
|
-
|
|
297
|
+
|
|
254
298
|
/**
|
|
255
299
|
* Appends content from a file to an existing CKBFS file
|
|
256
300
|
* @param filePath The path to the file containing the content to append
|
|
@@ -259,32 +303,32 @@ export class CKBFS {
|
|
|
259
303
|
* @returns Promise resolving to the transaction hash
|
|
260
304
|
*/
|
|
261
305
|
async appendFile(
|
|
262
|
-
filePath: string,
|
|
263
|
-
ckbfsCell: AppendOptions[
|
|
264
|
-
options: Omit<FileOptions,
|
|
306
|
+
filePath: string,
|
|
307
|
+
ckbfsCell: AppendOptions["ckbfsCell"],
|
|
308
|
+
options: Omit<FileOptions, "contentType" | "filename"> = {},
|
|
265
309
|
): Promise<string> {
|
|
266
310
|
// Read the file and split into chunks
|
|
267
311
|
const fileContent = readFileAsUint8Array(filePath);
|
|
268
312
|
const contentChunks = [];
|
|
269
|
-
|
|
313
|
+
|
|
270
314
|
for (let i = 0; i < fileContent.length; i += this.chunkSize) {
|
|
271
315
|
contentChunks.push(fileContent.slice(i, i + this.chunkSize));
|
|
272
316
|
}
|
|
273
|
-
|
|
317
|
+
|
|
274
318
|
// Create and sign the transaction using the utility function
|
|
275
319
|
const tx = await utilAppendCKBFS(this.signer, {
|
|
276
320
|
ckbfsCell,
|
|
277
321
|
contentChunks,
|
|
278
322
|
feeRate: options.feeRate,
|
|
279
323
|
network: options.network || this.network,
|
|
280
|
-
version: options.version || this.version
|
|
324
|
+
version: options.version || this.version,
|
|
281
325
|
});
|
|
282
326
|
|
|
283
|
-
console.log(
|
|
284
|
-
|
|
327
|
+
console.log("Append file tx:", tx.stringify());
|
|
328
|
+
|
|
285
329
|
// Send the transaction
|
|
286
330
|
const txHash = await this.signer.sendTransaction(tx);
|
|
287
|
-
|
|
331
|
+
|
|
288
332
|
return ensureHexPrefix(txHash);
|
|
289
333
|
}
|
|
290
334
|
|
|
@@ -297,10 +341,11 @@ export class CKBFS {
|
|
|
297
341
|
*/
|
|
298
342
|
async appendContent(
|
|
299
343
|
content: string | Uint8Array,
|
|
300
|
-
ckbfsCell: AppendOptions[
|
|
301
|
-
options: AppendContentOptions = {}
|
|
344
|
+
ckbfsCell: AppendOptions["ckbfsCell"],
|
|
345
|
+
options: AppendContentOptions = {},
|
|
302
346
|
): Promise<string> {
|
|
303
|
-
const contentBytes =
|
|
347
|
+
const contentBytes =
|
|
348
|
+
typeof content === "string" ? textEncoder.encode(content) : content;
|
|
304
349
|
const contentChunks = [];
|
|
305
350
|
|
|
306
351
|
for (let i = 0; i < contentBytes.length; i += this.chunkSize) {
|
|
@@ -312,41 +357,44 @@ export class CKBFS {
|
|
|
312
357
|
contentChunks,
|
|
313
358
|
feeRate: options.feeRate,
|
|
314
359
|
network: options.network || this.network,
|
|
315
|
-
version: options.version || this.version
|
|
360
|
+
version: options.version || this.version,
|
|
316
361
|
// No useTypeID option for append
|
|
317
362
|
});
|
|
318
363
|
|
|
319
|
-
console.log(
|
|
364
|
+
console.log("Append content tx:", tx.stringify());
|
|
320
365
|
|
|
321
366
|
const txHash = await this.signer.sendTransaction(tx);
|
|
322
367
|
return ensureHexPrefix(txHash);
|
|
323
368
|
}
|
|
324
|
-
|
|
369
|
+
|
|
325
370
|
/**
|
|
326
371
|
* Creates a new transaction for publishing a file but doesn't sign or send it
|
|
327
372
|
* @param filePath The path to the file to publish
|
|
328
373
|
* @param options Options for publishing the file
|
|
329
374
|
* @returns Promise resolving to the unsigned transaction
|
|
330
375
|
*/
|
|
331
|
-
async createPublishTransaction(
|
|
376
|
+
async createPublishTransaction(
|
|
377
|
+
filePath: string,
|
|
378
|
+
options: FileOptions = {},
|
|
379
|
+
): Promise<Transaction> {
|
|
332
380
|
// Read the file and split into chunks
|
|
333
381
|
const fileContent = readFileAsUint8Array(filePath);
|
|
334
382
|
const contentChunks = [];
|
|
335
|
-
|
|
383
|
+
|
|
336
384
|
for (let i = 0; i < fileContent.length; i += this.chunkSize) {
|
|
337
385
|
contentChunks.push(fileContent.slice(i, i + this.chunkSize));
|
|
338
386
|
}
|
|
339
|
-
|
|
387
|
+
|
|
340
388
|
// Get the lock script
|
|
341
389
|
const lock = await this.getLock();
|
|
342
|
-
|
|
390
|
+
|
|
343
391
|
// Determine content type if not provided
|
|
344
392
|
const contentType = options.contentType || getContentType(filePath);
|
|
345
|
-
|
|
393
|
+
|
|
346
394
|
// Use the filename from the path if not provided
|
|
347
395
|
const pathParts = filePath.split(/[\\\/]/);
|
|
348
396
|
const filename = options.filename || pathParts[pathParts.length - 1];
|
|
349
|
-
|
|
397
|
+
|
|
350
398
|
// Create the transaction using the utility function
|
|
351
399
|
return utilCreatePublishTransaction(this.signer, {
|
|
352
400
|
contentChunks,
|
|
@@ -357,7 +405,8 @@ export class CKBFS {
|
|
|
357
405
|
feeRate: options.feeRate,
|
|
358
406
|
network: options.network || this.network,
|
|
359
407
|
version: options.version || this.version,
|
|
360
|
-
useTypeID:
|
|
408
|
+
useTypeID:
|
|
409
|
+
options.useTypeID !== undefined ? options.useTypeID : this.useTypeID,
|
|
361
410
|
});
|
|
362
411
|
}
|
|
363
412
|
|
|
@@ -367,8 +416,12 @@ export class CKBFS {
|
|
|
367
416
|
* @param options Options for publishing the content (contentType and filename are required)
|
|
368
417
|
* @returns Promise resolving to the unsigned transaction
|
|
369
418
|
*/
|
|
370
|
-
async createPublishContentTransaction(
|
|
371
|
-
|
|
419
|
+
async createPublishContentTransaction(
|
|
420
|
+
content: string | Uint8Array,
|
|
421
|
+
options: PublishContentOptions,
|
|
422
|
+
): Promise<Transaction> {
|
|
423
|
+
const contentBytes =
|
|
424
|
+
typeof content === "string" ? textEncoder.encode(content) : content;
|
|
372
425
|
const contentChunks = [];
|
|
373
426
|
|
|
374
427
|
for (let i = 0; i < contentBytes.length; i += this.chunkSize) {
|
|
@@ -389,10 +442,11 @@ export class CKBFS {
|
|
|
389
442
|
feeRate: options.feeRate,
|
|
390
443
|
network: options.network || this.network,
|
|
391
444
|
version: options.version || this.version,
|
|
392
|
-
useTypeID:
|
|
445
|
+
useTypeID:
|
|
446
|
+
options.useTypeID !== undefined ? options.useTypeID : this.useTypeID,
|
|
393
447
|
});
|
|
394
448
|
}
|
|
395
|
-
|
|
449
|
+
|
|
396
450
|
/**
|
|
397
451
|
* Creates a new transaction for appending content from a file but doesn't sign or send it
|
|
398
452
|
* @param filePath The path to the file containing the content to append
|
|
@@ -401,25 +455,25 @@ export class CKBFS {
|
|
|
401
455
|
* @returns Promise resolving to the unsigned transaction
|
|
402
456
|
*/
|
|
403
457
|
async createAppendTransaction(
|
|
404
|
-
filePath: string,
|
|
405
|
-
ckbfsCell: AppendOptions[
|
|
406
|
-
options: Omit<FileOptions,
|
|
458
|
+
filePath: string,
|
|
459
|
+
ckbfsCell: AppendOptions["ckbfsCell"],
|
|
460
|
+
options: Omit<FileOptions, "contentType" | "filename"> = {},
|
|
407
461
|
): Promise<Transaction> {
|
|
408
462
|
// Read the file and split into chunks
|
|
409
463
|
const fileContent = readFileAsUint8Array(filePath);
|
|
410
464
|
const contentChunks = [];
|
|
411
|
-
|
|
465
|
+
|
|
412
466
|
for (let i = 0; i < fileContent.length; i += this.chunkSize) {
|
|
413
467
|
contentChunks.push(fileContent.slice(i, i + this.chunkSize));
|
|
414
468
|
}
|
|
415
|
-
|
|
469
|
+
|
|
416
470
|
// Create the transaction using the utility function
|
|
417
471
|
return utilCreateAppendTransaction(this.signer, {
|
|
418
472
|
ckbfsCell,
|
|
419
473
|
contentChunks,
|
|
420
474
|
feeRate: options.feeRate,
|
|
421
475
|
network: options.network || this.network,
|
|
422
|
-
version: options.version || this.version
|
|
476
|
+
version: options.version || this.version,
|
|
423
477
|
});
|
|
424
478
|
}
|
|
425
479
|
|
|
@@ -432,10 +486,11 @@ export class CKBFS {
|
|
|
432
486
|
*/
|
|
433
487
|
async createAppendContentTransaction(
|
|
434
488
|
content: string | Uint8Array,
|
|
435
|
-
ckbfsCell: AppendOptions[
|
|
436
|
-
options: AppendContentOptions = {}
|
|
489
|
+
ckbfsCell: AppendOptions["ckbfsCell"],
|
|
490
|
+
options: AppendContentOptions = {},
|
|
437
491
|
): Promise<Transaction> {
|
|
438
|
-
const contentBytes =
|
|
492
|
+
const contentBytes =
|
|
493
|
+
typeof content === "string" ? textEncoder.encode(content) : content;
|
|
439
494
|
const contentChunks = [];
|
|
440
495
|
|
|
441
496
|
for (let i = 0; i < contentBytes.length; i += this.chunkSize) {
|
|
@@ -447,7 +502,7 @@ export class CKBFS {
|
|
|
447
502
|
contentChunks,
|
|
448
503
|
feeRate: options.feeRate,
|
|
449
504
|
network: options.network || this.network,
|
|
450
|
-
version: options.version || this.version
|
|
505
|
+
version: options.version || this.version,
|
|
451
506
|
// No useTypeID option for append
|
|
452
507
|
});
|
|
453
508
|
}
|
|
@@ -460,14 +515,14 @@ export {
|
|
|
460
515
|
verifyChecksum,
|
|
461
516
|
updateChecksum,
|
|
462
517
|
verifyWitnessChecksum,
|
|
463
|
-
|
|
518
|
+
|
|
464
519
|
// Transaction utilities (Exporting original names from transaction.ts)
|
|
465
520
|
createCKBFSCell,
|
|
466
521
|
utilCreatePublishTransaction as createPublishTransaction,
|
|
467
522
|
utilCreateAppendTransaction as createAppendTransaction,
|
|
468
523
|
utilPublishCKBFS as publishCKBFS,
|
|
469
524
|
utilAppendCKBFS as appendCKBFS,
|
|
470
|
-
|
|
525
|
+
|
|
471
526
|
// File utilities
|
|
472
527
|
readFile,
|
|
473
528
|
readFileAsText,
|
|
@@ -478,33 +533,47 @@ export {
|
|
|
478
533
|
combineChunksToFile,
|
|
479
534
|
getFileContentFromChain,
|
|
480
535
|
saveFileFromChain,
|
|
481
|
-
|
|
536
|
+
getFileContentFromChainByTypeId,
|
|
537
|
+
saveFileFromChainByTypeId,
|
|
538
|
+
decodeFileFromChainByTypeId,
|
|
539
|
+
getFileContentFromChainByIdentifier,
|
|
540
|
+
saveFileFromChainByIdentifier,
|
|
541
|
+
decodeFileFromChainByIdentifier,
|
|
542
|
+
parseIdentifier,
|
|
543
|
+
IdentifierType,
|
|
544
|
+
decodeWitnessContent,
|
|
545
|
+
decodeMultipleWitnessContents,
|
|
546
|
+
extractFileFromWitnesses,
|
|
547
|
+
decodeFileFromWitnessData,
|
|
548
|
+
saveFileFromWitnessData,
|
|
549
|
+
|
|
482
550
|
// Witness utilities
|
|
483
551
|
createCKBFSWitness,
|
|
484
552
|
createTextCKBFSWitness,
|
|
485
553
|
extractCKBFSWitnessContent,
|
|
486
554
|
isCKBFSWitness,
|
|
487
555
|
createChunkedCKBFSWitnesses,
|
|
488
|
-
|
|
556
|
+
|
|
489
557
|
// Molecule definitions
|
|
490
558
|
CKBFSData,
|
|
491
559
|
BackLinkV1,
|
|
492
560
|
BackLinkV2,
|
|
493
|
-
|
|
561
|
+
|
|
494
562
|
// Types
|
|
495
563
|
CKBFSDataType,
|
|
496
564
|
BackLinkType,
|
|
497
565
|
CKBFSCellOptions,
|
|
498
566
|
PublishOptions,
|
|
499
567
|
AppendOptions,
|
|
500
|
-
|
|
568
|
+
|
|
501
569
|
// Constants
|
|
502
570
|
CKBFS_HEADER,
|
|
503
571
|
CKBFS_HEADER_STRING,
|
|
504
|
-
|
|
572
|
+
|
|
505
573
|
// CKBFS Protocol Constants & Configuration
|
|
506
574
|
NetworkType,
|
|
507
575
|
ProtocolVersion,
|
|
576
|
+
ProtocolVersionType,
|
|
508
577
|
DEFAULT_NETWORK,
|
|
509
578
|
DEFAULT_VERSION,
|
|
510
579
|
CKBFS_CODE_HASH,
|
|
@@ -514,5 +583,5 @@ export {
|
|
|
514
583
|
DEP_GROUP_TX_HASH,
|
|
515
584
|
DEPLOY_TX_HASH,
|
|
516
585
|
getCKBFSScriptConfig,
|
|
517
|
-
CKBFSScriptConfig
|
|
518
|
-
};
|
|
586
|
+
CKBFSScriptConfig,
|
|
587
|
+
};
|