@ckb-ccc/core 1.10.0 → 1.11.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 (71) hide show
  1. package/CHANGELOG.md +36 -4
  2. package/dist/ckb/hash.d.ts +18 -0
  3. package/dist/ckb/hash.d.ts.map +1 -0
  4. package/dist/ckb/hash.js +19 -0
  5. package/dist/ckb/index.d.ts +1 -0
  6. package/dist/ckb/index.d.ts.map +1 -1
  7. package/dist/ckb/index.js +1 -0
  8. package/dist/ckb/transaction.d.ts +179 -11
  9. package/dist/ckb/transaction.d.ts.map +1 -1
  10. package/dist/ckb/transaction.js +197 -59
  11. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  12. package/dist/client/clientPublicMainnet.advanced.js +0 -60
  13. package/dist/client/clientPublicTestnet.advanced.d.ts +0 -23
  14. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  15. package/dist/client/clientPublicTestnet.advanced.js +0 -144
  16. package/dist/client/knownScript.d.ts +1 -3
  17. package/dist/client/knownScript.d.ts.map +1 -1
  18. package/dist/client/knownScript.js +0 -4
  19. package/dist/hasher/hasherCkb.d.ts +0 -16
  20. package/dist/hasher/hasherCkb.d.ts.map +1 -1
  21. package/dist/hasher/hasherCkb.js +0 -18
  22. package/dist/molecule/codec.d.ts +30 -10
  23. package/dist/molecule/codec.d.ts.map +1 -1
  24. package/dist/molecule/codec.js +46 -13
  25. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +18 -6
  26. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  27. package/dist/signer/ckb/signerCkbScriptReadonly.js +18 -6
  28. package/dist/signer/signer/index.d.ts +11 -1
  29. package/dist/signer/signer/index.d.ts.map +1 -1
  30. package/dist/signer/signer/index.js +23 -0
  31. package/dist.commonjs/ckb/hash.d.ts +18 -0
  32. package/dist.commonjs/ckb/hash.d.ts.map +1 -0
  33. package/dist.commonjs/ckb/hash.js +22 -0
  34. package/dist.commonjs/ckb/index.d.ts +1 -0
  35. package/dist.commonjs/ckb/index.d.ts.map +1 -1
  36. package/dist.commonjs/ckb/index.js +1 -0
  37. package/dist.commonjs/ckb/transaction.d.ts +179 -11
  38. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  39. package/dist.commonjs/ckb/transaction.js +197 -59
  40. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  41. package/dist.commonjs/client/clientPublicMainnet.advanced.js +0 -60
  42. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +0 -23
  43. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  44. package/dist.commonjs/client/clientPublicTestnet.advanced.js +1 -145
  45. package/dist.commonjs/client/knownScript.d.ts +1 -3
  46. package/dist.commonjs/client/knownScript.d.ts.map +1 -1
  47. package/dist.commonjs/client/knownScript.js +0 -4
  48. package/dist.commonjs/hasher/hasherCkb.d.ts +0 -16
  49. package/dist.commonjs/hasher/hasherCkb.d.ts.map +1 -1
  50. package/dist.commonjs/hasher/hasherCkb.js +2 -21
  51. package/dist.commonjs/molecule/codec.d.ts +30 -10
  52. package/dist.commonjs/molecule/codec.d.ts.map +1 -1
  53. package/dist.commonjs/molecule/codec.js +46 -13
  54. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +18 -6
  55. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  56. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +18 -6
  57. package/dist.commonjs/signer/signer/index.d.ts +11 -1
  58. package/dist.commonjs/signer/signer/index.d.ts.map +1 -1
  59. package/dist.commonjs/signer/signer/index.js +23 -0
  60. package/package.json +1 -1
  61. package/prettier.config.mjs +1 -1
  62. package/src/ckb/hash.ts +28 -0
  63. package/src/ckb/index.ts +1 -0
  64. package/src/ckb/transaction.ts +231 -52
  65. package/src/client/clientPublicMainnet.advanced.ts +0 -68
  66. package/src/client/clientPublicTestnet.advanced.ts +0 -163
  67. package/src/client/knownScript.ts +0 -5
  68. package/src/hasher/hasherCkb.ts +0 -26
  69. package/src/molecule/codec.ts +50 -13
  70. package/src/signer/ckb/signerCkbScriptReadonly.ts +24 -8
  71. package/src/signer/signer/index.ts +34 -0
@@ -1,8 +1,6 @@
1
1
  import { blake2b } from "@noble/hashes/blake2b";
2
2
  import { BytesLike, bytesFrom } from "../bytes/index.js";
3
- import { CellInput, CellInputLike } from "../ckb/index.js";
4
3
  import { Hex, hexFrom } from "../hex/index.js";
