@ckb-ccc/core 0.0.4-alpha.8 → 0.0.5-alpha.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 (209) hide show
  1. package/README.md +6 -0
  2. package/dist/ckb/transaction.d.ts +85 -2
  3. package/dist/ckb/transaction.d.ts.map +1 -1
  4. package/dist/ckb/transaction.js +152 -7
  5. package/dist/client/client.d.ts +8 -3
  6. package/dist/client/client.d.ts.map +1 -1
  7. package/dist/client/client.js +29 -2
  8. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  9. package/dist/client/clientPublicMainnet.advanced.js +4 -0
  10. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  11. package/dist/client/clientPublicTestnet.advanced.js +4 -0
  12. package/dist/client/clientTypes.d.ts +48 -1
  13. package/dist/client/clientTypes.d.ts.map +1 -1
  14. package/dist/client/clientTypes.js +27 -1
  15. package/dist/client/jsonRpc/index.d.ts +9 -1
  16. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  17. package/dist/client/jsonRpc/index.js +15 -1
  18. package/dist/client/jsonRpc/transformers.d.ts +17 -3
  19. package/dist/client/jsonRpc/transformers.d.ts.map +1 -1
  20. package/dist/client/jsonRpc/transformers.js +37 -3
  21. package/dist/client/jsonRpc/types.d.ts +15 -0
  22. package/dist/client/jsonRpc/types.d.ts.map +1 -1
  23. package/dist/fixedPoint/index.d.ts.map +1 -1
  24. package/dist/fixedPoint/index.js +4 -1
  25. package/dist/fixedPoint/index.test.d.ts +2 -0
  26. package/dist/fixedPoint/index.test.d.ts.map +1 -0
  27. package/dist/fixedPoint/index.test.js +43 -0
  28. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  29. package/dist/signer/btc/signerBtc.js +2 -3
  30. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +12 -0
  31. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
  32. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +16 -0
  33. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +12 -0
  34. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  35. package/dist/signer/ckb/signerCkbScriptReadonly.js +16 -0
  36. package/dist/signer/evm/signerEvm.d.ts +5 -1
  37. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  38. package/dist/signer/evm/signerEvm.js +21 -8
  39. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +13 -1
  40. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
  41. package/dist/signer/evm/signerEvmAddressReadonly.js +16 -0
  42. package/dist/signer/index.d.ts +0 -1
  43. package/dist/signer/index.d.ts.map +1 -1
  44. package/dist/signer/index.js +0 -1
  45. package/dist/signer/signer.d.ts +26 -6
  46. package/dist/signer/signer.d.ts.map +1 -1
  47. package/dist/signer/signer.js +14 -3
  48. package/dist.commonjs/address/address.advanced.d.ts +65 -0
  49. package/dist.commonjs/address/address.advanced.d.ts.map +1 -0
  50. package/dist.commonjs/address/address.advanced.js +149 -0
  51. package/dist.commonjs/address/advanced.d.ts +2 -0
  52. package/dist.commonjs/address/advanced.d.ts.map +1 -0
  53. package/dist.commonjs/address/advanced.js +17 -0
  54. package/dist.commonjs/address/index.d.ts +54 -0
  55. package/dist.commonjs/address/index.d.ts.map +1 -0
  56. package/dist.commonjs/address/index.js +84 -0
  57. package/dist.commonjs/advanced.d.ts +3 -0
  58. package/dist.commonjs/advanced.d.ts.map +1 -0
  59. package/dist.commonjs/advanced.js +31 -0
  60. package/dist.commonjs/advancedBarrel.d.ts +5 -0
  61. package/dist.commonjs/advancedBarrel.d.ts.map +1 -0
  62. package/dist.commonjs/advancedBarrel.js +20 -0
  63. package/dist.commonjs/barrel.d.ts +11 -0
  64. package/dist.commonjs/barrel.d.ts.map +1 -0
  65. package/dist.commonjs/barrel.js +26 -0
  66. package/dist.commonjs/bytes/advanced.d.ts +5 -0
  67. package/dist.commonjs/bytes/advanced.d.ts.map +1 -0
  68. package/dist.commonjs/bytes/advanced.js +2 -0
  69. package/dist.commonjs/bytes/index.d.ts +64 -0
  70. package/dist.commonjs/bytes/index.d.ts.map +1 -0
  71. package/dist.commonjs/bytes/index.js +97 -0
  72. package/dist.commonjs/ckb/advanced.d.ts +4 -0
  73. package/dist.commonjs/ckb/advanced.d.ts.map +1 -0
  74. package/dist.commonjs/ckb/advanced.js +32 -0
  75. package/dist.commonjs/ckb/index.d.ts +3 -0
  76. package/dist.commonjs/ckb/index.d.ts.map +1 -0
  77. package/dist.commonjs/ckb/index.js +18 -0
  78. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts +355 -0
  79. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts.map +1 -0
  80. package/dist.commonjs/ckb/molecule.advanced/generated.js +1536 -0
  81. package/dist.commonjs/ckb/molecule.advanced/index.d.ts +6 -0
  82. package/dist.commonjs/ckb/molecule.advanced/index.d.ts.map +1 -0
  83. package/dist.commonjs/ckb/molecule.advanced/index.js +25 -0
  84. package/dist.commonjs/ckb/script.advanced.d.ts +5 -0
  85. package/dist.commonjs/ckb/script.advanced.d.ts.map +1 -0
  86. package/dist.commonjs/ckb/script.advanced.js +16 -0
  87. package/dist.commonjs/ckb/script.d.ts +126 -0
  88. package/dist.commonjs/ckb/script.d.ts.map +1 -0
  89. package/dist.commonjs/ckb/script.js +188 -0
  90. package/dist.commonjs/ckb/transaction.advanced.d.ts +5 -0
  91. package/dist.commonjs/ckb/transaction.advanced.d.ts.map +1 -0
  92. package/dist.commonjs/ckb/transaction.advanced.js +12 -0
  93. package/dist.commonjs/ckb/transaction.d.ts +616 -0
  94. package/dist.commonjs/ckb/transaction.d.ts.map +1 -0
  95. package/dist.commonjs/ckb/transaction.js +810 -0
  96. package/dist.commonjs/client/advanced.d.ts +4 -0
  97. package/dist.commonjs/client/advanced.d.ts.map +1 -0
  98. package/dist.commonjs/client/advanced.js +19 -0
  99. package/dist.commonjs/client/client.d.ts +24 -0
  100. package/dist.commonjs/client/client.d.ts.map +1 -0
  101. package/dist.commonjs/client/client.js +57 -0
  102. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +4 -0
  103. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -0
  104. package/dist.commonjs/client/clientPublicMainnet.advanced.js +30 -0
  105. package/dist.commonjs/client/clientPublicMainnet.d.ts +9 -0
  106. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -0
  107. package/dist.commonjs/client/clientPublicMainnet.js +17 -0
  108. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +4 -0
  109. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -0
  110. package/dist.commonjs/client/clientPublicTestnet.advanced.js +30 -0
  111. package/dist.commonjs/client/clientPublicTestnet.d.ts +9 -0
  112. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -0
  113. package/dist.commonjs/client/clientPublicTestnet.js +17 -0
  114. package/dist.commonjs/client/clientTypes.d.ts +55 -0
  115. package/dist.commonjs/client/clientTypes.d.ts.map +1 -0
  116. package/dist.commonjs/client/clientTypes.js +31 -0
  117. package/dist.commonjs/client/index.d.ts +6 -0
  118. package/dist.commonjs/client/index.d.ts.map +1 -0
  119. package/dist.commonjs/client/index.js +21 -0
  120. package/dist.commonjs/client/jsonRpc/advanced.d.ts +3 -0
  121. package/dist.commonjs/client/jsonRpc/advanced.d.ts.map +1 -0
  122. package/dist.commonjs/client/jsonRpc/advanced.js +18 -0
  123. package/dist.commonjs/client/jsonRpc/index.d.ts +76 -0
  124. package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -0
  125. package/dist.commonjs/client/jsonRpc/index.js +142 -0
  126. package/dist.commonjs/client/jsonRpc/transformers.d.ts +42 -0
  127. package/dist.commonjs/client/jsonRpc/transformers.d.ts.map +1 -0
  128. package/dist.commonjs/client/jsonRpc/transformers.js +160 -0
  129. package/dist.commonjs/client/jsonRpc/types.d.ts +56 -0
  130. package/dist.commonjs/client/jsonRpc/types.d.ts.map +1 -0
  131. package/dist.commonjs/client/jsonRpc/types.js +2 -0
  132. package/dist.commonjs/fixedPoint/index.d.ts +49 -0
  133. package/dist.commonjs/fixedPoint/index.d.ts.map +1 -0
  134. package/dist.commonjs/fixedPoint/index.js +68 -0
  135. package/dist.commonjs/fixedPoint/index.test.d.ts +2 -0
  136. package/dist.commonjs/fixedPoint/index.test.d.ts.map +1 -0
  137. package/dist.commonjs/fixedPoint/index.test.js +45 -0
  138. package/dist.commonjs/hasher/advanced.d.ts +2 -0
  139. package/dist.commonjs/hasher/advanced.d.ts.map +1 -0
  140. package/dist.commonjs/hasher/advanced.js +4 -0
  141. package/dist.commonjs/hasher/index.d.ts +52 -0
  142. package/dist.commonjs/hasher/index.d.ts.map +1 -0
  143. package/dist.commonjs/hasher/index.js +68 -0
  144. package/dist.commonjs/hex/index.d.ts +24 -0
  145. package/dist.commonjs/hex/index.d.ts.map +1 -0
  146. package/dist.commonjs/hex/index.js +20 -0
  147. package/dist.commonjs/index.d.ts +3 -0
  148. package/dist.commonjs/index.d.ts.map +1 -0
  149. package/dist.commonjs/index.js +31 -0
  150. package/dist.commonjs/num/index.d.ts +112 -0
  151. package/dist.commonjs/num/index.d.ts.map +1 -0
  152. package/dist.commonjs/num/index.js +139 -0
  153. package/dist.commonjs/signer/btc/index.d.ts +3 -0
  154. package/dist.commonjs/signer/btc/index.d.ts.map +1 -0
  155. package/dist.commonjs/signer/btc/index.js +18 -0
  156. package/dist.commonjs/signer/btc/signerBtc.d.ts +50 -0
  157. package/dist.commonjs/signer/btc/signerBtc.d.ts.map +1 -0
  158. package/dist.commonjs/signer/btc/signerBtc.js +70 -0
  159. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +60 -0
  160. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -0
  161. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +72 -0
  162. package/dist.commonjs/signer/ckb/index.d.ts +2 -0
  163. package/dist.commonjs/signer/ckb/index.d.ts.map +1 -0
  164. package/dist.commonjs/signer/ckb/index.js +17 -0
  165. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +59 -0
  166. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -0
  167. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +71 -0
  168. package/dist.commonjs/signer/evm/index.d.ts +3 -0
  169. package/dist.commonjs/signer/evm/index.d.ts.map +1 -0
  170. package/dist.commonjs/signer/evm/index.js +18 -0
  171. package/dist.commonjs/signer/evm/signerEvm.d.ts +46 -0
  172. package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -0
  173. package/dist.commonjs/signer/evm/signerEvm.js +82 -0
  174. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +52 -0
  175. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -0
  176. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +62 -0
  177. package/dist.commonjs/signer/index.d.ts +5 -0
  178. package/dist.commonjs/signer/index.d.ts.map +1 -0
  179. package/dist.commonjs/signer/index.js +20 -0
  180. package/dist.commonjs/signer/signer.d.ts +133 -0
  181. package/dist.commonjs/signer/signer.d.ts.map +1 -0
  182. package/dist.commonjs/signer/signer.js +117 -0
  183. package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +1 -0
  184. package/dist.commonjs/utils/index.d.ts +7 -0
  185. package/dist.commonjs/utils/index.d.ts.map +1 -0
  186. package/dist.commonjs/utils/index.js +10 -0
  187. package/jest.config.js +5 -0
  188. package/package.json +24 -7
  189. package/src/ckb/transaction.ts +198 -3
  190. package/src/client/client.ts +64 -3
  191. package/src/client/clientPublicMainnet.advanced.ts +5 -0
  192. package/src/client/clientPublicTestnet.advanced.ts +5 -0
  193. package/src/client/clientTypes.ts +70 -1
  194. package/src/client/jsonRpc/index.ts +38 -2
  195. package/src/client/jsonRpc/transformers.ts +73 -4
  196. package/src/client/jsonRpc/types.ts +16 -0
  197. package/src/fixedPoint/index.test.ts +55 -0
  198. package/src/fixedPoint/index.ts +7 -1
  199. package/src/signer/btc/signerBtc.ts +6 -3
  200. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +18 -0
  201. package/src/signer/ckb/signerCkbScriptReadonly.ts +18 -0
  202. package/src/signer/evm/signerEvm.ts +56 -14
  203. package/src/signer/evm/signerEvmAddressReadonly.ts +19 -1
  204. package/src/signer/index.ts +0 -1
  205. package/src/signer/signer.ts +29 -3
  206. package/dist/signer/helpers.d.ts +0 -40
  207. package/dist/signer/helpers.d.ts.map +0 -1
  208. package/dist/signer/helpers.js +0 -90
  209. package/src/signer/helpers.ts +0 -122
