@ckb-ccc/core 0.0.6-alpha.0 → 0.0.7-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 (215) hide show
  1. package/dist/address/address.advanced.d.ts.map +1 -1
  2. package/dist/address/address.advanced.js +2 -5
  3. package/dist/address/index.d.ts +1 -1
  4. package/dist/address/index.d.ts.map +1 -1
  5. package/dist/address/index.js +2 -5
  6. package/dist/ckb/script.d.ts +43 -1
  7. package/dist/ckb/script.d.ts.map +1 -1
  8. package/dist/ckb/script.js +51 -0
  9. package/dist/ckb/transaction.d.ts +198 -21
  10. package/dist/ckb/transaction.d.ts.map +1 -1
  11. package/dist/ckb/transaction.js +437 -21
  12. package/dist/client/advanced.d.ts +2 -0
  13. package/dist/client/advanced.d.ts.map +1 -1
  14. package/dist/client/advanced.js +2 -0
  15. package/dist/client/client.advanced.d.ts +10 -0
  16. package/dist/client/client.advanced.d.ts.map +1 -0
  17. package/dist/client/client.advanced.js +80 -0
  18. package/dist/client/client.d.ts +44 -8
  19. package/dist/client/client.d.ts.map +1 -1
  20. package/dist/client/client.js +137 -9
  21. package/dist/client/clientPublicMainnet.advanced.d.ts +4 -2
  22. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  23. package/dist/client/clientPublicMainnet.advanced.js +102 -0
  24. package/dist/client/clientPublicMainnet.d.ts +4 -2
  25. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  26. package/dist/client/clientPublicMainnet.js +5 -1
  27. package/dist/client/clientPublicTestnet.advanced.d.ts +4 -2
  28. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  29. package/dist/client/clientPublicTestnet.advanced.js +151 -0
  30. package/dist/client/clientPublicTestnet.d.ts +4 -2
  31. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  32. package/dist/client/clientPublicTestnet.js +6 -1
  33. package/dist/client/clientTypes.advanced.d.ts +23 -0
  34. package/dist/client/clientTypes.advanced.d.ts.map +1 -0
  35. package/dist/client/clientTypes.advanced.js +4 -0
  36. package/dist/client/clientTypes.d.ts +22 -36
  37. package/dist/client/clientTypes.d.ts.map +1 -1
  38. package/dist/client/clientTypes.js +15 -12
  39. package/dist/client/jsonRpc/index.d.ts +3 -3
  40. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  41. package/dist/client/jsonRpc/index.js +3 -3
  42. package/dist/client/jsonRpc/transformers.d.ts +5 -7
  43. package/dist/client/jsonRpc/transformers.d.ts.map +1 -1
  44. package/dist/client/jsonRpc/transformers.js +8 -6
  45. package/dist/hasher/index.d.ts +1 -1
  46. package/dist/hasher/index.d.ts.map +1 -1
  47. package/dist/hasher/index.js +4 -2
  48. package/dist/num/index.d.ts +12 -0
  49. package/dist/num/index.d.ts.map +1 -1
  50. package/dist/num/index.js +21 -0
  51. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  52. package/dist/signer/btc/signerBtc.js +2 -1
  53. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  54. package/dist/signer/evm/signerEvm.js +5 -3
  55. package/dist/signer/nostr/signerNostr.d.ts.map +1 -1
  56. package/dist/signer/nostr/signerNostr.js +2 -1
  57. package/dist/utils/index.d.ts +1 -1
  58. package/dist/utils/index.d.ts.map +1 -1
  59. package/dist/utils/index.js +1 -1
  60. package/dist.commonjs/address/address.advanced.d.ts +29 -24
  61. package/dist.commonjs/address/address.advanced.d.ts.map +1 -1
  62. package/dist.commonjs/address/address.advanced.js +115 -92
  63. package/dist.commonjs/address/advanced.d.ts +1 -1
  64. package/dist.commonjs/address/advanced.js +30 -14
  65. package/dist.commonjs/address/index.d.ts +51 -44
  66. package/dist.commonjs/address/index.d.ts.map +1 -1
  67. package/dist.commonjs/address/index.js +84 -68
  68. package/dist.commonjs/advanced.d.ts +1 -1
  69. package/dist.commonjs/advanced.js +47 -22
  70. package/dist.commonjs/advancedBarrel.d.ts +1 -1
  71. package/dist.commonjs/advancedBarrel.js +30 -14
  72. package/dist.commonjs/barrel.d.ts +1 -1
  73. package/dist.commonjs/barrel.js +30 -14
  74. package/dist.commonjs/bytes/advanced.d.ts +11 -2
  75. package/dist.commonjs/bytes/index.d.ts +9 -3
  76. package/dist.commonjs/bytes/index.js +28 -26
  77. package/dist.commonjs/ckb/advanced.d.ts +1 -1
  78. package/dist.commonjs/ckb/advanced.js +47 -22
  79. package/dist.commonjs/ckb/index.d.ts +1 -1
  80. package/dist.commonjs/ckb/index.js +30 -14
  81. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts +420 -260
  82. package/dist.commonjs/ckb/molecule.advanced/generated.js +1536 -1332
  83. package/dist.commonjs/ckb/molecule.advanced/index.d.ts +3 -3
  84. package/dist.commonjs/ckb/molecule.advanced/index.js +34 -18
  85. package/dist.commonjs/ckb/script.advanced.d.ts +1 -1
  86. package/dist.commonjs/ckb/script.advanced.js +12 -9
  87. package/dist.commonjs/ckb/script.d.ts +123 -77
  88. package/dist.commonjs/ckb/script.d.ts.map +1 -1
  89. package/dist.commonjs/ckb/script.js +221 -126
  90. package/dist.commonjs/ckb/transaction.advanced.d.ts +1 -1
  91. package/dist.commonjs/ckb/transaction.advanced.js +4 -4
  92. package/dist.commonjs/ckb/transaction.d.ts +796 -544
  93. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  94. package/dist.commonjs/ckb/transaction.js +1371 -725
  95. package/dist.commonjs/client/advanced.d.ts +3 -1
  96. package/dist.commonjs/client/advanced.d.ts.map +1 -1
  97. package/dist.commonjs/client/advanced.js +32 -14
  98. package/dist.commonjs/client/client.advanced.d.ts +27 -0
  99. package/dist.commonjs/client/client.advanced.d.ts.map +1 -0
  100. package/dist.commonjs/client/client.advanced.js +116 -0
  101. package/dist.commonjs/client/client.d.ts +118 -22
  102. package/dist.commonjs/client/client.d.ts.map +1 -1
  103. package/dist.commonjs/client/client.js +218 -56
  104. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +9 -3
  105. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  106. package/dist.commonjs/client/clientPublicMainnet.advanced.js +144 -25
  107. package/dist.commonjs/client/clientPublicMainnet.d.ts +9 -5
  108. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
  109. package/dist.commonjs/client/clientPublicMainnet.js +18 -12
  110. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +8 -3
  111. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  112. package/dist.commonjs/client/clientPublicTestnet.advanced.js +204 -28
  113. package/dist.commonjs/client/clientPublicTestnet.d.ts +9 -5
  114. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
  115. package/dist.commonjs/client/clientPublicTestnet.js +14 -9
  116. package/dist.commonjs/client/clientTypes.advanced.d.ts +23 -0
  117. package/dist.commonjs/client/clientTypes.advanced.d.ts.map +1 -0
  118. package/dist.commonjs/client/clientTypes.advanced.js +8 -0
  119. package/dist.commonjs/client/clientTypes.d.ts +57 -46
  120. package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
  121. package/dist.commonjs/client/clientTypes.js +60 -22
  122. package/dist.commonjs/client/index.d.ts +1 -1
  123. package/dist.commonjs/client/index.js +30 -14
  124. package/dist.commonjs/client/jsonRpc/advanced.d.ts +1 -1
  125. package/dist.commonjs/client/jsonRpc/advanced.js +30 -14
  126. package/dist.commonjs/client/jsonRpc/index.d.ts +94 -75
  127. package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -1
  128. package/dist.commonjs/client/jsonRpc/index.js +135 -108
  129. package/dist.commonjs/client/jsonRpc/transformers.d.ts +76 -38
  130. package/dist.commonjs/client/jsonRpc/transformers.d.ts.map +1 -1
  131. package/dist.commonjs/client/jsonRpc/transformers.js +174 -150
  132. package/dist.commonjs/client/jsonRpc/types.d.ts +38 -38
  133. package/dist.commonjs/fixedPoint/index.d.ts +9 -3
  134. package/dist.commonjs/fixedPoint/index.js +26 -20
  135. package/dist.commonjs/fixedPoint/index.test.d.ts +1 -1
  136. package/dist.commonjs/fixedPoint/index.test.js +42 -36
  137. package/dist.commonjs/hasher/advanced.d.ts +1 -1
  138. package/dist.commonjs/hasher/index.d.ts +37 -37
  139. package/dist.commonjs/hasher/index.d.ts.map +1 -1
  140. package/dist.commonjs/hasher/index.js +55 -46
  141. package/dist.commonjs/hex/index.d.ts +1 -1
  142. package/dist.commonjs/hex/index.js +1 -1
  143. package/dist.commonjs/index.d.ts +1 -1
  144. package/dist.commonjs/index.js +47 -22
  145. package/dist.commonjs/num/index.d.ts +13 -1
  146. package/dist.commonjs/num/index.d.ts.map +1 -1
  147. package/dist.commonjs/num/index.js +53 -19
  148. package/dist.commonjs/signer/btc/index.d.ts +1 -1
  149. package/dist.commonjs/signer/btc/index.js +30 -14
  150. package/dist.commonjs/signer/btc/signerBtc.d.ts +47 -47
  151. package/dist.commonjs/signer/btc/signerBtc.d.ts.map +1 -1
  152. package/dist.commonjs/signer/btc/signerBtc.js +89 -65
  153. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +45 -45
  154. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +52 -52
  155. package/dist.commonjs/signer/btc/verify.d.ts +6 -2
  156. package/dist.commonjs/signer/btc/verify.js +10 -3
  157. package/dist.commonjs/signer/ckb/index.d.ts +1 -1
  158. package/dist.commonjs/signer/ckb/index.js +30 -14
  159. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +45 -45
  160. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +56 -56
  161. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts +6 -2
  162. package/dist.commonjs/signer/ckb/verifyJoyId.js +11 -8
  163. package/dist.commonjs/signer/dummy/alwaysError.d.ts +4 -4
  164. package/dist.commonjs/signer/dummy/alwaysError.js +7 -7
  165. package/dist.commonjs/signer/dummy/dummy.d.ts +7 -7
  166. package/dist.commonjs/signer/dummy/dummy.js +16 -16
  167. package/dist.commonjs/signer/dummy/index.d.ts +1 -1
  168. package/dist.commonjs/signer/dummy/index.js +30 -14
  169. package/dist.commonjs/signer/dummy/openLink.d.ts +4 -4
  170. package/dist.commonjs/signer/dummy/openLink.js +7 -7
  171. package/dist.commonjs/signer/evm/index.d.ts +1 -1
  172. package/dist.commonjs/signer/evm/index.js +30 -14
  173. package/dist.commonjs/signer/evm/signerEvm.d.ts +42 -38
  174. package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -1
  175. package/dist.commonjs/signer/evm/signerEvm.js +105 -68
  176. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +37 -37
  177. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +42 -42
  178. package/dist.commonjs/signer/evm/verify.d.ts +6 -2
  179. package/dist.commonjs/signer/evm/verify.js +7 -2
  180. package/dist.commonjs/signer/index.d.ts +1 -1
  181. package/dist.commonjs/signer/index.js +30 -14
  182. package/dist.commonjs/signer/nostr/index.d.ts +1 -1
  183. package/dist.commonjs/signer/nostr/index.js +30 -14
  184. package/dist.commonjs/signer/nostr/signerNostr.d.ts +57 -57
  185. package/dist.commonjs/signer/nostr/signerNostr.d.ts.map +1 -1
  186. package/dist.commonjs/signer/nostr/signerNostr.js +100 -70
  187. package/dist.commonjs/signer/nostr/verify.d.ts +9 -3
  188. package/dist.commonjs/signer/nostr/verify.js +50 -39
  189. package/dist.commonjs/signer/signer/index.d.ts +156 -150
  190. package/dist.commonjs/signer/signer/index.js +208 -181
  191. package/dist.commonjs/utils/index.d.ts +38 -8
  192. package/dist.commonjs/utils/index.d.ts.map +1 -1
  193. package/dist.commonjs/utils/index.js +18 -12
  194. package/package.json +2 -2
  195. package/src/address/address.advanced.ts +2 -5
  196. package/src/address/index.ts +2 -5
  197. package/src/ckb/script.ts +62 -2
  198. package/src/ckb/transaction.ts +634 -46
  199. package/src/client/advanced.ts +2 -0
  200. package/src/client/client.advanced.ts +134 -0
  201. package/src/client/client.ts +225 -16
  202. package/src/client/clientPublicMainnet.advanced.ts +116 -2
  203. package/src/client/clientPublicMainnet.ts +8 -3
  204. package/src/client/clientPublicTestnet.advanced.ts +171 -2
  205. package/src/client/clientPublicTestnet.ts +9 -3
  206. package/src/client/clientTypes.advanced.ts +26 -0
  207. package/src/client/clientTypes.ts +41 -43
  208. package/src/client/jsonRpc/index.ts +4 -4
  209. package/src/client/jsonRpc/transformers.ts +13 -11
  210. package/src/hasher/index.ts +4 -2
  211. package/src/num/index.ts +22 -0
  212. package/src/signer/btc/signerBtc.ts +2 -1
  213. package/src/signer/evm/signerEvm.ts +5 -3
  214. package/src/signer/nostr/signerNostr.ts +2 -1
  215. package/src/utils/index.ts +6 -2
