@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.
Files changed (110) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/address/address.advanced.d.ts.map +1 -1
  3. package/dist/address/address.advanced.js +6 -2
  4. package/dist/barrel.d.ts +1 -1
  5. package/dist/barrel.d.ts.map +1 -1
  6. package/dist/barrel.js +1 -1
  7. package/dist/bytes/index.d.ts +35 -0
  8. package/dist/bytes/index.d.ts.map +1 -1
  9. package/dist/bytes/index.js +60 -4
  10. package/dist/ckb/advanced.d.ts +0 -1
  11. package/dist/ckb/advanced.d.ts.map +1 -1
  12. package/dist/ckb/advanced.js +0 -1
  13. package/dist/ckb/script.d.ts +17 -58
  14. package/dist/ckb/script.d.ts.map +1 -1
  15. package/dist/ckb/script.js +29 -78
  16. package/dist/ckb/transaction.d.ts +125 -264
  17. package/dist/ckb/transaction.d.ts.map +1 -1
  18. package/dist/ckb/transaction.js +146 -357
  19. package/dist/molecule/barrel.d.ts +4 -0
  20. package/dist/molecule/barrel.d.ts.map +1 -0
  21. package/dist/molecule/barrel.js +3 -0
  22. package/dist/molecule/codec.d.ts +2 -0
  23. package/dist/molecule/codec.d.ts.map +1 -1
  24. package/dist/molecule/codec.js +57 -44
  25. package/dist/molecule/entity.d.ts +119 -0
  26. package/dist/molecule/entity.d.ts.map +1 -0
  27. package/dist/molecule/entity.js +134 -0
  28. package/dist/molecule/index.d.ts +1 -2
  29. package/dist/molecule/index.d.ts.map +1 -1
  30. package/dist/molecule/index.js +1 -2
  31. package/dist/molecule/predefined.d.ts +13 -20
  32. package/dist/molecule/predefined.d.ts.map +1 -1
  33. package/dist/molecule/predefined.js +14 -54
  34. package/dist/num/index.d.ts.map +1 -1
  35. package/dist/num/index.js +2 -2
  36. package/dist/signer/btc/verify.js +1 -1
  37. package/dist/signer/doge/verify.d.ts.map +1 -1
  38. package/dist/signer/doge/verify.js +3 -1
  39. package/dist/utils/index.d.ts +3 -0
  40. package/dist/utils/index.d.ts.map +1 -1
  41. package/dist.commonjs/address/address.advanced.d.ts.map +1 -1
  42. package/dist.commonjs/address/address.advanced.js +6 -2
  43. package/dist.commonjs/barrel.d.ts +1 -1
  44. package/dist.commonjs/barrel.d.ts.map +1 -1
  45. package/dist.commonjs/barrel.js +1 -14
  46. package/dist.commonjs/bytes/index.d.ts +35 -0
  47. package/dist.commonjs/bytes/index.d.ts.map +1 -1
  48. package/dist.commonjs/bytes/index.js +62 -4
  49. package/dist.commonjs/ckb/advanced.d.ts +0 -1
  50. package/dist.commonjs/ckb/advanced.d.ts.map +1 -1
  51. package/dist.commonjs/ckb/advanced.js +0 -14
  52. package/dist.commonjs/ckb/script.d.ts +17 -58
  53. package/dist.commonjs/ckb/script.d.ts.map +1 -1
  54. package/dist.commonjs/ckb/script.js +29 -102
  55. package/dist.commonjs/ckb/transaction.d.ts +125 -264
  56. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  57. package/dist.commonjs/ckb/transaction.js +167 -408
  58. package/dist.commonjs/molecule/barrel.d.ts +4 -0
  59. package/dist.commonjs/molecule/barrel.d.ts.map +1 -0
  60. package/dist.commonjs/{ckb/molecule.advanced/index.js → molecule/barrel.js} +3 -8
  61. package/dist.commonjs/molecule/codec.d.ts +2 -0
  62. package/dist.commonjs/molecule/codec.d.ts.map +1 -1
  63. package/dist.commonjs/molecule/codec.js +56 -43
  64. package/dist.commonjs/molecule/entity.d.ts +119 -0
  65. package/dist.commonjs/molecule/entity.d.ts.map +1 -0
  66. package/dist.commonjs/molecule/entity.js +139 -0
  67. package/dist.commonjs/molecule/index.d.ts +1 -2
  68. package/dist.commonjs/molecule/index.d.ts.map +1 -1
  69. package/dist.commonjs/molecule/index.js +13 -4
  70. package/dist.commonjs/molecule/predefined.d.ts +13 -20
  71. package/dist.commonjs/molecule/predefined.d.ts.map +1 -1
  72. package/dist.commonjs/molecule/predefined.js +14 -77
  73. package/dist.commonjs/num/index.d.ts.map +1 -1
  74. package/dist.commonjs/num/index.js +2 -2
  75. package/dist.commonjs/signer/btc/verify.js +1 -1
  76. package/dist.commonjs/signer/doge/verify.d.ts.map +1 -1
  77. package/dist.commonjs/signer/doge/verify.js +3 -1
  78. package/dist.commonjs/utils/index.d.ts +3 -0
  79. package/dist.commonjs/utils/index.d.ts.map +1 -1
  80. package/package.json +1 -1
  81. package/src/address/address.advanced.ts +6 -2
  82. package/src/barrel.ts +1 -1
  83. package/src/bytes/index.ts +70 -6
  84. package/src/ckb/advanced.ts +0 -1
  85. package/src/ckb/script.ts +20 -85
  86. package/src/ckb/transaction.ts +123 -416
  87. package/src/molecule/barrel.ts +3 -0
  88. package/src/molecule/codec.ts +82 -66
  89. package/src/molecule/entity.ts +183 -0
  90. package/src/molecule/index.ts +1 -2
  91. package/src/molecule/predefined.ts +20 -69
  92. package/src/num/index.ts +2 -5
  93. package/src/signer/btc/verify.ts +1 -1
  94. package/src/signer/doge/verify.ts +3 -1
  95. package/src/utils/index.ts +5 -0
  96. package/dist/ckb/molecule.advanced/generated.d.ts +0 -355
  97. package/dist/ckb/molecule.advanced/generated.d.ts.map +0 -1
  98. package/dist/ckb/molecule.advanced/generated.js +0 -1468
  99. package/dist/ckb/molecule.advanced/index.d.ts +0 -6
  100. package/dist/ckb/molecule.advanced/index.d.ts.map +0 -1
  101. package/dist/ckb/molecule.advanced/index.js +0 -7
  102. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts +0 -355
  103. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts.map +0 -1
  104. package/dist.commonjs/ckb/molecule.advanced/generated.js +0 -1535
  105. package/dist.commonjs/ckb/molecule.advanced/index.d.ts +0 -6
  106. package/dist.commonjs/ckb/molecule.advanced/index.d.ts.map +0 -1
  107. package/src/ckb/molecule.advanced/blockchain.mol +0 -118
  108. package/src/ckb/molecule.advanced/generated.d.ts +0 -444
  109. package/src/ckb/molecule.advanced/generated.js +0 -1804
  110. package/src/ckb/molecule.advanced/index.ts +0 -11
