@ckb-ccc/core 1.0.0 → 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.
Files changed (55) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/address/address.advanced.d.ts.map +1 -1
  3. package/dist/address/address.advanced.js +6 -2
  4. package/dist/bytes/index.d.ts +21 -0
  5. package/dist/bytes/index.d.ts.map +1 -1
  6. package/dist/bytes/index.js +31 -4
  7. package/dist/ckb/script.d.ts +1 -1
  8. package/dist/ckb/script.d.ts.map +1 -1
  9. package/dist/ckb/script.js +2 -4
  10. package/dist/ckb/transaction.d.ts +19 -12
  11. package/dist/ckb/transaction.d.ts.map +1 -1
  12. package/dist/ckb/transaction.js +23 -38
  13. package/dist/molecule/codec.d.ts +2 -0
  14. package/dist/molecule/codec.d.ts.map +1 -1
  15. package/dist/molecule/codec.js +51 -41
  16. package/dist/molecule/entity.d.ts +25 -4
  17. package/dist/molecule/entity.d.ts.map +1 -1
  18. package/dist/molecule/entity.js +22 -1
  19. package/dist/num/index.d.ts.map +1 -1
  20. package/dist/num/index.js +2 -2
  21. package/dist/signer/btc/verify.js +1 -1
  22. package/dist/signer/doge/verify.d.ts.map +1 -1
  23. package/dist/signer/doge/verify.js +3 -1
  24. package/dist.commonjs/address/address.advanced.d.ts.map +1 -1
  25. package/dist.commonjs/address/address.advanced.js +6 -2
  26. package/dist.commonjs/bytes/index.d.ts +21 -0
  27. package/dist.commonjs/bytes/index.d.ts.map +1 -1
  28. package/dist.commonjs/bytes/index.js +32 -4
  29. package/dist.commonjs/ckb/script.d.ts +1 -1
  30. package/dist.commonjs/ckb/script.d.ts.map +1 -1
  31. package/dist.commonjs/ckb/script.js +2 -4
  32. package/dist.commonjs/ckb/transaction.d.ts +19 -12
  33. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  34. package/dist.commonjs/ckb/transaction.js +23 -38
  35. package/dist.commonjs/molecule/codec.d.ts +2 -0
  36. package/dist.commonjs/molecule/codec.d.ts.map +1 -1
  37. package/dist.commonjs/molecule/codec.js +50 -40
  38. package/dist.commonjs/molecule/entity.d.ts +25 -4
  39. package/dist.commonjs/molecule/entity.d.ts.map +1 -1
  40. package/dist.commonjs/molecule/entity.js +22 -1
  41. package/dist.commonjs/num/index.d.ts.map +1 -1
  42. package/dist.commonjs/num/index.js +2 -2
  43. package/dist.commonjs/signer/btc/verify.js +1 -1
  44. package/dist.commonjs/signer/doge/verify.d.ts.map +1 -1
  45. package/dist.commonjs/signer/doge/verify.js +3 -1
  46. package/package.json +1 -1
  47. package/src/address/address.advanced.ts +6 -2
  48. package/src/bytes/index.ts +36 -6
  49. package/src/ckb/script.ts +5 -7
  50. package/src/ckb/transaction.ts +43 -58
  51. package/src/molecule/codec.ts +73 -61
  52. package/src/molecule/entity.ts +25 -5
  53. package/src/num/index.ts +2 -5
  54. package/src/signer/btc/verify.ts +1 -1
  55. package/src/signer/doge/verify.ts +3 -1
@@ -33,6 +33,12 @@ class Codec {
33
33
  : this.decode(buffer)),
34
34
  });
35
35
  }
36
+ mapIn(map) {
37
+ return this.map({ inMap: map });
38
+ }
39
+ mapOut(map) {
40
+ return this.map({ outMap: map });
41
+ }
36
42
  }
37
43
  exports.Codec = Codec;
