@ckbfs/api 1.2.3 → 1.2.5

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.
@@ -1,6 +1,6 @@
1
1
  import { molecule, number } from "@ckb-lumos/codec";
2
2
  import { blockchain } from "@ckb-lumos/base";
3
- import { ProtocolVersion } from "./constants";
3
+ import { ProtocolVersion, ProtocolVersionType } from "./constants";
4
4
  import { ccc } from "@ckb-ccc/core";
5
5
 
6
6
  /**
@@ -17,7 +17,7 @@ export const BackLinkV1 = molecule.table(
17
17
  checksum: number.Uint32,
18
18
  txHash: blockchain.Byte32,
19
19
  },
20
- ["index", "checksum", "txHash"]
20
+ ["index", "checksum", "txHash"],
21
21
  );
22
22
 
23
23
  // V2: BackLink has indexes as vector of Uint32
@@ -27,7 +27,7 @@ export const BackLinkV2 = molecule.table(
27
27
  checksum: number.Uint32,
28
28
  txHash: blockchain.Byte32,
29
29
  },
30
- ["indexes", "checksum", "txHash"]
30
+ ["indexes", "checksum", "txHash"],
31
31
  );
32
32
 
33
33
  // Define the BackLinks vector for V1 and V2
@@ -43,7 +43,7 @@ export const CKBFSDataV1 = molecule.table(
43
43
  filename: blockchain.Bytes,
44
44
  backLinks: BackLinksV1,
45
45
  },
46
- ["index", "checksum", "contentType", "filename", "backLinks"]
46
+ ["index", "checksum", "contentType", "filename", "backLinks"],
47
47
  );
48
48
 
49
49
  // V2: CKBFSData has indexes as vector of Uint32
@@ -55,7 +55,7 @@ export const CKBFSDataV2 = molecule.table(
55
55
  filename: blockchain.Bytes,
56
56
  backLinks: BackLinksV2,
57
57
  },
58
- ["indexes", "checksum", "contentType", "filename", "backLinks"]
58
+ ["indexes", "checksum", "contentType", "filename", "backLinks"],
59
59
  );
60
60
 
61
61
  // Type definitions for TypeScript
@@ -92,20 +92,20 @@ export type CKBFSDataType = {
92
92
  // Helper function to get indexes array from data
93
93
  function getIndexes(data: CKBFSDataType): number[] {
94
94
  if (data.indexes) return data.indexes;
95
- if (typeof data.index === 'number') return [data.index];
95
+ if (typeof data.index === "number") return [data.index];
96
96
  return [];
97
97
  }
98
98
 
99
99
  // Helper function to get single index from data
100
100
  function getIndex(data: CKBFSDataType): number {
101
- if (typeof data.index === 'number') return data.index;
101
+ if (typeof data.index === "number") return data.index;
102
102
  if (data.indexes && data.indexes.length > 0) return data.indexes[0];
103
103
  return 0;
104
104
  }
105
105
 
106
106
  // Helper function to safely get either index or indexes from BackLinkType for V1
107
107
  function getBackLinkIndex(bl: BackLinkType): number {
108
- if (typeof bl.index === 'number') {
108
+ if (typeof bl.index === "number") {
109
109
  return bl.index;
110
110
  }
111
111
  if (Array.isArray(bl.indexes) && bl.indexes.length > 0) {
@@ -119,7 +119,7 @@ function getBackLinkIndexes(bl: BackLinkType): number[] {
119
119
  if (Array.isArray(bl.indexes)) {
120
120
  return bl.indexes;
121
121
  }
122
- if (typeof bl.index === 'number') {
122
+ if (typeof bl.index === "number") {
123
123
  return [bl.index];
124
124
  }
125
125
  return [0];
@@ -127,20 +127,24 @@ function getBackLinkIndexes(bl: BackLinkType): number[] {
127
127
 
128
128
  // Helper function to get the right CKBFSData based on version
129
129
  export const CKBFSData = {
130
- pack: (data: CKBFSDataType, version: string = ProtocolVersion.V2): Uint8Array => {
130
+ pack: (
131
+ data: CKBFSDataType,
132
+ version: ProtocolVersionType = ProtocolVersion.V2,
133
+ ): Uint8Array => {
131
134
  if (version === ProtocolVersion.V1) {
132
135
  // V1 formatting - uses single index
133
136
  return CKBFSDataV1.pack({
134
137
  index: getIndex(data),
135
138
  checksum: data.checksum,
136
- contentType: ccc.bytesFrom(data.contentType, 'utf8'),
137
- filename: ccc.bytesFrom(data.filename, 'utf8'),
138
- backLinks: data.backLinks.map(bl => {
139
+ contentType: ccc.bytesFrom(data.contentType, "utf8"),
140
+ filename: ccc.bytesFrom(data.filename, "utf8"),
141
+ backLinks: data.backLinks.map((bl) => {
139
142
  // Ensure txHash is in proper format for molecule encoding
140
- const txHash = typeof bl.txHash === 'string'
141
- ? ccc.bytesFrom(bl.txHash)
142
- : bl.txHash;
143
-
143
+ const txHash =
144
+ typeof bl.txHash === "string"
145
+ ? ccc.bytesFrom(bl.txHash)
146
+ : bl.txHash;
147
+
144
148
  return {
145
149
  index: getBackLinkIndex(bl),
146
150
  checksum: bl.checksum,
@@ -153,14 +157,12 @@ export const CKBFSData = {
153
157
  return CKBFSDataV2.pack({
154
158
  indexes: getIndexes(data),
155
159
  checksum: data.checksum,
156
- contentType: ccc.bytesFrom(data.contentType, 'utf8'),
157
- filename: ccc.bytesFrom(data.filename, 'utf8'),
158
- backLinks: data.backLinks.map(bl => {
160
+ contentType: ccc.bytesFrom(data.contentType, "utf8"),
161
+ filename: ccc.bytesFrom(data.filename, "utf8"),
162
+ backLinks: data.backLinks.map((bl) => {
159
163
  // Ensure txHash is in proper format for molecule encoding
160
- const txHash = typeof bl.txHash === 'string'
161
- ? bl.txHash
162
- : bl.txHash;
163
-
164
+ const txHash = typeof bl.txHash === "string" ? bl.txHash : bl.txHash;
165
+
164
166
  return {
165
167
  indexes: getBackLinkIndexes(bl),
166
168
  checksum: bl.checksum,
@@ -170,16 +172,19 @@ export const CKBFSData = {
170
172
  });
171
173
  }
172
174
  },
173
- unpack: (buf: Uint8Array, version: string = ProtocolVersion.V2): CKBFSDataType => {
175
+ unpack: (
176
+ buf: Uint8Array,
177
+ version: ProtocolVersionType = ProtocolVersion.V2,
178
+ ): CKBFSDataType => {
174
179
  try {
175
180
  if (version === ProtocolVersion.V1) {
176
181
  const unpacked = CKBFSDataV1.unpack(buf);
177
182
  return {
178
183
  index: unpacked.index,
179
184
  checksum: unpacked.checksum,
180
- contentType: ccc.bytesTo(unpacked.contentType, 'utf8'),
181
- filename: ccc.bytesTo(unpacked.filename, 'utf8'),
182
- backLinks: unpacked.backLinks.map(bl => ({
185
+ contentType: ccc.bytesTo(unpacked.contentType, "utf8"),
186
+ filename: ccc.bytesTo(unpacked.filename, "utf8"),
187
+ backLinks: unpacked.backLinks.map((bl) => ({
183
188
  index: bl.index,
184
189
  checksum: bl.checksum,
185
190
  txHash: bl.txHash,
@@ -191,9 +196,9 @@ export const CKBFSData = {
191
196
  return {
192
197
  indexes: unpacked.indexes,
193
198
  checksum: unpacked.checksum,
194
- contentType: ccc.bytesTo(unpacked.contentType, 'utf8'),
195
- filename: ccc.bytesTo(unpacked.filename, 'utf8'),
196
- backLinks: unpacked.backLinks.map(bl => ({
199
+ contentType: ccc.bytesTo(unpacked.contentType, "utf8"),
200
+ filename: ccc.bytesTo(unpacked.filename, "utf8"),
201
+ backLinks: unpacked.backLinks.map((bl) => ({
197
202
  indexes: bl.indexes,
198
203
  checksum: bl.checksum,
199
204
  txHash: bl.txHash,
@@ -201,12 +206,12 @@ export const CKBFSData = {
201
206
  };
202
207
  }
203
208
  } catch (error) {
204
- console.error('Error unpacking CKBFSData:', error);
205
- throw new Error('Failed to unpack CKBFSData: ' + error);
209
+ console.error("Error unpacking CKBFSData:", error);
210
+ throw new Error("Failed to unpack CKBFSData: " + error);
206
211
  }
207
- }
212
+ },
208
213
  };
209
214
 
210
215
  // Constants for CKBFS protocol
211
- export const CKBFS_HEADER = new Uint8Array([0x43, 0x4B, 0x42, 0x46, 0x53]); // "CKBFS" in ASCII
212
- export const CKBFS_HEADER_STRING = "CKBFS";
216
+ export const CKBFS_HEADER = new Uint8Array([0x43, 0x4b, 0x42, 0x46, 0x53]); // "CKBFS" in ASCII
217
+ export const CKBFS_HEADER_STRING = "CKBFS";