@ckbfs/api 1.5.1 → 2.0.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.
Files changed (45) hide show
  1. package/README.md +31 -6
  2. package/RFC.v3.md +210 -0
  3. package/dist/index.d.ts +72 -7
  4. package/dist/index.js +437 -75
  5. package/dist/utils/checksum.d.ts +16 -0
  6. package/dist/utils/checksum.js +74 -8
  7. package/dist/utils/constants.d.ts +2 -1
  8. package/dist/utils/constants.js +12 -2
  9. package/dist/utils/file.d.ts +44 -0
  10. package/dist/utils/file.js +303 -30
  11. package/dist/utils/molecule.d.ts +13 -1
  12. package/dist/utils/molecule.js +32 -5
  13. package/dist/utils/transaction-backup.d.ts +117 -0
  14. package/dist/utils/transaction-backup.js +624 -0
  15. package/dist/utils/transaction.d.ts +7 -115
  16. package/dist/utils/transaction.js +45 -622
  17. package/dist/utils/transactions/index.d.ts +8 -0
  18. package/dist/utils/transactions/index.js +31 -0
  19. package/dist/utils/transactions/shared.d.ts +57 -0
  20. package/dist/utils/transactions/shared.js +17 -0
  21. package/dist/utils/transactions/v1v2.d.ts +80 -0
  22. package/dist/utils/transactions/v1v2.js +592 -0
  23. package/dist/utils/transactions/v3.d.ts +124 -0
  24. package/dist/utils/transactions/v3.js +369 -0
  25. package/dist/utils/witness.d.ts +45 -0
  26. package/dist/utils/witness.js +145 -3
  27. package/examples/append-v3.ts +310 -0
  28. package/examples/chunked-publish.ts +307 -0
  29. package/examples/publish-v3.ts +152 -0
  30. package/examples/publish.ts +4 -4
  31. package/examples/retrieve-v3.ts +222 -0
  32. package/package.json +6 -2
  33. package/small-example.txt +1 -0
  34. package/src/index.ts +568 -87
  35. package/src/utils/checksum.ts +90 -9
  36. package/src/utils/constants.ts +19 -2
  37. package/src/utils/file.ts +386 -35
  38. package/src/utils/molecule.ts +43 -6
  39. package/src/utils/transaction-backup.ts +849 -0
  40. package/src/utils/transaction.ts +39 -848
  41. package/src/utils/transactions/index.ts +16 -0
  42. package/src/utils/transactions/shared.ts +64 -0
  43. package/src/utils/transactions/v1v2.ts +791 -0
  44. package/src/utils/transactions/v3.ts +564 -0
  45. package/src/utils/witness.ts +193 -0
@@ -46,6 +46,12 @@ export declare const CKBFSDataV2: molecule.ObjectLayoutCodec<{
46
46
  txHash: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
47
47
  }>>;
48
48
  }>;
