@ckb-ccc/core 0.1.0-alpha.5 → 0.1.0-alpha.7
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/CHANGELOG.md +13 -0
- package/dist/barrel.d.ts +1 -0
- package/dist/barrel.d.ts.map +1 -1
- package/dist/barrel.js +1 -0
- package/dist/ckb/transaction.d.ts.map +1 -1
- package/dist/ckb/transaction.js +9 -7
- package/dist/molecule/codec.d.ts +118 -0
- package/dist/molecule/codec.d.ts.map +1 -0
- package/dist/molecule/codec.js +446 -0
- package/dist/molecule/index.d.ts +3 -0
- package/dist/molecule/index.d.ts.map +1 -0
- package/dist/molecule/index.js +2 -0
- package/dist/molecule/predefined.d.ts +52 -0
- package/dist/molecule/predefined.d.ts.map +1 -0
- package/dist/molecule/predefined.js +95 -0
- package/dist.commonjs/barrel.d.ts +1 -0
- package/dist.commonjs/barrel.d.ts.map +1 -1
- package/dist.commonjs/barrel.js +14 -0
- package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
- package/dist.commonjs/ckb/transaction.js +7 -5
- package/dist.commonjs/molecule/codec.d.ts +118 -0
- package/dist.commonjs/molecule/codec.d.ts.map +1 -0
- package/dist.commonjs/molecule/codec.js +461 -0
- package/dist.commonjs/molecule/index.d.ts +3 -0
- package/dist.commonjs/molecule/index.d.ts.map +1 -0
- package/dist.commonjs/molecule/index.js +18 -0
- package/dist.commonjs/molecule/predefined.d.ts +52 -0
- package/dist.commonjs/molecule/predefined.d.ts.map +1 -0
- package/dist.commonjs/molecule/predefined.js +121 -0
- package/package.json +1 -1
- package/src/barrel.ts +1 -0
- package/src/ckb/transaction.ts +9 -12
- package/src/molecule/codec.ts +610 -0
- package/src/molecule/index.ts +2 -0
- package/src/molecule/predefined.ts +114 -0
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { bytesConcat, bytesFrom } from "../bytes/index.js";
|
|
3
|
+
import { numBeFromBytes, numBeToBytes, numFromBytes, numToBytes, } from "../num/index.js";
|
|
4
|
+
export class Codec {
|
|
5
|
+
constructor(encode, decode, byteLength) {
|
|
6
|
+
this.encode = encode;
|
|
7
|
+
this.decode = decode;
|
|
8
|
+
this.byteLength = byteLength;
|
|
9
|
+
}
|
|
10
|
+
static from({ encode, decode, byteLength, }) {
|
|
11
|
+
return new Codec(encode, decode, byteLength);
|
|
12
|
+
}
|
|
13
|
+
map({ inMap, outMap, }) {
|
|
14
|
+
return Codec.from({
|
|
15
|
+
byteLength: this.byteLength,
|
|
16
|
+
encode: (encodable) => this.encode((inMap ? inMap(encodable) : encodable)),
|
|
17
|
+
decode: (buffer) => (outMap
|
|
18
|
+
? outMap(this.decode(buffer))
|
|
19
|
+
: this.decode(buffer)),
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function uint32To(numLike) {
|
|
24
|
+
return numToBytes(numLike, 4);
|
|
25
|
+
}
|
|
26
|
+
function uint32From(bytesLike) {
|
|
27
|
+
return Number(numFromBytes(bytesLike));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Vector with fixed size item codec
|
|
31
|
+
* @param itemCodec fixed-size vector item codec
|
|
32
|
+
*/
|
|
33
|
+
export function fixedItemVec(itemCodec) {
|
|
34
|
+
const itemByteLength = itemCodec.byteLength;
|
|
35
|
+
if (itemByteLength === undefined) {
|
|
36
|
+
throw new Error("fixedItemVec: itemCodec requires a byte length");
|
|
37
|
+
}
|
|
38
|
+
return Codec.from({
|
|
39
|
+
encode(userDefinedItems) {
|
|
40
|
+
try {
|
|
41
|
+
return userDefinedItems.reduce((concatted, item) => bytesConcat(concatted, itemCodec.encode(item)), uint32To(userDefinedItems.length));
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
throw new Error(`fixedItemVec(${e?.toString()})`);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
decode(buffer) {
|
|
48
|
+
const value = bytesFrom(buffer);
|
|
49
|
+
if (value.byteLength < 4) {
|
|
50
|
+
throw new Error(`fixedItemVec: too short buffer, expected at least 4 bytes, but got ${value.byteLength}`);
|
|
51
|
+
}
|
|
52
|
+
const itemCount = uint32From(value.slice(0, 4));
|
|
53
|
+
const byteLength = 4 + itemCount * itemByteLength;
|
|
54
|
+
if (value.byteLength !== byteLength) {
|
|
55
|
+
throw new Error(`fixedItemVec: invalid buffer size, expected ${byteLength}, but got ${value.byteLength}`);
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const decodedArray = [];
|
|
59
|
+
for (let offset = 0; offset < byteLength; offset += itemByteLength) {
|
|
60
|
+
decodedArray.push(itemCodec.decode(value.slice(offset, offset + itemByteLength)));
|
|
61
|
+
}
|
|
62
|
+
return decodedArray;
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
throw new Error(`fixedItemVec(${e?.toString()})`);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Vector with dynamic size item codec, you can create a recursive vector with this function
|
|
72
|
+
* @param itemCodec the vector item codec. It can be fixed-size or dynamic-size.
|
|
73
|
+
*/
|
|
74
|
+
export function dynItemVec(itemCodec) {
|
|
75
|
+
return Codec.from({
|
|
76
|
+
encode(userDefinedItems) {
|
|
77
|
+
try {
|
|
78
|
+
const encoded = userDefinedItems.reduce(({ offset, header, body }, item) => {
|
|
79
|
+
const encodedItem = itemCodec.encode(item);
|
|
80
|
+
const packedHeader = uint32To(offset);
|
|
81
|
+
return {
|
|
82
|
+
header: bytesConcat(header, packedHeader),
|
|
83
|
+
body: bytesConcat(body, encodedItem),
|
|
84
|
+
offset: offset + bytesFrom(encodedItem).byteLength,
|
|
85
|
+
};
|
|
86
|
+
}, {
|
|
87
|
+
header: bytesFrom([]),
|
|
88
|
+
body: bytesFrom([]),
|
|
89
|
+
offset: 4 + userDefinedItems.length * 4,
|
|
90
|
+
});
|
|
91
|
+
const packedTotalSize = uint32To(encoded.header.byteLength + encoded.body.byteLength + 4);
|
|
92
|
+
return bytesConcat(packedTotalSize, encoded.header, encoded.body);
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
throw new Error(`dynItemVec(${e?.toString()})`);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
decode(buffer) {
|
|
99
|
+
const value = bytesFrom(buffer);
|
|
100
|
+
const byteLength = uint32From(value.slice(0, 4));
|
|
101
|
+
if (byteLength !== value.byteLength) {
|
|
102
|
+
throw new Error(`dynItemVec: invalid buffer size, expected ${byteLength}, but got ${value.byteLength}`);
|
|
103
|
+
}
|
|
104
|
+
if (value.byteLength < 4) {
|
|
105
|
+
throw new Error(`fixedItemVec: too short buffer, expected at least 4 bytes, but got ${value.byteLength}`);
|
|
106
|
+
}
|
|
107
|
+
const offset = uint32From(value.slice(4, 8));
|
|
108
|
+
const itemCount = (offset - 4) / 4;
|
|
109
|
+
const offsets = Array.from(new Array(itemCount), (_, index) => uint32From(value.slice(4 + index * 4, 8 + index * 4)));
|
|
110
|
+
offsets.push(byteLength);
|
|
111
|
+
try {
|
|
112
|
+
const decodedArray = [];
|
|
113
|
+
for (let index = 0; index < offsets.length - 1; index++) {
|
|
114
|
+
const start = offsets[index];
|
|
115
|
+
const end = offsets[index + 1];
|
|
116
|
+
const itemBuffer = value.slice(start, end);
|
|
117
|
+
decodedArray.push(itemCodec.decode(itemBuffer));
|
|
118
|
+
}
|
|
119
|
+
return decodedArray;
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
throw new Error(`dynItemVec(${e?.toString()})`);
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* General vector codec, if `itemCodec` is fixed size type, it will create a fixvec codec, otherwise a dynvec codec will be created.
|
|
129
|
+
* @param itemCodec
|
|
130
|
+
*/
|
|
131
|
+
export function vector(itemCodec) {
|
|
132
|
+
if (itemCodec.byteLength !== undefined) {
|
|
133
|
+
return fixedItemVec(itemCodec);
|
|
134
|
+
}
|
|
135
|
+
return dynItemVec(itemCodec);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Option is a dynamic-size type.
|
|
139
|
+
* Serializing an option depends on whether it is empty or not:
|
|
140
|
+
* - if it's empty, there is zero bytes (the size is 0).
|
|
141
|
+
* - if it's not empty, just serialize the inner item (the size is same as the inner item's size).
|
|
142
|
+
* @param innerCodec
|
|
143
|
+
*/
|
|
144
|
+
export function option(innerCodec) {
|
|
145
|
+
return Codec.from({
|
|
146
|
+
encode(userDefinedOrNull) {
|
|
147
|
+
if (!userDefinedOrNull) {
|
|
148
|
+
return bytesFrom([]);
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
return innerCodec.encode(userDefinedOrNull);
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
throw new Error(`option(${e?.toString()})`);
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
decode(buffer) {
|
|
158
|
+
const value = bytesFrom(buffer);
|
|
159
|
+
if (value.byteLength === 0) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
return innerCodec.decode(buffer);
|
|
164
|
+
}
|
|
165
|
+
catch (e) {
|
|
166
|
+
throw new Error(`option(${e?.toString()})`);
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Wrap the encoded value with a fixed-length buffer
|
|
173
|
+
* @param codec
|
|
174
|
+
*/
|
|
175
|
+
export function byteVec(codec) {
|
|
176
|
+
return Codec.from({
|
|
177
|
+
encode(userDefined) {
|
|
178
|
+
try {
|
|
179
|
+
const payload = bytesFrom(codec.encode(userDefined));
|
|
180
|
+
const byteLength = uint32To(payload.byteLength);
|
|
181
|
+
return bytesConcat(byteLength, payload);
|
|
182
|
+
}
|
|
183
|
+
catch (e) {
|
|
184
|
+
throw new Error(`byteVec(${e?.toString()})`);
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
decode(buffer) {
|
|
188
|
+
const value = bytesFrom(buffer);
|
|
189
|
+
if (value.byteLength < 4) {
|
|
190
|
+
throw new Error(`byteVec: too short buffer, expected at least 4 bytes, but got ${value.byteLength}`);
|
|
191
|
+
}
|
|
192
|
+
const byteLength = uint32From(value.slice(0, 4));
|
|
193
|
+
if (byteLength !== value.byteLength - 4) {
|
|
194
|
+
throw new Error(`byteVec: invalid buffer size, expected ${byteLength}, but got ${value.byteLength}`);
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
return codec.decode(value.slice(4));
|
|
198
|
+
}
|
|
199
|
+
catch (e) {
|
|
200
|
+
throw new Error(`byteVec(${e?.toString()})`);
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Table is a dynamic-size type. It can be considered as a dynvec but the length is fixed.
|
|
207
|
+
* @param codecLayout
|
|
208
|
+
*/
|
|
209
|
+
export function table(codecLayout) {
|
|
210
|
+
const keys = Object.keys(codecLayout);
|
|
211
|
+
return Codec.from({
|
|
212
|
+
encode(object) {
|
|
213
|
+
const headerLength = 4 + keys.length * 4;
|
|
214
|
+
const { header, body } = keys.reduce((result, key) => {
|
|
215
|
+
try {
|
|
216
|
+
const encodedItem = codecLayout[key].encode(object[key]);
|
|
217
|
+
const packedOffset = uint32To(result.offset);
|
|
218
|
+
return {
|
|
219
|
+
header: bytesConcat(result.header, packedOffset),
|
|
220
|
+
body: bytesConcat(result.body, encodedItem),
|
|
221
|
+
offset: result.offset + bytesFrom(encodedItem).byteLength,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
catch (e) {
|
|
225
|
+
throw new Error(`table.${key}(${e?.toString()})`);
|
|
226
|
+
}
|
|
227
|
+
}, {
|
|
228
|
+
header: bytesFrom([]),
|
|
229
|
+
body: bytesFrom([]),
|
|
230
|
+
offset: headerLength,
|
|
231
|
+
});
|
|
232
|
+
const packedTotalSize = uint32To(header.byteLength + body.byteLength + 4);
|
|
233
|
+
return bytesConcat(packedTotalSize, header, body);
|
|
234
|
+
},
|
|
235
|
+
decode(buffer) {
|
|
236
|
+
const value = bytesFrom(buffer);
|
|
237
|
+
const byteLength = uint32From(value.slice(0, 4));
|
|
238
|
+
if (byteLength !== value.byteLength) {
|
|
239
|
+
throw new Error(`table: invalid buffer size, expected ${byteLength}, but got ${value.byteLength}`);
|
|
240
|
+
}
|
|
241
|
+
if (byteLength <= 4) {
|
|
242
|
+
throw new Error("table: empty buffer");
|
|
243
|
+
}
|
|
244
|
+
const offsets = keys.map((_, index) => uint32From(value.slice(4 + index * 4, 8 + index * 4)));
|
|
245
|
+
offsets.push(byteLength);
|
|
246
|
+
const object = {};
|
|
247
|
+
for (let i = 0; i < offsets.length - 1; i++) {
|
|
248
|
+
const start = offsets[i];
|
|
249
|
+
const end = offsets[i + 1];
|
|
250
|
+
const field = keys[i];
|
|
251
|
+
const codec = codecLayout[field];
|
|
252
|
+
const payload = value.slice(start, end);
|
|
253
|
+
try {
|
|
254
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
255
|
+
Object.assign(object, { [field]: codec.decode(payload) });
|
|
256
|
+
}
|
|
257
|
+
catch (e) {
|
|
258
|
+
throw new Error(`table.${field}(${e?.toString()})`);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return object;
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Union is a dynamic-size type.
|
|
267
|
+
* Serializing a union has two steps:
|
|
268
|
+
* - Serialize an item type id in bytes as a 32 bit unsigned integer in little-endian. The item type id is the index of the inner items, and it's starting at 0.
|
|
269
|
+
* - Serialize the inner item.
|
|
270
|
+
* @param codecLayout the union item record
|
|
271
|
+
* @param fields the custom item type id record
|
|
272
|
+
* @example
|
|
273
|
+
* // without custom id
|
|
274
|
+
* union({ cafe: Uint8, bee: Uint8 })
|
|
275
|
+
* // with custom id
|
|
276
|
+
* union({ cafe: Uint8, bee: Uint8 }, { cafe: 0xcafe, bee: 0xbee })
|
|
277
|
+
*/
|
|
278
|
+
export function union(codecLayout, fields) {
|
|
279
|
+
const keys = Object.keys(codecLayout);
|
|
280
|
+
return Codec.from({
|
|
281
|
+
encode({ type, value }) {
|
|
282
|
+
const typeStr = type.toString();
|
|
283
|
+
const codec = codecLayout[typeStr];
|
|
284
|
+
if (!codec) {
|
|
285
|
+
throw new Error(`union: invalid type, expected ${keys.toString()}, but got ${typeStr}`);
|
|
286
|
+
}
|
|
287
|
+
const fieldId = fields ? (fields[typeStr] ?? -1) : keys.indexOf(typeStr);
|
|
288
|
+
if (fieldId < 0) {
|
|
289
|
+
throw new Error(`union: invalid field id ${fieldId} of ${typeStr}`);
|
|
290
|
+
}
|
|
291
|
+
const header = uint32To(fieldId);
|
|
292
|
+
try {
|
|
293
|
+
const body = codec.encode(value);
|
|
294
|
+
return bytesConcat(header, body);
|
|
295
|
+
}
|
|
296
|
+
catch (e) {
|
|
297
|
+
throw new Error(`union.(${typeStr})(${e?.toString()})`);
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
decode(buffer) {
|
|
301
|
+
const value = bytesFrom(buffer);
|
|
302
|
+
const fieldIndex = uint32From(value.slice(0, 4));
|
|
303
|
+
const keys = Object.keys(codecLayout);
|
|
304
|
+
const field = (() => {
|
|
305
|
+
if (!fields) {
|
|
306
|
+
return keys[fieldIndex];
|
|
307
|
+
}
|
|
308
|
+
const entry = Object.entries(fields).find(([, id]) => id === fieldIndex);
|
|
309
|
+
return entry?.[0];
|
|
310
|
+
})();
|
|
311
|
+
if (!field) {
|
|
312
|
+
if (!fields) {
|
|
313
|
+
throw new Error(`union: unknown union field index ${fieldIndex}, only ${keys.toString()} are allowed`);
|
|
314
|
+
}
|
|
315
|
+
const fieldKeys = Object.keys(fields);
|
|
316
|
+
throw new Error(`union: unknown union field index ${fieldIndex}, only ${fieldKeys.toString()} and ${keys.toString()} are allowed`);
|
|
317
|
+
}
|
|
318
|
+
return {
|
|
319
|
+
type: field,
|
|
320
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
321
|
+
value: codecLayout[field].decode(value.slice(4)),
|
|
322
|
+
};
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Struct is a fixed-size type: all fields in struct are fixed-size and it has a fixed quantity of fields.
|
|
328
|
+
* The size of a struct is the sum of all fields' size.
|
|
329
|
+
* @param codecLayout a object contains all fields' codec
|
|
330
|
+
*/
|
|
331
|
+
export function struct(codecLayout) {
|
|
332
|
+
const codecArray = Object.values(codecLayout);
|
|
333
|
+
if (codecArray.some((codec) => codec.byteLength === undefined)) {
|
|
334
|
+
throw new Error("struct: all fields must be fixed-size");
|
|
335
|
+
}
|
|
336
|
+
const keys = Object.keys(codecLayout);
|
|
337
|
+
return Codec.from({
|
|
338
|
+
byteLength: codecArray.reduce((sum, codec) => sum + codec.byteLength, 0),
|
|
339
|
+
encode(object) {
|
|
340
|
+
return keys.reduce((result, key) => {
|
|
341
|
+
try {
|
|
342
|
+
const encodedItem = codecLayout[key].encode(object[key]);
|
|
343
|
+
return bytesConcat(result, encodedItem);
|
|
344
|
+
}
|
|
345
|
+
catch (e) {
|
|
346
|
+
throw new Error(`struct.${key}(${e?.toString()})`);
|
|
347
|
+
}
|
|
348
|
+
}, bytesFrom([]));
|
|
349
|
+
},
|
|
350
|
+
decode(buffer) {
|
|
351
|
+
const value = bytesFrom(buffer);
|
|
352
|
+
const object = {};
|
|
353
|
+
let offset = 0;
|
|
354
|
+
Object.entries(codecLayout).forEach(([key, codec]) => {
|
|
355
|
+
const payload = value.slice(offset, offset + codec.byteLength);
|
|
356
|
+
try {
|
|
357
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
358
|
+
Object.assign(object, { [key]: codec.decode(payload) });
|
|
359
|
+
}
|
|
360
|
+
catch (e) {
|
|
361
|
+
throw new Error(`struct.${key}(${e.toString()})`);
|
|
362
|
+
}
|
|
363
|
+
offset = offset + codec.byteLength;
|
|
364
|
+
});
|
|
365
|
+
return object;
|
|
366
|
+
},
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* The array is a fixed-size type: it has a fixed-size inner type and a fixed length.
|
|
371
|
+
* The size of an array is the size of inner type times the length.
|
|
372
|
+
* @param itemCodec the fixed-size array item codec
|
|
373
|
+
* @param itemCount
|
|
374
|
+
*/
|
|
375
|
+
export function array(itemCodec, itemCount) {
|
|
376
|
+
if (itemCodec.byteLength === undefined) {
|
|
377
|
+
throw new Error("array: itemCodec requires a byte length");
|
|
378
|
+
}
|
|
379
|
+
const byteLength = itemCodec.byteLength * itemCount;
|
|
380
|
+
return Codec.from({
|
|
381
|
+
byteLength,
|
|
382
|
+
encode(items) {
|
|
383
|
+
try {
|
|
384
|
+
return items.reduce((concatted, item) => bytesConcat(concatted, itemCodec.encode(item)), bytesFrom([]));
|
|
385
|
+
}
|
|
386
|
+
catch (e) {
|
|
387
|
+
throw new Error(`array(${e?.toString()})`);
|
|
388
|
+
}
|
|
389
|
+
},
|
|
390
|
+
decode(buffer) {
|
|
391
|
+
const value = bytesFrom(buffer);
|
|
392
|
+
if (value.byteLength != byteLength) {
|
|
393
|
+
throw new Error(`array: invalid buffer size, expected ${byteLength}, but got ${value.byteLength}`);
|
|
394
|
+
}
|
|
395
|
+
try {
|
|
396
|
+
const result = [];
|
|
397
|
+
for (let i = 0; i < value.byteLength; i += itemCodec.byteLength) {
|
|
398
|
+
result.push(itemCodec.decode(value.slice(i, i + itemCodec.byteLength)));
|
|
399
|
+
}
|
|
400
|
+
return result;
|
|
401
|
+
}
|
|
402
|
+
catch (e) {
|
|
403
|
+
throw new Error(`array(${e?.toString()})`);
|
|
404
|
+
}
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Create a codec to deal with fixed LE or BE bytes.
|
|
410
|
+
* @param byteLength
|
|
411
|
+
* @param littleEndian
|
|
412
|
+
*/
|
|
413
|
+
export function uint(byteLength, littleEndian = false) {
|
|
414
|
+
return Codec.from({
|
|
415
|
+
byteLength,
|
|
416
|
+
encode: (numLike) => {
|
|
417
|
+
if (littleEndian) {
|
|
418
|
+
return numToBytes(numLike, byteLength);
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
return numBeToBytes(numLike, byteLength);
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
decode: (buffer) => {
|
|
425
|
+
if (littleEndian) {
|
|
426
|
+
return numFromBytes(buffer);
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
return numBeFromBytes(buffer);
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Create a codec to deal with fixed LE or BE bytes.
|
|
436
|
+
* @param byteLength
|
|
437
|
+
* @param littleEndian
|
|
438
|
+
*/
|
|
439
|
+
export function uintNumber(byteLength, littleEndian = false) {
|
|
440
|
+
if (byteLength > 4) {
|
|
441
|
+
throw new Error("uintNumber: byteLength must be less than or equal to 4");
|
|
442
|
+
}
|
|
443
|
+
return uint(byteLength, littleEndian).map({
|
|
444
|
+
outMap: (num) => Number(num),
|
|
445
|
+
});
|
|
446
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/molecule/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as ckb from "../ckb/index.js";
|
|
2
|
+
import { Hex, HexLike } from "../hex/index.js";
|
|
3
|
+
import { Codec } from "./codec.js";
|
|
4
|
+
export declare const Uint8: Codec<import("../barrel.js").NumLike, number>;
|
|
5
|
+
export declare const Uint16LE: Codec<import("../barrel.js").NumLike, number>;
|
|
6
|
+
export declare const Uint16BE: Codec<import("../barrel.js").NumLike, number>;
|
|
7
|
+
export declare const Uint16: Codec<import("../barrel.js").NumLike, number>;
|
|
8
|
+
export declare const Uint32LE: Codec<import("../barrel.js").NumLike, number>;
|
|
9
|
+
export declare const Uint32BE: Codec<import("../barrel.js").NumLike, number>;
|
|
10
|
+
export declare const Uint32: Codec<import("../barrel.js").NumLike, number>;
|
|
11
|
+
export declare const Uint64LE: Codec<import("../barrel.js").NumLike, bigint>;
|
|
12
|
+
export declare const Uint64BE: Codec<import("../barrel.js").NumLike, bigint>;
|
|
13
|
+
export declare const Uint64: Codec<import("../barrel.js").NumLike, bigint>;
|
|
14
|
+
export declare const Uint128LE: Codec<import("../barrel.js").NumLike, bigint>;
|
|
15
|
+
export declare const Uint128BE: Codec<import("../barrel.js").NumLike, bigint>;
|
|
16
|
+
export declare const Uint128: Codec<import("../barrel.js").NumLike, bigint>;
|
|
17
|
+
export declare const Uint256LE: Codec<import("../barrel.js").NumLike, bigint>;
|
|
18
|
+
export declare const Uint256BE: Codec<import("../barrel.js").NumLike, bigint>;
|
|
19
|
+
export declare const Uint256: Codec<import("../barrel.js").NumLike, bigint>;
|
|
20
|
+
export declare const Uint512LE: Codec<import("../barrel.js").NumLike, bigint>;
|
|
21
|
+
export declare const Uint512BE: Codec<import("../barrel.js").NumLike, bigint>;
|
|
22
|
+
export declare const Uint512: Codec<import("../barrel.js").NumLike, bigint>;
|
|
23
|
+
export declare const Uint8Opt: Codec<import("../barrel.js").NumLike | null | undefined, number | undefined>;
|
|
24
|
+
export declare const Uint16Opt: Codec<import("../barrel.js").NumLike | null | undefined, number | undefined>;
|
|
25
|
+
export declare const Uint32Opt: Codec<import("../barrel.js").NumLike | null | undefined, number | undefined>;
|
|
26
|
+
export declare const Uint64Opt: Codec<import("../barrel.js").NumLike | null | undefined, bigint | undefined>;
|
|
27
|
+
export declare const Uint128Opt: Codec<import("../barrel.js").NumLike | null | undefined, bigint | undefined>;
|
|
28
|
+
export declare const Uint256Opt: Codec<import("../barrel.js").NumLike | null | undefined, bigint | undefined>;
|
|
29
|
+
export declare const Uint512Opt: Codec<import("../barrel.js").NumLike | null | undefined, bigint | undefined>;
|
|
30
|
+
export declare const Bytes: Codec<HexLike, Hex>;
|
|
31
|
+
export declare const BytesOpt: Codec<import("../bytes/index.js").BytesLike | null | undefined, `0x${string}` | undefined>;
|
|
32
|
+
export declare const BytesVec: Codec<import("../bytes/index.js").BytesLike[], `0x${string}`[]>;
|
|
33
|
+
export declare const Byte32: Codec<HexLike, Hex>;
|
|
34
|
+
export declare const Byte32Opt: Codec<import("../bytes/index.js").BytesLike | null | undefined, `0x${string}` | undefined>;
|
|
35
|
+
export declare const Byte32Vec: Codec<import("../bytes/index.js").BytesLike[], `0x${string}`[]>;
|
|
36
|
+
export declare const String: Codec<string, string>;
|
|
37
|
+
export declare const StringVec: Codec<string[], string[]>;
|
|
38
|
+
export declare const StringOpt: Codec<string | null | undefined, string | undefined>;
|
|
39
|
+
export declare const Hash: Codec<import("../bytes/index.js").BytesLike, `0x${string}`>;
|
|
40
|
+
export declare const HashType: Codec<ckb.HashTypeLike, ckb.HashType>;
|
|
41
|
+
export declare const Script: Codec<ckb.ScriptLike, ckb.Script>;
|
|
42
|
+
export declare const ScriptOpt: Codec<ckb.ScriptLike | null | undefined, ckb.Script | undefined>;
|
|
43
|
+
export declare const OutPoint: Codec<ckb.OutPointLike, ckb.OutPoint>;
|
|
44
|
+
export declare const CellInput: Codec<ckb.CellInputLike, ckb.CellInput>;
|
|
45
|
+
export declare const CellInputVec: Codec<ckb.CellInputLike[], ckb.CellInput[]>;
|
|
46
|
+
export declare const CellOutput: Codec<ckb.CellOutputLike, ckb.CellOutput>;
|
|
47
|
+
export declare const CellOutputVec: Codec<ckb.CellOutputLike[], ckb.CellOutput[]>;
|
|
48
|
+
export declare const DepType: Codec<ckb.DepTypeLike, ckb.DepType>;
|
|
49
|
+
export declare const CellDep: Codec<ckb.CellDepLike, ckb.CellDep>;
|
|
50
|
+
export declare const CellDepVec: Codec<ckb.CellDepLike[], ckb.CellDep[]>;
|
|
51
|
+
export declare const Transaction: Codec<ckb.TransactionLike, ckb.Transaction>;
|
|
52
|
+
//# sourceMappingURL=predefined.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"predefined.d.ts","sourceRoot":"","sources":["../../src/molecule/predefined.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { bytesFrom, bytesTo } from "../bytes/index.js";
|
|
2
|
+
import * as ckb from "../ckb/index.js";
|
|
3
|
+
import { hexFrom } from "../hex/index.js";
|
|
4
|
+
import { byteVec, Codec, option, struct, table, uint, uintNumber, vector, } from "./codec.js";
|
|
5
|
+
export const Uint8 = uintNumber(1, true);
|
|
6
|
+
export const Uint16LE = uintNumber(2, true);
|
|
7
|
+
export const Uint16BE = uintNumber(2);
|
|
8
|
+
export const Uint16 = Uint16LE;
|
|
9
|
+
export const Uint32LE = uintNumber(4, true);
|
|
10
|
+
export const Uint32BE = uintNumber(4);
|
|
11
|
+
export const Uint32 = Uint32LE;
|
|
12
|
+
export const Uint64LE = uint(8, true);
|
|
13
|
+
export const Uint64BE = uint(8);
|
|
14
|
+
export const Uint64 = Uint64LE;
|
|
15
|
+
export const Uint128LE = uint(16, true);
|
|
16
|
+
export const Uint128BE = uint(16);
|
|
17
|
+
export const Uint128 = Uint128LE;
|
|
18
|
+
export const Uint256LE = uint(32, true);
|
|
19
|
+
export const Uint256BE = uint(32);
|
|
20
|
+
export const Uint256 = Uint256LE;
|
|
21
|
+
export const Uint512LE = uint(64, true);
|
|
22
|
+
export const Uint512BE = uint(64);
|
|
23
|
+
export const Uint512 = Uint512LE;
|
|
24
|
+
export const Uint8Opt = option(Uint8);
|
|
25
|
+
export const Uint16Opt = option(Uint16);
|
|
26
|
+
export const Uint32Opt = option(Uint32);
|
|
27
|
+
export const Uint64Opt = option(Uint64);
|
|
28
|
+
export const Uint128Opt = option(Uint128);
|
|
29
|
+
export const Uint256Opt = option(Uint256);
|
|
30
|
+
export const Uint512Opt = option(Uint512);
|
|
31
|
+
export const Bytes = byteVec({
|
|
32
|
+
encode: (value) => bytesFrom(value),
|
|
33
|
+
decode: (buffer) => hexFrom(buffer),
|
|
34
|
+
});
|
|
35
|
+
export const BytesOpt = option(Bytes);
|
|
36
|
+
export const BytesVec = vector(Bytes);
|
|
37
|
+
export const Byte32 = Codec.from({
|
|
38
|
+
byteLength: 32,
|
|
39
|
+
encode: (value) => bytesFrom(value),
|
|
40
|
+
decode: (buffer) => hexFrom(buffer),
|
|
41
|
+
});
|
|
42
|
+
export const Byte32Opt = option(Byte32);
|
|
43
|
+
export const Byte32Vec = vector(Byte32);
|
|
44
|
+
export const String = byteVec({
|
|
45
|
+
encode: (value) => bytesFrom(value, "utf8"),
|
|
46
|
+
decode: (buffer) => bytesTo(buffer, "utf8"),
|
|
47
|
+
});
|
|
48
|
+
export const StringVec = vector(String);
|
|
49
|
+
export const StringOpt = option(String);
|
|
50
|
+
export const Hash = Byte32;
|
|
51
|
+
export const HashType = Codec.from({
|
|
52
|
+
byteLength: 1,
|
|
53
|
+
encode: ckb.hashTypeToBytes,
|
|
54
|
+
decode: ckb.hashTypeFromBytes,
|
|
55
|
+
});
|
|
56
|
+
export const Script = table({
|
|
57
|
+
codeHash: Hash,
|
|
58
|
+
hashType: HashType,
|
|
59
|
+
args: Bytes,
|
|
60
|
+
}).map({ outMap: ckb.Script.from });
|
|
61
|
+
export const ScriptOpt = option(Script);
|
|
62
|
+
export const OutPoint = struct({
|
|
63
|
+
txHash: Hash,
|
|
64
|
+
index: Uint32,
|
|
65
|
+
}).map({ outMap: ckb.OutPoint.from });
|
|
66
|
+
export const CellInput = struct({
|
|
67
|
+
previousOutput: OutPoint,
|
|
68
|
+
since: Uint64,
|
|
69
|
+
}).map({ outMap: ckb.CellInput.from });
|
|
70
|
+
export const CellInputVec = vector(CellInput);
|
|
71
|
+
export const CellOutput = table({
|
|
72
|
+
capacity: Uint64,
|
|
73
|
+
lock: Script,
|
|
74
|
+
type: ScriptOpt,
|
|
75
|
+
}).map({ outMap: ckb.CellOutput.from });
|
|
76
|
+
export const CellOutputVec = vector(CellOutput);
|
|
77
|
+
export const DepType = Codec.from({
|
|
78
|
+
byteLength: 1,
|
|
79
|
+
encode: ckb.depTypeToBytes,
|
|
80
|
+
decode: ckb.depTypeFromBytes,
|
|
81
|
+
});
|
|
82
|
+
export const CellDep = struct({
|
|
83
|
+
outPoint: OutPoint,
|
|
84
|
+
depType: DepType,
|
|
85
|
+
}).map({ outMap: ckb.CellDep.from });
|
|
86
|
+
export const CellDepVec = vector(CellDep);
|
|
87
|
+
export const Transaction = table({
|
|
88
|
+
version: Uint32,
|
|
89
|
+
cellDeps: CellDepVec,
|
|
90
|
+
headerDeps: Byte32Vec,
|
|
91
|
+
inputs: CellInputVec,
|
|
92
|
+
outputs: CellOutputVec,
|
|
93
|
+
outputsData: BytesVec,
|
|
94
|
+
witnesses: BytesVec,
|
|
95
|
+
}).map({ outMap: ckb.Transaction.from });
|
|
@@ -6,6 +6,7 @@ export * from "./fixedPoint/index.js";
|
|
|
6
6
|
export * from "./hasher/index.js";
|
|
7
7
|
export * from "./hex/index.js";
|
|
8
8
|
export * from "./keystore/index.js";
|
|
9
|
+
export * as mol from "./molecule/index.js";
|
|
9
10
|
export * from "./num/index.js";
|
|
10
11
|
export * from "./signer/index.js";
|
|
11
12
|
export * from "./utils/index.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"barrel.d.ts","sourceRoot":"","sources":["../src/barrel.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"barrel.d.ts","sourceRoot":"","sources":["../src/barrel.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAC3C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC"}
|
package/dist.commonjs/barrel.js
CHANGED
|
@@ -10,10 +10,23 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
13
18
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
19
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
20
|
};
|
|
21
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
22
|
+
if (mod && mod.__esModule) return mod;
|
|
23
|
+
var result = {};
|
|
24
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
25
|
+
__setModuleDefault(result, mod);
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
16
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.mol = void 0;
|
|
17
30
|
__exportStar(require("./address/index.js"), exports);
|
|
18
31
|
__exportStar(require("./bytes/index.js"), exports);
|
|
19
32
|
__exportStar(require("./ckb/index.js"), exports);
|
|
@@ -22,6 +35,7 @@ __exportStar(require("./fixedPoint/index.js"), exports);
|
|
|
22
35
|
__exportStar(require("./hasher/index.js"), exports);
|
|
23
36
|
__exportStar(require("./hex/index.js"), exports);
|
|
24
37
|
__exportStar(require("./keystore/index.js"), exports);
|
|
38
|
+
exports.mol = __importStar(require("./molecule/index.js"));
|
|
25
39
|
__exportStar(require("./num/index.js"), exports);
|
|
26
40
|
__exportStar(require("./signer/index.js"), exports);
|
|
27
41
|
__exportStar(require("./utils/index.js"), exports);
|