@@ -1,8 +1,9 @@
1
1
  import { bytesFrom } from "../bytes";
2
+ import { Zero, fixedPointFrom } from "../fixedPoint";
2
3
  import { Hasher, ckbHash } from "../hasher";
3
4
  import { hexFrom } from "../hex";
4
5
  import { numFrom, numFromBytes, numToBytes, numToHex, } from "../num";
5
- import { apply } from "../utils";
6
+ import { apply, reduceAsync } from "../utils";
6
7
  import * as mol from "./molecule.advanced";
7
8
  import { Script } from "./script";
8
9
  import { DEP_TYPE_TO_NUM, NUM_TO_DEP_TYPE } from "./transaction.advanced";
@@ -76,6 +77,19 @@ export class OutPoint {
76
77
  this.txHash = txHash;
77
78
  this.index = index;
78
79
  }
80
+ /**
81
+ * Clone an OutPoint.
82
+ *
83
+ * @returns A cloned OutPoint instance.
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * const outPoint1 = outPoint0.clone();
88
+ * ```
89
+ */
90
+ clone() {
91
+ return new OutPoint(this.txHash, this.index);
92
+ }
79
93
  /**
80
94
  * Creates an OutPoint instance from an OutPointLike object.
81
95
  *
@@ -111,10 +125,10 @@ export class OutPoint {
111
125
  *
112
126
  * @example
113
127
  * ```typescript
114
- * const outPointBytes = outPoint.encode();
128
+ * const outPointBytes = outPoint.toBytes();
115
129
  * ```
116
130
  */
