@ckb-ccc/core 1.5.3 → 1.7.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 (161) hide show
  1. package/CHANGELOG.md +61 -7
  2. package/dist/ckb/script.d.ts +2 -1
  3. package/dist/ckb/script.d.ts.map +1 -1
  4. package/dist/ckb/transaction.d.ts +125 -7
  5. package/dist/ckb/transaction.d.ts.map +1 -1
  6. package/dist/ckb/transaction.js +263 -73
  7. package/dist/client/cache/cache.d.ts +67 -11
  8. package/dist/client/cache/cache.d.ts.map +1 -1
  9. package/dist/client/cache/cache.js +94 -9
  10. package/dist/client/cache/memory.advanced.d.ts +2 -2
  11. package/dist/client/cache/memory.advanced.d.ts.map +1 -1
  12. package/dist/client/cache/memory.advanced.js +19 -7
  13. package/dist/client/cache/memory.d.ts +24 -7
  14. package/dist/client/cache/memory.d.ts.map +1 -1
  15. package/dist/client/cache/memory.js +76 -18
  16. package/dist/client/client.d.ts +30 -7
  17. package/dist/client/client.d.ts.map +1 -1
  18. package/dist/client/client.js +114 -22
  19. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -1
  20. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  21. package/dist/client/clientPublicMainnet.advanced.js +1 -1
  22. package/dist/client/clientPublicMainnet.d.ts +2 -1
  23. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  24. package/dist/client/clientPublicMainnet.js +1 -6
  25. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -1
  26. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  27. package/dist/client/clientPublicTestnet.advanced.js +1 -1
  28. package/dist/client/clientPublicTestnet.d.ts +2 -1
  29. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  30. package/dist/client/clientTypes.advanced.d.ts +3 -0
  31. package/dist/client/clientTypes.advanced.d.ts.map +1 -1
  32. package/dist/client/clientTypes.advanced.js +3 -0
  33. package/dist/client/clientTypes.d.ts +110 -38
  34. package/dist/client/clientTypes.d.ts.map +1 -1
  35. package/dist/client/clientTypes.js +118 -31
  36. package/dist/client/index.d.ts +1 -0
  37. package/dist/client/index.d.ts.map +1 -1
  38. package/dist/client/index.js +1 -0
  39. package/dist/client/jsonRpc/client.d.ts +4 -4
  40. package/dist/client/jsonRpc/client.d.ts.map +1 -1
  41. package/dist/client/jsonRpc/client.js +4 -4
  42. package/dist/client/jsonRpc/transformers.js +3 -3
  43. package/dist/client/knownScript.d.ts +26 -0
  44. package/dist/client/knownScript.d.ts.map +1 -0
  45. package/dist/client/knownScript.js +27 -0
  46. package/dist/jsonRpc/transports/webSocket.d.ts.map +1 -1
  47. package/dist/jsonRpc/transports/webSocket.js +7 -3
  48. package/dist/molecule/predefined.d.ts +10 -10
  49. package/dist/molecule/predefined.d.ts.map +1 -1
  50. package/dist/num/index.d.ts.map +1 -1
  51. package/dist/num/index.js +17 -1
  52. package/dist/signer/ckb/signerCkbPublicKey.d.ts.map +1 -1
  53. package/dist/signer/ckb/signerCkbPublicKey.js +1 -5
  54. package/dist/signer/nostr/index.d.ts +1 -0
  55. package/dist/signer/nostr/index.d.ts.map +1 -1
  56. package/dist/signer/nostr/index.js +1 -0
  57. package/dist/signer/nostr/signerNostr.d.ts +1 -1
  58. package/dist/signer/nostr/signerNostr.d.ts.map +1 -1
  59. package/dist/signer/nostr/signerNostr.js +8 -0
  60. package/dist/signer/nostr/signerNostrPrivateKey.d.ts +5 -7
  61. package/dist/signer/nostr/signerNostrPrivateKey.d.ts.map +1 -1
  62. package/dist/signer/nostr/signerNostrPrivateKey.js +6 -12
  63. package/dist/signer/nostr/signerNostrPublicKeyReadonly.d.ts +11 -0
  64. package/dist/signer/nostr/signerNostrPublicKeyReadonly.d.ts.map +1 -0
  65. package/dist/signer/nostr/signerNostrPublicKeyReadonly.js +22 -0
  66. package/dist/utils/index.d.ts +4 -0
  67. package/dist/utils/index.d.ts.map +1 -1
  68. package/dist/utils/index.js +13 -1
  69. package/dist.commonjs/advanced.js +17 -7
  70. package/dist.commonjs/ckb/script.d.ts +2 -1
  71. package/dist.commonjs/ckb/script.d.ts.map +1 -1
  72. package/dist.commonjs/ckb/transaction.d.ts +125 -7
  73. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  74. package/dist.commonjs/ckb/transaction.js +352 -160
  75. package/dist.commonjs/client/cache/cache.d.ts +67 -11
  76. package/dist.commonjs/client/cache/cache.d.ts.map +1 -1
  77. package/dist.commonjs/client/cache/cache.js +94 -9
  78. package/dist.commonjs/client/cache/memory.advanced.d.ts +2 -2
  79. package/dist.commonjs/client/cache/memory.advanced.d.ts.map +1 -1
  80. package/dist.commonjs/client/cache/memory.advanced.js +19 -7
  81. package/dist.commonjs/client/cache/memory.d.ts +24 -7
  82. package/dist.commonjs/client/cache/memory.d.ts.map +1 -1
  83. package/dist.commonjs/client/cache/memory.js +75 -17
  84. package/dist.commonjs/client/client.d.ts +30 -7
  85. package/dist.commonjs/client/client.d.ts.map +1 -1
  86. package/dist.commonjs/client/client.js +112 -20
  87. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +2 -1
  88. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  89. package/dist.commonjs/client/clientPublicMainnet.advanced.js +21 -21
  90. package/dist.commonjs/client/clientPublicMainnet.d.ts +2 -1
  91. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
  92. package/dist.commonjs/client/clientPublicMainnet.js +1 -6
  93. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +2 -1
  94. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  95. package/dist.commonjs/client/clientPublicTestnet.advanced.js +21 -21
  96. package/dist.commonjs/client/clientPublicTestnet.d.ts +2 -1
  97. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
  98. package/dist.commonjs/client/clientTypes.advanced.d.ts +3 -0
  99. package/dist.commonjs/client/clientTypes.advanced.d.ts.map +1 -1
  100. package/dist.commonjs/client/clientTypes.advanced.js +4 -0
  101. package/dist.commonjs/client/clientTypes.d.ts +110 -38
  102. package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
  103. package/dist.commonjs/client/clientTypes.js +123 -31
  104. package/dist.commonjs/client/index.d.ts +1 -0
  105. package/dist.commonjs/client/index.d.ts.map +1 -1
  106. package/dist.commonjs/client/index.js +1 -0
  107. package/dist.commonjs/client/jsonRpc/client.d.ts +4 -4
  108. package/dist.commonjs/client/jsonRpc/client.d.ts.map +1 -1
  109. package/dist.commonjs/client/jsonRpc/client.js +4 -4
  110. package/dist.commonjs/client/jsonRpc/transformers.js +2 -2
  111. package/dist.commonjs/client/knownScript.d.ts +26 -0
  112. package/dist.commonjs/client/knownScript.d.ts.map +1 -0
  113. package/dist.commonjs/client/knownScript.js +30 -0
  114. package/dist.commonjs/index.js +17 -7
  115. package/dist.commonjs/jsonRpc/transports/webSocket.d.ts.map +1 -1
  116. package/dist.commonjs/jsonRpc/transports/webSocket.js +7 -3
  117. package/dist.commonjs/molecule/index.js +17 -7
  118. package/dist.commonjs/num/index.d.ts.map +1 -1
  119. package/dist.commonjs/num/index.js +17 -1
  120. package/dist.commonjs/signer/ckb/signerCkbPublicKey.d.ts.map +1 -1
  121. package/dist.commonjs/signer/ckb/signerCkbPublicKey.js +1 -5
  122. package/dist.commonjs/signer/nostr/index.d.ts +1 -0
  123. package/dist.commonjs/signer/nostr/index.d.ts.map +1 -1
  124. package/dist.commonjs/signer/nostr/index.js +1 -0
  125. package/dist.commonjs/signer/nostr/signerNostr.d.ts +1 -1
  126. package/dist.commonjs/signer/nostr/signerNostr.d.ts.map +1 -1
  127. package/dist.commonjs/signer/nostr/signerNostr.js +8 -0
  128. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.d.ts +5 -7
  129. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.d.ts.map +1 -1
  130. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.js +6 -12
  131. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.d.ts +11 -0
  132. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.d.ts.map +1 -0
  133. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.js +26 -0
  134. package/dist.commonjs/utils/index.d.ts +4 -0
  135. package/dist.commonjs/utils/index.d.ts.map +1 -1
  136. package/dist.commonjs/utils/index.js +13 -0
  137. package/package.json +1 -1
  138. package/src/ckb/script.ts +2 -1
  139. package/src/ckb/transaction.ts +376 -97
  140. package/src/client/cache/cache.ts +126 -13
  141. package/src/client/cache/memory.advanced.ts +24 -7
  142. package/src/client/cache/memory.ts +130 -30
  143. package/src/client/client.ts +172 -28
  144. package/src/client/clientPublicMainnet.advanced.ts +2 -1
  145. package/src/client/clientPublicMainnet.ts +3 -7
  146. package/src/client/clientPublicTestnet.advanced.ts +2 -1
  147. package/src/client/clientPublicTestnet.ts +2 -1
  148. package/src/client/clientTypes.advanced.ts +4 -0
  149. package/src/client/clientTypes.ts +224 -61
  150. package/src/client/index.ts +1 -0
  151. package/src/client/jsonRpc/client.ts +11 -9
  152. package/src/client/jsonRpc/transformers.ts +2 -2
  153. package/src/client/knownScript.ts +27 -0
  154. package/src/jsonRpc/transports/webSocket.ts +17 -13
  155. package/src/num/index.ts +21 -1
  156. package/src/signer/ckb/signerCkbPublicKey.ts +3 -5
  157. package/src/signer/nostr/index.ts +1 -0
  158. package/src/signer/nostr/signerNostr.ts +3 -1
  159. package/src/signer/nostr/signerNostrPrivateKey.ts +7 -14
  160. package/src/signer/nostr/signerNostrPublicKeyReadonly.ts +29 -0
  161. package/src/utils/index.ts +14 -1
