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