@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/src/utils/molecule.ts
CHANGED
@@ -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 ===
|
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 ===
|
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 ===
|
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 ===
|
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: (
|
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,
|
137
|
-
filename: ccc.bytesFrom(data.filename,
|
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 =
|
141
|
-
|
142
|
-
|
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,
|
157
|
-
filename: ccc.bytesFrom(data.filename,
|
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 ===
|
161
|
-
|
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: (
|
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,
|
181
|
-
filename: ccc.bytesTo(unpacked.filename,
|
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,
|
195
|
-
filename: ccc.bytesTo(unpacked.filename,
|
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(
|
205
|
-
throw new 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,
|
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";
|