@ckb-ccc/core 1.16.0 → 1.17.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 (53) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/address.advanced-DpcI420e.mjs +2 -0
  3. package/dist/address.advanced-DpcI420e.mjs.map +1 -0
  4. package/dist/advanced.d.mts +1 -1
  5. package/dist/advanced.mjs +1 -1
  6. package/dist/{advancedBarrel-4xTrm-An.d.mts → advancedBarrel-Bk19lQjR.d.mts} +1151 -161
  7. package/dist/advancedBarrel-Bk19lQjR.d.mts.map +1 -0
  8. package/dist/advancedBarrel.d.mts +1 -1
  9. package/dist/advancedBarrel.mjs +1 -1
  10. package/dist/barrel-BwuHQ4NS.mjs +7 -0
  11. package/dist/barrel-BwuHQ4NS.mjs.map +1 -0
  12. package/dist/barrel.d.mts +1 -1
  13. package/dist/barrel.mjs +1 -1
  14. package/dist/index.d.mts +1 -1
  15. package/dist/index.mjs +1 -1
  16. package/dist.commonjs/address.advanced--1eOp1GP.js +2 -0
  17. package/dist.commonjs/address.advanced--1eOp1GP.js.map +1 -0
  18. package/dist.commonjs/advanced.d.ts +1 -1
  19. package/dist.commonjs/advanced.js +1 -1
  20. package/dist.commonjs/{advancedBarrel-XD_hMVay.d.ts → advancedBarrel-DrAHz5UG.d.ts} +1151 -161
  21. package/dist.commonjs/advancedBarrel-DrAHz5UG.d.ts.map +1 -0
  22. package/dist.commonjs/advancedBarrel.d.ts +1 -1
  23. package/dist.commonjs/advancedBarrel.js +1 -1
  24. package/dist.commonjs/barrel-BIo8E9Ci.js +21 -0
  25. package/dist.commonjs/barrel-BIo8E9Ci.js.map +1 -0
  26. package/dist.commonjs/barrel.d.ts +1 -1
  27. package/dist.commonjs/barrel.js +1 -1
  28. package/dist.commonjs/index.d.ts +1 -1
  29. package/dist.commonjs/index.js +1 -1
  30. package/dist.commonjs/rolldown-runtime-DWNI8pZO.js +1 -0
  31. package/package.json +4 -3
  32. package/src/ckb/epoch.ts +9 -9
  33. package/src/ckb/script.ts +10 -2
  34. package/src/ckb/transaction.ts +301 -75
  35. package/src/client/jsonRpc/client.ts +7 -3
  36. package/src/codec/entity.ts +93 -0
  37. package/src/molecule/codec.ts +44 -20
  38. package/src/signer/btc/signerBtc.ts +1 -1
  39. package/src/signer/ckb/signerCkbPrivateKey.ts +2 -3
  40. package/src/signer/ckb/signerMultisigCkbReadonly.ts +5 -5
  41. package/src/signer/doge/signerDoge.ts +1 -1
  42. package/src/signer/evm/signerEvm.ts +2 -2
  43. package/src/signer/nostr/signerNostr.ts +1 -1
  44. package/dist/address.advanced-8N_Hcv6p.mjs +0 -2
  45. package/dist/address.advanced-8N_Hcv6p.mjs.map +0 -1
  46. package/dist/advancedBarrel-4xTrm-An.d.mts.map +0 -1
  47. package/dist/barrel-D3tMRxi5.mjs +0 -7
  48. package/dist/barrel-D3tMRxi5.mjs.map +0 -1
  49. package/dist.commonjs/address.advanced-CUIioHfv.js +0 -2
  50. package/dist.commonjs/address.advanced-CUIioHfv.js.map +0 -1
  51. package/dist.commonjs/advancedBarrel-XD_hMVay.d.ts.map +0 -1
  52. package/dist.commonjs/barrel-D6tLiPED.js +0 -21
  53. package/dist.commonjs/barrel-D6tLiPED.js.map +0 -1
@@ -32,6 +32,30 @@ import {
32
32
  } from "./transactionErrors.js";