38
44
  function uint32To(numLike) {
@@ -53,7 +59,12 @@ function fixedItemVec(itemCodec) {
53
59
  return Codec.from({
54
60
  encode(userDefinedItems) {
55
61
  try {
56
- return userDefinedItems.reduce((concatted, item) => (0, index_js_1.bytesConcat)(concatted, itemCodec.encode(item)), uint32To(userDefinedItems.length));
62
+ const concatted = [];
63
+ (0, index_js_1.bytesConcatTo)(concatted, uint32To(userDefinedItems.length));
64
+ for (const item of userDefinedItems) {
65
+ (0, index_js_1.bytesConcatTo)(concatted, itemCodec.encode(item));
66
+ }
67
+ return (0, index_js_1.bytesFrom)(concatted);
57
68
  }
58
69
  catch (e) {
59
70
  throw new Error(`fixedItemVec(${e?.toString()})`);
@@ -90,21 +101,17 @@ function dynItemVec(itemCodec) {
90
101
  return Codec.from({
91
102
  encode(userDefinedItems) {
92
103
  try {
93
- const encoded = userDefinedItems.reduce(({ offset, header, body }, item) => {
94
- const encodedItem = itemCodec.encode(item);
95
- const packedHeader = uint32To(offset);
96
- return {
97
- header: (0, index_js_1.bytesConcat)(header, packedHeader),
98
- body: (0, index_js_1.bytesConcat)(body, encodedItem),
99
- offset: offset + (0, index_js_1.bytesFrom)(encodedItem).byteLength,
100
- };
101
- }, {
102
- header: (0, index_js_1.bytesFrom)([]),
103
- body: (0, index_js_1.bytesFrom)([]),
104
- offset: 4 + userDefinedItems.length * 4,
105
- });
106
- const packedTotalSize = uint32To(encoded.header.byteLength + encoded.body.byteLength + 4);
107
- return (0, index_js_1.bytesConcat)(packedTotalSize, encoded.header, encoded.body);
104
+ let offset = 4 + userDefinedItems.length * 4;
105
+ const header = [];
106
+ const body = [];
107
+ for (const item of userDefinedItems) {
108
+ const encoded = itemCodec.encode(item);
109
+ (0, index_js_1.bytesConcatTo)(header, uint32To(offset));
110
+ (0, index_js_1.bytesConcatTo)(body, encoded);
111
+ offset += encoded.byteLength;
112
+ }
113
+ const packedTotalSize = uint32To(header.length + body.length + 4);
114
+ return (0, index_js_1.bytesConcat)(packedTotalSize, header, body);
108
115
  }
109
116
  catch (e) {
110
117
  throw new Error(`dynItemVec(${e?.toString()})`);
@@ -228,26 +235,21 @@ function table(codecLayout) {
228
235
  const keys = Object.keys(codecLayout);
229
236
  return Codec.from({
230
237
  encode(object) {
231
- const headerLength = 4 + keys.length * 4;
232
- const { header, body } = keys.reduce((result, key) => {
238
+ let offset = 4 + keys.length * 4;
239
+ const header = [];
240
+ const body = [];
241
+ for (const key of keys) {
233
242
  try {
234
- const encodedItem = codecLayout[key].encode(object[key]);
235
- const packedOffset = uint32To(result.offset);
236
- return {
237
- header: (0, index_js_1.bytesConcat)(result.header, packedOffset),
238
- body: (0, index_js_1.bytesConcat)(result.body, encodedItem),
239
- offset: result.offset + (0, index_js_1.bytesFrom)(encodedItem).byteLength,
240
- };
243
+ const encoded = codecLayout[key].encode(object[key]);
244
+ (0, index_js_1.bytesConcatTo)(header, uint32To(offset));
245
+ (0, index_js_1.bytesConcatTo)(body, encoded);
246
+ offset += encoded.byteLength;
241
247
  }
242
248
  catch (e) {
243
249
  throw new Error(`table.${key}(${e?.toString()})`);
244
250
  }
245
- }, {
246
- header: (0, index_js_1.bytesFrom)([]),
247
- body: (0, index_js_1.bytesFrom)([]),
248
- offset: headerLength,
249
- });
250
- const packedTotalSize = uint32To(header.byteLength + body.byteLength + 4);
251
+ }
252
+ const packedTotalSize = uint32To(header.length + body.length + 4);
251
253
  return (0, index_js_1.bytesConcat)(packedTotalSize, header, body);
252
254
  },
253
255
  decode(buffer) {
@@ -348,22 +350,26 @@ function union(codecLayout, fields) {
348
350
  */
349
351
  function struct(codecLayout) {
350
352
  const codecArray = Object.values(codecLayout);
351
- if (codecArray.some((codec) => codec.byteLength === undefined)) {
352
- throw new Error("struct: all fields must be fixed-size");
353
- }
354
353
  const keys = Object.keys(codecLayout);
355
354
  return Codec.from({
356
- byteLength: codecArray.reduce((sum, codec) => sum + codec.byteLength, 0),
355
+ byteLength: codecArray.reduce((acc, codec) => {
356
+ if (codec.byteLength === undefined) {
357
+ throw new Error("struct: all fields must be fixed-size");
358
+ }
359
+ return acc + codec.byteLength;
360
+ }, 0),
357
361
  encode(object) {
358
- return keys.reduce((result, key) => {
362
+ const bytes = [];
363
+ for (const key of keys) {
359
364
  try {
360
- const encodedItem = codecLayout[key].encode(object[key]);
361
- return (0, index_js_1.bytesConcat)(result, encodedItem);
365
+ const encoded = codecLayout[key].encode(object[key]);
366
+ (0, index_js_1.bytesConcatTo)(bytes, encoded);
362
367
  }
363
368
  catch (e) {
364
369
  throw new Error(`struct.${key}(${e?.toString()})`);
365
370
  }
366
- }, (0, index_js_1.bytesFrom)([]));
371
+ }
372
+ return (0, index_js_1.bytesFrom)(bytes);
367
373
  },
368
374
  decode(buffer) {
369
375
  const value = (0, index_js_1.bytesFrom)(buffer);
@@ -399,7 +405,11 @@ function array(itemCodec, itemCount) {
399
405
  byteLength,
400
406
  encode(items) {
401
407
  try {
402
- return items.reduce((concatted, item) => (0, index_js_1.bytesConcat)(concatted, itemCodec.encode(item)), (0, index_js_1.bytesFrom)([]));
408
+ const bytes = [];
409
+ for (const item of items) {
410
+ (0, index_js_1.bytesConcatTo)(bytes, itemCodec.encode(item));
411
+ }
412
+ return (0, index_js_1.bytesFrom)(bytes);
403
413
  }
404
414
  catch (e) {
405
415
  throw new Error(`array(${e?.toString()})`);
@@ -1,11 +1,17 @@
1
1
  import { Bytes, BytesLike } from "../bytes/index.js";
2
2
  import { Hex } from "../hex/index.js";
3
+ import { Constructor } from "../utils/index.js";
3
4
  import { Codec } from "./codec.js";
4
5
  /**
5
- * The base class of CCC to create a serializable instance
6
+ * The base class of CCC to create a serializable instance. This should be used with the {@link codec} decorator.
6
7
  * @public
7
8
  */
8
9
  export declare abstract class Entity {
10
+ /**
11
+ * Generate a base class of CCC to create a serializable instance.
12
+ * This should be used with the {@link codec} decorator.
13
+ * @public
14
+ */
9
15
  static Base<SubTypeLike, SubType = SubTypeLike>(): (abstract new () => {
10
16
  /**
11
17
  * Convert the entity to bytes
@@ -25,7 +31,7 @@ export declare abstract class Entity {
25
31
  * @param other - The other entity to compare with
26
32
  * @returns True if the entities are equal, false otherwise
27
33
  */
28
- eq(other: SubTypeLike | SubType): boolean;
34
+ eq(other: SubTypeLike): boolean;
29
35
  /**
30
36
  * Calculate the hash of the entity
31
37
  * @public
@@ -80,8 +86,23 @@ export declare abstract class Entity {
80
86
  abstract hash(): Hex;
81
87
  abstract clone(): Entity;
82
88
  }
83
- export declare function codec<Encodable, TypeLike extends Encodable, Decoded extends TypeLike, Type extends object & TypeLike, ConstructorType extends {
84
- new (...args: any[]): Type;
89
+ /**
90
+ * A class decorator to add methods implementation on the {@link Entity.Base} class
91
+ * @example
92
+ * ```typescript
93
+ * @mol.codec(
94
+ * mol.table({
95
+ * codeHash: mol.Byte32,
96
+ * hashType: HashTypeCodec,
97
+ * args: mol.Bytes,
98
+ * }),
99
+ * )
100
+ * export class Script extends mol.Entity.Base<ScriptLike, Script>() {
101
+ * from(scriptLike: ScriptLike): Script {}
102
+ * }
103
+ * ```
104
+ */
105
+ export declare function codec<Encodable, TypeLike extends Encodable, Decoded extends TypeLike, Type extends object & TypeLike, ConstructorType extends Constructor<Type> & {
85
106
  from(decoded: TypeLike): Type;
86
107
  byteLength?: number;
87
108
  encode(encodable: TypeLike): Bytes;
@@ -1 +1 @@
1
- {"version":3,"file":"entity.d.ts","sourceRoot":"","sources":["../../src/molecule/entity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAW,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9D,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;GAGG;AACH,8BAAsB,MAAM;IAC1B,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,OAAO,GAAG,WAAW;QA6D1C;;;;WAIG;mBACQ,KAAK;QAMhB;;;;WAIG;iBACM,OAAO;QAMhB;;;;;WAKG;kBACO,WAAW,GAAG,OAAO,GAAG,OAAO;QAezC;;;;WAIG;gBACK,GAAG;;QA3GX;;;;WAIG;qBACiB,MAAM;QAC1B;;;;;;;WAOG;kBACc,WAAW,GAAG,KAAK;QAKpC;;;;;;;WAOG;kBACc,SAAS,GAAG,OAAO;QAMpC;;;;;;;WAOG;0BACsB,SAAS,GAAG,OAAO;QAM5C;;;;;;;WAOG;gBACY,WAAW,GAAG,OAAO;;IA4DxC,QAAQ,CAAC,OAAO,IAAI,KAAK;IACzB,QAAQ,CAAC,IAAI,IAAI,GAAG;IACpB,QAAQ,CAAC,KAAK,IAAI,MAAM;CACzB;AAED,wBAAgB,KAAK,CACnB,SAAS,EACT,QAAQ,SAAS,SAAS,EAC1B,OAAO,SAAS,Q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,GAAG,OAAO;QAMpC;;;;;;;WAOG;0BACsB,SAAS,GAAG,OAAO;QAM5C;;;;;;;WAOG;gBACY,WAAW,GAAG,OAAO;;IA4DxC,QAAQ,CAAC,OAAO,IAAI,KAAK;IACzB,QAAQ,CAAC,IAAI,IAAI,GAAG;IACpB,QAAQ,CAAC,KAAK,IAAI,MAAM;CACzB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,KAAK,CACnB,SAAS,EACT,QAAQ,SAAS,SAAS,EAC1B,OAAO,SAAS,QAAQ,EACxB,IAAI,SAAS,MAAM,GAAG,QAAQ,EAC9B,eAAe,SAAS,WAAW,CAAC,IAAI,CAAC,GAAG;IAC1C,IAAI,CAAC,OAAO,EAAE,QAAQ,GAAG,IAAI,CAAC;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,SAAS,EAAE,QAAQ,GAAG,KAAK,CAAC;IACnC,MAAM,CAAC,SAAS,EAAE,SAAS,GAAG,QAAQ,CAAC;IACvC,SAAS,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;CACnC,EACD,KAAK,EAAE,KAAK,CAAC,SAAS,EAAE,OAAO,CAAC,iBACF,eAAe;;;sBAGhB,QAAQ,GAAG,KAAK;sBAGhB,SAAS,GAAG,IAAI;qBAIjB,SAAS,GAAG,IAAI;kBAjB5B,QAAQ,GAAG,IAAI;oBAsBhC"}
@@ -5,10 +5,15 @@ exports.codec = codec;
5
5
  const index_js_1 = require("../bytes/index.js");
6
6
  const index_js_2 = require("../hasher/index.js");
7
7
  /**
8
- * The base class of CCC to create a serializable instance
8
+ * The base class of CCC to create a serializable instance. This should be used with the {@link codec} decorator.
9
9
  * @public
10
10
  */
11
11
  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
+ */
12
17
  static Base() {
13
18
  class Impl {
14
19
  /**
@@ -98,6 +103,22 @@ class Entity {
98
103
  }
99
104
  }
100
105
  exports.Entity = Entity;
106
+ /**
107
+ * A class decorator to add methods implementation on the {@link Entity.Base} class
108
+ * @example
109
+ * ```typescript
110
+ * @mol.codec(
111
+ * mol.table({
112
+ * codeHash: mol.Byte32,
113
+ * hashType: HashTypeCodec,
114
+ * args: mol.Bytes,
115
+ * }),
116
+ * )
117
+ * export class Script extends mol.Entity.Base<ScriptLike, Script>() {
118
+ * from(scriptLike: ScriptLike): Script {}
119
+ * }
120
+ * ```
121
+ */
101
122
  function codec(codec) {
102
123
  return function (Constructor) {
103
124
  var _a;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/num/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAA0B,MAAM,mBAAmB,CAAC;AAC7E,OAAO,EAAE,GAAG,EAAE,OAAO,EAAW,MAAM,iBAAiB,CAAC;AAExD;;;GAGG;AACH,MAAM,MAAM,GAAG,GAAG,MAAM,CAAC;AAEzB;;;;GAIG;AACH,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAEzD;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE,GAAG,GAAG,CAS7D;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE,GAAG,GAAG,CAS7D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,CAUzC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,CAE1C;AAED;;;;;;;;;;;;GAYG;AAEH,wBAAgB,UAAU,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG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AAG,EAAE,OAAO,GAAG,GAAG,CAE1C;AAED;;;;;;;;;;;;GAYG;AAEH,wBAAgB,UAAU,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,KAAK,CAE9D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,KAAK,CAEhE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,KAAK,CAOhE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,SAAS,GAAG,GAAG,CAEhD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,SAAS,GAAG,GAAG,CAElD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,SAAS,GAAG,GAAG,CAElD"}
@@ -143,7 +143,7 @@ function numBeToBytes(val, bytes) {
143
143
  if (bytes == null) {
144
144
  return rawBytes;
145
145
  }
146
- return (0, index_js_1.bytesConcat)(Array.from(Array(bytes - rawBytes.length), () => 0), rawBytes);
146
+ return (0, index_js_1.bytesConcat)("00".repeat(bytes - rawBytes.length), rawBytes);
147
147
  }
148
148
  /**
149
149
  * Converts a byte array to a Num (bigint) assuming little-endian order.
@@ -173,7 +173,7 @@ function numFromBytes(val) {
173
173
  * ```
174
174
  */
175
175
  function numLeFromBytes(val) {
176
- return numBeFromBytes([...(0, index_js_1.bytesFrom)(val)].reverse());
176
+ return numBeFromBytes((0, index_js_1.bytesFrom)(val).reverse());
177
177
  }
178
178
  /**
179
179
  * Converts a byte array to a Num (bigint) assuming big-endian order.
@@ -37,6 +37,6 @@ function btcPublicKeyFromP2pkhAddress(address) {
37
37
  */
38
38
  function verifyMessageBtcEcdsa(message, signature, publicKey) {
39
39
  const challenge = typeof message === "string" ? message : (0, index_js_2.hexFrom)(message).slice(2);
40
- const [_, ...rawSign] = (0, index_js_1.bytesFrom)(signature, "base64");
40
+ const rawSign = (0, index_js_1.bytesFrom)(signature, "base64").slice(1);
41
41
  return secp256k1_1.secp256k1.verify((0, index_js_1.bytesFrom)(rawSign), (0, bitcoinjs_message_1.magicHash)(challenge), publicKey);
42
42
  }
@@ -1 +1 @@
1
- {"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../../../src/signer/doge/verify.ts"],"names":[],"mappings":"AAEA,OAAO,EAAa,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAO5D;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GACd,OAAO,CAqBT"}
1
+ {"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../../../src/signer/doge/verify.ts"],"names":[],"mappings":"AAEA,OAAO,EAAa,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAO5D;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GACd,OAAO,CAuBT"}
@@ -11,7 +11,9 @@ const verify_js_1 = require("../btc/verify.js");
11
11
  */
12
12
  function verifyMessageDogeEcdsa(message, signature, address) {
13
13
  const challenge = typeof message === "string" ? message : (0, index_js_2.hexFrom)(message).slice(2);
14
- const [recoveryBit, ...rawSign] = (0, index_js_1.bytesFrom)(signature, "base64");
14
+ const signatureBytes = (0, index_js_1.bytesFrom)(signature, "base64");
15
+ const recoveryBit = signatureBytes[0];
16
+ const rawSign = signatureBytes.slice(1);
15
17
  const sig = secp256k1_1.secp256k1.Signature.fromCompact((0, index_js_2.hexFrom)(rawSign).slice(2)).addRecoveryBit(recoveryBit - 31);
16
18
  return ((0, verify_js_1.btcPublicKeyFromP2pkhAddress)(address) ===
17
19
  (0, index_js_2.hexFrom)((0, verify_js_1.btcEcdsaPublicKeyHash)(sig
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/core",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Core of CCC - CKBer's Codebase",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -29,7 +29,9 @@ export function addressPayloadFromString(address: string): {
29
29
  // Try parse full format address
30
30
  try {
31
31
  const { words, prefix } = bech32m.decode(address, ADDRESS_BECH32_LIMIT);
32
- const [formatType, ...payload] = bech32m.fromWords(words);
32
+ const decoded = bech32m.fromWords(words);
33
+ const formatType = decoded[0];
34
+ const payload = decoded.slice(1);
33
35
 
34
36
  if (formatType === (AddressFormat.Full as number)) {
35
37
  return { prefix, format: AddressFormat.Full, payload };
@@ -39,7 +41,9 @@ export function addressPayloadFromString(address: string): {
39
41
  // Try parse legacy 2019 format address
40
42
  try {
41
43
  const { prefix, words } = bech32.decode(address, ADDRESS_BECH32_LIMIT);
42
- const [formatType, ...payload] = bech32.fromWords(words);
44
+ const decoded = bech32.fromWords(words);
45
+ const formatType = decoded[0];
46
+ const payload = decoded.slice(1);
43
47
  if (
44
48
  [
45
49
  AddressFormat.FullData,
@@ -10,6 +10,41 @@ export type Bytes = Uint8Array;
10
10
  */
11
11
  export type BytesLike = string | Uint8Array | ArrayBuffer | ArrayLike<number>;
12
12
 
13
+ /**
14
+ * Concatenates multiple byte-like arrays to the first number array.
15
+ * @public
16
+ *
17
+ * @param result - The number array as result
18
+ * @param args - The byte-like arrays to concatenate.
19
+ * @returns The first number array
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const concatenatedBytes = [1, 2];
24
+ * bytesConcatTo(
25
+ * concatenatedBytes
26
+ * new Uint8Array([3, 4]),
27
+ * "hello",
28
+ * [5, 6, 7]
29
+ * );
30
+ * console.log(concatenatedBytes); // Outputs [1, 2, 3, 4, /* bytes of "hello" *\/, 5, 6, 7]
31
+ * ```
32
+ */
33
+
34
+ export function bytesConcatTo(
35
+ result: number[],
36
+ ...args: BytesLike[]
37
+ ): number[] {
38
+ return args.reduce((acc: number[], v) => {
39
+ const bytes = bytesFrom(v);
40
+ // Spread operator will cause call stack size exceeded
41
+ for (const byte of bytes) {
42
+ result.push(byte);
43
+ }
44
+ return acc;
45
+ }, result);
46
+ }
47
+
13
48
  /**
14
49
  * Concatenates multiple byte-like arrays into a single byte array.
15
50
  * @public
@@ -30,12 +65,7 @@ export type BytesLike = string | Uint8Array | ArrayBuffer | ArrayLike<number>;
30
65
  */
31
66
 
32
67
  export function bytesConcat(...args: BytesLike[]): Bytes {
33
- return new Uint8Array(
34
- args.reduce((acc: number[], v) => {
35
- acc.push(...bytesFrom(v));
36
- return acc;
37
- }, []),
38
- );
68
+ return new Uint8Array(bytesConcatTo([], ...args));
39
69
  }
40
70
 
41
71
  /**
package/src/ckb/script.ts CHANGED
@@ -108,13 +108,11 @@ export type ScriptLike = {
108
108
  * @public
109
109
  */
110
110
  @mol.codec(
111
- mol
112
- .table({
113
- codeHash: mol.Byte32,
114
- hashType: HashTypeCodec,
115
- args: mol.Bytes,
116
- })
117
- .map({ outMap: (decoded) => Script.from(decoded) }),
111
+ mol.table({
112
+ codeHash: mol.Byte32,
113
+ hashType: HashTypeCodec,
114
+ args: mol.Bytes,
115
+ }),
118
116
  )
119
117
  export class Script extends mol.Entity.Base<ScriptLike, Script>() {
120
118
  /**
@@ -1,6 +1,6 @@
1
- import { ClientCollectableSearchKeyFilterLike } from "../advancedBarrel.js";
2
1
  import { Bytes, BytesLike, bytesFrom } from "../bytes/index.js";
3
- import { CellDepInfoLike, Client, KnownScript } from "../client/index.js";
2
+ import type { ClientCollectableSearchKeyFilterLike } from "../client/clientTypes.advanced.js";
3
+ import type { CellDepInfoLike, Client, KnownScript } from "../client/index.js";
4
4
  import {
5
5
  Zero,
6
6
  fixedPointFrom,
@@ -17,11 +17,11 @@ import {
17
17
  numToBytes,
18
18
  numToHex,
19
19
  } from "../num/index.js";
20
- import { Signer } from "../signer/index.js";
20
+ import type { Signer } from "../signer/index.js";
21
21
  import { apply, reduceAsync } from "../utils/index.js";
22
22
  import { Script, ScriptLike, ScriptOpt } from "./script.js";
23
23
  import { DEP_TYPE_TO_NUM, NUM_TO_DEP_TYPE } from "./transaction.advanced.js";
24
- import { LumosTransactionSkeletonType } from "./transactionLumos.js";
24
+ import type { LumosTransactionSkeletonType } from "./transactionLumos.js";
25
25
 
26
26
  export const DepTypeCodec: mol.Codec<DepTypeLike, DepType> = mol.Codec.from({
27
27
  byteLength: 1,
@@ -119,12 +119,10 @@ export type OutPointLike = {
119
119
  * @public
120
120
  */
121
121
  @mol.codec(
122
- mol
123
- .struct({
124
- txHash: mol.Byte32,
125
- index: mol.Uint32,
126
- })
127
- .map({ outMap: (decoded) => OutPoint.from(decoded) }),
122
+ mol.struct({
123
+ txHash: mol.Byte32,
124
+ index: mol.Uint32,
125
+ }),
128
126
  )
129
127
  export class OutPoint extends mol.Entity.Base<OutPointLike, OutPoint>() {
130
128
  /**
@@ -172,13 +170,11 @@ export type CellOutputLike = {
172
170
  * @public
173
171
  */
174
172
  @mol.codec(
175
- mol
176
- .table({
177
- capacity: mol.Uint64,
178
- lock: Script,
179
- type: ScriptOpt,
180
- })
181
- .map({ outMap: (decoded) => CellOutput.from(decoded) }),
173
+ mol.table({
174
+ capacity: mol.Uint64,
175
+ lock: Script,
176
+ type: ScriptOpt,
177
+ }),
182
178
  )
183
179
  export class CellOutput extends mol.Entity.Base<CellOutputLike, CellOutput>() {
184
180
  /**
@@ -347,10 +343,7 @@ export type SinceLike =
347
343
  * @public
348
344
  */
349
345
  @mol.codec(
350
- mol.Uint64.map<SinceLike, Since>({
351
- inMap: (encodable) => Since.from(encodable).toNum(),
352
- outMap: (decoded) => Since.from(decoded),
353
- }),
346
+ mol.Uint64.mapIn((encodable: SinceLike) => Since.from(encodable).toNum()),
354
347
  )
355
348
  export class Since extends mol.Entity.Base<SinceLike, Since>() {
356
349
  /**
@@ -472,10 +465,10 @@ export type CellInputLike = {
472
465
  since: Since,
473
466
  previousOutput: OutPoint,
474
467
  })
475
- .map<CellInputLike, CellInput>({
476
- inMap: (encodable) => ({ ...encodable, since: encodable.since ?? 0 }),
477
- outMap: (decoded) => CellInput.from(decoded),
478
- }),
468
+ .mapIn((encodable: CellInputLike) => ({
469
+ ...encodable,
470
+ since: encodable.since ?? 0,
471
+ })),
479
472
  )
480
473
  export class CellInput extends mol.Entity.Base<CellInputLike, CellInput>() {
481
474
  /**
@@ -560,12 +553,10 @@ export type CellDepLike = {
560
553
  * @public
561
554
  */
562
555
  @mol.codec(
563
- mol
564
- .struct({
565
- outPoint: OutPoint,
566
- depType: DepTypeCodec,
567
- })
568
- .map({ outMap: (decoded) => CellDep.from(decoded) }),
556
+ mol.struct({
557
+ outPoint: OutPoint,
558
+ depType: DepTypeCodec,
559
+ }),
569
560
  )
570
561
  export class CellDep extends mol.Entity.Base<CellDepLike, CellDep>() {
571
562
  /**
@@ -637,13 +628,11 @@ export type WitnessArgsLike = {
637
628
  * @public
638
629
  */
639
630
  @mol.codec(
640
- mol
641
- .table({
642
- lock: mol.BytesOpt,
643
- inputType: mol.BytesOpt,
644
- outputType: mol.BytesOpt,
645
- })
646
- .map({ outMap: (decoded) => WitnessArgs.from(decoded) }),
631
+ mol.table({
632
+ lock: mol.BytesOpt,
633
+ inputType: mol.BytesOpt,
634
+ outputType: mol.BytesOpt,
635
+ }),
647
636
  )
648
637
  export class WitnessArgs extends mol.Entity.Base<
649
638
  WitnessArgsLike,
@@ -706,16 +695,14 @@ export function udtBalanceFrom(dataLike: BytesLike): Num {
706
695
  return numFromBytes(data);
707
696
  }
708
697
 
709
- export const RawTransaction = mol
710
- .table({
711
- version: mol.Uint32,
712
- cellDeps: CellDepVec,
713
- headerDeps: mol.Byte32Vec,
714
- inputs: CellInputVec,
715
- outputs: CellOutputVec,
716
- outputsData: mol.BytesVec,
717
- })
718
- .map({ outMap: (decoded) => Transaction.from(decoded) });
698
+ export const RawTransaction = mol.table({
699
+ version: mol.Uint32,
700
+ cellDeps: CellDepVec,
701
+ headerDeps: mol.Byte32Vec,
702
+ inputs: CellInputVec,
703
+ outputs: CellOutputVec,
704
+ outputsData: mol.BytesVec,
705
+ });
719
706
 
720
707
  /**
721
708
  * @public
@@ -741,16 +728,14 @@ export type TransactionLike = {
741
728
  raw: RawTransaction,
742
729
  witnesses: mol.BytesVec,
743
730
  })
744
- .map<TransactionLike, Transaction>({
745
- inMap: (txLike) => {
746
- const tx = Transaction.from(txLike);
747
- return {
748
- raw: tx,
749
- witnesses: tx.witnesses,
750
- };
751
- },
752
- outMap: (tx) => Transaction.from({ ...tx.raw, witnesses: tx.witnesses }),
753
- }),
731
+ .mapIn((txLike: TransactionLike) => {
732
+ const tx = Transaction.from(txLike);
733
+ return {
734
+ raw: tx,
735
+ witnesses: tx.witnesses,
736
+ };
737
+ })
738
+ .mapOut((tx) => Transaction.from({ ...tx.raw, witnesses: tx.witnesses })),
754
739
  )
755
740
  export class Transaction extends mol.Entity.Base<
756
741
  TransactionLike,