@@ -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,
@@ -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,
@@ -16,12 +17,17 @@ import {
16
17
  numToBytes,
17
18
  numToHex,
18
19
  } from "../num/index.js";
19
- import { Signer } from "../signer/index.js";
20
+ import type { 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
- import { LumosTransactionSkeletonType } from "./transactionLumos.js";
24
+ import type { LumosTransactionSkeletonType } from "./transactionLumos.js";
25
+
26
+ export const DepTypeCodec: mol.Codec<DepTypeLike, DepType> = mol.Codec.from({
27
+ byteLength: 1,
28
+ encode: depTypeToBytes,
29
+ decode: depTypeFromBytes,
30
+ });
25
31
 
26
32
  /**
27
33
  * @public
@@ -112,7 +118,13 @@ export type OutPointLike = {
112
118
  /**
113
119
  * @public
114
120
  */
115
- export class OutPoint {
121
+ @mol.codec(
122
+ mol.struct({
123
+ txHash: mol.Byte32,
124
+ index: mol.Uint32,
125
+ }),
126
+ )
127
+ export class OutPoint extends mol.Entity.Base<OutPointLike, OutPoint>() {
116
128
  /**
117
129
  * Creates an instance of OutPoint.
118
130
  *
@@ -123,20 +135,8 @@ export class OutPoint {
123
135
  constructor(
124
136
  public txHash: Hex,
125
137
  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);
138
+ ) {
139
+ super();
140
140
  }
141
141
 
142
142
  /**
@@ -156,74 +156,6 @@ export class OutPoint {
156
156
  }
157
157
  return new OutPoint(hexFrom(outPoint.txHash), numFrom(outPoint.index));
158
158
  }
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
159
  }
228
160
 
229
161
  /**
@@ -237,7 +169,14 @@ export type CellOutputLike = {
237
169
  /**
238
170
  * @public
239
171
  */
240
- export class CellOutput {
172
+ @mol.codec(
173
+ mol.table({
174
+ capacity: mol.Uint64,
175
+ lock: Script,
176
+ type: ScriptOpt,
177
+ }),
178
+ )
179
+ export class CellOutput extends mol.Entity.Base<CellOutputLike, CellOutput>() {
241
180
  /**
242
181
  * Creates an instance of CellOutput.
243
182
  *
@@ -250,26 +189,14 @@ export class CellOutput {
250
189
  public capacity: Num,
251
190
  public lock: Script,
252
191
  public type?: Script,
253
- ) {}
192
+ ) {
193
+ super();
194
+ }
254
195
 
255
196
  get occupiedSize(): number {
256
197
  return 8 + this.lock.occupiedSize + (this.type?.occupiedSize ?? 0);
257
198
  }
258
199
 
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
200
  /**
274
201
  * Creates a CellOutput instance from a CellOutputLike object.
275
202
  *
@@ -296,61 +223,8 @@ export class CellOutput {
296
223
  apply(Script.from, cellOutput.type),
297
224
  );
298
225
  }
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
226
  }
227
+ export const CellOutputVec = mol.vector(CellOutput);
354
228
 
355
229
  /**
356
230
  * @public
@@ -468,7 +342,10 @@ export type SinceLike =
468
342
  /**
469
343
  * @public
470
344
  */
471
- export class Since {
345
+ @mol.codec(
346
+ mol.Uint64.mapIn((encodable: SinceLike) => Since.from(encodable).toNum()),
347
+ )
348
+ export class Since extends mol.Entity.Base<SinceLike, Since>() {
472
349
  /**
473
350
  * Creates an instance of Since.
474
351
  *
@@ -481,7 +358,9 @@ export class Since {
481
358
  public relative: "absolute" | "relative",
482
359
  public metric: "blockNumber" | "epoch" | "timestamp",
483
360
  public value: Num,
484
- ) {}
361
+ ) {
362
+ super();
363
+ }
485
364
 
486
365
  /**
487
366
  * Clone a Since.
@@ -580,7 +459,18 @@ export type CellInputLike = {
580
459
  /**
581
460
  * @public
582
461
  */
583
- export class CellInput {
462
+ @mol.codec(
463
+ mol
464
+ .struct({
465
+ since: Since,
466
+ previousOutput: OutPoint,
467
+ })
468
+ .mapIn((encodable: CellInputLike) => ({
469
+ ...encodable,
470
+ since: encodable.since ?? 0,
471
+ })),
472
+ )
473
+ export class CellInput extends mol.Entity.Base<CellInputLike, CellInput>() {
584
474
  /**
585
475
  * Creates an instance of CellInput.
586
476
  *
@@ -595,25 +485,8 @@ export class CellInput {
595
485
  public since: Num,
596
486
  public cellOutput?: CellOutput,
597
487
  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
- );
488
+ ) {
489
+ super();
617
490
  }
618
491
 
619
492
  /**
@@ -666,59 +539,8 @@ export class CellInput {
666
539
  this.cellOutput = cell.cellOutput;
667
540
  this.outputData = cell.outputData;
668
541
  }
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
542
  }
543
+ export const CellInputVec = mol.vector(CellInput);
722
544
 
723
545
  /**
724
546
  * @public
@@ -730,7 +552,13 @@ export type CellDepLike = {
730
552
  /**
731
553
  * @public
732
554
  */
733
- export class CellDep {
555
+ @mol.codec(
556
+ mol.struct({
557
+ outPoint: OutPoint,
558
+ depType: DepTypeCodec,
559
+ }),
560
+ )
561
+ export class CellDep extends mol.Entity.Base<CellDepLike, CellDep>() {
734
562
  /**
735
563
  * Creates an instance of CellDep.
736
564
  *
@@ -741,7 +569,9 @@ export class CellDep {
741
569
  constructor(
742
570
  public outPoint: OutPoint,
743
571
  public depType: DepType,
744
- ) {}
572
+ ) {
573
+ super();
574
+ }
745
575
 
746
576
  /**
747
577
  * Clone a CellDep.
@@ -783,75 +613,8 @@ export class CellDep {
783
613
  depTypeFrom(cellDep.depType),
784
614
  );
785
615
  }
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
616
  }
617
+ export const CellDepVec = mol.vector(CellDep);
855
618
 
856
619
  /**
857
620
  * @public
@@ -864,7 +627,17 @@ export type WitnessArgsLike = {
864
627
  /**
865
628
  * @public
866
629
  */
867
- export class WitnessArgs {
630
+ @mol.codec(
631
+ mol.table({
632
+ lock: mol.BytesOpt,
633
+ inputType: mol.BytesOpt,
634
+ outputType: mol.BytesOpt,
635
+ }),
636
+ )
637
+ export class WitnessArgs extends mol.Entity.Base<
638
+ WitnessArgsLike,
639
+ WitnessArgs
640
+ >() {
868
641
  /**
869
642
  * Creates an instance of WitnessArgs.
870
643
  *
@@ -877,7 +650,9 @@ export class WitnessArgs {
877
650
  public lock?: Hex,
878
651
  public inputType?: Hex,
879
652
  public outputType?: Hex,
880
- ) {}
653
+ ) {
654
+ super();
655
+ }
881
656
 
882
657
  /**
883
658
  * Creates a WitnessArgs instance from a WitnessArgsLike object.
@@ -906,60 +681,6 @@ export class WitnessArgs {
906
681
  apply(hexFrom, witnessArgs.outputType),
907
682
  );
908
683
  }
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
684
  }
964
685
 
965
686
  /**
@@ -974,6 +695,15 @@ export function udtBalanceFrom(dataLike: BytesLike): Num {
974
695
  return numFromBytes(data);
975
696
  }
976
697
 
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
+ });
706
+
977
707
  /**
978
708
  * @public
979
709
  */
@@ -992,7 +722,25 @@ export type TransactionLike = {
992
722
  /**
993
723
  * @public
994
724
  */
995
- export class Transaction {
725
+ @mol.codec(
726
+ mol
727
+ .table({
728
+ raw: RawTransaction,
729
+ witnesses: mol.BytesVec,
730
+ })
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 })),
739
+ )
740
+ export class Transaction extends mol.Entity.Base<
741
+ TransactionLike,
742
+ Transaction
743
+ >() {
996
744
  /**
997
745
  * Creates an instance of Transaction.
998
746
  *
@@ -1013,7 +761,9 @@ export class Transaction {
1013
761
  public outputs: CellOutput[],
1014
762
  public outputsData: Hex[],
1015
763
  public witnesses: Hex[],
1016
- ) {}
764
+ ) {
765
+ super();
766
+ }
1017
767
 
1018
768
  /**
1019
769
  * Creates a default Transaction instance with empty fields.
@@ -1048,28 +798,6 @@ export class Transaction {
1048
798
  this.witnesses = tx.witnesses;
1049
799
  }
1050
800
 
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
801
  /**
1074
802
  * Creates a Transaction instance from a TransactionLike object.
1075
803
  *
@@ -1188,57 +916,36 @@ export class Transaction {
1188
916
  * ```
1189
917
  */
1190
918
  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
- );
919
+ return RawTransaction.encode(this);
1201
920
  }
1202
921
 
1203
922
  /**
1204
- * Converts the whole transaction data to bytes.
923
+ * Calculates the hash of the transaction without witnesses. This is the transaction hash in the usual sense.
924
+ * To calculate the hash of the whole transaction including the witnesses, use transaction.hashFull() instead.
1205
925
  *
1206
- * @returns A Uint8Array containing the full transaction bytes.
926
+ * @returns The hash of the transaction.
1207
927
  *
1208
928
  * @example
1209
929
  * ```typescript
1210
- * const txBytes = transaction.toBytes();
930
+ * const txHash = transaction.hash();
1211
931
  * ```
1212
932
  */
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
- );
933
+ hash(): Hex {
934
+ return hashCkb(this.rawToBytes());
1227
935
  }
1228
936
 
1229
937
  /**
1230
- * Calculates the hash of the transaction.
938
+ * Calculates the hash of the transaction with witnesses.
1231
939
  *
1232
- * @returns The hash of the transaction.
940
+ * @returns The hash of the transaction with witnesses.
1233
941
  *
1234
942
  * @example
1235
943
  * ```typescript
1236
- * const txHash = transaction.hash();
944
+ * const txFullHash = transaction.hashFull();
1237
945
  * ```
1238
946
  */
1239
-
1240
- hash(): Hex {
1241
- return hashCkb(this.rawToBytes());
947
+ hashFull(): Hex {
948
+ return hashCkb(this.toBytes());
1242
949
  }
1243
950
 
1244
951
  /**