@ckb-ccc/core 0.0.6-alpha.0 → 0.0.7-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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 +440 -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 +1373 -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 +637 -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,23 @@ 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
+ if (o.capacity === Zero) {
687
+ o.capacity = fixedPointFrom(o.occupiedSize +
688
+ (apply(bytesFrom, tx.outputsData?.[i])?.length ?? 0));
689
+ }
690
+ return o;
691
+ }) ?? [];
692
+ const outputsData = outputs.map((_, i) => hexFrom(tx.outputsData?.[i] ?? "0x"));
693
+ if (tx.outputsData !== undefined &&
694
+ outputsData.length < tx.outputsData.length) {
695
+ outputsData.push(...tx.outputsData.slice(outputsData.length).map((d) => hexFrom(d)));
696
+ }
697
+ 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
698
  }
551
699
  /**
552
700
  * Creates a Transaction instance from a Lumos skeleton.
@@ -610,6 +758,29 @@ export class Transaction {
610
758
  outputsData: this.outputsData.map((header) => bytesFrom(header)),
611
759
  }));
612
760
  }
761
+ /**
762
+ * Converts the whole transaction data to bytes.
763
+ *
764
+ * @returns A Uint8Array containing the full transaction bytes.
765
+ *
766
+ * @example
767
+ * ```typescript
768
+ * const txBytes = transaction.toBytes();
769
+ * ```
770
+ */
771
+ toBytes() {
772
+ return bytesFrom(mol.SerializeTransaction({
773
+ raw: {
774
+ version: numToBytes(this.version, 4),
775
+ cellDeps: this.cellDeps.map((d) => d._toMolData()),
776
+ headerDeps: this.headerDeps.map((header) => bytesFrom(header)),
777
+ inputs: this.inputs.map((i) => i._toMolData()),
778
+ outputs: this.outputs.map((o) => o._toMolData()),
779
+ outputsData: this.outputsData.map((header) => bytesFrom(header)),
780
+ },
781
+ witnesses: this.witnesses.map((witness) => bytesFrom(witness)),
782
+ }));
783
+ }
613
784
  /**
614
785
  * Calculates the hash of the transaction.
615
786
  *
@@ -662,11 +833,11 @@ export class Transaction {
662
833
  const hasher = new Hasher();
663
834
  hasher.update(this.hash());
664
835
  for (let i = 0; i < this.witnesses.length; i += 1) {
665
- const input = this.inputs[i];
836
+ const input = await this.inputs[i];
837
+ await input.completeExtraInfos(client);
666
838
  if (input) {
667
- await input.completeExtraInfos(client);
668
839
  if (!input.cellOutput) {
669
- throw Error("Unable to resolve inputs info");
840
+ throw new Error("Unable to complete input");
670
841
  }
671
842
  if (!script.eq(input.cellOutput.lock)) {
672
843
  continue;
@@ -706,7 +877,7 @@ export class Transaction {
706
877
  const input = this.inputs[i];
707
878
  await input.completeExtraInfos(client);
708
879
  if (!input.cellOutput) {
709
- throw Error("Unable to resolve inputs info");
880
+ throw new Error("Unable to complete input");
710
881
  }
711
882
  if (script.eq(input.cellOutput.lock)) {
712
883
  return i;
@@ -731,13 +902,101 @@ export class Transaction {
731
902
  const input = this.inputs[i];
732
903
  await input.completeExtraInfos(client);
733
904
  if (!input.cellOutput) {
734
- throw Error("Unable to resolve inputs info");
905
+ throw new Error("Unable to complete input");
735
906
  }
736
907
  if (script.eq(input.cellOutput.lock)) {
737
908
  return i;
738
909
  }
739
910
  }
740
911
  }
912
+ /**
913
+ * Add cell deps if they are not existed
914
+ *
915
+ * @param cellDepLikes - The cell deps to add
916
+ *
917
+ * @example
918
+ * ```typescript
919
+ * tx.addCellDeps(cellDep);
920
+ * ```
921
+ */
922
+ addCellDeps(...cellDepsLike) {
923
+ cellDepsLike.flat().forEach((cellDepLike) => {
924
+ const cellDep = CellDep.from(cellDepLike);
925
+ if (this.cellDeps.some((c) => c.eq(cellDep))) {
926
+ return;
927
+ }
928
+ this.cellDeps.push(cellDep);
929
+ });
930
+ }
931
+ /**
932
+ * Add cell dep from infos if they are not existed
933
+ *
934
+ * @param client - A client for searching cell deps
935
+ * @param cellDepLikes - The cell dep infos to add
936
+ *
937
+ * @example
938
+ * ```typescript
939
+ * tx.addCellDepInfos(client, cellDepInfos);
940
+ * ```
941
+ */
942
+ async addCellDepInfos(client, ...cellDepInfosLike) {
943
+ this.addCellDeps(await client.getCellDeps(...cellDepInfosLike));
944
+ }
945
+ /**
946
+ * Add cell deps from known script
947
+ *
948
+ * @param client - The client for searching known script and cell deps
949
+ * @param scripts - The known scripts to add
950
+ *
951
+ * @example
952
+ * ```typescript
953
+ * tx.addCellDepsOfKnownScripts(client, KnownScript.OmniLock);
954
+ * ```
955
+ */
956
+ async addCellDepsOfKnownScripts(client, ...scripts) {
957
+ await Promise.all(scripts
958
+ .flat()
959
+ .map(async (script) => this.addCellDepInfos(client, (await client.getKnownScript(script)).cellDeps)));
960
+ }
961
+ /**
962
+ * Set output data at index.
963
+ *
964
+ * @param index - The index of the output data.
965
+ * @param witness - The data to set.
966
+ *
967
+ * @example
968
+ * ```typescript
969
+ * await tx.setOutputData(0, "0x00");
970
+ * ```
971
+ */
972
+ setOutputDataAt(index, data) {
973
+ if (this.outputsData.length < index) {
974
+ this.outputsData.push(...Array.from(new Array(index - this.outputsData.length), () => "0x"));
975
+ }
976
+ this.outputsData[index] = hexFrom(data);
977
+ }
978
+ /**
979
+ * Add output
980
+ *
981
+ * @param output - The cell output to add
982
+ * @param data - optional output data
983
+ *
984
+ * @example
985
+ * ```typescript
986
+ * await tx.addOutput(cellOutput, "0xabcd");
987
+ * ```
988
+ */
989
+ addOutput(outputLike, outputData = "0x") {
990
+ const output = CellOutput.from({
991
+ ...outputLike,
992
+ capacity: outputLike.capacity ?? 0,
993
+ });
994
+ if (output.capacity === Zero) {
995
+ output.capacity = fixedPointFrom(output.occupiedSize + bytesFrom(outputData).length);
996
+ }
997
+ const i = this.outputs.push(output) - 1;
998
+ this.setOutputDataAt(i, outputData);
999
+ }
741
1000
  /**
742
1001
  * Get witness at index as WitnessArgs
743
1002
  *
@@ -760,7 +1019,6 @@ export class Transaction {
760
1019
  *
761
1020
  * @param index - The index of the witness.
762
1021
  * @param witness - The WitnessArgs to set.
763
- * @returns The transaction itself.
764
1022
  *
765
1023
  * @example
766
1024
  * ```typescript
@@ -795,4 +1053,165 @@ export class Transaction {
795
1053
  witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
796
1054
  this.setWitnessArgsAt(position, witness);
797
1055
  }
1056
+ async getInputsCapacity(client) {
1057
+ return reduceAsync(this.inputs, async (acc, input) => {
1058
+ await input.completeExtraInfos(client);
1059
+ if (!input.cellOutput) {
1060
+ throw new Error("Unable to complete input");
1061
+ }
1062
+ return acc + input.cellOutput.capacity;
1063
+ }, numFrom(0));
1064
+ }
1065
+ getOutputsCapacity() {
1066
+ return this.outputs.reduce((acc, { capacity }) => acc + capacity, numFrom(0));
1067
+ }
1068
+ async getInputsUdtBalance(client, type) {
1069
+ return reduceAsync(this.inputs, async (acc, input) => {
1070
+ await input.completeExtraInfos(client);
1071
+ if (!input.cellOutput || !input.outputData) {
1072
+ throw new Error("Unable to complete input");
1073
+ }
1074
+ if (!input.cellOutput.type?.eq(type)) {
1075
+ return;
1076
+ }
1077
+ return acc + udtBalanceFrom(input.outputData);
1078
+ }, numFrom(0));
1079
+ }
1080
+ getOutputsUdtBalance(type) {
1081
+ return this.outputs.reduce((acc, output, i) => {
1082
+ if (!output.type?.eq(type)) {
1083
+ return acc;
1084
+ }
1085
+ return acc + udtBalanceFrom(this.outputsData[i]);
1086
+ }, numFrom(0));
1087
+ }
1088
+ async completeInputs(from, filter, accumulator, init) {
1089
+ const scripts = (await from.getAddressObjs()).map(({ script }) => script);
1090
+ const collectedCells = [];
1091
+ let acc = init;
1092
+ for (const script of scripts) {
1093
+ for await (const cell of from.client.findCellsByCollectableSearchKey({
1094
+ script,
1095
+ scriptType: "lock",
1096
+ filter,
1097
+ scriptSearchMode: "exact",
1098
+ withData: true,
1099
+ })) {
1100
+ if (this.inputs.some(({ previousOutput }) => previousOutput.eq(cell.outPoint))) {
1101
+ continue;
1102
+ }
1103
+ const i = collectedCells.push(cell);
1104
+ const next = await Promise.resolve(accumulator(acc, cell, i - 1, collectedCells));
1105
+ if (next === undefined) {
1106
+ this.inputs.push(...collectedCells.map(({ outPoint, outputData, cellOutput }) => CellInput.from({
1107
+ previousOutput: outPoint,
1108
+ since: 0,
1109
+ outputData,
1110
+ cellOutput,
1111
+ })));
1112
+ return collectedCells.length;
1113
+ }
1114
+ acc = next;
1115
+ }
1116
+ }
1117
+ throw new Error("Failed to find enough cells for input");
1118
+ }
1119
+ async completeInputsByCapacity(from, capacityTweak, filter) {
1120
+ const exceptedCapacity = this.getOutputsCapacity() + numFrom(capacityTweak ?? 0);
1121
+ const inputsCapacity = await this.getInputsCapacity(from.client);
1122
+ if (inputsCapacity >= exceptedCapacity) {
1123
+ return 0;
1124
+ }
1125
+ return this.completeInputs(from, filter ?? {
1126
+ scriptLenRange: [0, 1],
1127
+ outputDataLenRange: [0, 1],
1128
+ }, (acc, { cellOutput: { capacity } }) => {
1129
+ const sum = acc + capacity;
1130
+ return sum > exceptedCapacity ? undefined : sum;
1131
+ }, inputsCapacity);
1132
+ }
1133
+ async completeInputsByUdt(from, type) {
1134
+ const exceptedBalance = this.getOutputsUdtBalance(type);
1135
+ const inputsBalance = await this.getInputsUdtBalance(from.client, type);
1136
+ if (inputsBalance >= exceptedBalance) {
1137
+ return 0;
1138
+ }
1139
+ return this.completeInputs(from, {
1140
+ script: type,
1141
+ outputDataLenRange: [16, numFrom("0xffffffff")],
1142
+ }, (acc, { outputData }) => {
1143
+ const balance = udtBalanceFrom(outputData);
1144
+ const sum = acc + balance;
1145
+ return sum > exceptedBalance ? undefined : sum;
1146
+ }, inputsBalance);
1147
+ }
1148
+ estimateFee(feeRate) {
1149
+ const txSize = this.toBytes().length + 4;
1150
+ return (numFrom(txSize) * numFrom(feeRate) + numFrom(1000)) / numFrom(1000);
1151
+ }
1152
+ async completeFee(from, change, feeRate, filter) {
1153
+ // Complete all inputs extra infos for cache
1154
+ await this.getInputsCapacity(from.client);
1155
+ let leastFee = this.estimateFee(feeRate);
1156
+ let leastExtraCapacity = Zero;
1157
+ while (true) {
1158
+ const prepared = await from.prepareTransaction(this.clone());
1159
+ const collected = await prepared.completeInputsByCapacity(from, leastFee + leastExtraCapacity, filter);
1160
+ const extraCapacity = (await prepared.getInputsCapacity(from.client)) -
1161
+ prepared.getOutputsCapacity();
1162
+ // The extra capacity paid the fee without a change
1163
+ if (extraCapacity === leastFee) {
1164
+ this.copy(prepared);
1165
+ return [collected, false];
1166
+ }
1167
+ let changed = prepared.clone();
1168
+ const needed = numFrom(await Promise.resolve(change(changed, extraCapacity - leastFee)));
1169
+ // No enough extra capacity to create new cells for change
1170
+ if (needed > Zero) {
1171
+ leastExtraCapacity = needed;
1172
+ continue;
1173
+ }
1174
+ if ((await changed.getInputsCapacity(from.client)) -
1175
+ changed.getOutputsCapacity() !==
1176
+ leastFee) {
1177
+ throw new Error("The change function doesn't use all available capacity");
1178
+ }
1179
+ // New change cells created, update the fee
1180
+ await from.prepareTransaction(changed);
1181
+ const changedFee = changed.estimateFee(feeRate);
1182
+ if (leastFee > changedFee) {
1183
+ throw new Error("The change function removed existed transaction data");
1184
+ }
1185
+ // The fee has been paid
1186
+ if (leastFee === changedFee) {
1187
+ this.copy(changed);
1188
+ return [collected, true];
1189
+ }
1190
+ // The fee after changing is more than the original fee
1191
+ leastFee = changedFee;
1192
+ }
1193
+ }
1194
+ completeFeeChangeToLock(from, change, feeRate, filter) {
1195
+ const script = Script.from(change);
1196
+ return this.completeFee(from, (tx, capacity) => {
1197
+ const changeCell = CellOutput.from({ capacity: 0, lock: script });
1198
+ const occupiedCapacity = fixedPointFrom(changeCell.occupiedSize);
1199
+ if (capacity < occupiedCapacity) {
1200
+ return occupiedCapacity;
1201
+ }
1202
+ changeCell.capacity = capacity;
1203
+ tx.addOutput(changeCell);
1204
+ return 0;
1205
+ }, feeRate, filter);
1206
+ }
1207
+ completeFeeChangeToOutput(from, index, feeRate, filter) {
1208
+ const change = Number(numFrom(index));
1209
+ if (!this.outputs[change]) {
1210
+ throw new Error("Non-existed output to change");
1211
+ }
1212
+ return this.completeFee(from, (tx, capacity) => {
1213
+ tx.outputs[change].capacity += capacity;
1214
+ return 0;
1215
+ }, feeRate, filter);
1216
+ }
798
1217
  }
@@ -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"}