5
- import { NumLike, numLeToBytes } from "../num/index.js";
6
4
  import { CKB_BLAKE2B_PERSONAL } from "./advanced.js";
7
5
  import { Hasher } from "./hasher.js";
8
6
 
@@ -81,27 +79,3 @@ export function hashCkb(...data: BytesLike[]): Hex {
81
79
  data.forEach((d) => hasher.update(d));
82
80
  return hasher.digest();
83
81
  }
84
-
85
- /**
86
- * Computes the Type ID hash of the given data.
87
- * @public
88
- *
89
- * @param cellInputLike - The first cell input of the transaction.
90
- * @param outputIndex - The output index of the Type ID cell.
91
- * @returns The hexadecimal string representation of the hash.
92
- *
93
- * @example
94
- * ```typescript
95
- * const hash = hashTypeId(cellInput, outputIndex); // Outputs something like "0x..."
96
- * ```
97
- */
98
-
99
- export function hashTypeId(
100
- cellInputLike: CellInputLike,
101
- outputIndex: NumLike,
102
- ): Hex {
103
- return hashCkb(
104
- CellInput.from(cellInputLike).toBytes(),
105
- numLeToBytes(outputIndex, 8),
106
- );
107
- }
@@ -459,34 +459,71 @@ type UnionDecoded<
459
459
  : never;
460
460
 
461
461
  /**
462
- * Union is a dynamic-size type.
463
- * Serializing a union has two steps:
464
- * - Serialize an item type id in bytes as a 32 bit unsigned integer in little-endian. The item type id is the index of the inner items, and it's starting at 0.
465
- * - Serialize the inner item.
466
- * @param codecLayout the union item record
467
- * @param fields the custom item type id record
462
+ * Constructs a union codec that can serialize and deserialize values tagged with a type identifier.
463
+ *
464
+ * If all variants have the same fixed size, the resulting union codec is fixed-size (header + payload).
465
+ * Otherwise, it falls back to a dynamic-size codec.
466
+ *
467
+ * Serialization format:
468
+ * 1. 4-byte little-endian unsigned integer for the variant index.
469
+ * 2. Encoded bytes of the selected variant.
470
+ *
471
+ * @typeParam T
472
+ * A record mapping variant names to codecs.
473
+ * @param codecLayout
474
+ * An object whose keys are variant names and values are codecs for each variant.
475
+ * @param fields
476
+ * Optional mapping from variant names to custom numeric IDs. If omitted, the index
477
+ * of each variant in `codecLayout` is used as its ID.
478
+ *
479
+ *
468
480
  * @example
469
- * // without custom id
470
- * union({ cafe: Uint8, bee: Uint8 })
471
- * // with custom id
472
- * union({ cafe: Uint8, bee: Uint8 }, { cafe: 0xcafe, bee: 0xbee })
481
+ * // Dynamic union without custom numeric IDs
482
+ * union({ cafe: Uint8, bee: Uint16 })
483
+ *
484
+ * // Dynamic union with custom numeric IDs
485
+ * union({ cafe: Uint8, bee: Uint16 }, { cafe: 0xcafe, bee: 0xbee })
486
+ *
487
+ * // Fixed-size union without custom numeric IDs
488
+ * const PaddedUint8 = struct({ data : u8, padding : u8 })
489
+ * union({ cafe: PaddedUint8, bee: Uint16 });
490
+ *
491
+ * // Fixed-size union with custom numeric IDs
492
+ * union({ cafe: PaddedUint8, bee: Uint16 }, { cafe: 0xcafe, bee: 0xbee })
473
493
  */