@@ -3,6 +3,7 @@ import {
3
3
  CellDep,
4
4
  CellDepLike,
5
5
  Epoch,
6
+ EpochLike,
6
7
  HashType,
7
8
  HashTypeLike,
8
9
  OutPoint,
@@ -10,6 +11,8 @@ import {
10
11
  Script,
11
12
  ScriptLike,
12
13
  Transaction,
14
+ TransactionLike,
15
+ epochFrom,
13
16
  hashTypeFrom,
14
17
  } from "../ckb/index.js";
15
18
  import { Hex, HexLike, hexFrom } from "../hex/index.js";
@@ -21,34 +24,6 @@ import {
21
24
  clientSearchKeyRangeFrom,
22
25
  } from "./clientTypes.advanced.js";
23
26
 
24
- /**
25
- * @public
26
- */
27
- export enum KnownScript {
28
- NervosDao = "NervosDao",
29
- Secp256k1Blake160 = "Secp256k1Blake160",
30
- Secp256k1Multisig = "Secp256k1Multisig",
31
- AnyoneCanPay = "AnyoneCanPay",
32
- TypeId = "TypeId",
33
- XUdt = "XUdt",
34
- JoyId = "JoyId",
35
- COTA = "COTA",
36
- PWLock = "PWLock",
37
- OmniLock = "OmniLock",
38
- NostrLock = "NostrLock",
39
- UniqueType = "UniqueType",
40
-
41
- // ckb-proxy-locks https://github.com/ckb-devrel/ckb-proxy-locks
42
- AlwaysSuccess = "AlwaysSuccess",
43
- InputTypeProxyLock = "InputTypeProxyLock",
44
- OutputTypeProxyLock = "OutputTypeProxyLock",
45
- LockProxyLock = "LockProxyLock",
46
- SingleUseLock = "SingleUseLock",
47
- TypeBurnLock = "TypeBurnLock",
48
- EasyToDiscoverType = "EasyToDiscoverType",
49
- TimeLock = "TimeLock",
50
- }
51
-
52
27
  /**
53
28
  * @public
54
29
  */
@@ -67,6 +42,10 @@ export class CellDepInfo {
67
42
  ) {}
68
43
 
69
44
  static from(cellDepInfoLike: CellDepInfoLike): CellDepInfo {
45
+ if (cellDepInfoLike instanceof CellDepInfo) {
46
+ return cellDepInfoLike;
47
+ }
48
+
70
49
  return new CellDepInfo(
71
50
  CellDep.from(cellDepInfoLike.cellDep),
72
51
  apply(Script.from, cellDepInfoLike.type),
@@ -94,6 +73,10 @@ export class ScriptInfo {
94
73
  ) {}
95
74
 
96
75
  static from(scriptInfoLike: ScriptInfoLike): ScriptInfo {
76
+ if (scriptInfoLike instanceof ScriptInfo) {
77
+ return scriptInfoLike;
78
+ }
79
+
97
80
  return new ScriptInfo(
98
81
  hexFrom(scriptInfoLike.codeHash),
99
82
  hashTypeFrom(scriptInfoLike.hashType),
@@ -120,15 +103,59 @@ export type TransactionStatus =
120
103
  /**
121
104
  * @public
122
105
  */
123
- export type ClientTransactionResponse = {
124
- transaction: Transaction;
106
+ export type ClientTransactionResponseLike = {
107
+ transaction: TransactionLike;
125
108
  status: TransactionStatus;
126
- cycles?: Num;
127
- blockHash?: Hex;
128
- blockNumber?: Num;
129
- txIndex?: Num;
109
+ cycles?: NumLike;
110
+ blockHash?: HexLike;
111
+ blockNumber?: NumLike;
112
+ txIndex?: NumLike;
130
113
  reason?: string;
131
114
  };
115
+ /**
116
+ * @public
117
+ */
118
+ export class ClientTransactionResponse {
119
+ constructor(
120
+ public transaction: Transaction,
121
+ public status: TransactionStatus,
122
+ public cycles?: Num,
123
+ public blockHash?: Hex,
124
+ public blockNumber?: Num,
125
+ public txIndex?: Num,
126
+ public reason?: string,
127
+ ) {}
128
+
129
+ static from(
130
+ responseLike: ClientTransactionResponseLike,
131
+ ): ClientTransactionResponse {
132
+ if (responseLike instanceof ClientTransactionResponse) {
133
+ return responseLike;
134
+ }
135
+
136
+ return new ClientTransactionResponse(
137
+ Transaction.from(responseLike.transaction),
138
+ responseLike.status,
139
+ apply(numFrom, responseLike.cycles),
140
+ apply(hexFrom, responseLike.blockHash),
141
+ apply(numFrom, responseLike.blockNumber),
142
+ apply(numFrom, responseLike.txIndex),
143
+ responseLike.reason,
144
+ );
145
+ }
146
+
147
+ clone() {
148
+ return new ClientTransactionResponse(
149
+ this.transaction.clone(),
150
+ this.status,
151
+ this.cycles,
152
+ this.blockHash,
153
+ this.blockNumber,
154
+ this.txIndex,
155
+ this.reason,
156
+ );
157
+ }
158
+ }
132
159
 
133
160
  /**
134
161
  * @public
@@ -154,6 +181,10 @@ export class ClientIndexerSearchKeyFilter {
154
181
  static from(
155
182
  filterLike: ClientIndexerSearchKeyFilterLike,
156
183
  ): ClientIndexerSearchKeyFilter {
184
+ if (filterLike instanceof ClientIndexerSearchKeyFilter) {
185
+ return filterLike;
186
+ }
187
+
157
188
  return new ClientIndexerSearchKeyFilter(
158
189
  apply(Script.from, filterLike.script),
159
190
  apply(clientSearchKeyRangeFrom, filterLike.scriptLenRange),
@@ -186,6 +217,10 @@ export class ClientIndexerSearchKey {
186
217
  ) {}
187
218
 
188
219
  static from(keyLike: ClientIndexerSearchKeyLike): ClientIndexerSearchKey {
220
+ if (keyLike instanceof ClientIndexerSearchKey) {
221
+ return keyLike;
222
+ }
223
+
189
224
  return new ClientIndexerSearchKey(
190
225
  Script.from(keyLike.script),
191
226
  keyLike.scriptType,
@@ -230,6 +265,10 @@ export class ClientIndexerSearchKeyTransaction {
230
265
  static from(
231
266
  keyLike: ClientIndexerSearchKeyTransactionLike,
232
267
  ): ClientIndexerSearchKeyTransaction {
268
+ if (keyLike instanceof ClientIndexerSearchKeyTransaction) {
269
+ return keyLike;
270
+ }
271
+
233
272
  return new ClientIndexerSearchKeyTransaction(
234
273
  Script.from(keyLike.script),
235
274
  keyLike.scriptType,
@@ -273,58 +312,167 @@ export type ClientFindTransactionsGroupedResponse = {
273
312
  /**
274
313
  * @public
275
314
  */
276
- export type ClientBlockHeader = {
277
- compactTarget: Num;
315
+ export type ClientBlockHeaderLike = {
316
+ compactTarget: NumLike;
278
317
  dao: {
279
318
  /**
280
319
  * C_i: the total issuance up to and including block i.
281
320
  */
282
- c: Num;
321
+ c: NumLike;
283
322
  /**
284
323
  * AR_i: the current accumulated rate at block i.
285
324
  * AR_j / AR_i reflects the CKByte amount if one deposit 1 CKB to Nervos DAO at block i, and withdraw at block j.
286
325
  */
287
- ar: Num;
326
+ ar: NumLike;
288
327
  /**
289
328
  * S_i: the total unissued secondary issuance up to and including block i,
290
329
  * including unclaimed Nervos DAO compensation and treasury funds.
291
330
  */
292
- s: Num;
331
+ s: NumLike;
293
332
  /**
294
333
  * U_i: the total occupied capacities currently in the blockchain up to and including block i.
295
334
  * Occupied capacity is the sum of capacities used to store all cells.
296
335
  */
297
- u: Num;
336
+ u: NumLike;
298
337
  };
299
- epoch: Epoch;
300
- extraHash: Hex;
301
- hash: Hex;
302
- nonce: Num;
303
- number: Num;
304
- parentHash: Hex;
305
- proposalsHash: Hex;
306
- timestamp: Num;
307
- transactionsRoot: Hex;
308
- version: Num;
338
+ epoch: EpochLike;
339
+ extraHash: HexLike;
340
+ hash: HexLike;
341
+ nonce: NumLike;
342
+ number: NumLike;
343
+ parentHash: HexLike;
344
+ proposalsHash: HexLike;
345
+ timestamp: NumLike;
346
+ transactionsRoot: HexLike;
347
+ version: NumLike;
309
348
  };
349
+ /**
350
+ * @public
351
+ */
352
+ export class ClientBlockHeader {
353
+ constructor(
354
+ public compactTarget: Num,
355
+ public dao: {
356
+ /**
357
+ * C_i: the total issuance up to and including block i.
358
+ */
359
+ c: Num;
360
+ /**
361
+ * AR_i: the current accumulated rate at block i.
362
+ * AR_j / AR_i reflects the CKByte amount if one deposit 1 CKB to Nervos DAO at block i, and withdraw at block j.
363
+ */
364
+ ar: Num;
365
+ /**
366
+ * S_i: the total unissued secondary issuance up to and including block i,
367
+ * including unclaimed Nervos DAO compensation and treasury funds.
368
+ */
369
+ s: Num;
370
+ /**
371
+ * U_i: the total occupied capacities currently in the blockchain up to and including block i.
372
+ * Occupied capacity is the sum of capacities used to store all cells.
373
+ */
374
+ u: Num;
375
+ },
376
+ public epoch: Epoch,
377
+ public extraHash: Hex,
378
+ public hash: Hex,
379
+ public nonce: Num,
380
+ public number: Num,
381
+ public parentHash: Hex,
382
+ public proposalsHash: Hex,
383
+ public timestamp: Num,
384
+ public transactionsRoot: Hex,
385
+ public version: Num,
386
+ ) {}
387
+
388
+ static from(headerLike: ClientBlockHeaderLike): ClientBlockHeader {
389
+ if (headerLike instanceof ClientBlockHeader) {
390
+ return headerLike;
391
+ }
392
+
393
+ return new ClientBlockHeader(
394
+ numFrom(headerLike.compactTarget),
395
+ {
396
+ c: numFrom(headerLike.dao.c),
397
+ ar: numFrom(headerLike.dao.ar),
398
+ s: numFrom(headerLike.dao.s),
399
+ u: numFrom(headerLike.dao.u),
400
+ },
401
+ epochFrom(headerLike.epoch),
402
+ hexFrom(headerLike.extraHash),
403
+ hexFrom(headerLike.hash),
404
+ numFrom(headerLike.nonce),
405
+ numFrom(headerLike.number),
406
+ hexFrom(headerLike.parentHash),
407
+ hexFrom(headerLike.proposalsHash),
408
+ numFrom(headerLike.timestamp),
409
+ hexFrom(headerLike.transactionsRoot),
410
+ numFrom(headerLike.version),
411
+ );
412
+ }
413
+ }
310
414
 
311
415
  /**
312
416
  * @public
313
417
  */
314
- export type ClientBlockUncle = {
315
- header: ClientBlockHeader;
316
- proposals: Hex[];
418
+ export type ClientBlockUncleLike = {
419
+ header: ClientBlockHeaderLike;
420
+ proposals: HexLike[];
317
421
  };
422
+ /**
423
+ * @public
424
+ */
425
+ export class ClientBlockUncle {
426
+ constructor(
427
+ public header: ClientBlockHeader,
428
+ public proposals: Hex[],
429
+ ) {}
430
+
431
+ static from(uncleLike: ClientBlockUncleLike): ClientBlockUncle {
432
+ if (uncleLike instanceof ClientBlockUncle) {
433
+ return uncleLike;
434
+ }
435
+
436
+ return new ClientBlockUncle(
437
+ ClientBlockHeader.from(uncleLike.header),
438
+ uncleLike.proposals.map(hexFrom),
439
+ );
440
+ }
441
+ }
318
442
 
319
443
  /**
320
444
  * @public
321
445
  */
322
- export type ClientBlock = {
323
- header: ClientBlockHeader;
324
- proposals: Hex[];
325
- transactions: Transaction[];
326
- uncles: ClientBlockUncle[];
446
+ export type ClientBlockLike = {
447
+ header: ClientBlockHeaderLike;
448
+ proposals: HexLike[];
449
+ transactions: TransactionLike[];
450
+ uncles: ClientBlockUncleLike[];
327
451
  };
452
+ /**
453
+ * @public
454
+ */
455
+ export class ClientBlock {
456
+ constructor(
457
+ public header: ClientBlockHeader,
458
+ public proposals: Hex[],
459
+ public transactions: Transaction[],
460
+ public uncles: ClientBlockUncle[],
461
+ ) {}
462
+
463
+ static from(blockLike: ClientBlockLike): ClientBlock {
464
+ if (blockLike instanceof ClientBlock) {
465
+ return blockLike;
466
+ }
467
+
468
+ return new ClientBlock(
469
+ ClientBlockHeader.from(blockLike.header),
470
+ blockLike.proposals.map(hexFrom),
471
+ blockLike.transactions.map(Transaction.from),
472
+ blockLike.uncles.map(ClientBlockUncle.from),
473
+ );
474
+ }
475
+ }
328
476
 
329
477
  export interface ErrorClientBaseLike {
330
478
  message?: string;
@@ -392,8 +540,23 @@ export class ErrorClientRBFRejected extends ErrorClientBase {
392
540
  }
393
541
  }
394
542
 
395
- export class ErrorClientWaitTransactionTimeout extends Error {
396
- constructor() {
397
- super("Wait transaction timeout");
543
+ export class ErrorClientWaitTransactionTimeout extends ErrorClientBase {
544
+ constructor(timeoutLike: NumLike) {
545
+ const timeout = numFrom(timeoutLike).toString();
546
+ super({
547
+ message: `Wait transaction timeout ${timeout}ms`,
548
+ data: JSON.stringify({ timeout }),
549
+ });
550
+ }
551
+ }
552
+
553
+ export class ErrorClientMaxFeeRateExceeded extends ErrorClientBase {
554
+ constructor(limitLike: NumLike, actualLike: NumLike) {
555
+ const limit = numFrom(limitLike).toString();
556
+ const actual = numFrom(actualLike).toString();
557
+ super({
558
+ message: `Max fee rate exceeded limit ${limit}, actual ${actual}. Developer might forgot to complete transaction fee before sending. See https://docs.ckbccc.com/classes/_ckb_ccc_core.index.ccc.Transaction.html#completeFeeBy.`,
559
+ data: JSON.stringify({ limit, actual }),
560
+ });
398
561
  }
399
562
  }
@@ -4,3 +4,4 @@ export * from "./clientPublicMainnet.js";
4
4
  export * from "./clientPublicTestnet.js";
5
5
  export * from "./clientTypes.js";
6
6
  export * from "./jsonRpc/index.js";
7
+ export * from "./knownScript.js";
@@ -169,11 +169,11 @@ export abstract class ClientJsonRpc extends Client {
169
169
  * @param withCycles - whether the return cycles of block transactions. (Optional, default false.)
170
170
  * @returns Block
171
171
  */
172
- getBlockByNumber = this.buildSender(
172
+ getBlockByNumberNoCache = this.buildSender(
173
173
  "get_block_by_number",
174
174
  [(v: NumLike) => numToHex(numFrom(v))],
175
175
  (b: JsonRpcBlock) => apply(JsonRpcTransformers.blockTo, b),
176
- ) as Client["getBlockByNumber"];
176
+ ) as Client["getBlockByNumberNoCache"];
177
177
 
178
178
  /**
179
179
  * Get block by block hash
@@ -183,9 +183,11 @@ export abstract class ClientJsonRpc extends Client {
183
183
  * @param withCycles - whether the return cycles of block transactions. (Optional, default false.)
184
184
  * @returns Block
185
185
  */
186
- getBlockByHash = this.buildSender("get_block", [hexFrom], (b: JsonRpcBlock) =>
187
- apply(JsonRpcTransformers.blockTo, b),
188
- ) as Client["getBlockByHash"];
186
+ getBlockByHashNoCache = this.buildSender(
187
+ "get_block",
188
+ [hexFrom],
189
+ (b: JsonRpcBlock) => apply(JsonRpcTransformers.blockTo, b),
190
+ ) as Client["getBlockByHashNoCache"];
189
191
 
190
192
  /**
191
193
  * Get header by block number
@@ -194,11 +196,11 @@ export abstract class ClientJsonRpc extends Client {
194
196
  * @param verbosity - result format which allows 0 and 1. (Optional, the default is 1.)
195
197
  * @returns BlockHeader
196
198
  */
197
- getHeaderByNumber = this.buildSender(
199
+ getHeaderByNumberNoCache = this.buildSender(
198
200
  "get_header_by_number",
199
201
  [(v: NumLike) => numToHex(numFrom(v))],
200
202
  (b: JsonRpcBlockHeader) => apply(JsonRpcTransformers.blockHeaderTo, b),
201
- ) as Client["getHeaderByNumber"];
203
+ ) as Client["getHeaderByNumberNoCache"];
202
204
 
203
205
  /**
204
206
  * Get header by block hash
@@ -207,11 +209,11 @@ export abstract class ClientJsonRpc extends Client {
207
209
  * @param verbosity - result format which allows 0 and 1. (Optional, the default is 1.)
208
210
  * @returns BlockHeader
209
211
  */
210
- getHeaderByHash = this.buildSender(
212
+ getHeaderByHashNoCache = this.buildSender(
211
213
  "get_header",
212
214
  [hexFrom],
213
215
  (b: JsonRpcBlockHeader) => apply(JsonRpcTransformers.blockHeaderTo, b),
214
- ) as Client["getHeaderByHash"];
216
+ ) as Client["getHeaderByHashNoCache"];
215
217
 
216
218
  /**
217
219
  * Estimate cycles of a transaction.
@@ -192,7 +192,7 @@ export class JsonRpcTransformers {
192
192
  return;
193
193
  }
194
194
 
195
- return {
195
+ return ClientTransactionResponse.from({
196
196
  transaction: JsonRpcTransformers.transactionTo(transaction),
197
197
  status,
198
198
  cycles: apply(numFrom, cycles),
@@ -200,7 +200,7 @@ export class JsonRpcTransformers {
200
200
  blockNumber: apply(numFrom, block_number),
201
201
  txIndex: apply(numFrom, tx_index),
202
202
  reason,
203
- };
203
+ });
204
204
  }
205
205
  static blockHeaderTo(header: JsonRpcBlockHeader): ClientBlockHeader {
206
206
  const dao = bytesFrom(header.dao);
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @public
3
+ */
4
+ export enum KnownScript {
5
+ NervosDao = "NervosDao",
6
+ Secp256k1Blake160 = "Secp256k1Blake160",
7
+ Secp256k1Multisig = "Secp256k1Multisig",
8
+ AnyoneCanPay = "AnyoneCanPay",
9
+ TypeId = "TypeId",
10
+ XUdt = "XUdt",
11
+ JoyId = "JoyId",
12
+ COTA = "COTA",
13
+ PWLock = "PWLock",
14
+ OmniLock = "OmniLock",
15
+ NostrLock = "NostrLock",
16
+ UniqueType = "UniqueType",
17
+
18
+ // ckb-proxy-locks https://github.com/ckb-devrel/ckb-proxy-locks
19
+ AlwaysSuccess = "AlwaysSuccess",
20
+ InputTypeProxyLock = "InputTypeProxyLock",
21
+ OutputTypeProxyLock = "OutputTypeProxyLock",
22
+ LockProxyLock = "LockProxyLock",
23
+ SingleUseLock = "SingleUseLock",
24
+ TypeBurnLock = "TypeBurnLock",
25
+ EasyToDiscoverType = "EasyToDiscoverType",
26
+ TimeLock = "TimeLock",
27
+ }
@@ -87,24 +87,28 @@ export class TransportWebSocket implements Transport {
87
87
  reject,
88
88
  setTimeout(() => {
89
89
  this.ongoing.delete(data.id);
90
- void socket.then((socket) => socket.close());
90
+ void socket
91
+ .then((socket) => socket.close())
92
+ .catch((err) => reject(err));
91
93
  reject(new Error("Request timeout"));
92
94
  }, this.timeout),
93
95
  ];
94
96
  this.ongoing.set(data.id, req);
95
97
 
96
- void socket.then((socket) => {
97
- if (
98
- socket.readyState === socket.CLOSED ||
99
- socket.readyState === socket.CLOSING
100
- ) {
101
- clearTimeout(req[2]);
102
- this.ongoing.delete(data.id);
103
- reject(new Error("Connection closed"));
104
- } else {
105
- socket.send(JSON.stringify(data));
106
- }
107
- });
98
+ void socket
99
+ .then((socket) => {
100
+ if (
101
+ socket.readyState === socket.CLOSED ||
102
+ socket.readyState === socket.CLOSING
103
+ ) {
104
+ clearTimeout(req[2]);
105
+ this.ongoing.delete(data.id);
106
+ reject(new Error("Connection closed"));
107
+ } else {
108
+ socket.send(JSON.stringify(data));
109
+ }
110
+ })
111
+ .catch((err) => reject(err));
108
112
  });
109
113
  }
110
114
  }
package/src/num/index.ts CHANGED
@@ -154,11 +154,31 @@ export function numLeToBytes(val: NumLike, bytes?: number): Bytes {
154
154
  * ```
155
155
  */
156
156
  export function numBeToBytes(val: NumLike, bytes?: number): Bytes {
157
- const rawBytes = bytesFrom(numFrom(val).toString(16));
157
+ let num = numFrom(val);
158
+ if (num < numFrom(0)) {
159
+ if (bytes == null) {
160
+ throw Error(
161
+ "negative number can not be serialized without knowing bytes length",
162
+ );
163
+ }
164
+
165
+ // 0x100............00 - abs(num)
166
+ // | . bytes * 8 .|
167
+ // 2's complement for negative number
168
+ num = (numFrom(1) << (numFrom(8) * numFrom(bytes))) + num;
169
+ if (num < 0) {
170
+ throw Error("negative number underflow");
171
+ }
172
+ }
173
+
174
+ const rawBytes = bytesFrom(num.toString(16));
158
175
  if (bytes == null) {
159
176
  return rawBytes;
160
177
  }
161
178
 
179
+ if (rawBytes.length > bytes) {
180
+ throw Error("number overflow");
181
+ }
162
182
  return bytesConcat("00".repeat(bytes - rawBytes.length), rawBytes);
163
183
  }
164
184
 
@@ -104,12 +104,10 @@ export class SignerCkbPublicKey extends Signer {
104
104
 
105
105
  const scripts: { script: Script; cellDeps: CellDepInfo[] }[] = [];
106
106
  for (const input of tx.inputs) {
107
- await input.completeExtraInfos(this.client);
108
- if (!input.cellOutput) {
109
- throw new Error("Unable to complete input");
110
- }
107
+ const {
108
+ cellOutput: { lock },
109
+ } = await input.getCell(this.client);
111
110
 
112
- const { lock } = input.cellOutput;
113
111
  if (scripts.some(({ script }) => script.eq(lock))) {
114
112
  continue;
115
113
  }
@@ -1,3 +1,4 @@
1
1
  export * from "./signerNostr.js";
2
2
  export * from "./signerNostrPrivateKey.js";
3
+ export * from "./signerNostrPublicKeyReadonly.js";
3
4
  export * from "./verify.js";
@@ -50,7 +50,9 @@ export abstract class SignerNostr extends Signer {
50
50
  *
51
51
  * @returns A promise that resolves to the signed event.
52
52
  */
53
- abstract signNostrEvent(event: NostrEvent): Promise<Required<NostrEvent>>;
53
+ async signNostrEvent(_event: NostrEvent): Promise<Required<NostrEvent>> {
54
+ throw Error("SignerNostr.signNostrEvent not implemented");
55
+ }
54
56
 
55
57
  /**
56
58
  * Sign a message.
@@ -1,25 +1,18 @@
1
1
  import { schnorr } from "@noble/curves/secp256k1";
2
2
  import { Client } from "../../client/index.js";
3
3
  import { Hex, hexFrom, HexLike } from "../../hex/index.js";
4
- import { NostrEvent, SignerNostr } from "./signerNostr.js";
4
+ import { NostrEvent } from "./signerNostr.js";
5
+ import { SignerNostrPublicKeyReadonly } from "./signerNostrPublicKeyReadonly.js";
5
6
  import { nostrEventHash } from "./verify.js";
6
7
 
7
- export class SignerNostrPrivateKey extends SignerNostr {
8
+ export class SignerNostrPrivateKey extends SignerNostrPublicKeyReadonly {
8
9
  private readonly privateKey: Hex;
9
10
 
10
- constructor(client: Client, privateKey: HexLike) {
11
- super(client);
12
- this.privateKey = hexFrom(privateKey);
13
- }
14
-
15
- async connect(): Promise<void> {}
16
-
17
- async isConnected(): Promise<boolean> {
18
- return true;
19
- }
11
+ constructor(client: Client, privateKeyLike: HexLike) {
12
+ const privateKey = hexFrom(privateKeyLike);
13
+ super(client, schnorr.getPublicKey(privateKey.slice(2)));
20
14
 
21
- async getNostrPublicKey(): Promise<Hex> {
22
- return hexFrom(schnorr.getPublicKey(this.privateKey.slice(2)));
15
+ this.privateKey = privateKey;
23
16
  }
24
17
 
25
18
  async signNostrEvent(event: NostrEvent): Promise<Required<NostrEvent>> {
@@ -0,0 +1,29 @@
1
+ import { bech32 } from "bech32";
2
+ import { Client } from "../../client/index.js";
3
+ import { Hex, hexFrom, HexLike } from "../../hex/index.js";
4
+ import { SignerNostr } from "./signerNostr.js";
5
+
6
+ export class SignerNostrPublicKeyReadonly extends SignerNostr {
7
+ public readonly publicKey: Hex;
8
+
9
+ constructor(client: Client, publicKey: HexLike) {
10
+ super(client);
11
+
12
+ if (typeof publicKey === "string" && publicKey.startsWith("npub")) {
13
+ const { words } = bech32.decode(publicKey);
14
+ this.publicKey = hexFrom(bech32.fromWords(words));
15
+ } else {
16
+ this.publicKey = hexFrom(publicKey);
17
+ }
18
+ }
19
+
20
+ async connect(): Promise<void> {}
21
+
22
+ async isConnected(): Promise<boolean> {
23
+ return true;
24
+ }
25
+
26
+ async getNostrPublicKey(): Promise<Hex> {
27
+ return this.publicKey;
28
+ }
29
+ }