@ckb-ccc/core 0.1.2 → 1.0.1
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 +14 -0
- package/dist/address/address.advanced.d.ts.map +1 -1
- package/dist/address/address.advanced.js +6 -2
- package/dist/barrel.d.ts +1 -1
- package/dist/barrel.d.ts.map +1 -1
- package/dist/barrel.js +1 -1
- package/dist/bytes/index.d.ts +35 -0
- package/dist/bytes/index.d.ts.map +1 -1
- package/dist/bytes/index.js +60 -4
- package/dist/ckb/advanced.d.ts +0 -1
- package/dist/ckb/advanced.d.ts.map +1 -1
- package/dist/ckb/advanced.js +0 -1
- package/dist/ckb/script.d.ts +17 -58
- package/dist/ckb/script.d.ts.map +1 -1
- package/dist/ckb/script.js +29 -78
- package/dist/ckb/transaction.d.ts +125 -264
- package/dist/ckb/transaction.d.ts.map +1 -1
- package/dist/ckb/transaction.js +146 -357
- package/dist/molecule/barrel.d.ts +4 -0
- package/dist/molecule/barrel.d.ts.map +1 -0
- package/dist/molecule/barrel.js +3 -0
- package/dist/molecule/codec.d.ts +2 -0
- package/dist/molecule/codec.d.ts.map +1 -1
- package/dist/molecule/codec.js +57 -44
- package/dist/molecule/entity.d.ts +119 -0
- package/dist/molecule/entity.d.ts.map +1 -0
- package/dist/molecule/entity.js +134 -0
- package/dist/molecule/index.d.ts +1 -2
- package/dist/molecule/index.d.ts.map +1 -1
- package/dist/molecule/index.js +1 -2
- package/dist/molecule/predefined.d.ts +13 -20
- package/dist/molecule/predefined.d.ts.map +1 -1
- package/dist/molecule/predefined.js +14 -54
- package/dist/num/index.d.ts.map +1 -1
- package/dist/num/index.js +2 -2
- package/dist/signer/btc/verify.js +1 -1
- package/dist/signer/doge/verify.d.ts.map +1 -1
- package/dist/signer/doge/verify.js +3 -1
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist.commonjs/address/address.advanced.d.ts.map +1 -1
- package/dist.commonjs/address/address.advanced.js +6 -2
- package/dist.commonjs/barrel.d.ts +1 -1
- package/dist.commonjs/barrel.d.ts.map +1 -1
- package/dist.commonjs/barrel.js +1 -14
- package/dist.commonjs/bytes/index.d.ts +35 -0
- package/dist.commonjs/bytes/index.d.ts.map +1 -1
- package/dist.commonjs/bytes/index.js +62 -4
- package/dist.commonjs/ckb/advanced.d.ts +0 -1
- package/dist.commonjs/ckb/advanced.d.ts.map +1 -1
- package/dist.commonjs/ckb/advanced.js +0 -14
- package/dist.commonjs/ckb/script.d.ts +17 -58
- package/dist.commonjs/ckb/script.d.ts.map +1 -1
- package/dist.commonjs/ckb/script.js +29 -102
- package/dist.commonjs/ckb/transaction.d.ts +125 -264
- package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
- package/dist.commonjs/ckb/transaction.js +167 -408
- package/dist.commonjs/molecule/barrel.d.ts +4 -0
- package/dist.commonjs/molecule/barrel.d.ts.map +1 -0
- package/dist.commonjs/{ckb/molecule.advanced/index.js → molecule/barrel.js} +3 -8
- package/dist.commonjs/molecule/codec.d.ts +2 -0
- package/dist.commonjs/molecule/codec.d.ts.map +1 -1
- package/dist.commonjs/molecule/codec.js +56 -43
- package/dist.commonjs/molecule/entity.d.ts +119 -0
- package/dist.commonjs/molecule/entity.d.ts.map +1 -0
- package/dist.commonjs/molecule/entity.js +139 -0
- package/dist.commonjs/molecule/index.d.ts +1 -2
- package/dist.commonjs/molecule/index.d.ts.map +1 -1
- package/dist.commonjs/molecule/index.js +13 -4
- package/dist.commonjs/molecule/predefined.d.ts +13 -20
- package/dist.commonjs/molecule/predefined.d.ts.map +1 -1
- package/dist.commonjs/molecule/predefined.js +14 -77
- package/dist.commonjs/num/index.d.ts.map +1 -1
- package/dist.commonjs/num/index.js +2 -2
- package/dist.commonjs/signer/btc/verify.js +1 -1
- package/dist.commonjs/signer/doge/verify.d.ts.map +1 -1
- package/dist.commonjs/signer/doge/verify.js +3 -1
- package/dist.commonjs/utils/index.d.ts +3 -0
- package/dist.commonjs/utils/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/address/address.advanced.ts +6 -2
- package/src/barrel.ts +1 -1
- package/src/bytes/index.ts +70 -6
- package/src/ckb/advanced.ts +0 -1
- package/src/ckb/script.ts +20 -85
- package/src/ckb/transaction.ts +123 -416
- package/src/molecule/barrel.ts +3 -0
- package/src/molecule/codec.ts +82 -66
- package/src/molecule/entity.ts +183 -0
- package/src/molecule/index.ts +1 -2
- package/src/molecule/predefined.ts +20 -69
- package/src/num/index.ts +2 -5
- package/src/signer/btc/verify.ts +1 -1
- package/src/signer/doge/verify.ts +3 -1
- package/src/utils/index.ts +5 -0
- package/dist/ckb/molecule.advanced/generated.d.ts +0 -355
- package/dist/ckb/molecule.advanced/generated.d.ts.map +0 -1
- package/dist/ckb/molecule.advanced/generated.js +0 -1468
- package/dist/ckb/molecule.advanced/index.d.ts +0 -6
- package/dist/ckb/molecule.advanced/index.d.ts.map +0 -1
- package/dist/ckb/molecule.advanced/index.js +0 -7
- package/dist.commonjs/ckb/molecule.advanced/generated.d.ts +0 -355
- package/dist.commonjs/ckb/molecule.advanced/generated.d.ts.map +0 -1
- package/dist.commonjs/ckb/molecule.advanced/generated.js +0 -1535
- package/dist.commonjs/ckb/molecule.advanced/index.d.ts +0 -6
- package/dist.commonjs/ckb/molecule.advanced/index.d.ts.map +0 -1
- package/src/ckb/molecule.advanced/blockchain.mol +0 -118
- package/src/ckb/molecule.advanced/generated.d.ts +0 -444
- package/src/ckb/molecule.advanced/generated.js +0 -1804
- package/src/ckb/molecule.advanced/index.ts +0 -11
package/src/molecule/codec.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
Bytes,
|
|
5
|
+
bytesConcat,
|
|
6
|
+
bytesConcatTo,
|
|
7
|
+
bytesFrom,
|
|
8
|
+
BytesLike,
|
|
9
|
+
} from "../bytes/index.js";
|
|
4
10
|
import {
|
|
5
11
|
Num,
|
|
6
12
|
numBeFromBytes,
|
|
@@ -47,6 +53,18 @@ export class Codec<Encodable, Decoded = Encodable> {
|
|
|
47
53
|
: this.decode(buffer)) as NewDecoded,
|
|
48
54
|
});
|
|
49
55
|
}
|
|
56
|
+
|
|
57
|
+
mapIn<NewEncodable>(
|
|
58
|
+
map: (encodable: NewEncodable) => Encodable,
|
|
59
|
+
): Codec<NewEncodable, Decoded> {
|
|
60
|
+
return this.map({ inMap: map });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
mapOut<NewDecoded>(
|
|
64
|
+
map: (decoded: Decoded) => NewDecoded,
|
|
65
|
+
): Codec<Encodable, NewDecoded> {
|
|
66
|
+
return this.map({ outMap: map });
|
|
67
|
+
}
|
|
50
68
|
}
|
|
51
69
|
|
|
52
70
|
export type EncodableType<T extends CodecLike<any, any>> =
|
|
@@ -77,10 +95,12 @@ export function fixedItemVec<Encodable, Decoded>(
|
|
|
77
95
|
return Codec.from({
|
|
78
96
|
encode(userDefinedItems) {
|
|
79
97
|
try {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
98
|
+
const concatted: number[] = [];
|
|
99
|
+
bytesConcatTo(concatted, uint32To(userDefinedItems.length));
|
|
100
|
+
for (const item of userDefinedItems) {
|
|
101
|
+
bytesConcatTo(concatted, itemCodec.encode(item));
|
|
102
|
+
}
|
|
103
|
+
return bytesFrom(concatted);
|
|
84
104
|
} catch (e: unknown) {
|
|
85
105
|
throw new Error(`fixedItemVec(${e?.toString()})`);
|
|
86
106
|
}
|
|
@@ -102,7 +122,7 @@ export function fixedItemVec<Encodable, Decoded>(
|
|
|
102
122
|
|
|
103
123
|
try {
|
|
104
124
|
const decodedArray: Array<Decoded> = [];
|
|
105
|
-
for (let offset =
|
|
125
|
+
for (let offset = 4; offset < byteLength; offset += itemByteLength) {
|
|
106
126
|
decodedArray.push(
|
|
107
127
|
itemCodec.decode(value.slice(offset, offset + itemByteLength)),
|
|
108
128
|
);
|
|
@@ -125,42 +145,39 @@ export function dynItemVec<Encodable, Decoded>(
|
|
|
125
145
|
return Codec.from({
|
|
126
146
|
encode(userDefinedItems) {
|
|
127
147
|
try {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
offset: 4 + userDefinedItems.length * 4,
|
|
142
|
-
},
|
|
143
|
-
);
|
|
144
|
-
const packedTotalSize = uint32To(
|
|
145
|
-
encoded.header.byteLength + encoded.body.byteLength + 4,
|
|
146
|
-
);
|
|
147
|
-
return bytesConcat(packedTotalSize, encoded.header, encoded.body);
|
|
148
|
+
let offset = 4 + userDefinedItems.length * 4;
|
|
149
|
+
const header: number[] = [];
|
|
150
|
+
const body: number[] = [];
|
|
151
|
+
|
|
152
|
+
for (const item of userDefinedItems) {
|
|
153
|
+
const encoded = itemCodec.encode(item);
|
|
154
|
+
bytesConcatTo(header, uint32To(offset));
|
|
155
|
+
bytesConcatTo(body, encoded);
|
|
156
|
+
offset += encoded.byteLength;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const packedTotalSize = uint32To(header.length + body.length + 4);
|
|
160
|
+
return bytesConcat(packedTotalSize, header, body);
|
|
148
161
|
} catch (e) {
|
|
149
162
|
throw new Error(`dynItemVec(${e?.toString()})`);
|
|
150
163
|
}
|
|
151
164
|
},
|
|
152
165
|
decode(buffer) {
|
|
153
166
|
const value = bytesFrom(buffer);
|
|
167
|
+
if (value.byteLength < 4) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`dynItemVec: too short buffer, expected at least 4 bytes, but got ${value.byteLength}`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
154
172
|
const byteLength = uint32From(value.slice(0, 4));
|
|
155
173
|
if (byteLength !== value.byteLength) {
|
|
156
174
|
throw new Error(
|
|
157
175
|
`dynItemVec: invalid buffer size, expected ${byteLength}, but got ${value.byteLength}`,
|
|
158
176
|
);
|
|
159
177
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
);
|
|
178
|
+
|
|
179
|
+
if (byteLength === 4) {
|
|
180
|
+
return [];
|
|
164
181
|
}
|
|
165
182
|
|
|
166
183
|
const offset = uint32From(value.slice(4, 8));
|
|
@@ -313,29 +330,22 @@ export function table<
|
|
|
313
330
|
|
|
314
331
|
return Codec.from({
|
|
315
332
|
encode(object) {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
{
|
|
333
|
-
header: bytesFrom([]),
|
|
334
|
-
body: bytesFrom([]),
|
|
335
|
-
offset: headerLength,
|
|
336
|
-
},
|
|
337
|
-
);
|
|
338
|
-
const packedTotalSize = uint32To(header.byteLength + body.byteLength + 4);
|
|
333
|
+
let offset = 4 + keys.length * 4;
|
|
334
|
+
const header: number[] = [];
|
|
335
|
+
const body: number[] = [];
|
|
336
|
+
|
|
337
|
+
for (const key of keys) {
|
|
338
|
+
try {
|
|
339
|
+
const encoded = codecLayout[key].encode((object as any)[key]);
|
|
340
|
+
bytesConcatTo(header, uint32To(offset));
|
|
341
|
+
bytesConcatTo(body, encoded);
|
|
342
|
+
offset += encoded.byteLength;
|
|
343
|
+
} catch (e: unknown) {
|
|
344
|
+
throw new Error(`table.${key}(${e?.toString()})`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const packedTotalSize = uint32To(header.length + body.length + 4);
|
|
339
349
|
return bytesConcat(packedTotalSize, header, body);
|
|
340
350
|
},
|
|
341
351
|
decode(buffer) {
|
|
@@ -478,23 +488,27 @@ export function struct<
|
|
|
478
488
|
Decoded extends DecodedRecord<T>,
|
|
479
489
|
>(codecLayout: T): Codec<Encodable, Decoded> {
|
|
480
490
|
const codecArray = Object.values(codecLayout);
|
|
481
|
-
if (codecArray.some((codec) => codec.byteLength === undefined)) {
|
|
482
|
-
throw new Error("struct: all fields must be fixed-size");
|
|
483
|
-
}
|
|
484
|
-
|
|
485
491
|
const keys = Object.keys(codecLayout);
|
|
486
492
|
|
|
487
493
|
return Codec.from({
|
|
488
|
-
byteLength: codecArray.reduce((
|
|
494
|
+
byteLength: codecArray.reduce((acc, codec) => {
|
|
495
|
+
if (codec.byteLength === undefined) {
|
|
496
|
+
throw new Error("struct: all fields must be fixed-size");
|
|
497
|
+
}
|
|
498
|
+
return acc + codec.byteLength;
|
|
499
|
+
}, 0),
|
|
489
500
|
encode(object) {
|
|
490
|
-
|
|
501
|
+
const bytes: number[] = [];
|
|
502
|
+
for (const key of keys) {
|
|
491
503
|
try {
|
|
492
|
-
const
|
|
493
|
-
|
|
504
|
+
const encoded = codecLayout[key].encode((object as any)[key]);
|
|
505
|
+
bytesConcatTo(bytes, encoded);
|
|
494
506
|
} catch (e: unknown) {
|
|
495
507
|
throw new Error(`struct.${key}(${e?.toString()})`);
|
|
496
508
|
}
|
|
497
|
-
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return bytesFrom(bytes);
|
|
498
512
|
},
|
|
499
513
|
decode(buffer) {
|
|
500
514
|
const value = bytesFrom(buffer);
|
|
@@ -534,10 +548,12 @@ export function array<Encodable, Decoded>(
|
|
|
534
548
|
byteLength,
|
|
535
549
|
encode(items) {
|
|
536
550
|
try {
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
551
|
+
const bytes: number[] = [];
|
|
552
|
+
for (const item of items) {
|
|
553
|
+
bytesConcatTo(bytes, itemCodec.encode(item));
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
return bytesFrom(bytes);
|
|
541
557
|
} catch (e: unknown) {
|
|
542
558
|
throw new Error(`array(${e?.toString()})`);
|
|
543
559
|
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { Bytes, bytesEq, BytesLike } from "../bytes/index.js";
|
|
2
|
+
import { hashCkb } from "../hasher/index.js";
|
|
3
|
+
import { Hex } from "../hex/index.js";
|
|
4
|
+
import { Constructor } from "../utils/index.js";
|
|
5
|
+
import { Codec } from "./codec.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The base class of CCC to create a serializable instance. This should be used with the {@link codec} decorator.
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export abstract class Entity {
|
|
12
|
+
/**
|
|
13
|
+
* Generate a base class of CCC to create a serializable instance.
|
|
14
|
+
* This should be used with the {@link codec} decorator.
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
static Base<SubTypeLike, SubType = SubTypeLike>() {
|
|
18
|
+
abstract class Impl {
|
|
19
|
+
/**
|
|
20
|
+
* The bytes length of the entity, if it is fixed, otherwise undefined
|
|
21
|
+
* @public
|
|
22
|
+
* @static
|
|
23
|
+
*/
|
|
24
|
+
static byteLength?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Encode the entity into bytes
|
|
27
|
+
* @public
|
|
28
|
+
* @static
|
|
29
|
+
* @param _ - The entity to encode
|
|
30
|
+
* @returns The encoded bytes
|
|
31
|
+
* @throws Will throw an error if the entity is not serializable
|
|
32
|
+
*/
|
|
33
|
+
static encode(_: SubTypeLike): Bytes {
|
|
34
|
+
throw new Error(
|
|
35
|
+
"encode not implemented, use @ccc.mol.codec to decorate your type",
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Decode the entity from bytes
|
|
40
|
+
* @public
|
|
41
|
+
* @static
|
|
42
|
+
* @param _ - The bytes to decode
|
|
43
|
+
* @returns The decoded entity
|
|
44
|
+
* @throws Will throw an error if the entity is not serializable
|
|
45
|
+
*/
|
|
46
|
+
static decode(_: BytesLike): SubType {
|
|
47
|
+
throw new Error(
|
|
48
|
+
"decode not implemented, use @ccc.mol.codec to decorate your type",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Create an entity from bytes
|
|
54
|
+
* @public
|
|
55
|
+
* @static
|
|
56
|
+
* @param _ - The bytes to create the entity from
|
|
57
|
+
* @returns The created entity
|
|
58
|
+
* @throws Will throw an error if the entity is not serializable
|
|
59
|
+
*/
|
|
60
|
+
static fromBytes(_bytes: BytesLike): SubType {
|
|
61
|
+
throw new Error(
|
|
62
|
+
"fromBytes not implemented, use @ccc.mol.codec to decorate your type",
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Create an entity from a serializable object
|
|
68
|
+
* @public
|
|
69
|
+
* @static
|
|
70
|
+
* @param _ - The serializable object to create the entity from
|
|
71
|
+
* @returns The created entity
|
|
72
|
+
* @throws Will throw an error if the entity is not serializable
|
|
73
|
+
*/
|
|
74
|
+
static from(_: SubTypeLike): SubType {
|
|
75
|
+
throw new Error("from not implemented");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Convert the entity to bytes
|
|
80
|
+
* @public
|
|
81
|
+
* @returns The bytes representation of the entity
|
|
82
|
+
*/
|
|
83
|
+
toBytes(): Bytes {
|
|
84
|
+
return (this.constructor as typeof Impl).encode(
|
|
85
|
+
this as unknown as SubTypeLike,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Create a clone of the entity
|
|
91
|
+
* @public
|
|
92
|
+
* @returns A clone of the entity
|
|
93
|
+
*/
|
|
94
|
+
clone(): SubType {
|
|
95
|
+
return (this.constructor as typeof Impl).fromBytes(
|
|
96
|
+
this.toBytes(),
|
|
97
|
+
) as unknown as SubType;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Check if the entity is equal to another entity
|
|
102
|
+
* @public
|
|
103
|
+
* @param other - The other entity to compare with
|
|
104
|
+
* @returns True if the entities are equal, false otherwise
|
|
105
|
+
*/
|
|
106
|
+
eq(other: SubTypeLike): boolean {
|
|
107
|
+
if (this === (other as unknown as this)) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return bytesEq(
|
|
112
|
+
this.toBytes(),
|
|
113
|
+
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any */
|
|
114
|
+
(
|
|
115
|
+
((this.constructor as any)?.from(other) ?? other) as unknown as Impl
|
|
116
|
+
).toBytes(),
|
|
117
|
+
/* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any */
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Calculate the hash of the entity
|
|
123
|
+
* @public
|
|
124
|
+
* @returns The hash of the entity
|
|
125
|
+
*/
|
|
126
|
+
hash(): Hex {
|
|
127
|
+
return hashCkb(this.toBytes());
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return Impl;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
abstract toBytes(): Bytes;
|
|
135
|
+
abstract hash(): Hex;
|
|
136
|
+
abstract clone(): Entity;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A class decorator to add methods implementation on the {@link Entity.Base} class
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* @mol.codec(
|
|
144
|
+
* mol.table({
|
|
145
|
+
* codeHash: mol.Byte32,
|
|
146
|
+
* hashType: HashTypeCodec,
|
|
147
|
+
* args: mol.Bytes,
|
|
148
|
+
* }),
|
|
149
|
+
* )
|
|
150
|
+
* export class Script extends mol.Entity.Base<ScriptLike, Script>() {
|
|
151
|
+
* from(scriptLike: ScriptLike): Script {}
|
|
152
|
+
* }
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
export function codec<
|
|
156
|
+
Encodable,
|
|
157
|
+
TypeLike extends Encodable,
|
|
158
|
+
Decoded extends TypeLike,
|
|
159
|
+
Type extends object & TypeLike,
|
|
160
|
+
ConstructorType extends Constructor<Type> & {
|
|
161
|
+
from(decoded: TypeLike): Type;
|
|
162
|
+
byteLength?: number;
|
|
163
|
+
encode(encodable: TypeLike): Bytes;
|
|
164
|
+
decode(bytesLike: BytesLike): TypeLike;
|
|
165
|
+
fromBytes(bytes: BytesLike): Type;
|
|
166
|
+
},
|
|
167
|
+
>(codec: Codec<Encodable, Decoded>) {
|
|
168
|
+
return function (Constructor: ConstructorType) {
|
|
169
|
+
return class Extended extends Constructor {
|
|
170
|
+
static byteLength = codec.byteLength;
|
|
171
|
+
static encode(encodable: TypeLike): Bytes {
|
|
172
|
+
return codec.encode(encodable);
|
|
173
|
+
}
|
|
174
|
+
static decode(bytesLike: BytesLike): Type {
|
|
175
|
+
return Constructor.from(codec.decode(bytesLike));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
static fromBytes(bytes: BytesLike): Type {
|
|
179
|
+
return Constructor.from(codec.decode(bytes));
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
}
|
package/src/molecule/index.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./predefined.js";
|
|
1
|
+
export * as mol from "./barrel.js";
|
|
@@ -1,44 +1,46 @@
|
|
|
1
1
|
import { bytesFrom, bytesTo } from "../bytes/index.js";
|
|
2
|
-
import * as ckb from "../ckb/index.js";
|
|
3
2
|
import { Hex, hexFrom, HexLike } from "../hex/index.js";
|
|
4
|
-
import {
|
|
5
|
-
byteVec,
|
|
6
|
-
Codec,
|
|
7
|
-
option,
|
|
8
|
-
struct,
|
|
9
|
-
table,
|
|
10
|
-
uint,
|
|
11
|
-
uintNumber,
|
|
12
|
-
vector,
|
|
13
|
-
} from "./codec.js";
|
|
3
|
+
import { byteVec, Codec, option, uint, uintNumber, vector } from "./codec.js";
|
|
14
4
|
|
|
15
5
|
export const Uint8 = uintNumber(1, true);
|
|
6
|
+
export const Uint8Opt = option(Uint8);
|
|
7
|
+
export const Uint8Vec = vector(Uint8);
|
|
8
|
+
|
|
16
9
|
export const Uint16LE = uintNumber(2, true);
|
|
17
10
|
export const Uint16BE = uintNumber(2);
|
|
18
11
|
export const Uint16 = Uint16LE;
|
|
12
|
+
export const Uint16Opt = option(Uint16);
|
|
13
|
+
export const Uint16Vec = vector(Uint16);
|
|
14
|
+
|
|
19
15
|
export const Uint32LE = uintNumber(4, true);
|
|
20
16
|
export const Uint32BE = uintNumber(4);
|
|
21
17
|
export const Uint32 = Uint32LE;
|
|
18
|
+
export const Uint32Opt = option(Uint32);
|
|
19
|
+
export const Uint32Vec = vector(Uint32);
|
|
20
|
+
|
|
22
21
|
export const Uint64LE = uint(8, true);
|
|
23
22
|
export const Uint64BE = uint(8);
|
|
24
23
|
export const Uint64 = Uint64LE;
|
|
24
|
+
export const Uint64Opt = option(Uint64);
|
|
25
|
+
export const Uint64Vec = vector(Uint64);
|
|
26
|
+
|
|
25
27
|
export const Uint128LE = uint(16, true);
|
|
26
28
|
export const Uint128BE = uint(16);
|
|
27
29
|
export const Uint128 = Uint128LE;
|
|
30
|
+
export const Uint128Opt = option(Uint128);
|
|
31
|
+
export const Uint128Vec = vector(Uint128);
|
|
32
|
+
|
|
28
33
|
export const Uint256LE = uint(32, true);
|
|
29
34
|
export const Uint256BE = uint(32);
|
|
30
35
|
export const Uint256 = Uint256LE;
|
|
36
|
+
export const Uint256Opt = option(Uint256);
|
|
37
|
+
export const Uint256Vec = vector(Uint256);
|
|
38
|
+
|
|
31
39
|
export const Uint512LE = uint(64, true);
|
|
32
40
|
export const Uint512BE = uint(64);
|
|
33
41
|
export const Uint512 = Uint512LE;
|
|
34
|
-
|
|
35
|
-
export const Uint8Opt = option(Uint8);
|
|
36
|
-
export const Uint16Opt = option(Uint16);
|
|
37
|
-
export const Uint32Opt = option(Uint32);
|
|
38
|
-
export const Uint64Opt = option(Uint64);
|
|
39
|
-
export const Uint128Opt = option(Uint128);
|
|
40
|
-
export const Uint256Opt = option(Uint256);
|
|
41
42
|
export const Uint512Opt = option(Uint512);
|
|
43
|
+
export const Uint512Vec = vector(Uint512);
|
|
42
44
|
|
|
43
45
|
export const Bytes: Codec<HexLike, Hex> = byteVec({
|
|
44
46
|
encode: (value) => bytesFrom(value),
|
|
@@ -61,54 +63,3 @@ export const String = byteVec({
|
|
|
61
63
|
});
|
|
62
64
|
export const StringVec = vector(String);
|
|
63
65
|
export const StringOpt = option(String);
|
|
64
|
-
|
|
65
|
-
export const Hash = Byte32;
|
|
66
|
-
export const HashType: Codec<ckb.HashTypeLike, ckb.HashType> = Codec.from({
|
|
67
|
-
byteLength: 1,
|
|
68
|
-
encode: ckb.hashTypeToBytes,
|
|
69
|
-
decode: ckb.hashTypeFromBytes,
|
|
70
|
-
});
|
|
71
|
-
export const Script: Codec<ckb.ScriptLike, ckb.Script> = table({
|
|
72
|
-
codeHash: Hash,
|
|
73
|
-
hashType: HashType,
|
|
74
|
-
args: Bytes,
|
|
75
|
-
}).map({ outMap: ckb.Script.from });
|
|
76
|
-
export const ScriptOpt = option(Script);
|
|
77
|
-
|
|
78
|
-
export const OutPoint: Codec<ckb.OutPointLike, ckb.OutPoint> = struct({
|
|
79
|
-
txHash: Hash,
|
|
80
|
-
index: Uint32,
|
|
81
|
-
}).map({ outMap: ckb.OutPoint.from });
|
|
82
|
-
export const CellInput: Codec<ckb.CellInputLike, ckb.CellInput> = struct({
|
|
83
|
-
previousOutput: OutPoint,
|
|
84
|
-
since: Uint64,
|
|
85
|
-
}).map({ outMap: ckb.CellInput.from });
|
|
86
|
-
export const CellInputVec = vector(CellInput);
|
|
87
|
-
|
|
88
|
-
export const CellOutput: Codec<ckb.CellOutputLike, ckb.CellOutput> = table({
|
|
89
|
-
capacity: Uint64,
|
|
90
|
-
lock: Script,
|
|
91
|
-
type: ScriptOpt,
|
|
92
|
-
}).map({ outMap: ckb.CellOutput.from });
|
|
93
|
-
export const CellOutputVec = vector(CellOutput);
|
|
94
|
-
|
|
95
|
-
export const DepType: Codec<ckb.DepTypeLike, ckb.DepType> = Codec.from({
|
|
96
|
-
byteLength: 1,
|
|
97
|
-
encode: ckb.depTypeToBytes,
|
|
98
|
-
decode: ckb.depTypeFromBytes,
|
|
99
|
-
});
|
|
100
|
-
export const CellDep: Codec<ckb.CellDepLike, ckb.CellDep> = struct({
|
|
101
|
-
outPoint: OutPoint,
|
|
102
|
-
depType: DepType,
|
|
103
|
-
}).map({ outMap: ckb.CellDep.from });
|
|
104
|
-
export const CellDepVec = vector(CellDep);
|
|
105
|
-
|
|
106
|
-
export const Transaction: Codec<ckb.TransactionLike, ckb.Transaction> = table({
|
|
107
|
-
version: Uint32,
|
|
108
|
-
cellDeps: CellDepVec,
|
|
109
|
-
headerDeps: Byte32Vec,
|
|
110
|
-
inputs: CellInputVec,
|
|
111
|
-
outputs: CellOutputVec,
|
|
112
|
-
outputsData: BytesVec,
|
|
113
|
-
witnesses: BytesVec,
|
|
114
|
-
}).map({ outMap: ckb.Transaction.from });
|
package/src/num/index.ts
CHANGED
|
@@ -155,10 +155,7 @@ export function numBeToBytes(val: NumLike, bytes?: number): Bytes {
|
|
|
155
155
|
return rawBytes;
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
return bytesConcat(
|
|
159
|
-
Array.from(Array(bytes - rawBytes.length), () => 0),
|
|
160
|
-
rawBytes,
|
|
161
|
-
);
|
|
158
|
+
return bytesConcat("00".repeat(bytes - rawBytes.length), rawBytes);
|
|
162
159
|
}
|
|
163
160
|
|
|
164
161
|
/**
|
|
@@ -190,7 +187,7 @@ export function numFromBytes(val: BytesLike): Num {
|
|
|
190
187
|
* ```
|
|
191
188
|
*/
|
|
192
189
|
export function numLeFromBytes(val: BytesLike): Num {
|
|
193
|
-
return numBeFromBytes(
|
|
190
|
+
return numBeFromBytes(bytesFrom(val).reverse());
|
|
194
191
|
}
|
|
195
192
|
|
|
196
193
|
/**
|
package/src/signer/btc/verify.ts
CHANGED
|
@@ -43,7 +43,7 @@ export function verifyMessageBtcEcdsa(
|
|
|
43
43
|
const challenge =
|
|
44
44
|
typeof message === "string" ? message : hexFrom(message).slice(2);
|
|
45
45
|
|
|
46
|
-
const
|
|
46
|
+
const rawSign = bytesFrom(signature, "base64").slice(1);
|
|
47
47
|
|
|
48
48
|
return secp256k1.verify(bytesFrom(rawSign), magicHash(challenge), publicKey);
|
|
49
49
|
}
|
|
@@ -17,7 +17,9 @@ export function verifyMessageDogeEcdsa(
|
|
|
17
17
|
): boolean {
|
|
18
18
|
const challenge =
|
|
19
19
|
typeof message === "string" ? message : hexFrom(message).slice(2);
|
|
20
|
-
const
|
|
20
|
+
const signatureBytes = bytesFrom(signature, "base64");
|
|
21
|
+
const recoveryBit = signatureBytes[0];
|
|
22
|
+
const rawSign = signatureBytes.slice(1);
|
|
21
23
|
|
|
22
24
|
const sig = secp256k1.Signature.fromCompact(
|
|
23
25
|
hexFrom(rawSign).slice(2),
|
package/src/utils/index.ts
CHANGED
|
@@ -172,6 +172,11 @@ export function sleep(ms: NumLike) {
|
|
|
172
172
|
return new Promise((resolve) => setTimeout(resolve, Number(numFrom(ms))));
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
+
export type Constructor<T> = {
|
|
176
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
177
|
+
new (...args: any[]): T;
|
|
178
|
+
};
|
|
179
|
+
|
|
175
180
|
/**
|
|
176
181
|
* @public
|
|
177
182
|
*/
|