49
+ export declare const CKBFSDataV3: molecule.ObjectLayoutCodec<{
50
+ index: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
51
+ checksum: import("@ckb-lumos/codec/lib/base").FixedBytesCodec<number, number.BIish>;
52
+ contentType: import("@ckb-lumos/codec/lib/base").BytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
53
+ filename: import("@ckb-lumos/codec/lib/base").BytesCodec<string, import("@ckb-lumos/codec").BytesLike>;
54
+ }>;
49
55
  export type BackLinkTypeV1 = {
50
56
  index: number;
51
57
  checksum: number;
@@ -62,13 +68,19 @@ export type BackLinkType = {
62
68
  checksum: number;
63
69
  txHash: string;
64
70
  };
71
+ export type CKBFSDataTypeV3 = {
72
+ index: number;
73
+ checksum: number;
74
+ contentType: string;
75
+ filename: string;
76
+ };
65
77
  export type CKBFSDataType = {
66
78
  index?: number;
67
79
  indexes?: number[];
68
80
  checksum: number;
69
81
  contentType: string;
70
82
  filename: string;
71
- backLinks: BackLinkType[];
83
+ backLinks?: BackLinkType[];
72
84
  };
73
85
  export declare const CKBFSData: {
74
86
  pack: (data: CKBFSDataType, version?: ProtocolVersionType) => Uint8Array;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
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;
3
+ exports.CKBFS_HEADER_STRING = exports.CKBFS_HEADER = exports.CKBFSData = exports.CKBFSDataV3 = exports.CKBFSDataV2 = exports.CKBFSDataV1 = exports.BackLinksV2 = exports.BackLinksV1 = exports.BackLinkV2 = exports.BackLinkV1 = exports.Indexes = void 0;
4
4
  const codec_1 = require("@ckb-lumos/codec");
5
5
  const base_1 = require("@ckb-lumos/base");
6
6
  const constants_1 = require("./constants");
@@ -41,6 +41,13 @@ exports.CKBFSDataV2 = codec_1.molecule.table({
41
41
  filename: base_1.blockchain.Bytes,
42
42
  backLinks: exports.BackLinksV2,
43
43
  }, ["indexes", "checksum", "contentType", "filename", "backLinks"]);
44
+ // V3: CKBFSData has no backLinks (moved to witnesses)
45
+ exports.CKBFSDataV3 = codec_1.molecule.table({
46
+ index: codec_1.number.Uint32,
47
+ checksum: codec_1.number.Uint32,
48
+ contentType: base_1.blockchain.Bytes,
49
+ filename: base_1.blockchain.Bytes,
50
+ }, ["index", "checksum", "contentType", "filename"]);
44
51
  // Helper function to get indexes array from data
45
52
  function getIndexes(data) {
46
53
  if (data.indexes)
@@ -80,14 +87,23 @@ function getBackLinkIndexes(bl) {
80
87
  // Helper function to get the right CKBFSData based on version
81
88
  exports.CKBFSData = {
82
89
  pack: (data, version = constants_1.ProtocolVersion.V2) => {
83
- if (version === constants_1.ProtocolVersion.V1) {
90
+ if (version === constants_1.ProtocolVersion.V3) {
91
+ // V3 formatting - no backLinks, uses single index
92
+ return exports.CKBFSDataV3.pack({
93
+ index: getIndex(data),
94
+ checksum: data.checksum,
95
+ contentType: core_1.ccc.bytesFrom(data.contentType, "utf8"),
96
+ filename: core_1.ccc.bytesFrom(data.filename, "utf8"),
97
+ });
98
+ }
99
+ else if (version === constants_1.ProtocolVersion.V1) {
84
100
  // V1 formatting - uses single index
85
101
  return exports.CKBFSDataV1.pack({
86
102
  index: getIndex(data),
87
103
  checksum: data.checksum,
88
104
  contentType: core_1.ccc.bytesFrom(data.contentType, "utf8"),
89
105
  filename: core_1.ccc.bytesFrom(data.filename, "utf8"),
90
- backLinks: data.backLinks.map((bl) => {
106
+ backLinks: (data.backLinks || []).map((bl) => {
91
107
  // Ensure txHash is in proper format for molecule encoding
92
108
  const txHash = typeof bl.txHash === "string"
93
109
  ? core_1.ccc.bytesFrom(bl.txHash)
@@ -107,7 +123,7 @@ exports.CKBFSData = {
107
123
  checksum: data.checksum,
108
124
  contentType: core_1.ccc.bytesFrom(data.contentType, "utf8"),
109
125
  filename: core_1.ccc.bytesFrom(data.filename, "utf8"),
110
- backLinks: data.backLinks.map((bl) => {
126
+ backLinks: (data.backLinks || []).map((bl) => {
111
127
  // Ensure txHash is in proper format for molecule encoding
112
128
  const txHash = typeof bl.txHash === "string" ? bl.txHash : bl.txHash;
113
129
  return {
@@ -121,7 +137,18 @@ exports.CKBFSData = {
121
137
  },
122
138
  unpack: (buf, version = constants_1.ProtocolVersion.V2) => {
123
139
  try {
124
- if (version === constants_1.ProtocolVersion.V1) {
140
+ if (version === constants_1.ProtocolVersion.V3) {
141
+ // V3 format - no backLinks
142
+ const unpacked = exports.CKBFSDataV3.unpack(buf);
143
+ return {
144
+ index: unpacked.index,
145
+ checksum: unpacked.checksum,
146
+ contentType: core_1.ccc.bytesTo(unpacked.contentType, "utf8"),
147
+ filename: core_1.ccc.bytesTo(unpacked.filename, "utf8"),
148
+ backLinks: [], // V3 has no backLinks in cell data
149
+ };
150
+ }
151
+ else if (version === constants_1.ProtocolVersion.V1) {
125
152
  const unpacked = exports.CKBFSDataV1.unpack(buf);
126
153
  return {
127
154
  index: unpacked.index,
@@ -0,0 +1,117 @@
1
+ import { ccc, Transaction, Script, Signer } from "@ckb-ccc/core";
2
+ import { CKBFSDataType } from "./molecule";
3
+ import { NetworkType, ProtocolVersionType } 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?: ProtocolVersionType;
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
+ from?: Transaction;
26
+ }
27
+ /**
28
+ * Options for appending content to a CKBFS file
29
+ */
30
+ export interface AppendOptions {
31
+ ckbfsCell: {
32
+ outPoint: {
33
+ txHash: string;
34
+ index: number;
35
+ };
36
+ data: CKBFSDataType;
37
+ type: Script;
38
+ lock: Script;
39
+ capacity: bigint;
40
+ };
41
+ contentChunks: Uint8Array[];
42
+ feeRate?: number;
43
+ network?: NetworkType;
44
+ version?: ProtocolVersionType;
45
+ from?: Transaction;
46
+ }
47
+ /**
48
+ * Ensures a string is prefixed with '0x'
49
+ * @param value The string to ensure is hex prefixed
50
+ * @returns A hex prefixed string
51
+ */
52
+ export declare function ensureHexPrefix(value: string): `0x${string}`;
53
+ /**
54
+ * Creates a CKBFS cell
55
+ * @param options Options for creating the CKBFS cell
56
+ * @returns The created cell output
57
+ */
58
+ export declare function createCKBFSCell(options: CKBFSCellOptions): {
59
+ lock: ccc.Script;
60
+ type: ccc.Script;
61
+ capacity: bigint;
62
+ };
63
+ /**
64
+ * Prepares a transaction for publishing a file to CKBFS without fee and change handling
65
+ * You will need to manually set the typeID if you did not provide inputs, or just check is return value emptyTypeID is true
66
+ * @param options Options for publishing the file
67
+ * @returns Promise resolving to the prepared transaction and the output index of CKBFS Cell
68
+ */
69
+ export declare function preparePublishTransaction(options: PublishOptions): Promise<{
70
+ tx: Transaction;
71
+ outputIndex: number;
72
+ emptyTypeID: boolean;
73
+ }>;
74
+ /**
75
+ * Creates a transaction for publishing a file to CKBFS
76
+ * @param signer The signer to use for the transaction
77
+ * @param options Options for publishing the file
78
+ * @returns Promise resolving to the created transaction
79
+ */
80
+ export declare function createPublishTransaction(signer: Signer, options: PublishOptions): Promise<Transaction>;
81
+ /**
82
+ * Prepares a transaction for appending content to a CKBFS file without fee and change handling
83
+ * @param options Options for appending content
84
+ * @returns Promise resolving to the prepared transaction and the output index of CKBFS Cell
85
+ */
86
+ export declare function prepareAppendTransaction(options: AppendOptions): Promise<{
87
+ tx: Transaction;
88
+ outputIndex: number;
89
+ }>;
90
+ /**
91
+ * Creates a transaction for appending content to a CKBFS file
92
+ * @param signer The signer to use for the transaction
93
+ * @param options Options for appending content
94
+ * @returns Promise resolving to the created transaction
95
+ */
96
+ export declare function createAppendTransaction(signer: Signer, options: AppendOptions): Promise<Transaction>;
97
+ /**
98
+ * Creates a transaction for appending content to a CKBFS file
99
+ * @param signer The signer to use for the transaction
100
+ * @param options Options for appending content
101
+ * @returns Promise resolving to the created transaction
102
+ */
103
+ export declare function createAppendTransactionDry(signer: Signer, options: AppendOptions): Promise<Transaction>;
104
+ /**
105
+ * Creates a complete transaction for publishing a file to CKBFS
106
+ * @param signer The signer to use for the transaction
107
+ * @param options Options for publishing the file
108
+ * @returns Promise resolving to the signed transaction
109
+ */
110
+ export declare function publishCKBFS(signer: Signer, options: PublishOptions): Promise<Transaction>;
111
+ /**
112
+ * Creates a complete transaction for appending content to a CKBFS file
113
+ * @param signer The signer to use for the transaction
114
+ * @param options Options for appending content
115
+ * @returns Promise resolving to the signed transaction
116
+ */
117
+ export declare function appendCKBFS(signer: Signer, options: AppendOptions): Promise<Transaction>;