33
33
  import type { LumosTransactionSkeletonType } from "./transactionLumos.js";
34
34
 
35
+ const MAX_ARRAY_INDEX = 2n ** 32n - 2n;
36
+
37
+ function transactionIndexFrom(index: NumLike): number | undefined {
38
+ try {
39
+ const value = numFrom(index);
40
+ if (value < 0n || value > MAX_ARRAY_INDEX) {
41
+ return;
42
+ }
43
+ return Number(value);
44
+ } catch (_) {
45
+ return;
46
+ }
47
+ }
48
+
49
+ function transactionIndexOrThrow(index: number): number {
50
+ const value = transactionIndexFrom(index);
51
+ if (value === undefined) {
52
+ throw new Error(
53
+ `Index ${index} must be a valid array index between 0 and ${MAX_ARRAY_INDEX}`,
54
+ );
55
+ }
56
+ return value;
57
+ }
58
+
35
59
  export const DepTypeCodec: Codec<DepTypeLike, DepType> = Codec.from({
36
60
  byteLength: 1,
37
61
  encode: depTypeToBytes,
@@ -177,7 +201,10 @@ export class OutPoint extends Entity.Base<OutPointLike, OutPoint>() {
177
201
  * ```
178
202
  */
179
203
  clone(): OutPoint {
180
- return new OutPoint(this.txHash, this.index);
204
+ return OutPoint.from({
205
+ txHash: this.txHash,
206
+ index: this.index,
207
+ });
181
208
  }
182
209
 
183
210
  /**
@@ -297,7 +324,11 @@ export class CellOutput extends Entity.Base<CellOutputLike, CellOutput>() {
297
324
  * ```
298
325
  */
299
326
  clone(): CellOutput {
300
- return new CellOutput(this.capacity, this.lock.clone(), this.type?.clone());
327
+ return CellOutput.from({
328
+ capacity: this.capacity,
329
+ lock: this.lock.clone(),
330
+ type: this.type?.clone(),
331
+ });
301
332
  }
302
333
  }
303
334
  export const CellOutputVec = mol.vector(CellOutput);
@@ -442,11 +473,11 @@ export class CellAny {
442
473
  * ```
443
474
  */
444
475
  clone(): CellAny {
445
- return new CellAny(
446
- this.cellOutput.clone(),
447
- this.outputData,
448
- this.outPoint?.clone(),
449
- );
476
+ return CellAny.from({
477
+ cellOutput: this.cellOutput.clone(),
478
+ outputData: this.outputData,
479
+ outPoint: this.outPoint?.clone(),
480
+ });
450
481
  }
451
482
  }
452
483
 
@@ -646,11 +677,11 @@ export class Cell extends CellAny {
646
677
  * ```
647
678
  */
648
679
  clone(): Cell {
649
- return new Cell(
650
- this.outPoint.clone(),
651
- this.cellOutput.clone(),
652
- this.outputData,
653
- );
680
+ return Cell.from({
681
+ outPoint: this.outPoint.clone(),
682
+ cellOutput: this.cellOutput.clone(),
683
+ outputData: this.outputData,
684
+ });
654
685
  }
655
686
  }
656
687
 
@@ -698,7 +729,11 @@ export class Since extends Entity.Base<SinceLike, Since>() {
698
729
  * ```
699
730
  */
700
731
  clone(): Since {
701
- return new Since(this.relative, this.metric, this.value);
732
+ return Since.from({
733
+ relative: this.relative,
734
+ metric: this.metric,
735
+ value: this.value,
736
+ });
702
737
  }
703
738
 
704
739
  /**
@@ -905,12 +940,12 @@ export class CellInput extends Entity.Base<CellInputLike, CellInput>() {
905
940
  * ```
906
941
  */
907
942
  clone(): CellInput {
908
- return new CellInput(
909
- this.previousOutput.clone(),
910
- this.since,
911
- this.cellOutput?.clone(),
912
- this.outputData,
913
- );
943
+ return CellInput.from({
944
+ previousOutput: this.previousOutput.clone(),
945
+ since: this.since,
946
+ cellOutput: this.cellOutput?.clone(),
947
+ outputData: this.outputData,
948
+ });
914
949
  }
915
950
  }
916
951
  export const CellInputVec = mol.vector(CellInput);
@@ -983,7 +1018,10 @@ export class CellDep extends Entity.Base<CellDepLike, CellDep>() {
983
1018
  * ```
984
1019
  */
985
1020
  clone(): CellDep {
986
- return new CellDep(this.outPoint.clone(), this.depType);
1021
+ return CellDep.from({
1022
+ outPoint: this.outPoint.clone(),
1023
+ depType: this.depType,
1024
+ });
987
1025
  }
988
1026
  }
989
1027
  export const CellDepVec = mol.vector(CellDep);
@@ -1140,7 +1178,7 @@ export class Transaction extends Entity.Base<TransactionLike, Transaction>() {
1140
1178
  * ```
1141
1179
  */
1142
1180
  static default(): Transaction {
1143
- return new Transaction(0n, [], [], [], [], [], []);
1181
+ return Transaction.from({});
1144
1182
  }
1145
1183
 
1146
1184
  /**
@@ -1203,15 +1241,15 @@ export class Transaction extends Entity.Base<TransactionLike, Transaction>() {
1203
1241
  * - Version - bigint is immutable
1204
1242
  */
1205
1243
  clone(): Transaction {
1206
- return new Transaction(
1207
- this.version,
1208
- this.cellDeps.map((c) => c.clone()),
1209
- this.headerDeps.map((h) => h),
1210
- this.inputs.map((i) => i.clone()),
1211
- this.outputs.map((o) => o.clone()),
1212
- this.outputsData.map((o) => o),
1213
- this.witnesses.map((w) => w),
1214
- );
1244
+ return Transaction.from({
1245
+ version: this.version,
1246
+ cellDeps: this.cellDeps.map((c) => c.clone()),
1247
+ headerDeps: this.headerDeps.map((h) => h),
1248
+ inputs: this.inputs.map((i) => i.clone()),
1249
+ outputs: this.outputs.map((o) => o.clone()),
1250
+ outputsData: this.outputsData.map((o) => o),
1251
+ witnesses: this.witnesses.map((w) => w),
1252
+ });
1215
1253
  }
1216
1254
 
1217
1255
  /**
@@ -1606,41 +1644,75 @@ export class Transaction extends Entity.Base<TransactionLike, Transaction>() {
1606
1644
  }
1607
1645
 
1608
1646
  /**
1609
- * Set output data at index.
1647
+ * Sets output data at an index.
1648
+ *
1649
+ * @deprecated Use {@link setOutputData} instead.
1650
+ */
1651
+ setOutputDataAt(index: number, data: HexLike): void {
1652
+ this.setOutputData(index, data);
1653
+ }
1654
+
1655
+ /**
1656
+ * Sets output data at an index, filling any skipped positions with empty data.
1610
1657
  *
1611
1658
  * @param index - The index of the output data.
1612
1659
  * @param data - The data to set.
1660
+ * @throws If `index` is invalid or does not identify an existing output.
1613
1661
  *
1614
1662
  * @example
1615
1663
  * ```typescript
1616
- * tx.setOutputDataAt(0, "0x00");
1664
+ * tx.setOutputData(0, "0x00");
1617
1665
  * ```
1618
1666
  */
1619
- setOutputDataAt(index: number, data: HexLike): void {
1620
- if (this.outputsData.length < index) {
1667
+ setOutputData(index: number, data: HexLike): void {
1668
+ const i = transactionIndexOrThrow(index);
1669
+ if (i >= this.outputs.length) {
1670
+ throw new Error(
1671
+ `Index ${index} is out of bounds for outputs of length ${this.outputs.length}`,
1672
+ );
1673
+ }
1674
+
1675
+ if (this.outputsData.length < i) {
1621
1676
  this.outputsData.push(
1622
- ...Array.from(
1623
- new Array(index - this.outputsData.length),
1624
- (): Hex => "0x",
1625
- ),
1677
+ ...Array.from(new Array(i - this.outputsData.length), (): Hex => "0x"),
1626
1678
  );
1627
1679
  }
1628
1680
 
1629
- this.outputsData[index] = hexFrom(data);
1681
+ this.outputsData[i] = hexFrom(data);
1682
+ }
1683
+
1684
+ /**
1685
+ * Gets the data associated with an output.
1686
+ *
1687
+ * @param index - The output index.
1688
+ * @returns The output data, or `undefined` if the index is invalid or out of bounds.
1689
+ *
1690
+ * @example
1691
+ * ```typescript
1692
+ * const data = tx.getOutputData(0);
1693
+ * ```
1694
+ */
1695
+ getOutputData(index: NumLike): Hex | undefined {
1696
+ const i = transactionIndexFrom(index);
1697
+ return i !== undefined && i < this.outputs.length
1698
+ ? (this.outputsData[i] ?? "0x")
1699
+ : undefined;
1630
1700
  }
1631
1701
 
1632
1702
  /**
1633
- * get input
1703
+ * Gets an input.
1634
1704
  *
1635
- * @param index - The cell input index
1705
+ * @param index - The cell input index.
1706
+ * @returns The input, or `undefined` if the index is invalid or out of bounds.
1636
1707
  *
1637
1708
  * @example
1638
1709
  * ```typescript
1639
- * await tx.getInput(0);
1710
+ * tx.getInput(0);
1640
1711
  * ```
1641
1712
  */
1642
1713
  getInput(index: NumLike): CellInput | undefined {
1643
- return this.inputs[Number(numFrom(index))];
1714
+ const i = transactionIndexFrom(index);
1715
+ return i === undefined ? undefined : this.inputs[i];
1644
1716
  }
1645
1717
  /**
1646
1718
  * add input
@@ -1661,18 +1733,19 @@ export class Transaction extends Entity.Base<TransactionLike, Transaction>() {
1661
1733
  }
1662
1734
 
1663
1735
  /**
1664
- * get output
1736
+ * Gets an output cell together with its data.
1665
1737
  *
1666
- * @param index - The cell output index
1738
+ * @param index - The cell output index.
1739
+ * @returns The output cell, or `undefined` if the index is invalid or out of bounds.
1667
1740
  *
1668
1741
  * @example
1669
1742
  * ```typescript
1670
- * await tx.getOutput(0);
1743
+ * tx.getOutput(0);
1671
1744
  * ```
1672
1745
  */
1673
1746
  getOutput(index: NumLike): CellAny | undefined {
1674
- const i = Number(numFrom(index));
1675
- if (i >= this.outputs.length) {
1747
+ const i = transactionIndexFrom(index);
1748
+ if (i === undefined || i >= this.outputs.length) {
1676
1749
  return;
1677
1750
  }
1678
1751
  return CellAny.from({
@@ -1755,85 +1828,238 @@ export class Transaction extends Entity.Base<TransactionLike, Transaction>() {
1755
1828
  });
1756
1829
 
1757
1830
  const len = this.outputs.push(cell.cellOutput);
1758
- this.setOutputDataAt(len - 1, cell.outputData);
1831
+ this.setOutputData(len - 1, cell.outputData);
1759
1832
 
1760
1833
  return len;
1761
1834
  }
1762
1835
 
1763
1836
  /**
1764
- * Get witness at index as WitnessArgs
1837
+ * Sets or appends an output cell.
1838
+ *
1839
+ * An index equal to the current output count appends the cell. Existing
1840
+ * outputs are replaced together with their corresponding output data.
1841
+ *
1842
+ * @param index - The output index to set, from zero through the current output count.
1843
+ * @param cellLike - A cell-like object containing both the output and its data.
1844
+ * @throws If `index` is not a valid array index or is greater than the output count.
1845
+ *
1846
+ * @example
1847
+ * ```typescript
1848
+ * tx.setOutput(0, {
1849
+ * cellOutput: { lock: recipientLock },
1850
+ * outputData: "0x1234",
1851
+ * });
1852
+ * ```
1853
+ */
1854
+ setOutput(index: number, cellLike: CellAnyLike): void;
1855
+
1856
+ /**
1857
+ * Sets or appends an output and its data.
1858
+ *
1859
+ * @param index - The output index to set, from zero through the current output count.
1860
+ * @param outputLike - The cell output to set.
1861
+ * @param outputDataLike - The output data, defaulting to `"0x"`.
1862
+ * @throws If `index` is not a valid array index or is greater than the output count.
1863
+ *
1864
+ * @example
1865
+ * ```typescript
1866
+ * tx.setOutput(0, { lock: recipientLock }, "0x1234");
1867
+ * ```
1868
+ */
1869
+ setOutput(
1870
+ index: number,
1871
+ outputLike: CellOutputLike,
1872
+ outputDataLike?: HexLike | null,
1873
+ ): void;
1874
+ setOutput(
1875
+ index: number,
1876
+ cellOrOutputLike: CellAnyLike | CellOutputLike,
1877
+ outputDataLike?: HexLike | null,
1878
+ ): void {
1879
+ const i = transactionIndexOrThrow(index);
1880
+ if (i > this.outputs.length) {
1881
+ throw new Error(
1882
+ `Index ${index} is out of bounds for outputs of length ${this.outputs.length}`,
1883
+ );
1884
+ }
1885
+
1886
+ const cell =
1887
+ "cellOutput" in cellOrOutputLike
1888
+ ? CellAny.from(cellOrOutputLike)
1889
+ : CellAny.from({
1890
+ cellOutput: cellOrOutputLike,
1891
+ outputData: outputDataLike,
1892
+ });
1893
+
1894
+ if (i === this.outputs.length) {
1895
+ this.addOutput(cell);
1896
+ } else {
1897
+ this.outputs[i] = cell.cellOutput;
1898
+ this.setOutputData(i, cell.outputData);
1899
+ }
1900
+ }
1901
+
1902
+ /**
1903
+ * Sets or appends an input.
1904
+ *
1905
+ * An index equal to the current input count appends the input using
1906
+ * {@link addInput}, preserving witness alignment. An existing index replaces
1907
+ * only that input.
1908
+ *
1909
+ * @param index - The input index to set, from zero through the current input count.
1910
+ * @param inputLike - The cell input to set.
1911
+ * @throws If `index` is not a valid array index or is greater than the input count.
1912
+ *
1913
+ * @example
1914
+ * ```typescript
1915
+ * tx.setInput(0, {
1916
+ * previousOutput: { txHash: "0x...", index: 0 },
1917
+ * });
1918
+ * ```
1919
+ */
1920
+ setInput(index: number, inputLike: CellInputLike): void {
1921
+ const i = transactionIndexOrThrow(index);
1922
+ if (i > this.inputs.length) {
1923
+ throw new Error(
1924
+ `Index ${index} is out of bounds for inputs of length ${this.inputs.length}`,
1925
+ );
1926
+ }
1927
+
1928
+ if (i === this.inputs.length) {
1929
+ this.addInput(inputLike);
1930
+ } else {
1931
+ this.inputs[i] = CellInput.from(inputLike);
1932
+ }
1933
+ }
1934
+
1935
+ /**
1936
+ * Gets a raw witness.
1937
+ *
1938
+ * @param index - The witness index.
1939
+ * @returns The witness, or `undefined` if the index is invalid or out of bounds.
1940
+ *
1941
+ * @example
1942
+ * ```typescript
1943
+ * const witness = tx.getWitness(0);
1944
+ * ```
1945
+ */
1946
+ getWitness(index: NumLike): Hex | undefined {
1947
+ const i = transactionIndexFrom(index);
1948
+ return i === undefined ? undefined : this.witnesses[i];
1949
+ }
1950
+
1951
+ /**
1952
+ * Gets the witness at an index as `WitnessArgs`.
1765
1953
  *
1766
1954
  * @param index - The index of the witness.
1767
1955
  * @returns The witness parsed as WitnessArgs.
1768
1956
  *
1769
1957
  * @example
1770
1958
  * ```typescript
1771
- * const witnessArgs = tx.getWitnessArgsAt(0);
1959
+ * const witnessArgs = tx.getWitnessArgs(0);
1772
1960
  * ```
1773
1961
  */
1774
- getWitnessArgsAt(index: number): WitnessArgs | undefined {
1962
+ getWitnessArgs(index: NumLike): WitnessArgs | undefined {
1775
1963
  try {
1776
- return this.getWitnessArgsAtUnsafe(index);
1964
+ return this.getWitnessArgsUnsafe(index);
1777
1965
  } catch (_) {
1778
1966
  return undefined;
1779
1967
  }
1780
1968
  }
1781
1969
 
1782
1970
  /**
1783
- * Get witness at index as WitnessArgs, throw if failed to decode
1971
+ * Gets the witness at an index as `WitnessArgs`, throwing if decoding fails.
1784
1972
  *
1785
1973
  * @param index - The index of the witness.
1786
1974
  * @returns The witness parsed as WitnessArgs.
1975
+ * @throws If the witness cannot be decoded as `WitnessArgs`.
1787
1976
  *
1788
1977
  * @example
1789
1978
  * ```typescript
1790
- * const witnessArgs = tx.getWitnessArgsAtUnsafe(0);
1979
+ * const witnessArgs = tx.getWitnessArgsUnsafe(0);
1791
1980
  * ```
1792
1981
  */
1793
- getWitnessArgsAtUnsafe(index: number): WitnessArgs | undefined {
1794
- const rawWitness = this.witnesses[index];
1795
- return (rawWitness ?? "0x") !== "0x"
1982
+ getWitnessArgsUnsafe(index: NumLike): WitnessArgs | undefined {
1983
+ const rawWitness = this.getWitness(index);
1984
+ return rawWitness && rawWitness !== "0x"
1796
1985
  ? WitnessArgs.fromBytes(rawWitness)
1797
1986
  : undefined;
1798
1987
  }
1799
1988
 
1800
1989
  /**
1801
- * Set witness at index by WitnessArgs
1990
+ * Sets a witness at an index from `WitnessArgs`.
1802
1991
  *
1803
1992
  * @param index - The index of the witness.
1804
- * @param witness - The WitnessArgs to set.
1993
+ * @param witnessLike - The WitnessArgs-like value to set.
1994
+ * @throws If `index` is not a valid array index.
1805
1995
  *
1806
1996
  * @example
1807
1997
  * ```typescript
1808
- * await tx.setWitnessArgsAt(0, witnessArgs);
1998
+ * tx.setWitnessArgs(0, witnessArgs);
1809
1999
  * ```
1810
2000
  */
1811
- setWitnessArgsAt(index: number, witness: WitnessArgs): void {
1812
- this.setWitnessAt(index, witness.toBytes());
2001
+ setWitnessArgs(index: number, witnessLike: WitnessArgsLike): void {
2002
+ this.setWitness(index, WitnessArgs.from(witnessLike).toBytes());
1813
2003
  }
1814
2004
 
1815
2005
  /**
1816
- * Set witness at index
2006
+ * Sets a witness at an index, filling any skipped positions with empty witnesses.
1817
2007
  *
1818
2008
  * @param index - The index of the witness.
1819
2009
  * @param witness - The witness to set.
2010
+ * @throws If `index` is not a valid array index.
1820
2011
  *
1821
2012
  * @example
1822
2013
  * ```typescript
1823
- * await tx.setWitnessAt(0, witness);
2014
+ * tx.setWitness(0, witness);
1824
2015
  * ```
1825
2016
  */
1826
- setWitnessAt(index: number, witness: HexLike): void {
1827
- if (this.witnesses.length < index) {
2017
+ setWitness(index: number, witness: HexLike): void {
2018
+ const i = transactionIndexOrThrow(index);
2019
+
2020
+ if (this.witnesses.length < i) {
1828
2021
  this.witnesses.push(
1829
- ...Array.from(
1830
- new Array(index - this.witnesses.length),
1831
- (): Hex => "0x",
1832
- ),
2022
+ ...Array.from(new Array(i - this.witnesses.length), (): Hex => "0x"),
1833
2023
  );
1834
2024
  }
1835
2025
 
1836
- this.witnesses[index] = hexFrom(witness);
2026
+ this.witnesses[i] = hexFrom(witness);
2027
+ }
2028
+
2029
+ /**
2030
+ * Gets the witness at an index as `WitnessArgs`.
2031
+ *
2032
+ * @deprecated Use {@link getWitnessArgs} instead.
2033
+ */
2034
+ getWitnessArgsAt(index: NumLike): WitnessArgs | undefined {
2035
+ return this.getWitnessArgs(index);
2036
+ }
2037
+
2038
+ /**
2039
+ * Gets the witness at an index as `WitnessArgs`, throwing if decoding fails.
2040
+ *
2041
+ * @deprecated Use {@link getWitnessArgsUnsafe} instead.
2042
+ */
2043
+ getWitnessArgsAtUnsafe(index: NumLike): WitnessArgs | undefined {
2044
+ return this.getWitnessArgsUnsafe(index);
2045
+ }
2046
+
2047
+ /**
2048
+ * Sets a witness at an index from `WitnessArgs`.
2049
+ *
2050
+ * @deprecated Use {@link setWitnessArgs} instead.
2051
+ */
2052
+ setWitnessArgsAt(index: number, witnessLike: WitnessArgsLike): void {
2053
+ this.setWitnessArgs(index, witnessLike);
2054
+ }
2055
+
2056
+ /**
2057
+ * Sets a witness at an index.
2058
+ *
2059
+ * @deprecated Use {@link setWitness} instead.
2060
+ */
2061
+ setWitnessAt(index: number, witness: HexLike): void {
2062
+ this.setWitness(index, witness);
1837
2063
  }
1838
2064
 
1839
2065
  /**
@@ -1859,9 +2085,9 @@ export class Transaction extends Entity.Base<TransactionLike, Transaction>() {
1859
2085
  return;
1860
2086
  }
1861
2087
 
1862
- const witness = this.getWitnessArgsAt(position) ?? WitnessArgs.from({});
2088
+ const witness = this.getWitnessArgs(position) ?? WitnessArgs.from({});
1863
2089
  witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
1864
- this.setWitnessArgsAt(position, witness);
2090
+ this.setWitnessArgs(position, witness);
1865
2091
  }
1866
2092
 
1867
2093
  async getInputsCapacityExtra(client: Client): Promise<Num> {
@@ -2563,5 +2789,5 @@ export function calcDaoClaimEpoch(
2563
2789
  withdrawInteger += -partialCycle + fullCycle;
2564
2790
  }
2565
2791
 
2566
- return new Epoch(withdrawInteger, deposit.numerator, deposit.denominator);
2792
+ return Epoch.from([withdrawInteger, deposit.numerator, deposit.denominator]);
2567
2793
  }
@@ -13,6 +13,7 @@ import { Num, NumLike, numFrom, numToHex } from "../../num/index.js";
13
13
  import { apply } from "../../utils/index.js";
14
14
  import { ClientCache } from "../cache/index.js";
15
15
  import { Client } from "../client.js";
16
+ import { DEFAULT_MIN_FEE_RATE } from "../clientTypes.advanced.js";
16
17
  import {
17
18
  ClientFindCellsResponse,
18
19
  ClientIndexerSearchKeyLike,
@@ -131,9 +132,12 @@ export abstract class ClientJsonRpc extends Client {
131
132
  getFeeRateStatistics = this.buildSender(
132
133
  "get_fee_rate_statistics",
133
134
  [(n: NumLike) => apply(numFrom, n)],
134
- ({ mean, median }: { mean: NumLike; median: NumLike }) => ({
135
- mean: numFrom(mean),
136
- median: numFrom(median),
135
+ // TODO: This is a temporary workaround for the fact that the node may return null for fee rate statistics.
136
+ // We should change the method signature to return null instead of falling back to the default value,
137
+ // but this would be a breaking change.
138
+ (res: { mean: NumLike; median: NumLike } | null) => ({
139
+ mean: numFrom(res?.mean ?? DEFAULT_MIN_FEE_RATE),
140
+ median: numFrom(res?.median ?? DEFAULT_MIN_FEE_RATE),
137
141
  }),
138
142
  ) as Client["getFeeRateStatistics"];
139
143
 
@@ -1,7 +1,9 @@
1
1
  import { Bytes, bytesEq, bytesFrom, BytesLike } from "../bytes/index.js";
2
2
  import { hashCkb } from "../hasher/index.js";
3
3
  import { Hex, hexFrom } from "../hex/index.js";
4
+ import type { UnionDecoded, UnionMatchHandlers } from "../molecule/codec.js";
4
5
  import { Constructor } from "../utils/index.js";
6
+ import type { CodecLike, DecodedType } from "./codec.js";
5
7
 
6
8
  /**
7
9
  * The base class of CCC to create a serializable instance. This should be used with the {@link codec} decorator.
@@ -144,10 +146,101 @@ export abstract class Entity {
144
146
  return Impl;
145
147
  }
146
148
 
149
+ /**
150
+ * Convert the entity to bytes.
151
+ * @public
152
+ * @returns The bytes representation of the entity.
153
+ */
147
154
  abstract toBytes(): Bytes;
155
+
156
+ /**
157
+ * Calculate the hash of the entity.
158
+ * @public
159
+ * @returns The hash of the entity.
160
+ */
148
161
  abstract hash(): Hex;
162
+
163
+ /**
164
+ * Convert the entity to a full-byte untrimmed Hex representation.
165
+ * @public
166
+ * @returns The entity full-byte untrimmed hexadecimal representation.
167
+ */
149
168
  abstract toHex(): Hex;
169
+
170
+ /**
171
+ * Create a clone of the entity.
172
+ * @public
173
+ * @returns A clone of the entity.
174
+ */
150
175
  abstract clone(): Entity;
176
+
177
+ /**
178
+ * Generate a base class of CCC to create a serializable Union instance.
179
+ * This should be used with the {@link codec} decorator.
180
+ * @public
181
+ */
182
+ static BaseUnion<
183
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
184
+ CodecType extends CodecLike<any, UnionDecoded<any, any>>,
185
+ SubTypeLike,
186
+ SubType = SubTypeLike,
187
+ >() {
188
+ type Handlers<Result> = UnionMatchHandlers<CodecType, Result>;
189
+ type Decoded = DecodedType<CodecType>;
190
+
191
+ abstract class Impl extends Entity.Base<SubTypeLike, SubType>() {
192
+ /**
193
+ * The inner decoded object representing the union value.
194
+ * @public
195
+ */
196
+ constructor(public readonly inner: Decoded) {
197
+ super();
198
+ }
199
+
200
+ /**
201
+ * Match the union type with a set of handlers.
202
+ * @public
203
+ * @param handlers - A map of type-specific handlers, all variants must be handled if no default handler `_` is provided.
204
+ * @returns The result returned by the matching handler.
205
+ */
206
+ match<Result>(
207
+ handlers: Handlers<Result> & {
208
+ _?: undefined | null;
209
+ },
210
+ ): Result;
211
+ /**
212
+ * Match the union type with a set of handlers.
213
+ * @public
214
+ * @param handlers - A map of type-specific handlers, unmatched variants will be handled by the default handler `_`.
215
+ * @returns The result returned by the matching handler.
216
+ */
217
+ match<Result>(
218
+ handlers: Partial<Handlers<Result>> & {
219
+ _: (inner: Decoded) => Result;
220
+ },
221
+ ): Result;
222
+ match<Result>(
223
+ handlers:
224
+ | (Handlers<Result> & {
225
+ _?: undefined | null;
226
+ })
227
+ | (Partial<Handlers<Result>> & {
228
+ _: (inner: Decoded) => Result;
229
+ }),
230
+ ): Result {
231
+ const type = this.inner.type as Decoded["type"];
232
+ const handler = (handlers as Partial<Handlers<Result>>)[type];
233
+
234
+ if (handler != undefined) {
235
+ return handler(this.inner.value);
236
+ }
237
+
238
+ return (handlers._ as (inner: Decoded) => Result)(this.inner);
239
+ }
240
+ }
241
+
242
+ return Impl;
243
+ }
151
244
  }
152
245
 
153
246
  /**