@ckb-ccc/core 0.1.1 → 1.0.0

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 (162) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/barrel.d.ts +1 -1
  3. package/dist/barrel.d.ts.map +1 -1
  4. package/dist/barrel.js +1 -1
  5. package/dist/bytes/index.d.ts +14 -0
  6. package/dist/bytes/index.d.ts.map +1 -1
  7. package/dist/bytes/index.js +29 -0
  8. package/dist/ckb/advanced.d.ts +0 -1
  9. package/dist/ckb/advanced.d.ts.map +1 -1
  10. package/dist/ckb/advanced.js +0 -1
  11. package/dist/ckb/script.d.ts +17 -58
  12. package/dist/ckb/script.d.ts.map +1 -1
  13. package/dist/ckb/script.js +31 -78
  14. package/dist/ckb/transaction.d.ts +114 -260
  15. package/dist/ckb/transaction.d.ts.map +1 -1
  16. package/dist/ckb/transaction.js +162 -358
  17. package/dist/molecule/barrel.d.ts +4 -0
  18. package/dist/molecule/barrel.d.ts.map +1 -0
  19. package/dist/molecule/barrel.js +3 -0
  20. package/dist/molecule/codec.d.ts.map +1 -1
  21. package/dist/molecule/codec.js +6 -3
  22. package/dist/molecule/entity.d.ts +98 -0
  23. package/dist/molecule/entity.d.ts.map +1 -0
  24. package/dist/molecule/entity.js +113 -0
  25. package/dist/molecule/index.d.ts +1 -2
  26. package/dist/molecule/index.d.ts.map +1 -1
  27. package/dist/molecule/index.js +1 -2
  28. package/dist/molecule/predefined.d.ts +13 -20
  29. package/dist/molecule/predefined.d.ts.map +1 -1
  30. package/dist/molecule/predefined.js +14 -54
  31. package/dist/utils/index.d.ts +3 -0
  32. package/dist/utils/index.d.ts.map +1 -1
  33. package/dist.commonjs/address/address.advanced.js +28 -23
  34. package/dist.commonjs/address/advanced.js +17 -1
  35. package/dist.commonjs/address/index.js +15 -11
  36. package/dist.commonjs/advanced.js +31 -2
  37. package/dist.commonjs/advancedBarrel.js +21 -5
  38. package/dist.commonjs/barrel.d.ts +1 -1
  39. package/dist.commonjs/barrel.d.ts.map +1 -1
  40. package/dist.commonjs/barrel.js +28 -12
  41. package/dist.commonjs/bytes/advanced.js +2 -1
  42. package/dist.commonjs/bytes/index.d.ts +14 -0
  43. package/dist.commonjs/bytes/index.d.ts.map +1 -1
  44. package/dist.commonjs/bytes/index.js +42 -7
  45. package/dist.commonjs/ckb/advanced.d.ts +0 -1
  46. package/dist.commonjs/ckb/advanced.d.ts.map +1 -1
  47. package/dist.commonjs/ckb/advanced.js +18 -3
  48. package/dist.commonjs/ckb/index.js +19 -3
  49. package/dist.commonjs/ckb/script.advanced.js +6 -3
  50. package/dist.commonjs/ckb/script.d.ts +17 -58
  51. package/dist.commonjs/ckb/script.d.ts.map +1 -1
  52. package/dist.commonjs/ckb/script.js +49 -90
  53. package/dist.commonjs/ckb/transaction.advanced.js +6 -3
  54. package/dist.commonjs/ckb/transaction.d.ts +114 -260
  55. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  56. package/dist.commonjs/ckb/transaction.js +249 -434
  57. package/dist.commonjs/ckb/transactionLumos.js +2 -1
  58. package/dist.commonjs/client/advanced.js +22 -6
  59. package/dist.commonjs/client/cache/advanced.js +17 -1
  60. package/dist.commonjs/client/cache/cache.js +7 -3
  61. package/dist.commonjs/client/cache/index.js +18 -2
  62. package/dist.commonjs/client/cache/memory.advanced.js +28 -21
  63. package/dist.commonjs/client/cache/memory.js +23 -19
  64. package/dist.commonjs/client/client.js +25 -21
  65. package/dist.commonjs/client/clientPublicMainnet.advanced.js +25 -22
  66. package/dist.commonjs/client/clientPublicMainnet.js +15 -8
  67. package/dist.commonjs/client/clientPublicTestnet.advanced.js +25 -22
  68. package/dist.commonjs/client/clientPublicTestnet.js +15 -8
  69. package/dist.commonjs/client/clientTypes.advanced.js +6 -3
  70. package/dist.commonjs/client/clientTypes.js +43 -29
  71. package/dist.commonjs/client/index.js +22 -6
  72. package/dist.commonjs/client/jsonRpc/advanced.js +18 -2
  73. package/dist.commonjs/client/jsonRpc/index.js +41 -37
  74. package/dist.commonjs/client/jsonRpc/transformers.js +61 -57
  75. package/dist.commonjs/client/jsonRpc/types.js +2 -1
  76. package/dist.commonjs/client/transports/advanced.js +25 -8
  77. package/dist.commonjs/client/transports/http.js +5 -1
  78. package/dist.commonjs/client/transports/transport.js +2 -1
  79. package/dist.commonjs/client/transports/webSocket.js +10 -3
  80. package/dist.commonjs/fixedPoint/index.js +9 -4
  81. package/dist.commonjs/hasher/advanced.js +4 -1
  82. package/dist.commonjs/hasher/hasher.js +2 -1
  83. package/dist.commonjs/hasher/hasherCkb.js +20 -14
  84. package/dist.commonjs/hasher/hasherKeecak256.js +11 -7
  85. package/dist.commonjs/hasher/index.js +19 -3
  86. package/dist.commonjs/hex/index.js +6 -3
  87. package/dist.commonjs/index.js +31 -2
  88. package/dist.commonjs/keystore/index.js +25 -21
  89. package/dist.commonjs/molecule/barrel.d.ts +4 -0
  90. package/dist.commonjs/molecule/barrel.d.ts.map +1 -0
  91. package/dist.commonjs/molecule/barrel.js +19 -0
  92. package/dist.commonjs/molecule/codec.d.ts.map +1 -1
  93. package/dist.commonjs/molecule/codec.js +69 -51
  94. package/dist.commonjs/molecule/entity.d.ts +98 -0
  95. package/dist.commonjs/molecule/entity.d.ts.map +1 -0
  96. package/dist.commonjs/molecule/entity.js +118 -0
  97. package/dist.commonjs/molecule/index.d.ts +1 -2
  98. package/dist.commonjs/molecule/index.d.ts.map +1 -1
  99. package/dist.commonjs/molecule/index.js +27 -2
  100. package/dist.commonjs/molecule/predefined.d.ts +13 -20
  101. package/dist.commonjs/molecule/predefined.d.ts.map +1 -1
  102. package/dist.commonjs/molecule/predefined.js +54 -91
  103. package/dist.commonjs/num/index.js +29 -17
  104. package/dist.commonjs/signer/btc/index.js +19 -3
  105. package/dist.commonjs/signer/btc/signerBtc.js +24 -20
  106. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +8 -4
  107. package/dist.commonjs/signer/btc/verify.js +26 -17
  108. package/dist.commonjs/signer/ckb/index.js +21 -5
  109. package/dist.commonjs/signer/ckb/signerCkbPrivateKey.js +20 -16
  110. package/dist.commonjs/signer/ckb/signerCkbPublicKey.js +24 -20
  111. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +12 -8
  112. package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.js +16 -12
  113. package/dist.commonjs/signer/ckb/verifyJoyId.js +8 -5
  114. package/dist.commonjs/signer/doge/index.js +20 -4
  115. package/dist.commonjs/signer/doge/signerDoge.js +26 -19
  116. package/dist.commonjs/signer/doge/signerDogeAddressReadonly.js +6 -2
  117. package/dist.commonjs/signer/doge/signerDogePrivateKey.js +17 -13
  118. package/dist.commonjs/signer/doge/verify.js +15 -12
  119. package/dist.commonjs/signer/dummy/alwaysError.js +6 -2
  120. package/dist.commonjs/signer/dummy/dummy.js +7 -3
  121. package/dist.commonjs/signer/dummy/index.js +19 -3
  122. package/dist.commonjs/signer/dummy/openLink.js +6 -2
  123. package/dist.commonjs/signer/evm/index.js +19 -3
  124. package/dist.commonjs/signer/evm/signerEvm.js +35 -31
  125. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +8 -4
  126. package/dist.commonjs/signer/evm/verify.js +7 -4
  127. package/dist.commonjs/signer/index.js +23 -7
  128. package/dist.commonjs/signer/nostr/index.js +18 -2
  129. package/dist.commonjs/signer/nostr/signerNostr.js +25 -21
  130. package/dist.commonjs/signer/nostr/verify.js +16 -12
  131. package/dist.commonjs/signer/signer/index.js +25 -19
  132. package/dist.commonjs/utils/index.d.ts +3 -0
  133. package/dist.commonjs/utils/index.d.ts.map +1 -1
  134. package/dist.commonjs/utils/index.js +12 -6
  135. package/package.json +1 -2
  136. package/src/barrel.ts +1 -1
  137. package/src/bytes/index.ts +34 -0
  138. package/src/ckb/advanced.ts +0 -1
  139. package/src/ckb/script.ts +22 -85
  140. package/src/ckb/transaction.ts +134 -412
  141. package/src/molecule/barrel.ts +3 -0
  142. package/src/molecule/codec.ts +9 -5
  143. package/src/molecule/entity.ts +163 -0
  144. package/src/molecule/index.ts +1 -2
  145. package/src/molecule/predefined.ts +20 -69
  146. package/src/utils/index.ts +5 -0
  147. package/dist/ckb/molecule.advanced/generated.d.ts +0 -355
  148. package/dist/ckb/molecule.advanced/generated.d.ts.map +0 -1
  149. package/dist/ckb/molecule.advanced/generated.js +0 -1468
  150. package/dist/ckb/molecule.advanced/index.d.ts +0 -6
  151. package/dist/ckb/molecule.advanced/index.d.ts.map +0 -1
  152. package/dist/ckb/molecule.advanced/index.js +0 -7
  153. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts +0 -355
  154. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts.map +0 -1
  155. package/dist.commonjs/ckb/molecule.advanced/generated.js +0 -1468
  156. package/dist.commonjs/ckb/molecule.advanced/index.d.ts +0 -6
  157. package/dist.commonjs/ckb/molecule.advanced/index.d.ts.map +0 -1
  158. package/dist.commonjs/ckb/molecule.advanced/index.js +0 -7
  159. package/src/ckb/molecule.advanced/blockchain.mol +0 -118
  160. package/src/ckb/molecule.advanced/generated.d.ts +0 -444
  161. package/src/ckb/molecule.advanced/generated.js +0 -1804
  162. package/src/ckb/molecule.advanced/index.ts +0 -11