@@ -0,0 +1,810 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.Transaction = exports.WitnessArgs = exports.CellDep = exports.CellInput = exports.Cell = exports.CellOutput = exports.OutPoint = exports.depTypeFromBytes = exports.depTypeToBytes = exports.depTypeFrom = void 0;
27
+ const bytes_1 = require("../bytes");
28
+ const hasher_1 = require("../hasher");
29
+ const hex_1 = require("../hex");
30
+ const num_1 = require("../num");
31
+ const utils_1 = require("../utils");
32
+ const mol = __importStar(require("./molecule.advanced"));
33
+ const script_1 = require("./script");
34
+ const transaction_advanced_1 = require("./transaction.advanced");
35
+ /**
36
+ * Converts a DepTypeLike value to a DepType.
37
+ *
38
+ * @param val - The value to convert, which can be a string, number, or bigint.
39
+ * @returns The corresponding DepType.
40
+ *
41
+ * @throws Will throw an error if the input value is not a valid dep type.
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * const depType = depTypeFrom(1); // Outputs "code"
46
+ * const depType = depTypeFrom("depGroup"); // Outputs "depGroup"
47
+ * ```
48
+ */
49
+ function depTypeFrom(val) {
50
+ const depType = (() => {
51
+ if (typeof val === "number") {
52
+ return transaction_advanced_1.NUM_TO_DEP_TYPE[val];
53
+ }
54
+ if (typeof val === "bigint") {
55
+ return transaction_advanced_1.NUM_TO_DEP_TYPE[Number(val)];
56
+ }
57
+ return val;
58
+ })();
59
+ if (depType === undefined) {
60
+ throw new Error(`Invalid dep type ${val}`);
61
+ }
62
+ return depType;
63
+ }
64
+ exports.depTypeFrom = depTypeFrom;
65
+ /**
66
+ * Converts a DepTypeLike value to its corresponding byte representation.
67
+ *
68
+ * @param depType - The dep type value to convert.
69
+ * @returns A Uint8Array containing the byte representation of the dep type.
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const depTypeBytes = depTypeToBytes("code"); // Outputs Uint8Array [1]
74
+ * ```
75
+ */
76
+ function depTypeToBytes(depType) {
77
+ return (0, bytes_1.bytesFrom)([transaction_advanced_1.DEP_TYPE_TO_NUM[depTypeFrom(depType)]]);
78
+ }
79
+ exports.depTypeToBytes = depTypeToBytes;
80
+ /**
81
+ * Converts a byte-like value to a DepType.
82
+ *
83
+ * @param bytes - The byte-like value to convert.
84
+ * @returns The corresponding DepType.
85
+ *
86
+ * @throws Will throw an error if the input bytes do not correspond to a valid dep type.
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * const depType = depTypeFromBytes(new Uint8Array([1])); // Outputs "code"
91
+ * ```
92
+ */
93
+ function depTypeFromBytes(bytes) {
94
+ return transaction_advanced_1.NUM_TO_DEP_TYPE[(0, bytes_1.bytesFrom)(bytes)[0]];
95
+ }
96
+ exports.depTypeFromBytes = depTypeFromBytes;
97
+ class OutPoint {
98
+ /**
99
+ * Creates an instance of OutPoint.
100
+ *
101
+ * @param txHash - The transaction hash.
102
+ * @param index - The index of the output in the transaction.
103
+ */
104
+ constructor(txHash, index) {
105
+ this.txHash = txHash;
106
+ this.index = index;
107
+ }
108
+ /**
109
+ * Creates an OutPoint instance from an OutPointLike object.
110
+ *
111
+ * @param outPoint - An OutPointLike object or an instance of OutPoint.
112
+ * @returns An OutPoint instance.
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * const outPoint = OutPoint.from({ txHash: "0x...", index: 0 });
117
+ * ```
118
+ */
119
+ static from(outPoint) {
120
+ if (outPoint instanceof OutPoint) {
121
+ return outPoint;
122
+ }
123
+ return new OutPoint((0, hex_1.hexFrom)(outPoint.txHash), (0, num_1.numFrom)(outPoint.index));
124
+ }
125
+ /**
126
+ * Converts the OutPoint instance to molecule data format.
127
+ *
128
+ * @returns An object representing the outpoint in molecule data format.
129
+ */
130
+ _toMolData() {
131
+ return {
132
+ txHash: (0, bytes_1.bytesFrom)(this.txHash),
133
+ index: (0, num_1.numToBytes)(this.index, 4),
134
+ };
135
+ }
136
+ /**
137
+ * Converts the OutPoint instance to bytes.
138
+ *
139
+ * @returns A Uint8Array containing the outpoint bytes.
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * const outPointBytes = outPoint.encode();
144
+ * ```
145
+ */
146
+ encode() {
147
+ return (0, bytes_1.bytesFrom)(mol.SerializeOutPoint(this._toMolData()));
148
+ }
149
+ /**
150
+ * Creates an OutPoint instance from a byte-like value or molecule OutPoint.
151
+ *
152
+ * @param bytes - The byte-like value or molecule OutPoint to convert.
153
+ * @returns An OutPoint instance.
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * const outPoint = OutPoint.fromBytes(new Uint8Array([/* outpoint bytes *\/]));
158
+ * ```
159
+ */
160
+ static fromBytes(bytes) {
161
+ const view = bytes instanceof mol.OutPoint
162
+ ? bytes
163
+ : new mol.OutPoint((0, bytes_1.bytesFrom)(bytes));
164
+ return new OutPoint((0, hex_1.hexFrom)(view.getTxHash().raw()), (0, num_1.numFromBytes)(view.getIndex().raw()));
165
+ }
166
+ }
167
+ exports.OutPoint = OutPoint;
168
+ class CellOutput {
169
+ /**
170
+ * Creates an instance of CellOutput.
171
+ *
172
+ * @param capacity - The capacity of the cell.
173
+ * @param lock - The lock script of the cell.
174
+ * @param type - The optional type script of the cell.
175
+ */
176
+ constructor(capacity, lock, type) {
177
+ this.capacity = capacity;
178
+ this.lock = lock;
179
+ this.type = type;
180
+ }
181
+ /**
182
+ * Creates a CellOutput instance from a CellOutputLike object.
183
+ *
184
+ * @param cellOutput - A CellOutputLike object or an instance of CellOutput.
185
+ * @returns A CellOutput instance.
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * const cellOutput = CellOutput.from({
190
+ * capacity: 1000n,
191
+ * lock: { codeHash: "0x...", hashType: "type", args: "0x..." },
192
+ * type: { codeHash: "0x...", hashType: "type", args: "0x..." }
193
+ * });
194
+ * ```
195
+ */
196
+ static from(cellOutput) {
197
+ if (cellOutput instanceof CellOutput) {
198
+ return cellOutput;
199
+ }
200
+ return new CellOutput((0, num_1.numFrom)(cellOutput.capacity), script_1.Script.from(cellOutput.lock), (0, utils_1.apply)(script_1.Script.from, cellOutput.type));
201
+ }
202
+ /**
203
+ * Converts the CellOutput instance to molecule data format.
204
+ *
205
+ * @returns An object representing the cell output in molecule data format.
206
+ */
207
+ _toMolData() {
208
+ return {
209
+ capacity: (0, num_1.numToBytes)(this.capacity, 8),
210
+ lock: this.lock._toMolData(),
211
+ type: this.type?._toMolData(),
212
+ };
213
+ }
214
+ /**
215
+ * Converts the CellOutput instance to bytes.
216
+ *
217
+ * @returns A Uint8Array containing the cell output bytes.
218
+ *
219
+ * @example
220
+ * ```typescript
221
+ * const cellOutputBytes = cellOutput.toBytes();
222
+ * ```
223
+ */
224
+ toBytes() {
225
+ return (0, bytes_1.bytesFrom)(mol.SerializeCellOutput(this._toMolData()));
226
+ }
227
+ /**
228
+ * Creates a CellOutput instance from a byte-like value or molecule CellOutput.
229
+ *
230
+ * @param bytes - The byte-like value or molecule CellOutput to convert.
231
+ * @returns A CellOutput instance.
232
+ *
233
+ * @example
234
+ * ```typescript
235
+ * const cellOutput = CellOutput.fromBytes(new Uint8Array([/* cell output bytes *\/]));
236
+ * ```
237
+ */
238
+ static fromBytes(bytes) {
239
+ const view = bytes instanceof mol.CellOutput
240
+ ? bytes
241
+ : new mol.CellOutput((0, bytes_1.bytesFrom)(bytes));
242
+ return new CellOutput((0, num_1.numFromBytes)(view.getCapacity().raw()), script_1.Script.fromBytes(view.getLock()), (0, utils_1.apply)(script_1.Script.fromBytes, mol.molOptional(view.getType())));
243
+ }
244
+ }
245
+ exports.CellOutput = CellOutput;
246
+ class Cell {
247
+ /**
248
+ * Creates an instance of Cell.
249
+ *
250
+ * @param outPoint - The output point of the cell.
251
+ * @param cellOutput - The cell output of the cell.
252
+ * @param outputData - The output data of the cell.
253
+ * @param blockNumber - The block number of the cell.
254
+ */
255
+ constructor(outPoint, cellOutput, outputData, blockNumber) {
256
+ this.outPoint = outPoint;
257
+ this.cellOutput = cellOutput;
258
+ this.outputData = outputData;
259
+ this.blockNumber = blockNumber;
260
+ }
261
+ /**
262
+ * Creates a Cell instance from a CellLike object.
263
+ *
264
+ * @param cell - A CellLike object or an instance of Cell.
265
+ * @returns A Cell instance.
266
+ */
267
+ static from(cell) {
268
+ if (cell instanceof Cell) {
269
+ return cell;
270
+ }
271
+ return new Cell(OutPoint.from(cell.outPoint), CellOutput.from(cell.cellOutput), (0, hex_1.hexFrom)(cell.outputData), (0, num_1.numFrom)(cell.blockNumber));
272
+ }
273
+ }
274
+ exports.Cell = Cell;
275
+ class CellInput {
276
+ /**
277
+ * Creates an instance of CellInput.
278
+ *
279
+ * @param previousOutput - The previous outpoint of the cell.
280
+ * @param since - The since value of the cell input.
281
+ * @param cellOutput - The optional cell output associated with the cell input.
282
+ * @param outputData - The optional output data associated with the cell input.
283
+ * @param blockNumber - The optional block number associated with the cell input.
284
+ */
285
+ constructor(previousOutput, since, cellOutput, outputData, blockNumber) {
286
+ this.previousOutput = previousOutput;
287
+ this.since = since;
288
+ this.cellOutput = cellOutput;
289
+ this.outputData = outputData;
290
+ this.blockNumber = blockNumber;
291
+ }
292
+ /**
293
+ * Creates a CellInput instance from a CellInputLike object.
294
+ *
295
+ * @param cellInput - A CellInputLike object or an instance of CellInput.
296
+ * @returns A CellInput instance.
297
+ *
298
+ * @example
299
+ * ```typescript
300
+ * const cellInput = CellInput.from({
301
+ * previousOutput: { txHash: "0x...", index: 0 },
302
+ * since: 0n
303
+ * });
304
+ * ```
305
+ */
306
+ static from(cellInput) {
307
+ if (cellInput instanceof CellInput) {
308
+ return cellInput;
309
+ }
310
+ return new CellInput(OutPoint.from(cellInput.previousOutput), (0, num_1.numFrom)(cellInput.since), (0, utils_1.apply)(CellOutput.from, cellInput.cellOutput), (0, utils_1.apply)(hex_1.hexFrom, cellInput.outputData), (0, utils_1.apply)(num_1.numFrom, cellInput.blockNumber));
311
+ }
312
+ /**
313
+ * Complete extra infos in the input. Like the output of the out point.
314
+ * The instance will be modified.
315
+ *
316
+ * @returns The completed instance.
317
+ * @example
318
+ * ```typescript
319
+ * if (!cellInput.cellOutput) {
320
+ * await cellInput.completeExtraInfos();
321
+ * }
322
+ * ```
323
+ */
324
+ async completeExtraInfos(client) {
325
+ if (this.cellOutput && this.outputData && this.blockNumber) {
326
+ return this;
327
+ }
328
+ const cell = await client.getCell(this.previousOutput);
329
+ if (cell) {
330
+ this.cellOutput = cell.cellOutput;
331
+ this.outputData = cell.outputData;
332
+ this.blockNumber = cell.blockNumber;
333
+ }
334
+ return this;
335
+ }
336
+ /**
337
+ * Converts the CellInput instance to molecule data format.
338
+ *
339
+ * @returns An object representing the cell input in molecule data format.
340
+ */
341
+ _toMolData() {
342
+ return {
343
+ previousOutput: this.previousOutput._toMolData(),
344
+ since: (0, num_1.numToBytes)(this.since, 8),
345
+ };
346
+ }
347
+ /**
348
+ * Converts the CellInput instance to bytes.
349
+ *
350
+ * @returns A Uint8Array containing the cell input bytes.
351
+ *
352
+ * @example
353
+ * ```typescript
354
+ * const cellInputBytes = cellInput.toBytes();
355
+ * ```
356
+ */
357
+ toBytes() {
358
+ return (0, bytes_1.bytesFrom)(mol.SerializeCellInput(this._toMolData()));
359
+ }
360
+ /**
361
+ * Creates a CellInput instance from a byte-like value or molecule CellInput.
362
+ *
363
+ * @param bytes - The byte-like value or molecule CellInput to convert.
364
+ * @returns A CellInput instance.
365
+ *
366
+ * @example
367
+ * ```typescript
368
+ * const cellInput = CellInput.fromBytes(new Uint8Array([/* cell input bytes *\/]));
369
+ * ```
370
+ */
371
+ static fromBytes(bytes) {
372
+ const view = bytes instanceof mol.CellInput
373
+ ? bytes
374
+ : new mol.CellInput((0, bytes_1.bytesFrom)(bytes));
375
+ return new CellInput(OutPoint.fromBytes(view.getPreviousOutput()), (0, num_1.numFromBytes)(view.getSince().raw()));
376
+ }
377
+ }
378
+ exports.CellInput = CellInput;
379
+ class CellDep {
380
+ /**
381
+ * Creates an instance of CellDep.
382
+ *
383
+ * @param outPoint - The outpoint of the cell dependency.
384
+ * @param depType - The dependency type.
385
+ */
386
+ constructor(outPoint, depType) {
387
+ this.outPoint = outPoint;
388
+ this.depType = depType;
389
+ }
390
+ /**
391
+ * Creates a CellDep instance from a CellDepLike object.
392
+ *
393
+ * @param cellDep - A CellDepLike object or an instance of CellDep.
394
+ * @returns A CellDep instance.
395
+ *
396
+ * @example
397
+ * ```typescript
398
+ * const cellDep = CellDep.from({
399
+ * outPoint: { txHash: "0x...", index: 0 },
400
+ * depType: "depGroup"
401
+ * });
402
+ * ```
403
+ */
404
+ static from(cellDep) {
405
+ if (cellDep instanceof CellDep) {
406
+ return cellDep;
407
+ }
408
+ return new CellDep(OutPoint.from(cellDep.outPoint), depTypeFrom(cellDep.depType));
409
+ }
410
+ /**
411
+ * Converts the CellDep instance to molecule data format.
412
+ *
413
+ * @returns An object representing the cell dependency in molecule data format.
414
+ */
415
+ _toMolData() {
416
+ return {
417
+ outPoint: this.outPoint._toMolData(),
418
+ depType: depTypeToBytes(this.depType),
419
+ };
420
+ }
421
+ /**
422
+ * Converts the CellDep instance to bytes.
423
+ *
424
+ * @returns A Uint8Array containing the cell dependency bytes.
425
+ *
426
+ * @example
427
+ * ```typescript
428
+ * const cellDepBytes = cellDep.toBytes();
429
+ * ```
430
+ */
431
+ toBytes() {
432
+ return (0, bytes_1.bytesFrom)(mol.SerializeCellDep(this._toMolData()));
433
+ }
434
+ /**
435
+ * Creates a CellDep instance from a byte-like value or molecule CellDep.
436
+ *
437
+ * @param bytes - The byte-like value or molecule CellDep to convert.
438
+ * @returns A CellDep instance.
439
+ *
440
+ * @example
441
+ * ```typescript
442
+ * const cellDep = CellDep.fromBytes(new Uint8Array([/* cell dep bytes *\/]));
443
+ * ```
444
+ */
445
+ fromBytes(bytes) {
446
+ const view = bytes instanceof mol.CellDep ? bytes : new mol.CellDep((0, bytes_1.bytesFrom)(bytes));
447
+ return new CellDep(OutPoint.fromBytes(view.getOutPoint()), depTypeFromBytes([view.getDepType()]));
448
+ }
449
+ }
450
+ exports.CellDep = CellDep;
451
+ class WitnessArgs {
452
+ /**
453
+ * Creates an instance of WitnessArgs.
454
+ *
455
+ * @param lock - The optional lock field of the witness.
456
+ * @param inputType - The optional input type field of the witness.
457
+ * @param outputType - The optional output type field of the witness.
458
+ */
459
+ constructor(lock, inputType, outputType) {
460
+ this.lock = lock;
461
+ this.inputType = inputType;
462
+ this.outputType = outputType;
463
+ }
464
+ /**
465
+ * Creates a WitnessArgs instance from a WitnessArgsLike object.
466
+ *
467
+ * @param witnessArgs - A WitnessArgsLike object or an instance of WitnessArgs.
468
+ * @returns A WitnessArgs instance.
469
+ *
470
+ * @example
471
+ * ```typescript
472
+ * const witnessArgs = WitnessArgs.from({
473
+ * lock: "0x...",
474
+ * inputType: "0x...",
475
+ * outputType: "0x..."
476
+ * });
477
+ * ```
478
+ */
479
+ static from(witnessArgs) {
480
+ if (witnessArgs instanceof WitnessArgs) {
481
+ return witnessArgs;
482
+ }
483
+ return new WitnessArgs((0, utils_1.apply)(hex_1.hexFrom, witnessArgs.lock), (0, utils_1.apply)(hex_1.hexFrom, witnessArgs.inputType), (0, utils_1.apply)(hex_1.hexFrom, witnessArgs.outputType));
484
+ }
485
+ /**
486
+ * Converts the WitnessArgs instance to molecule data format.
487
+ *
488
+ * @returns An object representing the witness arguments in molecule data format.
489
+ */
490
+ _toMolData() {
491
+ return {
492
+ lock: (0, utils_1.apply)(bytes_1.bytesFrom, this.lock),
493
+ inputType: (0, utils_1.apply)(bytes_1.bytesFrom, this.inputType),
494
+ outputType: (0, utils_1.apply)(bytes_1.bytesFrom, this.outputType),
495
+ };
496
+ }
497
+ /**
498
+ * Converts the WitnessArgs instance to bytes.
499
+ *
500
+ * @returns A Uint8Array containing the witness arguments bytes.
501
+ *
502
+ * @example
503
+ * ```typescript
504
+ * const witnessArgsBytes = witnessArgs.toBytes();
505
+ * ```
506
+ */
507
+ toBytes() {
508
+ return (0, bytes_1.bytesFrom)(mol.SerializeWitnessArgs(this._toMolData()));
509
+ }
510
+ /**
511
+ * Creates a WitnessArgs instance from a byte-like value or molecule WitnessArgs.
512
+ *
513
+ * @param bytes - The byte-like value or molecule WitnessArgs to convert.
514
+ * @returns A WitnessArgs instance.
515
+ *
516
+ * @example
517
+ * ```typescript
518
+ * const witnessArgs = WitnessArgs.fromBytes(new Uint8Array([/* witness args bytes *\/]));
519
+ * ```
520
+ */
521
+ static fromBytes(bytes) {
522
+ const view = bytes instanceof mol.WitnessArgs
523
+ ? bytes
524
+ : new mol.WitnessArgs((0, bytes_1.bytesFrom)(bytes));
525
+ return new WitnessArgs((0, utils_1.apply)(hex_1.hexFrom, mol.molOptional(view.getLock())?.raw()), (0, utils_1.apply)(hex_1.hexFrom, mol.molOptional(view.getInputType())?.raw()), (0, utils_1.apply)(hex_1.hexFrom, mol.molOptional(view.getOutputType())?.raw()));
526
+ }
527
+ }
528
+ exports.WitnessArgs = WitnessArgs;
529
+ class Transaction {
530
+ /**
531
+ * Creates an instance of Transaction.
532
+ *
533
+ * @param version - The version of the transaction.
534
+ * @param cellDeps - The cell dependencies of the transaction.
535
+ * @param headerDeps - The header dependencies of the transaction.
536
+ * @param inputs - The inputs of the transaction.
537
+ * @param outputs - The outputs of the transaction.
538
+ * @param outputsData - The data associated with the outputs.
539
+ * @param witnesses - The witnesses of the transaction.
540
+ */
541
+ constructor(version, cellDeps, headerDeps, inputs, outputs, outputsData, witnesses) {
542
+ this.version = version;
543
+ this.cellDeps = cellDeps;
544
+ this.headerDeps = headerDeps;
545
+ this.inputs = inputs;
546
+ this.outputs = outputs;
547
+ this.outputsData = outputsData;
548
+ this.witnesses = witnesses;
549
+ }
550
+ /**
551
+ * Creates a default Transaction instance with empty fields.
552
+ *
553
+ * @returns A default Transaction instance.
554
+ *
555
+ * @example
556
+ * ```typescript
557
+ * const defaultTx = Transaction.default();
558
+ * ```
559
+ */
560
+ static default() {
561
+ return new Transaction(0n, [], [], [], [], [], []);
562
+ }
563
+ /**
564
+ * Creates a Transaction instance from a TransactionLike object.
565
+ *
566
+ * @param tx - A TransactionLike object or an instance of Transaction.
567
+ * @returns A Transaction instance.
568
+ *
569
+ * @example
570
+ * ```typescript
571
+ * const transaction = Transaction.from({
572
+ * version: 0,
573
+ * cellDeps: [],
574
+ * headerDeps: [],
575
+ * inputs: [],
576
+ * outputs: [],
577
+ * outputsData: [],
578
+ * witnesses: []
579
+ * });
580
+ * ```
581
+ */
582
+ static from(tx) {
583
+ if (tx instanceof Transaction) {
584
+ return tx;
585
+ }
586
+ return new Transaction((0, num_1.numFrom)(tx.version), tx.cellDeps.map((cellDep) => CellDep.from(cellDep)), tx.headerDeps.map(hex_1.hexFrom), tx.inputs.map((input) => CellInput.from(input)), tx.outputs.map((output) => CellOutput.from(output)), tx.outputsData.map(hex_1.hexFrom), tx.witnesses.map(hex_1.hexFrom));
587
+ }
588
+ /**
589
+ * Creates a Transaction instance from a Lumos skeleton.
590
+ *
591
+ * @param skeleton - The Lumos transaction skeleton.
592
+ * @returns A Transaction instance.
593
+ *
594
+ * @throws Will throw an error if an input's outPoint is missing.
595
+ *
596
+ * @example
597
+ * ```typescript
598
+ * const transaction = Transaction.fromLumosSkeleton(skeleton);
599
+ * ```
600
+ */
601
+ static fromLumosSkeleton(skeleton) {
602
+ return Transaction.from({
603
+ version: 0n,
604
+ cellDeps: skeleton.cellDeps.toArray(),
605
+ headerDeps: skeleton.headerDeps.toArray(),
606
+ inputs: skeleton.inputs.toArray().map((input, i) => {
607
+ if (!input.outPoint) {
608
+ throw new Error("outPoint is required in input");
609
+ }
610
+ return CellInput.from({
611
+ previousOutput: input.outPoint,
612
+ since: skeleton.inputSinces.get(i, "0x0"),
613
+ cellOutput: input.cellOutput,
614
+ outputData: input.data,
615
+ });
616
+ }),
617
+ outputs: skeleton.outputs.toArray().map((output) => output.cellOutput),
618
+ outputsData: skeleton.outputs.toArray().map((output) => output.data),
619
+ witnesses: skeleton.witnesses.toArray(),
620
+ });
621
+ }
622
+ stringify() {
623
+ return JSON.stringify(this, (_, value) => {
624
+ if (typeof value === "bigint") {
625
+ return (0, num_1.numToHex)(value);
626
+ }
627
+ return value;
628
+ });
629
+ }
630
+ /**
631
+ * Converts the raw transaction data to bytes.
632
+ *
633
+ * @returns A Uint8Array containing the raw transaction bytes.
634
+ *
635
+ * @example
636
+ * ```typescript
637
+ * const rawTxBytes = transaction.rawToBytes();
638
+ * ```
639
+ */
640
+ rawToBytes() {
641
+ return (0, bytes_1.bytesFrom)(mol.SerializeRawTransaction({
642
+ version: (0, num_1.numToBytes)(this.version, 4),
643
+ cellDeps: this.cellDeps.map((d) => d._toMolData()),
644
+ headerDeps: this.headerDeps.map((header) => (0, bytes_1.bytesFrom)(header)),
645
+ inputs: this.inputs.map((i) => i._toMolData()),
646
+ outputs: this.outputs.map((o) => o._toMolData()),
647
+ outputsData: this.outputsData.map((header) => (0, bytes_1.bytesFrom)(header)),
648
+ }));
649
+ }
650
+ /**
651
+ * Calculates the hash of the transaction.
652
+ *
653
+ * @returns The hash of the transaction.
654
+ *
655
+ * @example
656
+ * ```typescript
657
+ * const txHash = transaction.hash();
658
+ * ```
659
+ */
660
+ hash() {
661
+ return (0, hasher_1.ckbHash)(this.rawToBytes());
662
+ }
663
+ /**
664
+ * Hashes a witness and updates the hasher.
665
+ *
666
+ * @param witness - The witness to hash.
667
+ * @param hasher - The hasher instance to update.
668
+ *
669
+ * @example
670
+ * ```typescript
671
+ * Transaction.hashWitnessToHasher("0x...", hasher);
672
+ * ```
673
+ */
674
+ static hashWitnessToHasher(witness, hasher) {
675
+ const raw = (0, bytes_1.bytesFrom)((0, hex_1.hexFrom)(witness));
676
+ hasher.update((0, num_1.numToBytes)(raw.length, 8));
677
+ hasher.update(raw);
678
+ }
679
+ /**
680
+ * Computes the signing hash information for a given script.
681
+ *
682
+ * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
683
+ * @param client - The client for complete extra infos in the transaction.
684
+ * @returns A promise that resolves to an object containing the signing message and the witness position,
685
+ * or undefined if no matching input is found.
686
+ *
687
+ * @example
688
+ * ```typescript
689
+ * const signHashInfo = await tx.getSignHashInfo(scriptLike, client);
690
+ * if (signHashInfo) {
691
+ * console.log(signHashInfo.message); // Outputs the signing message
692
+ * console.log(signHashInfo.position); // Outputs the witness position
693
+ * }
694
+ * ```
695
+ */
696
+ async getSignHashInfo(scriptLike, client) {
697
+ const script = script_1.Script.from(scriptLike);
698
+ let position = -1;
699
+ const hasher = new hasher_1.Hasher();
700
+ hasher.update(this.hash());
701
+ for (let i = 0; i < this.witnesses.length; i += 1) {
702
+ if (this.inputs[i]) {
703
+ const input = await this.inputs[i].completeExtraInfos(client);
704
+ if (!input.cellOutput) {
705
+ throw Error("Unable to resolve inputs info");
706
+ }
707
+ if (!script.eq(input.cellOutput.lock)) {
708
+ continue;
709
+ }
710
+ if (position === -1) {
711
+ position = i;
712
+ }
713
+ }
714
+ if (position === -1) {
715
+ return undefined;
716
+ }
717
+ Transaction.hashWitnessToHasher(this.witnesses[i], hasher);
718
+ }
719
+ if (position === -1) {
720
+ return undefined;
721
+ }
722
+ return {
723
+ message: hasher.digest(),
724
+ position,
725
+ };
726
+ }
727
+ /**
728
+ * Find the first occurrence of a input with the specified lock
729
+ *
730
+ * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
731
+ * @param client - The client for complete extra infos in the transaction.
732
+ * @returns A promise that resolves to the prepared transaction
733
+ *
734
+ * @example
735
+ * ```typescript
736
+ * const index = await tx.findInputIndexByLock(scriptLike, client);
737
+ * ```
738
+ */
739
+ async findInputIndexByLock(scriptLike, client) {
740
+ const script = script_1.Script.from(scriptLike);
741
+ for (let i = 0; i < this.inputs.length; i += 1) {
742
+ const input = await this.inputs[i].completeExtraInfos(client);
743
+ if (!input.cellOutput) {
744
+ throw Error("Unable to resolve inputs info");
745
+ }
746
+ if (script.eq(input.cellOutput.lock)) {
747
+ return i;
748
+ }
749
+ }
750
+ }
751
+ /**
752
+ * Get witness at index as WitnessArgs
753
+ *
754
+ * @param index - The index of the witness.
755
+ * @returns The witness parsed as WitnessArgs.
756
+ *
757
+ * @example
758
+ * ```typescript
759
+ * const witnessArgs = await tx.getWitnessArgsAt(0);
760
+ * ```
761
+ */
762
+ getWitnessArgsAt(index) {
763
+ const rawWitness = this.witnesses[index];
764
+ return (rawWitness ?? "0x") !== "0x"
765
+ ? WitnessArgs.fromBytes(rawWitness)
766
+ : undefined;
767
+ }
768
+ /**
769
+ * Set witness at index by WitnessArgs
770
+ *
771
+ * @param index - The index of the witness.
772
+ * @param witness - The WitnessArgs to set.
773
+ * @returns The transaction itself.
774
+ *
775
+ * @example
776
+ * ```typescript
777
+ * await tx.setWitnessArgsAt(0, witnessArgs);
778
+ * ```
779
+ */
780
+ setWitnessArgsAt(index, witness) {
781
+ if (this.witnesses.length < index) {
782
+ this.witnesses.push(...Array.from(new Array(index - this.witnesses.length), () => "0x"));
783
+ }
784
+ this.witnesses[index] = (0, hex_1.hexFrom)(witness.toBytes());
785
+ return this;
786
+ }
787
+ /**
788
+ * Prepare dummy witness for sighash all method
789
+ *
790
+ * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
791
+ * @param lockLen - The length of dummy lock bytes.
792
+ * @param client - The client for complete extra infos in the transaction.
793
+ * @returns A promise that resolves to the prepared transaction
794
+ *
795
+ * @example
796
+ * ```typescript
797
+ * await tx.prepareSighashAllWitness(scriptLike, 85, client);
798
+ * ```
799
+ */
800
+ async prepareSighashAllWitness(scriptLike, lockLen, client) {
801
+ const position = await this.findInputIndexByLock(scriptLike, client);
802
+ if (position === undefined) {
803
+ return this;
804
+ }
805
+ const witness = this.getWitnessArgsAt(position) ?? WitnessArgs.from({});
806
+ witness.lock = (0, hex_1.hexFrom)(Array.from(new Array(lockLen), () => 0));
807
+ return this.setWitnessArgsAt(position, witness);
808
+ }
809
+ }
810
+ exports.Transaction = Transaction;