494
+
474
495
  export function union<T extends Record<string, CodecLike<any, any>>>(
475
496
  codecLayout: T,
476
497
  fields?: Record<keyof T, number | undefined | null>,
477
498
  ): Codec<UnionEncodable<T>, UnionDecoded<T>> {
478
- const keys = Object.keys(codecLayout);
499
+ const entries = Object.entries(codecLayout);
500
+
501
+ // Determine if all variants have a fixed and equal byteLength.
502
+ let byteLength: number | undefined;
503
+ if (entries.length > 0) {
504
+ const firstLen = entries[0][1].byteLength;
505
+ if (
506
+ firstLen !== undefined &&
507
+ entries.every(([, { byteLength: len }]) => len === firstLen)
508
+ ) {
509
+ // Add 4 bytes for the type header
510
+ byteLength = firstLen + 4;
511
+ }
512
+ }
479
513
 
480
514
  return Codec.from({
515
+ byteLength,
481
516
  encode({ type, value }) {
482
517
  const typeStr = type.toString();
483
518
  const codec = codecLayout[typeStr];
484
519
  if (!codec) {
485
520
  throw new Error(
486
- `union: invalid type, expected ${keys.toString()}, but got ${typeStr}`,
521
+ `union: invalid type, expected ${entries.map((e) => e[0]).toString()}, but got ${typeStr}`,
487
522
  );
488
523
  }
489
- const fieldId = fields ? (fields[typeStr] ?? -1) : keys.indexOf(typeStr);
524
+ const fieldId = fields
525
+ ? (fields[typeStr] ?? -1)
526
+ : entries.findIndex((e) => e[0] === typeStr);
490
527
  if (fieldId < 0) {
491
528
  throw new Error(`union: invalid field id ${fieldId} of ${typeStr}`);
492
529
  }
@@ -4,31 +4,46 @@ import { Client } from "../../client/index.js";
4
4
  import { Signer, SignerSignType, SignerType } from "../signer/index.js";
5
5
 
6
6
  /**
7
- * A class extending Signer that provides read-only access to a CKB script.
8
- * This class does not support signing operations.
7
+ * A read-only signer for a CKB script. It can be used to get addresses,
8
+ * but not to sign transactions. This is useful when you want to watch an address
9
+ * without having the private key.
10
+ *
9
11
  * @public
10
12
  */
11
13
  export class SignerCkbScriptReadonly extends Signer {
14
+ /**
15
+ * The type of the signer.
16
+ */
12
17
  get type(): SignerType {
13
18
  return SignerType.CKB;
14
19
  }
15
20
 
21
+ /**
22
+ * The sign type of the signer.
23
+ * As this is a read-only signer, the sign type is {@link SignerSignType.Unknown}.
24
+ */
16
25
  get signType(): SignerSignType {
17
26
  return SignerSignType.Unknown;
18
27
  }
19
28
 
20
- private readonly script: Script;
29
+ /**
30
+ * The scripts associated with the signer.
31
+ */
32
+ public readonly scripts: Script[];
21
33
 
22
34
  /**
23
35
  * Creates an instance of SignerCkbScriptReadonly.
24
36
  *
25
37
  * @param client - The client instance used for communication.
26
- * @param script - The script associated with the signer.
38
+ * @param scripts - The scripts associated with the signer. Can be a single script, an array of scripts, or multiple script arguments.
27
39
  */
28
- constructor(client: Client, script: ScriptLike) {
40
+ constructor(client: Client, ...scripts: (ScriptLike | ScriptLike[])[]) {
29
41
  super(client);
30
42
 
31
- this.script = Script.from(script);
43
+ this.scripts = scripts.flat().map(Script.from);
44
+ if (this.scripts.length === 0) {
45
+ throw new Error("SignerCkbScriptReadonly requires at least one script.");
46
+ }
32
47
  }
33
48
 
34
49
  /**
@@ -71,8 +86,9 @@ export class SignerCkbScriptReadonly extends Signer {
71
86
  * const addressObjs = await signer.getAddressObjs(); // Outputs the array of Address objects
72
87
  * ```
73
88
  */
74
-
75
89
  async getAddressObjs(): Promise<Address[]> {
76
- return [Address.fromScript(this.script, this.client)];
90
+ return this.scripts.map((script) =>
91
+ Address.fromScript(script, this.client),
92
+ );
77
93
  }
78
94
  }
@@ -6,6 +6,7 @@ import {
6
6
  Client,
7
7
  ClientFindTransactionsGroupedResponse,
8
8
  ClientFindTransactionsResponse,
9
+ ClientIndexerSearchKeyFilterLike,
9
10
  } from "../../client/index.js";
10
11
  import { Hex } from "../../hex/index.js";
11
12
  import { Num } from "../../num/index.js";
@@ -239,6 +240,39 @@ export abstract class Signer {
239
240
  );
240
241
  }
241
242
 
243
+ /**
244
+ * Find cells of this signer
245
+ *
246
+ * @param filter - The filter for the search key.
247
+ * @param withData - Whether to include cell data in the response.
248
+ * @param order - The order of the returned cells, can be "asc" or "desc".
249
+ * @param limit - The maximum number of cells for every querying chunk.
250
+ * @returns A async generator that yields all matching cells
251
+ */
252
+ async *findCellsOnChain(
253
+ filter: ClientIndexerSearchKeyFilterLike,
254
+ withData?: boolean | null,
255
+ order?: "asc" | "desc",
256
+ limit?: number,
257
+ ): AsyncGenerator<Cell> {
258
+ const scripts = await this.getAddressObjs();
259
+ for (const { script } of scripts) {
260
+ for await (const cell of this.client.findCellsOnChain(
261
+ {
262
+ script,
263
+ scriptType: "lock",
264
+ filter,
265
+ scriptSearchMode: "exact",
266
+ withData,
267
+ },
268
+ order,
269
+ limit,
270
+ )) {
271
+ yield cell;
272
+ }
273
+ }
274
+ }
275
+
242
276
  /**
243
277
  * Find cells of this signer
244
278
  *