117
- encode() {
131
+ toBytes() {
118
132
  return bytesFrom(mol.SerializeOutPoint(this._toMolData()));
119
133
  }
120
134
  /**
@@ -134,6 +148,21 @@ export class OutPoint {
134
148
  : new mol.OutPoint(bytesFrom(bytes));
135
149
  return new OutPoint(hexFrom(view.getTxHash().raw()), numFromBytes(view.getIndex().raw()));
136
150
  }
151
+ /**
152
+ * Compares the current OutPoint instance with another OutPointLike object for equality.
153
+ *
154
+ * @param val - The OutPointLike object to compare with.
155
+ * @returns True if the out points are equal, otherwise false.
156
+ *
157
+ * @example
158
+ * ```typescript
159
+ * const isEqual = outPoint.eq(anotherOutPoint);
160
+ * ```
161
+ */
162
+ eq(val) {
163
+ const outPoint = OutPoint.from(val);
164
+ return this.txHash === outPoint.txHash && this.index === outPoint.index;
165
+ }
137
166
  }
138
167
  export class CellOutput {
139
168
  /**
@@ -148,6 +177,22 @@ export class CellOutput {
148
177
  this.lock = lock;
149
178
  this.type = type;
150
179
  }
180
+ get occupiedSize() {
181
+ return 8 + this.lock.occupiedSize + (this.type?.occupiedSize ?? 0);
182
+ }
183
+ /**
184
+ * Clone a CellOutput.
185
+ *
186
+ * @returns A cloned CellOutput instance.
187
+ *
188
+ * @example
189
+ * ```typescript
190
+ * const cellOutput1 = cellOutput0.clone();
191
+ * ```
192
+ */
193
+ clone() {
194
+ return new CellOutput(this.capacity, this.lock.clone(), this.type?.clone());
195
+ }
151
196
  /**
152
197
  * Creates a CellOutput instance from a CellOutputLike object.
153
198
  *
@@ -219,13 +264,11 @@ export class Cell {
219
264
  * @param outPoint - The output point of the cell.
220
265
  * @param cellOutput - The cell output of the cell.
221
266
  * @param outputData - The output data of the cell.
222
- * @param blockNumber - The block number of the cell.
223
267
  */
224
- constructor(outPoint, cellOutput, outputData, blockNumber) {
268
+ constructor(outPoint, cellOutput, outputData) {
225
269
  this.outPoint = outPoint;
226
270
  this.cellOutput = cellOutput;
227
271
  this.outputData = outputData;
228
- this.blockNumber = blockNumber;
229
272
  }
230
273
  /**
231
274
  * Creates a Cell instance from a CellLike object.
@@ -237,7 +280,20 @@ export class Cell {
237
280
  if (cell instanceof Cell) {
238
281
  return cell;
239
282
  }
240
- return new Cell(OutPoint.from(cell.outPoint), CellOutput.from(cell.cellOutput), hexFrom(cell.outputData), numFrom(cell.blockNumber));
283
+ return new Cell(OutPoint.from(cell.outPoint), CellOutput.from(cell.cellOutput), hexFrom(cell.outputData));
284
+ }
285
+ /**
286
+ * Clone a Cell
287
+ *
288
+ * @returns A cloned Cell instance.
289
+ *
290
+ * @example
291
+ * ```typescript
292
+ * const cell1 = cell0.clone();
293
+ * ```
294
+ */
295
+ clone() {
296
+ return new Cell(this.outPoint.clone(), this.cellOutput.clone(), this.outputData);
241
297
  }
242
298
  }
243
299
  export class CellInput {
@@ -248,14 +304,25 @@ export class CellInput {
248
304
  * @param since - The since value of the cell input.
249
305
  * @param cellOutput - The optional cell output associated with the cell input.
250
306
  * @param outputData - The optional output data associated with the cell input.
251
- * @param blockNumber - The optional block number associated with the cell input.
252
307
  */
253
- constructor(previousOutput, since, cellOutput, outputData, blockNumber) {
308
+ constructor(previousOutput, since, cellOutput, outputData) {
254
309
  this.previousOutput = previousOutput;
255
310
  this.since = since;
256
311
  this.cellOutput = cellOutput;
257
312
  this.outputData = outputData;
258
- this.blockNumber = blockNumber;
313
+ }
314
+ /**
315
+ * Clone a CellInput.
316
+ *
317
+ * @returns A cloned CellInput instance.
318
+ *
319
+ * @example
320
+ * ```typescript
321
+ * const cellInput1 = cellInput0.clone();
322
+ * ```
323
+ */
324
+ clone() {
325
+ return new CellInput(this.previousOutput.clone(), this.since, this.cellOutput?.clone(), this.outputData);
259
326
  }
260
327
  /**
261
328
  * Creates a CellInput instance from a CellInputLike object.
@@ -275,20 +342,20 @@ export class CellInput {
275
342
  if (cellInput instanceof CellInput) {
276
343
  return cellInput;
277
344
  }
278
- return new CellInput(OutPoint.from(cellInput.previousOutput), numFrom(cellInput.since), apply(CellOutput.from, cellInput.cellOutput), apply(hexFrom, cellInput.outputData), apply(numFrom, cellInput.blockNumber));
345
+ return new CellInput(OutPoint.from(cellInput.previousOutput), numFrom(cellInput.since ?? 0), apply(CellOutput.from, cellInput.cellOutput), apply(hexFrom, cellInput.outputData));
279
346
  }
280
347
  /**
281
348
  * Complete extra infos in the input. Like the output of the out point.
282
349
  * The instance will be modified.
283
350
  *
284
- * @returns The completed instance.
351
+ * @returns true if succeed.
285
352
  * @example
286
353
  * ```typescript
287
354
  * await cellInput.completeExtraInfos();
288
355
  * ```
289
356
  */
290
357
  async completeExtraInfos(client) {
291
- if (this.cellOutput && this.outputData && this.blockNumber) {
358
+ if (this.cellOutput && this.outputData) {
292
359
  return;
293
360
  }
294
361
  const cell = await client.getCell(this.previousOutput);
@@ -297,7 +364,6 @@ export class CellInput {
297
364
  }
298
365
  this.cellOutput = cell.cellOutput;
299
366
  this.outputData = cell.outputData;
300
- this.blockNumber = cell.blockNumber;
301
367
  }
302
368
  /**
303
369
  * Converts the CellInput instance to molecule data format.
@@ -352,6 +418,19 @@ export class CellDep {
352
418
  this.outPoint = outPoint;
353
419
  this.depType = depType;
354
420
  }
421
+ /**
422
+ * Clone a CellDep.
423
+ *
424
+ * @returns A cloned CellDep instance.
425
+ *
426
+ * @example
427
+ * ```typescript
428
+ * const cellDep1 = cellDep0.clone();
429
+ * ```
430
+ */
431
+ clone() {
432
+ return new CellDep(this.outPoint.clone(), this.depType);
433
+ }
355
434
  /**
356
435
  * Creates a CellDep instance from a CellDepLike object.
357
436
  *
@@ -411,6 +490,21 @@ export class CellDep {
411
490
  const view = bytes instanceof mol.CellDep ? bytes : new mol.CellDep(bytesFrom(bytes));
412
491
  return new CellDep(OutPoint.fromBytes(view.getOutPoint()), depTypeFromBytes([view.getDepType()]));
413
492
  }
493
+ /**
494
+ * Compares the current CellDep instance with another CellDepLike object for equality.
495
+ *
496
+ * @param val - The CellDepLike object to compare with.
497
+ * @returns True if the cell deps are equal, otherwise false.
498
+ *
499
+ * @example
500
+ * ```typescript
501
+ * const isEqual = cellDep.eq(anotherCellDep);
502
+ * ```
503
+ */
504
+ eq(val) {
505
+ const cellDep = CellDep.from(val);
506
+ return (this.outPoint.eq(cellDep.outPoint) && this.depType === cellDep.depType);
507
+ }
414
508
  }
415
509
  export class WitnessArgs {
416
510
  /**
@@ -489,6 +583,13 @@ export class WitnessArgs {
489
583
  return new WitnessArgs(apply(hexFrom, mol.molOptional(view.getLock())?.raw()), apply(hexFrom, mol.molOptional(view.getInputType())?.raw()), apply(hexFrom, mol.molOptional(view.getOutputType())?.raw()));
490
584
  }
491
585
  }
586
+ export function udtBalanceFrom(dataLike) {
587
+ const data = bytesFrom(dataLike).slice(0, 16);
588
+ if (data.length !== 16) {
589
+ throw new Error("Invalid UDT cell data");
590
+ }
591
+ return numFromBytes(data);
592
+ }
492
593
  export class Transaction {
493
594
  /**
494
595
  * Creates an instance of Transaction.
@@ -523,6 +624,37 @@ export class Transaction {
523
624
  static default() {
524
625
  return new Transaction(0n, [], [], [], [], [], []);
525
626
  }
627
+ /**
628
+ * Copy every properties from another transaction.
629
+ *
630
+ * @example
631
+ * ```typescript
632
+ * this.copy(Transaction.default());
633
+ * ```
634
+ */
635
+ copy(txLike) {
636
+ const tx = Transaction.from(txLike);
637
+ this.version = tx.version;
638
+ this.cellDeps = tx.cellDeps;
639
+ this.headerDeps = tx.headerDeps;
640
+ this.inputs = tx.inputs;
641
+ this.outputs = tx.outputs;
642
+ this.outputsData = tx.outputsData;
643
+ this.witnesses = tx.witnesses;
644
+ }
645
+ /**
646
+ * Clone a Transaction.
647
+ *
648
+ * @returns A cloned instance
649
+ *
650
+ * @example
651
+ * ```typescript
652
+ * const tx1 = tx0.clone();
653
+ * ```
654
+ */
655
+ clone() {
656
+ return new Transaction(0n, this.cellDeps.map((c) => c.clone()), [...this.headerDeps], this.inputs.map((i) => i.clone()), this.outputs.map((o) => o.clone()), [...this.outputsData], [...this.witnesses]);
657
+ }
526
658
  /**
527
659
  * Creates a Transaction instance from a TransactionLike object.
528
660
  *
@@ -546,7 +678,20 @@ export class Transaction {
546
678
  if (tx instanceof Transaction) {
547
679
  return tx;
548
680
  }
549
- return new Transaction(numFrom(tx.version), tx.cellDeps.map((cellDep) => CellDep.from(cellDep)), tx.headerDeps.map(hexFrom), tx.inputs.map((input) => CellInput.from(input)), tx.outputs.map((output) => CellOutput.from(output)), tx.outputsData.map(hexFrom), tx.witnesses.map(hexFrom));
681
+ const outputs = tx.outputs?.map((output, i) => {
682
+ const o = CellOutput.from({
683
+ ...output,
684
+ capacity: output.capacity ?? 0,
685
+ });
686
+ o.capacity = fixedPointFrom(o.occupiedSize + (apply(bytesFrom, tx.outputsData?.[i])?.length ?? 0));
687
+ return o;
688
+ }) ?? [];
689
+ const outputsData = outputs.map((_, i) => hexFrom(tx.outputsData?.[i] ?? "0x"));
690
+ if (tx.outputsData !== undefined &&
691
+ outputsData.length < tx.outputsData.length) {
692
+ outputsData.push(...tx.outputsData.slice(outputsData.length).map((d) => hexFrom(d)));
693
+ }
694
+ return new Transaction(numFrom(tx.version ?? 0), tx.cellDeps?.map((cellDep) => CellDep.from(cellDep)) ?? [], tx.headerDeps?.map(hexFrom) ?? [], tx.inputs?.map((input) => CellInput.from(input)) ?? [], outputs, outputsData, tx.witnesses?.map(hexFrom) ?? []);
550
695
  }
551
696
  /**
552
697
  * Creates a Transaction instance from a Lumos skeleton.
@@ -610,6 +755,29 @@ export class Transaction {
610
755
  outputsData: this.outputsData.map((header) => bytesFrom(header)),
611
756
  }));
612
757
  }
758
+ /**
759
+ * Converts the whole transaction data to bytes.
760
+ *
761
+ * @returns A Uint8Array containing the full transaction bytes.
762
+ *
763
+ * @example
764
+ * ```typescript
765
+ * const txBytes = transaction.toBytes();
766
+ * ```
767
+ */
768
+ toBytes() {
769
+ return bytesFrom(mol.SerializeTransaction({
770
+ raw: {
771
+ version: numToBytes(this.version, 4),
772
+ cellDeps: this.cellDeps.map((d) => d._toMolData()),
773
+ headerDeps: this.headerDeps.map((header) => bytesFrom(header)),
774
+ inputs: this.inputs.map((i) => i._toMolData()),
775
+ outputs: this.outputs.map((o) => o._toMolData()),
776
+ outputsData: this.outputsData.map((header) => bytesFrom(header)),
777
+ },
778
+ witnesses: this.witnesses.map((witness) => bytesFrom(witness)),
779
+ }));
780
+ }
613
781
  /**
614
782
  * Calculates the hash of the transaction.
615
783
  *
@@ -662,11 +830,11 @@ export class Transaction {
662
830
  const hasher = new Hasher();
663
831
  hasher.update(this.hash());
664
832
  for (let i = 0; i < this.witnesses.length; i += 1) {
665
- const input = this.inputs[i];
833
+ const input = await this.inputs[i];
834
+ await input.completeExtraInfos(client);
666
835
  if (input) {
667
- await input.completeExtraInfos(client);
668
836
  if (!input.cellOutput) {
669
- throw Error("Unable to resolve inputs info");
837
+ throw new Error("Unable to complete input");
670
838
  }
671
839
  if (!script.eq(input.cellOutput.lock)) {
672
840
  continue;
@@ -706,7 +874,7 @@ export class Transaction {
706
874
  const input = this.inputs[i];
707
875
  await input.completeExtraInfos(client);
708
876
  if (!input.cellOutput) {
709
- throw Error("Unable to resolve inputs info");
877
+ throw new Error("Unable to complete input");
710
878
  }
711
879
  if (script.eq(input.cellOutput.lock)) {
712
880
  return i;
@@ -731,13 +899,101 @@ export class Transaction {
731
899
  const input = this.inputs[i];
732
900
  await input.completeExtraInfos(client);
733
901
  if (!input.cellOutput) {
734
- throw Error("Unable to resolve inputs info");
902
+ throw new Error("Unable to complete input");
735
903
  }
736
904
  if (script.eq(input.cellOutput.lock)) {
737
905
  return i;
738
906
  }
739
907
  }
740
908
  }
909
+ /**
910
+ * Add cell deps if they are not existed
911
+ *
912
+ * @param cellDepLikes - The cell deps to add
913
+ *
914
+ * @example
915
+ * ```typescript
916
+ * tx.addCellDeps(cellDep);
917
+ * ```
918
+ */
919
+ addCellDeps(...cellDepsLike) {
920
+ cellDepsLike.flat().forEach((cellDepLike) => {
921
+ const cellDep = CellDep.from(cellDepLike);
922
+ if (this.cellDeps.some((c) => c.eq(cellDep))) {
923
+ return;
924
+ }
925
+ this.cellDeps.push(cellDep);
926
+ });
927
+ }
928
+ /**
929
+ * Add cell dep from infos if they are not existed
930
+ *
931
+ * @param client - A client for searching cell deps
932
+ * @param cellDepLikes - The cell dep infos to add
933
+ *
934
+ * @example
935
+ * ```typescript
936
+ * tx.addCellDepInfos(client, cellDepInfos);
937
+ * ```
938
+ */
939
+ async addCellDepInfos(client, ...cellDepInfosLike) {
940
+ this.addCellDeps(await client.getCellDeps(...cellDepInfosLike));
941
+ }
942
+ /**
943
+ * Add cell deps from known script
944
+ *
945
+ * @param client - The client for searching known script and cell deps
946
+ * @param scripts - The known scripts to add
947
+ *
948
+ * @example
949
+ * ```typescript
950
+ * tx.addCellDepsOfKnownScripts(client, KnownScript.OmniLock);
951
+ * ```
952
+ */
953
+ async addCellDepsOfKnownScripts(client, ...scripts) {
954
+ await Promise.all(scripts
955
+ .flat()
956
+ .map(async (script) => this.addCellDepInfos(client, (await client.getKnownScript(script)).cellDeps)));
957
+ }
958
+ /**
959
+ * Set output data at index.
960
+ *
961
+ * @param index - The index of the output data.
962
+ * @param witness - The data to set.
963
+ *
964
+ * @example
965
+ * ```typescript
966
+ * await tx.setOutputData(0, "0x00");
967
+ * ```
968
+ */
969
+ setOutputDataAt(index, data) {
970
+ if (this.outputsData.length < index) {
971
+ this.outputsData.push(...Array.from(new Array(index - this.outputsData.length), () => "0x"));
972
+ }
973
+ this.outputsData[index] = hexFrom(data);
974
+ }
975
+ /**
976
+ * Add output
977
+ *
978
+ * @param output - The cell output to add
979
+ * @param data - optional output data
980
+ *
981
+ * @example
982
+ * ```typescript
983
+ * await tx.addOutput(cellOutput, "0xabcd");
984
+ * ```
985
+ */
986
+ addOutput(outputLike, outputData = "0x") {
987
+ const output = CellOutput.from({
988
+ ...outputLike,
989
+ capacity: outputLike.capacity ?? 0,
990
+ });
991
+ if (output.capacity === Zero) {
992
+ output.capacity = fixedPointFrom(output.occupiedSize + bytesFrom(outputData).length);
993
+ }
994
+ const i = this.outputs.push(output) - 1;
995
+ this.setOutputDataAt(i, outputData);
996
+ }
741
997
  /**
742
998
  * Get witness at index as WitnessArgs
743
999
  *
@@ -760,7 +1016,6 @@ export class Transaction {
760
1016
  *
761
1017
  * @param index - The index of the witness.
762
1018
  * @param witness - The WitnessArgs to set.
763
- * @returns The transaction itself.
764
1019
  *
765
1020
  * @example
766
1021
  * ```typescript
@@ -795,4 +1050,165 @@ export class Transaction {
795
1050
  witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
796
1051
  this.setWitnessArgsAt(position, witness);
797
1052
  }
1053
+ async getInputsCapacity(client) {
1054
+ return reduceAsync(this.inputs, async (acc, input) => {
1055
+ await input.completeExtraInfos(client);
1056
+ if (!input.cellOutput) {
1057
+ throw new Error("Unable to complete input");
1058
+ }
1059
+ return acc + input.cellOutput.capacity;
1060
+ }, numFrom(0));
1061
+ }
1062
+ getOutputsCapacity() {
1063
+ return this.outputs.reduce((acc, { capacity }) => acc + capacity, numFrom(0));
1064
+ }
1065
+ async getInputsUdtBalance(client, type) {
1066
+ return reduceAsync(this.inputs, async (acc, input) => {
1067
+ await input.completeExtraInfos(client);
1068
+ if (!input.cellOutput || !input.outputData) {
1069
+ throw new Error("Unable to complete input");
1070
+ }
1071
+ if (!input.cellOutput.type?.eq(type)) {
1072
+ return;
1073
+ }
1074
+ return acc + udtBalanceFrom(input.outputData);
1075
+ }, numFrom(0));
1076
+ }
1077
+ getOutputsUdtBalance(type) {
1078
+ return this.outputs.reduce((acc, output, i) => {
1079
+ if (!output.type?.eq(type)) {
1080
+ return acc;
1081
+ }
1082
+ return acc + udtBalanceFrom(this.outputsData[i]);
1083
+ }, numFrom(0));
1084
+ }
1085
+ async completeInputs(from, filter, accumulator, init) {
1086
+ const scripts = (await from.getAddressObjs()).map(({ script }) => script);
1087
+ const collectedCells = [];
1088
+ let acc = init;
1089
+ for (const script of scripts) {
1090
+ for await (const cell of from.client.findCellsByCollectableSearchKey({
1091
+ script,
1092
+ scriptType: "lock",
1093
+ filter,
1094
+ scriptSearchMode: "exact",
1095
+ withData: true,
1096
+ })) {
1097
+ if (this.inputs.some(({ previousOutput }) => previousOutput.eq(cell.outPoint))) {
1098
+ continue;
1099
+ }
1100
+ const i = collectedCells.push(cell);
1101
+ const next = await Promise.resolve(accumulator(acc, cell, i - 1, collectedCells));
1102
+ if (next === undefined) {
1103
+ this.inputs.push(...collectedCells.map(({ outPoint, outputData, cellOutput }) => CellInput.from({
1104
+ previousOutput: outPoint,
1105
+ since: 0,
1106
+ outputData,
1107
+ cellOutput,
1108
+ })));
1109
+ return collectedCells.length;
1110
+ }
1111
+ acc = next;
1112
+ }
1113
+ }
1114
+ throw new Error("Failed to find enough cells for input");
1115
+ }
1116
+ async completeInputsByCapacity(from, capacityTweak, filter) {
1117
+ const exceptedCapacity = this.getOutputsCapacity() + numFrom(capacityTweak ?? 0);
1118
+ const inputsCapacity = await this.getInputsCapacity(from.client);
1119
+ if (inputsCapacity >= exceptedCapacity) {
1120
+ return 0;
1121
+ }
1122
+ return this.completeInputs(from, filter ?? {
1123
+ scriptLenRange: [0, 1],
1124
+ outputDataLenRange: [0, 1],
1125
+ }, (acc, { cellOutput: { capacity } }) => {
1126
+ const sum = acc + capacity;
1127
+ return sum > exceptedCapacity ? undefined : sum;
1128
+ }, inputsCapacity);
1129
+ }
1130
+ async completeInputsByUdt(from, type) {
1131
+ const exceptedBalance = this.getOutputsUdtBalance(type);
1132
+ const inputsBalance = await this.getInputsUdtBalance(from.client, type);
1133
+ if (inputsBalance >= exceptedBalance) {
1134
+ return 0;
1135
+ }
1136
+ return this.completeInputs(from, {
1137
+ script: type,
1138
+ outputDataLenRange: [16, numFrom("0xffffffff")],
1139
+ }, (acc, { outputData }) => {
1140
+ const balance = udtBalanceFrom(outputData);
1141
+ const sum = acc + balance;
1142
+ return sum > exceptedBalance ? undefined : sum;
1143
+ }, inputsBalance);
1144
+ }
1145
+ estimateFee(feeRate) {
1146
+ const txSize = this.toBytes().length + 4;
1147
+ return (numFrom(txSize) * numFrom(feeRate) + numFrom(1000)) / numFrom(1000);
1148
+ }
1149
+ async completeFee(from, change, feeRate, filter) {
1150
+ // Complete all inputs extra infos for cache
1151
+ await this.getInputsCapacity(from.client);
1152
+ let leastFee = this.estimateFee(feeRate);
1153
+ let leastExtraCapacity = Zero;
1154
+ while (true) {
1155
+ const prepared = await from.prepareTransaction(this.clone());
1156
+ const collected = await prepared.completeInputsByCapacity(from, leastFee + leastExtraCapacity, filter);
1157
+ const extraCapacity = (await prepared.getInputsCapacity(from.client)) -
1158
+ prepared.getOutputsCapacity();
1159
+ // The extra capacity paid the fee without a change
1160
+ if (extraCapacity === leastFee) {
1161
+ this.copy(prepared);
1162
+ return [collected, false];
1163
+ }
1164
+ let changed = prepared.clone();
1165
+ const needed = numFrom(await Promise.resolve(change(changed, extraCapacity - leastFee)));
1166
+ // No enough extra capacity to create new cells for change
1167
+ if (needed > Zero) {
1168
+ leastExtraCapacity = needed;
1169
+ continue;
1170
+ }
1171
+ if ((await changed.getInputsCapacity(from.client)) -
1172
+ changed.getOutputsCapacity() !==
1173
+ leastFee) {
1174
+ throw new Error("The change function doesn't use all available capacity");
1175
+ }
1176
+ // New change cells created, update the fee
1177
+ await from.prepareTransaction(changed);
1178
+ const changedFee = changed.estimateFee(feeRate);
1179
+ if (leastFee > changedFee) {
1180
+ throw new Error("The change function removed existed transaction data");
1181
+ }
1182
+ // The fee has been paid
1183
+ if (leastFee === changedFee) {
1184
+ this.copy(changed);
1185
+ return [collected, true];
1186
+ }
1187
+ // The fee after changing is more than the original fee
1188
+ leastFee = changedFee;
1189
+ }
1190
+ }
1191
+ completeFeeChangeToLock(from, change, feeRate, filter) {
1192
+ const script = Script.from(change);
1193
+ return this.completeFee(from, (tx, capacity) => {
1194
+ const changeCell = CellOutput.from({ capacity: 0, lock: script });
1195
+ const occupiedCapacity = fixedPointFrom(changeCell.occupiedSize);
1196
+ if (capacity < occupiedCapacity) {
1197
+ return occupiedCapacity;
1198
+ }
1199
+ changeCell.capacity = capacity;
1200
+ tx.addOutput(changeCell);
1201
+ return 0;
1202
+ }, feeRate, filter);
1203
+ }
1204
+ completeFeeChangeToOutput(from, index, feeRate, filter) {
1205
+ const change = Number(numFrom(index));
1206
+ if (!this.outputs[change]) {
1207
+ throw new Error("Non-existed output to change");
1208
+ }
1209
+ return this.completeFee(from, (tx, capacity) => {
1210
+ tx.outputs[change].capacity += capacity;
1211
+ return 0;
1212
+ }, feeRate, filter);
1213
+ }
798
1214
  }
@@ -1,4 +1,6 @@
1
+ export * from "./client.advanced";
1
2
  export * from "./clientPublicMainnet.advanced";
2
3
  export * from "./clientPublicTestnet.advanced";
4
+ export * from "./clientTypes.advanced";
3
5
  export * from "./jsonRpc/advanced";
4
6
  //# sourceMappingURL=advanced.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"advanced.d.ts","sourceRoot":"","sources":["../../src/client/advanced.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,oBAAoB,CAAC"}
1
+ {"version":3,"file":"advanced.d.ts","sourceRoot":"","sources":["../../src/client/advanced.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC"}
@@ -1,3 +1,5 @@
1
+ export * from "./client.advanced";
1
2
  export * from "./clientPublicMainnet.advanced";
2
3
  export * from "./clientPublicTestnet.advanced";
4
+ export * from "./clientTypes.advanced";
3
5
  export * from "./jsonRpc/advanced";
@@ -0,0 +1,10 @@
1
+ import { CellLike, ScriptLike } from "../ckb";
2
+ import { HexLike } from "../hex";
3
+ import { NumLike } from "../num";
4
+ import { ClientCollectableSearchKeyLike } from "./clientTypes.advanced";
5
+ export declare function filterData(dataLike: HexLike, filterLike: HexLike | undefined, filterMode: "exact" | "prefix" | "partial"): boolean;
6
+ export declare function filterScript(valueLike: ScriptLike | undefined, filterLike: ScriptLike | undefined, filterMode: "prefix" | "exact" | "partial"): boolean;
7
+ export declare function filterNumByRange(lengthLike: NumLike, range: [NumLike, NumLike] | undefined): boolean;
8
+ export declare function filterScriptByLenRange(valueLike?: ScriptLike, scriptLenRange?: [NumLike, NumLike]): boolean;
9
+ export declare function filterCell(searchKeyLike: ClientCollectableSearchKeyLike, cellLike: CellLike): boolean;
10
+ //# sourceMappingURL=client.advanced.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.advanced.d.ts","sourceRoot":"","sources":["../../src/client/client.advanced.ts"],"names":[],"mappings":"AACA,OAAO,EAAQ,QAAQ,EAAU,UAAU,EAAE,MAAM,QAAQ,CAAC;AAC5D,OAAO,EAAE,OAAO,EAAW,MAAM,QAAQ,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAW,MAAM,QAAQ,CAAC;AAE1C,OAAO,EACL,8BAA8B,EAE/B,MAAM,wBAAwB,CAAC;AAEhC,wBAAgB,UAAU,CACxB,QAAQ,EAAE,OAAO,EACjB,UAAU,EAAE,OAAO,GAAG,SAAS,EAC/B,UAAU,EAAE,OAAO,GAAG,QAAQ,GAAG,SAAS,GACzC,OAAO,CAgBT;AAED,wBAAgB,YAAY,CAC1B,SAAS,EAAE,UAAU,GAAG,SAAS,EACjC,UAAU,EAAE,UAAU,GAAG,SAAS,EAClC,UAAU,EAAE,QAAQ,GAAG,OAAO,GAAG,SAAS,GACzC,OAAO,CAkBT;AAED,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,OAAO,EACnB,KAAK,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,SAAS,WAStC;AAED,wBAAgB,sBAAsB,CACpC,SAAS,CAAC,EAAE,UAAU,EACtB,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,GAClC,OAAO,CAYT;AAED,wBAAgB,UAAU,CACxB,aAAa,EAAE,8BAA8B,EAC7C,QAAQ,EAAE,QAAQ,GACjB,OAAO,CA4CT"}