@@ -8,6 +8,7 @@ import {
8
8
  } from "../fixedPoint/index.js";
9
9
  import { Hasher, HasherCkb, hashCkb } from "../hasher/index.js";
10
10
  import { Hex, HexLike, hexFrom } from "../hex/index.js";
11
+ import { mol } from "../molecule/index.js";
11
12
  import {
12
13
  Num,
13
14
  NumLike,
@@ -18,11 +19,16 @@ import {
18
19
  } from "../num/index.js";
19
20
  import { Signer } from "../signer/index.js";
20
21
  import { apply, reduceAsync } from "../utils/index.js";
21
- import * as mol from "./molecule.advanced/index.js";
22
- import { Script, ScriptLike } from "./script.js";
22
+ import { Script, ScriptLike, ScriptOpt } from "./script.js";
23
23
  import { DEP_TYPE_TO_NUM, NUM_TO_DEP_TYPE } from "./transaction.advanced.js";
24
24
  import { LumosTransactionSkeletonType } from "./transactionLumos.js";
25
25
 
26
+ export const DepTypeCodec: mol.Codec<DepTypeLike, DepType> = mol.Codec.from({
27
+ byteLength: 1,
28
+ encode: depTypeToBytes,
29
+ decode: depTypeFromBytes,
30
+ });
31
+
26
32
  /**
27
33
  * @public
28
34
  */
@@ -112,7 +118,15 @@ export type OutPointLike = {
112
118
  /**
113
119
  * @public
114
120
  */
115
- export class OutPoint {
121
+ @mol.codec(
122
+ mol
123
+ .struct({
124
+ txHash: mol.Byte32,
125
+ index: mol.Uint32,
126
+ })
127
+ .map({ outMap: (decoded) => OutPoint.from(decoded) }),
128
+ )
129
+ export class OutPoint extends mol.Entity.Base<OutPointLike, OutPoint>() {
116
130
  /**
117
131
  * Creates an instance of OutPoint.
118
132
  *
@@ -123,20 +137,8 @@ export class OutPoint {
123
137
  constructor(
124
138
  public txHash: Hex,
125
139
  public index: Num,
126
- ) {}
127
-
128
- /**
129
- * Clone an OutPoint.
130
- *
131
- * @returns A cloned OutPoint instance.
132
- *
133
- * @example
134
- * ```typescript
135
- * const outPoint1 = outPoint0.clone();
136
- * ```
137
- */
138
- clone(): OutPoint {
139
- return new OutPoint(this.txHash, this.index);
140
+ ) {
141
+ super();
140
142
  }
141
143
 
142
144
  /**
@@ -156,74 +158,6 @@ export class OutPoint {
156
158
  }
157
159
  return new OutPoint(hexFrom(outPoint.txHash), numFrom(outPoint.index));
158
160
  }
159
-
160
- /**
161
- * Converts the OutPoint instance to molecule data format.
162
- *
163
- * @returns An object representing the outpoint in molecule data format.
164
- */
165
-
166
- _toMolData() {
167
- return {
168
- txHash: bytesFrom(this.txHash),
169
- index: numToBytes(this.index, 4),
170
- };
171
- }
172
-
173
- /**
174
- * Converts the OutPoint instance to bytes.
175
- *
176
- * @returns A Uint8Array containing the outpoint bytes.
177
- *
178
- * @example
179
- * ```typescript
180
- * const outPointBytes = outPoint.toBytes();
181
- * ```
182
- */
183
-
184
- toBytes(): Bytes {
185
- return bytesFrom(mol.SerializeOutPoint(this._toMolData()));
186
- }
187
-
188
- /**
189
- * Creates an OutPoint instance from a byte-like value or molecule OutPoint.
190
- *
191
- * @param bytes - The byte-like value or molecule OutPoint to convert.
192
- * @returns An OutPoint instance.
193
- *
194
- * @example
195
- * ```typescript
196
- * const outPoint = OutPoint.fromBytes(new Uint8Array([/* outpoint bytes *\/]));
197
- * ```
198
- */
199
-
200
- static fromBytes(bytes: BytesLike | mol.OutPoint): OutPoint {
201
- const view =
202
- bytes instanceof mol.OutPoint
203
- ? bytes
204
- : new mol.OutPoint(bytesFrom(bytes));
205
-
206
- return new OutPoint(
207
- hexFrom(view.getTxHash().raw()),
208
- numFromBytes(view.getIndex().raw()),
209
- );
210
- }
211
-
212
- /**
213
- * Compares the current OutPoint instance with another OutPointLike object for equality.
214
- *
215
- * @param val - The OutPointLike object to compare with.
216
- * @returns True if the out points are equal, otherwise false.
217
- *
218
- * @example
219
- * ```typescript
220
- * const isEqual = outPoint.eq(anotherOutPoint);
221
- * ```
222
- */
223
- eq(val: OutPointLike): boolean {
224
- const outPoint = OutPoint.from(val);
225
- return this.txHash === outPoint.txHash && this.index === outPoint.index;
226
- }
227
161
  }
228
162
 
229
163
  /**
@@ -237,7 +171,16 @@ export type CellOutputLike = {
237
171
  /**
238
172
  * @public
239
173
  */
240
- export class CellOutput {
174
+ @mol.codec(
175
+ mol
176
+ .table({
177
+ capacity: mol.Uint64,
178
+ lock: Script,
179
+ type: ScriptOpt,
180
+ })
181
+ .map({ outMap: (decoded) => CellOutput.from(decoded) }),
182
+ )
183
+ export class CellOutput extends mol.Entity.Base<CellOutputLike, CellOutput>() {
241
184
  /**
242
185
  * Creates an instance of CellOutput.
243
186
  *
@@ -250,26 +193,14 @@ export class CellOutput {
250
193
  public capacity: Num,
251
194
  public lock: Script,
252
195
  public type?: Script,
253
- ) {}
196
+ ) {
197
+ super();
198
+ }
254
199
 
255
200
  get occupiedSize(): number {
256
201
  return 8 + this.lock.occupiedSize + (this.type?.occupiedSize ?? 0);
257
202
  }
258
203
 
259
- /**
260
- * Clone a CellOutput.
261
- *
262
- * @returns A cloned CellOutput instance.
263
- *
264
- * @example
265
- * ```typescript
266
- * const cellOutput1 = cellOutput0.clone();
267
- * ```
268
- */
269
- clone(): CellOutput {
270
- return new CellOutput(this.capacity, this.lock.clone(), this.type?.clone());
271
- }
272
-
273
204
  /**
274
205
  * Creates a CellOutput instance from a CellOutputLike object.
275
206
  *
@@ -296,61 +227,8 @@ export class CellOutput {
296
227
  apply(Script.from, cellOutput.type),
297
228
  );
298
229
  }
299
-
300
- /**
301
- * Converts the CellOutput instance to molecule data format.
302
- *
303
- * @returns An object representing the cell output in molecule data format.
304
- */
305
-
306
- _toMolData() {
307
- return {
308
- capacity: numToBytes(this.capacity, 8),
309
- lock: this.lock._toMolData(),
310
- type: this.type?._toMolData(),
311
- };
312
- }
313
-
314
- /**
315
- * Converts the CellOutput instance to bytes.
316
- *
317
- * @returns A Uint8Array containing the cell output bytes.
318
- *
319
- * @example
320
- * ```typescript
321
- * const cellOutputBytes = cellOutput.toBytes();
322
- * ```
323
- */
324
-
325
- toBytes(): Bytes {
326
- return bytesFrom(mol.SerializeCellOutput(this._toMolData()));
327
- }
328
-
329
- /**
330
- * Creates a CellOutput instance from a byte-like value or molecule CellOutput.
331
- *
332
- * @param bytes - The byte-like value or molecule CellOutput to convert.
333
- * @returns A CellOutput instance.
334
- *
335
- * @example
336
- * ```typescript
337
- * const cellOutput = CellOutput.fromBytes(new Uint8Array([/* cell output bytes *\/]));
338
- * ```
339
- */
340
-
341
- static fromBytes(bytes: BytesLike | mol.CellOutput): CellOutput {
342
- const view =
343
- bytes instanceof mol.CellOutput
344
- ? bytes
345
- : new mol.CellOutput(bytesFrom(bytes));
346
-
347
- return new CellOutput(
348
- numFromBytes(view.getCapacity().raw()),
349
- Script.fromBytes(view.getLock()),
350
- apply(Script.fromBytes, mol.molOptional(view.getType())),
351
- );
352
- }
353
230
  }
231
+ export const CellOutputVec = mol.vector(CellOutput);
354
232
 
355
233
  /**
356
234
  * @public
@@ -468,7 +346,13 @@ export type SinceLike =
468
346
  /**
469
347
  * @public
470
348
  */
471
- export class Since {
349
+ @mol.codec(
350
+ mol.Uint64.map<SinceLike, Since>({
351
+ inMap: (encodable) => Since.from(encodable).toNum(),
352
+ outMap: (decoded) => Since.from(decoded),
353
+ }),
354
+ )
355
+ export class Since extends mol.Entity.Base<SinceLike, Since>() {
472
356
  /**
473
357
  * Creates an instance of Since.
474
358
  *
@@ -481,7 +365,9 @@ export class Since {
481
365
  public relative: "absolute" | "relative",
482
366
  public metric: "blockNumber" | "epoch" | "timestamp",
483
367
  public value: Num,
484
- ) {}
368
+ ) {
369
+ super();
370
+ }
485
371
 
486
372
  /**
487
373
  * Clone a Since.
@@ -580,7 +466,18 @@ export type CellInputLike = {
580
466
  /**
581
467
  * @public
582
468
  */
583
- export class CellInput {
469
+ @mol.codec(
470
+ mol
471
+ .struct({
472
+ since: Since,
473
+ previousOutput: OutPoint,
474
+ })
475
+ .map<CellInputLike, CellInput>({
476
+ inMap: (encodable) => ({ ...encodable, since: encodable.since ?? 0 }),
477
+ outMap: (decoded) => CellInput.from(decoded),
478
+ }),
479
+ )
480
+ export class CellInput extends mol.Entity.Base<CellInputLike, CellInput>() {
584
481
  /**
585
482
  * Creates an instance of CellInput.
586
483
  *
@@ -595,25 +492,8 @@ export class CellInput {
595
492
  public since: Num,
596
493
  public cellOutput?: CellOutput,
597
494
  public outputData?: Hex,
598
- ) {}
599
-
600
- /**
601
- * Clone a CellInput.
602
- *
603
- * @returns A cloned CellInput instance.
604
- *
605
- * @example
606
- * ```typescript
607
- * const cellInput1 = cellInput0.clone();
608
- * ```
609
- */
610
- clone(): CellInput {
611
- return new CellInput(
612
- this.previousOutput.clone(),
613
- this.since,
614
- this.cellOutput?.clone(),
615
- this.outputData,
616
- );
495
+ ) {
496
+ super();
617
497
  }
618
498
 
619
499
  /**
@@ -666,59 +546,8 @@ export class CellInput {
666
546
  this.cellOutput = cell.cellOutput;
667
547
  this.outputData = cell.outputData;
668
548
  }
669
-
670
- /**
671
- * Converts the CellInput instance to molecule data format.
672
- *
673
- * @returns An object representing the cell input in molecule data format.
674
- */
675
-
676
- _toMolData() {
677
- return {
678
- previousOutput: this.previousOutput._toMolData(),
679
- since: numToBytes(this.since, 8),
680
- };
681
- }
682
-
683
- /**
684
- * Converts the CellInput instance to bytes.
685
- *
686
- * @returns A Uint8Array containing the cell input bytes.
687
- *
688
- * @example
689
- * ```typescript
690
- * const cellInputBytes = cellInput.toBytes();
691
- * ```
692
- */
693
-
694
- toBytes(): Bytes {
695
- return bytesFrom(mol.SerializeCellInput(this._toMolData()));
696
- }
697
-
698
- /**
699
- * Creates a CellInput instance from a byte-like value or molecule CellInput.
700
- *
701
- * @param bytes - The byte-like value or molecule CellInput to convert.
702
- * @returns A CellInput instance.
703
- *
704
- * @example
705
- * ```typescript
706
- * const cellInput = CellInput.fromBytes(new Uint8Array([/* cell input bytes *\/]));
707
- * ```
708
- */
709
-
710
- static fromBytes(bytes: BytesLike | mol.CellInput): CellInput {
711
- const view =
712
- bytes instanceof mol.CellInput
713
- ? bytes
714
- : new mol.CellInput(bytesFrom(bytes));
715
-
716
- return new CellInput(
717
- OutPoint.fromBytes(view.getPreviousOutput()),
718
- numFromBytes(view.getSince().raw()),
719
- );
720
- }
721
549
  }
550
+ export const CellInputVec = mol.vector(CellInput);
722
551
 
723
552
  /**
724
553
  * @public
@@ -730,7 +559,15 @@ export type CellDepLike = {
730
559
  /**
731
560
  * @public
732
561
  */
733
- export class CellDep {
562
+ @mol.codec(
563
+ mol
564
+ .struct({
565
+ outPoint: OutPoint,
566
+ depType: DepTypeCodec,
567
+ })
568
+ .map({ outMap: (decoded) => CellDep.from(decoded) }),
569
+ )
570
+ export class CellDep extends mol.Entity.Base<CellDepLike, CellDep>() {
734
571
  /**
735
572
  * Creates an instance of CellDep.
736
573
  *
@@ -741,7 +578,9 @@ export class CellDep {
741
578
  constructor(
742
579
  public outPoint: OutPoint,
743
580
  public depType: DepType,
744
- ) {}
581
+ ) {
582
+ super();
583
+ }
745
584
 
746
585
  /**
747
586
  * Clone a CellDep.
@@ -783,75 +622,8 @@ export class CellDep {
783
622
  depTypeFrom(cellDep.depType),
784
623
  );
785
624
  }
786
-
787
- /**
788
- * Converts the CellDep instance to molecule data format.
789
- *
790
- * @returns An object representing the cell dependency in molecule data format.
791
- */
792
-
793
- _toMolData() {
794
- return {
795
- outPoint: this.outPoint._toMolData(),
796
- depType: depTypeToBytes(this.depType),
797
- };
798
- }
799
-
800
- /**
801
- * Converts the CellDep instance to bytes.
802
- *
803
- * @returns A Uint8Array containing the cell dependency bytes.
804
- *
805
- * @example
806
- * ```typescript
807
- * const cellDepBytes = cellDep.toBytes();
808
- * ```
809
- */
810
-
811
- toBytes(): Bytes {
812
- return bytesFrom(mol.SerializeCellDep(this._toMolData()));
813
- }
814
-
815
- /**
816
- * Creates a CellDep instance from a byte-like value or molecule CellDep.
817
- *
818
- * @param bytes - The byte-like value or molecule CellDep to convert.
819
- * @returns A CellDep instance.
820
- *
821
- * @example
822
- * ```typescript
823
- * const cellDep = CellDep.fromBytes(new Uint8Array([/* cell dep bytes *\/]));
824
- * ```
825
- */
826
-
827
- fromBytes(bytes: BytesLike | mol.CellDep): CellDep {
828
- const view =
829
- bytes instanceof mol.CellDep ? bytes : new mol.CellDep(bytesFrom(bytes));
830
-
831
- return new CellDep(
832
- OutPoint.fromBytes(view.getOutPoint()),
833
- depTypeFromBytes([view.getDepType()]),
834
- );
835
- }
836
-
837
- /**
838
- * Compares the current CellDep instance with another CellDepLike object for equality.
839
- *
840
- * @param val - The CellDepLike object to compare with.
841
- * @returns True if the cell deps are equal, otherwise false.
842
- *
843
- * @example
844
- * ```typescript
845
- * const isEqual = cellDep.eq(anotherCellDep);
846
- * ```
847
- */
848
- eq(val: CellDepLike): boolean {
849
- const cellDep = CellDep.from(val);
850
- return (
851
- this.outPoint.eq(cellDep.outPoint) && this.depType === cellDep.depType
852
- );
853
- }
854
625
  }
626
+ export const CellDepVec = mol.vector(CellDep);
855
627
 
856
628
  /**
857
629
  * @public
@@ -864,7 +636,19 @@ export type WitnessArgsLike = {
864
636
  /**
865
637
  * @public
866
638
  */
867
- export class WitnessArgs {
639
+ @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) }),
647
+ )
648
+ export class WitnessArgs extends mol.Entity.Base<
649
+ WitnessArgsLike,
650
+ WitnessArgs
651
+ >() {
868
652
  /**
869
653
  * Creates an instance of WitnessArgs.
870
654
  *
@@ -877,7 +661,9 @@ export class WitnessArgs {
877
661
  public lock?: Hex,
878
662
  public inputType?: Hex,
879
663
  public outputType?: Hex,
880
- ) {}
664
+ ) {
665
+ super();
666
+ }
881
667
 
882
668
  /**
883
669
  * Creates a WitnessArgs instance from a WitnessArgsLike object.
@@ -906,60 +692,6 @@ export class WitnessArgs {
906
692
  apply(hexFrom, witnessArgs.outputType),
907
693
  );
908
694
  }
909
-
910
- /**
911
- * Converts the WitnessArgs instance to molecule data format.
912
- *
913
- * @returns An object representing the witness arguments in molecule data format.
914
- */
915
-
916
- _toMolData() {
917
- return {
918
- lock: apply(bytesFrom, this.lock),
919
- inputType: apply(bytesFrom, this.inputType),
920
- outputType: apply(bytesFrom, this.outputType),
921
- };
922
- }
923
-
924
- /**
925
- * Converts the WitnessArgs instance to bytes.
926
- *
927
- * @returns A Uint8Array containing the witness arguments bytes.
928
- *
929
- * @example
930
- * ```typescript
931
- * const witnessArgsBytes = witnessArgs.toBytes();
932
- * ```
933
- */
934
-
935
- toBytes(): Bytes {
936
- return bytesFrom(mol.SerializeWitnessArgs(this._toMolData()));
937
- }
938
-
939
- /**
940
- * Creates a WitnessArgs instance from a byte-like value or molecule WitnessArgs.
941
- *
942
- * @param bytes - The byte-like value or molecule WitnessArgs to convert.
943
- * @returns A WitnessArgs instance.
944
- *
945
- * @example
946
- * ```typescript
947
- * const witnessArgs = WitnessArgs.fromBytes(new Uint8Array([/* witness args bytes *\/]));
948
- * ```
949
- */
950
-
951
- static fromBytes(bytes: BytesLike | mol.WitnessArgs): WitnessArgs {
952
- const view =
953
- bytes instanceof mol.WitnessArgs
954
- ? bytes
955
- : new mol.WitnessArgs(bytesFrom(bytes));
956
-
957
- return new WitnessArgs(
958
- apply(hexFrom, mol.molOptional(view.getLock())?.raw()),
959
- apply(hexFrom, mol.molOptional(view.getInputType())?.raw()),
960
- apply(hexFrom, mol.molOptional(view.getOutputType())?.raw()),
961
- );
962
- }
963
695
  }
964
696
 
965
697
  /**
@@ -974,6 +706,17 @@ export function udtBalanceFrom(dataLike: BytesLike): Num {
974
706
  return numFromBytes(data);
975
707
  }
976
708
 
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) });
719
+
977
720
  /**
978
721
  * @public
979
722
  */
@@ -992,7 +735,27 @@ export type TransactionLike = {
992
735
  /**
993
736
  * @public
994
737
  */
995
- export class Transaction {
738
+ @mol.codec(
739
+ mol
740
+ .table({
741
+ raw: RawTransaction,
742
+ witnesses: mol.BytesVec,
743
+ })
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
+ }),
754
+ )
755
+ export class Transaction extends mol.Entity.Base<
756
+ TransactionLike,
757
+ Transaction
758
+ >() {
996
759
  /**
997
760
  * Creates an instance of Transaction.
998
761
  *
@@ -1013,7 +776,9 @@ export class Transaction {
1013
776
  public outputs: CellOutput[],
1014
777
  public outputsData: Hex[],
1015
778
  public witnesses: Hex[],
1016
- ) {}
779
+ ) {
780
+ super();
781
+ }
1017
782
 
1018
783
  /**
1019
784
  * Creates a default Transaction instance with empty fields.
@@ -1048,28 +813,6 @@ export class Transaction {
1048
813
  this.witnesses = tx.witnesses;
1049
814
  }
1050
815
 
1051
- /**
1052
- * Clone a Transaction.
1053
- *
1054
- * @returns A cloned instance
1055
- *
1056
- * @example
1057
- * ```typescript
1058
- * const tx1 = tx0.clone();
1059
- * ```
1060
- */
1061
- clone(): Transaction {
1062
- return new Transaction(
1063
- 0n,
1064
- this.cellDeps.map((c) => c.clone()),
1065
- [...this.headerDeps],
1066
- this.inputs.map((i) => i.clone()),
1067
- this.outputs.map((o) => o.clone()),
1068
- [...this.outputsData],
1069
- [...this.witnesses],
1070
- );
1071
- }
1072
-
1073
816
  /**
1074
817
  * Creates a Transaction instance from a TransactionLike object.
1075
818
  *
@@ -1188,57 +931,36 @@ export class Transaction {
1188
931
  * ```
1189
932
  */
1190
933
  rawToBytes(): Bytes {
1191
- return bytesFrom(
1192
- mol.SerializeRawTransaction({
1193
- version: numToBytes(this.version, 4),
1194
- cellDeps: this.cellDeps.map((d) => d._toMolData()),
1195
- headerDeps: this.headerDeps.map((header) => bytesFrom(header)),
1196
- inputs: this.inputs.map((i) => i._toMolData()),
1197
- outputs: this.outputs.map((o) => o._toMolData()),
1198
- outputsData: this.outputsData.map((header) => bytesFrom(header)),
1199
- }),
1200
- );
934
+ return RawTransaction.encode(this);
1201
935
  }
1202
936
 
1203
937
  /**
1204
- * Converts the whole transaction data to bytes.
938
+ * Calculates the hash of the transaction without witnesses. This is the transaction hash in the usual sense.
939
+ * To calculate the hash of the whole transaction including the witnesses, use transaction.hashFull() instead.
1205
940
  *
1206
- * @returns A Uint8Array containing the full transaction bytes.
941
+ * @returns The hash of the transaction.
1207
942
  *
1208
943
  * @example
1209
944
  * ```typescript
1210
- * const txBytes = transaction.toBytes();
945
+ * const txHash = transaction.hash();
1211
946
  * ```
1212
947
  */
1213
- toBytes(): Bytes {
1214
- return bytesFrom(
1215
- mol.SerializeTransaction({
1216
- raw: {
1217
- version: numToBytes(this.version, 4),
1218
- cellDeps: this.cellDeps.map((d) => d._toMolData()),
1219
- headerDeps: this.headerDeps.map((header) => bytesFrom(header)),
1220
- inputs: this.inputs.map((i) => i._toMolData()),
1221
- outputs: this.outputs.map((o) => o._toMolData()),
1222
- outputsData: this.outputsData.map((header) => bytesFrom(header)),
1223
- },
1224
- witnesses: this.witnesses.map((witness) => bytesFrom(witness)),
1225
- }),
1226
- );
948
+ hash(): Hex {
949
+ return hashCkb(this.rawToBytes());
1227
950
  }
1228
951
 
1229
952
  /**
1230
- * Calculates the hash of the transaction.
953
+ * Calculates the hash of the transaction with witnesses.
1231
954
  *
1232
- * @returns The hash of the transaction.
955
+ * @returns The hash of the transaction with witnesses.
1233
956
  *
1234
957
  * @example
1235
958
  * ```typescript
1236
- * const txHash = transaction.hash();
959
+ * const txFullHash = transaction.hashFull();
1237
960
  * ```
1238
961
  */
1239
-
1240
- hash(): Hex {
1241
- return hashCkb(this.rawToBytes());
962
+ hashFull(): Hex {
963
+ return hashCkb(this.toBytes());
1242
964
  }
1243
965
 
1244
966
  /**