@ckb-ccc/core 0.0.4-alpha.1 → 0.0.4-alpha.11

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 (114) hide show
  1. package/README.md +12 -7
  2. package/dist/address/address.advanced.d.ts +34 -0
  3. package/dist/address/address.advanced.d.ts.map +1 -1
  4. package/dist/address/address.advanced.js +41 -7
  5. package/dist/address/index.d.ts +36 -0
  6. package/dist/address/index.d.ts.map +1 -1
  7. package/dist/address/index.js +44 -5
  8. package/dist/bytes/advanced.d.ts +3 -0
  9. package/dist/bytes/advanced.d.ts.map +1 -1
  10. package/dist/bytes/index.d.ts +57 -0
  11. package/dist/bytes/index.d.ts.map +1 -1
  12. package/dist/bytes/index.js +57 -0
  13. package/dist/ckb/script.d.ts +98 -1
  14. package/dist/ckb/script.d.ts.map +1 -1
  15. package/dist/ckb/script.js +98 -1
  16. package/dist/ckb/transaction.d.ts +392 -1
  17. package/dist/ckb/transaction.d.ts.map +1 -1
  18. package/dist/ckb/transaction.js +403 -1
  19. package/dist/client/advanced.d.ts +1 -0
  20. package/dist/client/advanced.d.ts.map +1 -1
  21. package/dist/client/advanced.js +1 -0
  22. package/dist/client/client.d.ts +10 -8
  23. package/dist/client/client.d.ts.map +1 -1
  24. package/dist/client/client.js +18 -0
  25. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -2
  26. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  27. package/dist/client/clientPublicMainnet.d.ts +3 -3
  28. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  29. package/dist/client/clientPublicMainnet.js +2 -2
  30. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -2
  31. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  32. package/dist/client/clientPublicTestnet.d.ts +3 -3
  33. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  34. package/dist/client/clientPublicTestnet.js +2 -2
  35. package/dist/client/clientTypes.d.ts +6 -0
  36. package/dist/client/clientTypes.d.ts.map +1 -1
  37. package/dist/client/index.d.ts +1 -0
  38. package/dist/client/index.d.ts.map +1 -1
  39. package/dist/client/index.js +1 -0
  40. package/dist/client/jsonRpc/advanced.d.ts +2 -87
  41. package/dist/client/jsonRpc/advanced.d.ts.map +1 -1
  42. package/dist/client/jsonRpc/advanced.js +2 -65
  43. package/dist/client/jsonRpc/index.d.ts +60 -6
  44. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  45. package/dist/client/jsonRpc/index.js +72 -7
  46. package/dist/client/jsonRpc/transformers.d.ts +28 -0
  47. package/dist/client/jsonRpc/transformers.d.ts.map +1 -0
  48. package/dist/client/jsonRpc/transformers.js +122 -0
  49. package/dist/client/jsonRpc/types.d.ts +41 -0
  50. package/dist/client/jsonRpc/types.d.ts.map +1 -0
  51. package/dist/client/jsonRpc/types.js +1 -0
  52. package/dist/fixedPoint/index.d.ts +43 -1
  53. package/dist/fixedPoint/index.d.ts.map +1 -1
  54. package/dist/fixedPoint/index.js +39 -1
  55. package/dist/hasher/index.d.ts +43 -2
  56. package/dist/hasher/index.d.ts.map +1 -1
  57. package/dist/hasher/index.js +42 -0
  58. package/dist/hex/index.d.ts +19 -0
  59. package/dist/hex/index.d.ts.map +1 -1
  60. package/dist/hex/index.js +12 -0
  61. package/dist/num/index.d.ts +99 -0
  62. package/dist/num/index.d.ts.map +1 -1
  63. package/dist/num/index.js +92 -0
  64. package/dist/signer/btc/signerBtc.d.ts +40 -2
  65. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  66. package/dist/signer/btc/signerBtc.js +36 -4
  67. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +48 -0
  68. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
  69. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +53 -0
  70. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +47 -0
  71. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  72. package/dist/signer/ckb/signerCkbScriptReadonly.js +52 -0
  73. package/dist/signer/evm/signerEvm.d.ts +34 -2
  74. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  75. package/dist/signer/evm/signerEvm.js +35 -4
  76. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +43 -1
  77. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
  78. package/dist/signer/evm/signerEvmAddressReadonly.js +47 -0
  79. package/dist/signer/helpers.d.ts +35 -2
  80. package/dist/signer/helpers.d.ts.map +1 -1
  81. package/dist/signer/helpers.js +67 -11
  82. package/dist/signer/signer.d.ts +98 -7
  83. package/dist/signer/signer.d.ts.map +1 -1
  84. package/dist/signer/signer.js +70 -3
  85. package/package.json +3 -3
  86. package/src/address/address.advanced.ts +54 -18
  87. package/src/address/index.ts +51 -7
  88. package/src/bytes/advanced.ts +12 -9
  89. package/src/bytes/index.ts +60 -0
  90. package/src/ckb/script.ts +111 -2
  91. package/src/ckb/transaction.ts +455 -2
  92. package/src/client/advanced.ts +1 -0
  93. package/src/client/client.ts +31 -8
  94. package/src/client/clientPublicMainnet.advanced.ts +5 -2
  95. package/src/client/clientPublicMainnet.ts +6 -4
  96. package/src/client/clientPublicTestnet.advanced.ts +5 -2
  97. package/src/client/clientPublicTestnet.ts +6 -4
  98. package/src/client/clientTypes.ts +13 -0
  99. package/src/client/index.ts +1 -0
  100. package/src/client/jsonRpc/advanced.ts +2 -93
  101. package/src/client/jsonRpc/index.ts +99 -11
  102. package/src/client/jsonRpc/transformers.ts +161 -0
  103. package/src/client/jsonRpc/types.ts +48 -0
  104. package/src/fixedPoint/index.ts +52 -2
  105. package/src/hasher/index.ts +47 -1
  106. package/src/hex/index.ts +19 -0
  107. package/src/num/index.ts +101 -0
  108. package/src/signer/btc/signerBtc.ts +47 -4
  109. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +54 -0
  110. package/src/signer/ckb/signerCkbScriptReadonly.ts +54 -0
  111. package/src/signer/evm/signerEvm.ts +42 -4
  112. package/src/signer/evm/signerEvmAddressReadonly.ts +50 -1
  113. package/src/signer/helpers.ts +87 -11
  114. package/src/signer/signer.ts +109 -7
@@ -4,7 +4,14 @@ import { Hex } from "../hex";
4
4
  import { CKB_BLAKE2B_PERSONAL } from "./advanced";
5
5
 
6
6
  export class Hasher {
7
- hasher: Blake2b;
7
+ private readonly hasher: Blake2b;
8
+
9
+ /**
10
+ * Creates an instance of Hasher.
11
+ *
12
+ * @param outLength - The output length of the hash in bytes. Default is 32.
13
+ * @param personal - The personal string for the Blake2b algorithm. Default is CKB_BLAKE2B_PERSONAL.
14
+ */
8
15
 
9
16
  constructor(outLength = 32, personal = CKB_BLAKE2B_PERSONAL) {
10
17
  this.hasher = blake2b(
@@ -15,16 +22,55 @@ export class Hasher {
15
22
  );
16
23
  }
17
24
 
25
+ /**
26
+ * Updates the hash with the given data.
27
+ *
28
+ * @param data - The data to update the hash with.
29
+ * @returns The current Hasher instance for chaining.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * const hasher = new Hasher();
34
+ * hasher.update("some data").update("more data");
35
+ * const hash = hasher.digest();
36
+ * ```
37
+ */
38
+
18
39
  update(data: BytesLike): Hasher {
19
40
  this.hasher.update(bytesFrom(data));
20
41
  return this;
21
42
  }
22
43
 
44
+ /**
45
+ * Finalizes the hash and returns the digest as a hexadecimal string.
46
+ *
47
+ * @returns The hexadecimal string representation of the hash.
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const hasher = new Hasher();
52
+ * hasher.update("some data");
53
+ * const hash = hasher.digest(); // Outputs something like "0x..."
54
+ * ```
55
+ */
56
+
23
57
  digest(): Hex {
24
58
  return `0x${this.hasher.digest("hex")}`;
25
59
  }
26
60
  }
27
61
 
62
+ /**
63
+ * Computes the CKB hash of the given data using the Blake2b algorithm.
64
+ *
65
+ * @param data - The data to hash.
66
+ * @returns The hexadecimal string representation of the hash.
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * const hash = ckbHash("some data"); // Outputs something like "0x..."
71
+ * ```
72
+ */
73
+
28
74
  export function ckbHash(data: BytesLike): Hex {
29
75
  return new Hasher().update(data).digest();
30
76
  }
package/src/hex/index.ts CHANGED
@@ -1,8 +1,27 @@
1
1
  import { bytesFrom, BytesLike, bytesTo } from "../bytes";
2
2
 
3
+ /**
4
+ * Represents a hexadecimal string prefixed with "0x".
5
+ */
3
6
  export type Hex = `0x${string}`;
7
+ /**
8
+ * Represents a value that can be converted to a hexadecimal string.
9
+ * It extends the BytesLike type.
10
+ */
4
11
  export type HexLike = BytesLike;
5
12
 
13
+ /**
14
+ * Converts a HexLike value to a Hex string.
15
+ *
16
+ * @param hex - The value to convert, which can be a string, Uint8Array, ArrayBuffer, or number array.
17
+ * @returns A Hex string representing the value.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const hexString = hexFrom("68656c6c6f"); // Outputs "0x68656c6c6f"
22
+ * const hexStringFromBytes = hexFrom(new Uint8Array([104, 101, 108, 108, 111])); // Outputs "0x68656c6c6f"
23
+ * ```
24
+ */
6
25
  export function hexFrom(hex: HexLike): Hex {
7
26
  return `0x${bytesTo(bytesFrom(hex), "hex")}`;
8
27
  }
package/src/num/index.ts CHANGED
@@ -1,9 +1,29 @@
1
1
  import { Bytes, BytesLike, bytesConcat, bytesFrom } from "../bytes";
2
2
  import { Hex, HexLike, hexFrom } from "../hex";
3
3
 
4
+ /**
5
+ * Represents a numeric value as a bigint.
6
+ */
4
7
  export type Num = bigint;
8
+
9
+ /**
10
+ * Represents a value that can be converted to a numeric value.
11
+ * It can be a string, number, bigint, or HexLike.
12
+ */
5
13
  export type NumLike = string | number | bigint | HexLike;
6
14
 
15
+ /**
16
+ * Converts a NumLike value to a Num (bigint).
17
+ *
18
+ * @param val - The value to convert, which can be a string, number, bigint, or HexLike.
19
+ * @returns A Num (bigint) representing the value.
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const num = numFrom("12345"); // Outputs 12345n
24
+ * const numFromHex = numFrom("0x3039"); // Outputs 12345n
25
+ * ```
26
+ */
7
27
  export function numFrom(val: NumLike): Num {
8
28
  if (typeof val === "bigint") {
9
29
  return val;
@@ -16,18 +36,66 @@ export function numFrom(val: NumLike): Num {
16
36
  return BigInt(hexFrom(val));
17
37
  }
18
38
 
39
+ /**
40
+ * Converts a NumLike value to a hexadecimal string.
41
+ *
42
+ * @param val - The value to convert, which can be a string, number, bigint, or HexLike.
43
+ * @returns A Hex string representing the numeric value.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const hex = numToHex(12345); // Outputs "0x3039"
48
+ * ```
49
+ */
19
50
  export function numToHex(val: NumLike): Hex {
20
51
  return `0x${numFrom(val).toString(16)}`;
21
52
  }
22
53
 
54
+ /**
55
+ * Converts a NumLike value to a byte array in little-endian order.
56
+ *
57
+ * @param val - The value to convert, which can be a string, number, bigint, or HexLike.
58
+ * @param bytes - The number of bytes to use for the representation. If not provided, the exact number of bytes needed is used.
59
+ * @returns A Uint8Array containing the byte representation of the numeric value.
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const bytes = numToBytes(12345, 4); // Outputs Uint8Array [57, 48, 0, 0]
64
+ * ```
65
+ */
66
+
23
67
  export function numToBytes(val: NumLike, bytes?: number): Bytes {
24
68
  return numLeToBytes(val, bytes);
25
69
  }
26
70
 
71
+ /**
72
+ * Converts a NumLike value to a byte array in little-endian order.
73
+ *
74
+ * @param val - The value to convert, which can be a string, number, bigint, or HexLike.
75
+ * @param bytes - The number of bytes to use for the representation. If not provided, the exact number of bytes needed is used.
76
+ * @returns A Uint8Array containing the byte representation of the numeric value.
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const bytes = numLeToBytes(12345, 4); // Outputs Uint8Array [57, 48, 0, 0]
81
+ * ```
82
+ */
27
83
  export function numLeToBytes(val: NumLike, bytes?: number): Bytes {
28
84
  return numBeToBytes(val, bytes).reverse();
29
85
  }
30
86
 
87
+ /**
88
+ * Converts a NumLike value to a byte array in big-endian order.
89
+ *
90
+ * @param val - The value to convert, which can be a string, number, bigint, or HexLike.
91
+ * @param bytes - The number of bytes to use for the representation. If not provided, the exact number of bytes needed is used.
92
+ * @returns A Uint8Array containing the byte representation of the numeric value.
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * const bytes = numBeToBytes(12345, 4); // Outputs Uint8Array [0, 0, 48, 57]
97
+ * ```
98
+ */
31
99
  export function numBeToBytes(val: NumLike, bytes?: number): Bytes {
32
100
  const rawBytes = bytesFrom(numFrom(val).toString(16));
33
101
  if (bytes == null) {
@@ -40,14 +108,47 @@ export function numBeToBytes(val: NumLike, bytes?: number): Bytes {
40
108
  );
41
109
  }
42
110
 
111
+ /**
112
+ * Converts a byte array to a Num (bigint) assuming little-endian order.
113
+ *
114
+ * @param val - The byte array to convert.
115
+ * @returns A Num (bigint) representing the numeric value.
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * const num = numFromBytes(new Uint8Array([57, 48, 0, 0])); // Outputs 12345n
120
+ * ```
121
+ */
43
122
  export function numFromBytes(val: BytesLike): Num {
44
123
  return numLeFromBytes(val);
45
124
  }
46
125
 
126
+ /**
127
+ * Converts a byte array to a Num (bigint) assuming little-endian order.
128
+ *
129
+ * @param val - The byte array to convert.
130
+ * @returns A Num (bigint) representing the numeric value.
131
+ *
132
+ * @example
133
+ * ```typescript
134
+ * const num = numLeFromBytes(new Uint8Array([57, 48, 0, 0])); // Outputs 12345n
135
+ * ```
136
+ */
47
137
  export function numLeFromBytes(val: BytesLike): Num {
48
138
  return numBeFromBytes(bytesFrom(val).reverse());
49
139
  }
50
140
 
141
+ /**
142
+ * Converts a byte array to a Num (bigint) assuming big-endian order.
143
+ *
144
+ * @param val - The byte array to convert.
145
+ * @returns A Num (bigint) representing the numeric value.
146
+ *
147
+ * @example
148
+ * ```typescript
149
+ * const num = numBeFromBytes(new Uint8Array([0, 0, 48, 57])); // Outputs 12345n
150
+ * ```
151
+ */
51
152
  export function numBeFromBytes(val: BytesLike): Num {
52
153
  return numFrom(bytesFrom(val));
53
154
  }
@@ -2,22 +2,47 @@ import { ripemd160 } from "@noble/hashes/ripemd160";
2
2
  import { sha256 } from "@noble/hashes/sha256";
3
3
  import { Address } from "../../address";
4
4
  import { bytesConcat, bytesFrom } from "../../bytes";
5
- import { Transaction, WitnessArgs } from "../../ckb";
5
+ import { Transaction, TransactionLike, WitnessArgs } from "../../ckb";
6
6
  import { KnownScript } from "../../client";
7
7
  import { HexLike, hexFrom } from "../../hex";
8
8
  import { numToBytes } from "../../num";
9
- import { getSignHashInfo } from "../helpers";
9
+ import { getSignHashInfo, prepareSighashAllWitness } from "../helpers";
10
10
  import { Signer } from "../signer";
11
11
 
12
+ /**
13
+ * An abstract class extending the Signer class for Bitcoin-like signing operations.
14
+ * This class provides methods to get Bitcoin account, public key, and internal address,
15
+ * as well as signing transactions.
16
+ */
12
17
  export abstract class SignerBtc extends Signer {
18
+ /**
19
+ * Gets the Bitcoin account associated with the signer.
20
+ *
21
+ * @returns A promise that resolves to a string representing the Bitcoin account.
22
+ */
13
23
  abstract getBtcAccount(): Promise<string>;
14
24
 
25
+ /**
26
+ * Gets the Bitcoin public key associated with the signer.
27
+ *
28
+ * @returns A promise that resolves to a HexLike value representing the Bitcoin public key.
29
+ */
15
30
  abstract getBtcPublicKey(): Promise<HexLike>;
16
31
 
32
+ /**
33
+ * Gets the internal address, which is the Bitcoin account in this case.
34
+ *
35
+ * @returns A promise that resolves to a string representing the internal address.
36
+ */
17
37
  async getInternalAddress(): Promise<string> {
18
38
  return this.getBtcAccount();
19
39
  }
20
40
 
41
+ /**
42
+ * Gets an array of Address objects representing the known script addresses for the signer.
43
+ *
44
+ * @returns A promise that resolves to an array of Address objects.
45
+ */
21
46
  async getAddressObjs(): Promise<Address[]> {
22
47
  const publicKey = await this.getBtcPublicKey();
23
48
  const hash = ripemd160(sha256(bytesFrom(publicKey)));
@@ -31,9 +56,27 @@ export abstract class SignerBtc extends Signer {
31
56
  ];
32
57
  }
33
58
 
34
- async signOnlyTransaction(tx: Transaction): Promise<Transaction> {
59
+ /**
60
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
61
+ *
62
+ * @param txLike - The transaction to prepare, represented as a TransactionLike object.
63
+ * @returns A promise that resolves to the prepared Transaction object.
64
+ */
65
+ async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
35
66
  const { script } = await this.getRecommendedAddressObj();
36
- const info = await getSignHashInfo(tx, script);
67
+ return prepareSighashAllWitness(txLike, script, 85, this.client);
68
+ }
69
+
70
+ /**
71
+ * Signs a transaction without modifying it.
72
+ *
73
+ * @param txLike - The transaction to sign, represented as a TransactionLike object.
74
+ * @returns A promise that resolves to a signed Transaction object.
75
+ */
76
+ async signOnlyTransaction(txLike: TransactionLike): Promise<Transaction> {
77
+ const tx = Transaction.from(txLike);
78
+ const { script } = await this.getRecommendedAddressObj();
79
+ const info = await getSignHashInfo(tx, script, this.client);
37
80
  if (!info) {
38
81
  return tx;
39
82
  }
@@ -2,9 +2,20 @@ import { Client } from "../../client";
2
2
  import { Hex, HexLike, hexFrom } from "../../hex";
3
3
  import { SignerBtc } from "./signerBtc";
4
4
 
5
+ /**
6
+ * A class extending SignerBtc that provides read-only access to a Bitcoin public key and account.
7
+ * This class does not support signing operations.
8
+ */
5
9
  export class SignerBtcPublicKeyReadonly extends SignerBtc {
6
10
  private readonly publicKey: Hex;
7
11
 
12
+ /**
13
+ * Creates an instance of SignerBtcPublicKeyReadonly.
14
+ *
15
+ * @param client - The client instance used for communication.
16
+ * @param account - The Bitcoin account associated with the signer.
17
+ * @param publicKey - The public key associated with the signer.
18
+ */
8
19
  constructor(
9
20
  client: Client,
10
21
  private readonly account: string,
@@ -15,12 +26,55 @@ export class SignerBtcPublicKeyReadonly extends SignerBtc {
15
26
  this.publicKey = hexFrom(publicKey);
16
27
  }
17
28
 
29
+ /**
30
+ * Construct a new signer with the client replaced.
31
+ *
32
+ * @returns A promise that resolves the new Signer.
33
+ */
34
+ async replaceClient(client: Client): Promise<SignerBtcPublicKeyReadonly> {
35
+ return new SignerBtcPublicKeyReadonly(client, this.account, this.publicKey);
36
+ }
37
+
38
+ /**
39
+ * Connects to the client. This implementation does nothing as the class is read-only.
40
+ *
41
+ * @returns A promise that resolves when the connection is complete.
42
+ */
18
43
  async connect(): Promise<void> {}
19
44
 
45
+ /**
46
+ * Check if the signer is connected.
47
+ *
48
+ * @returns A promise that resolves the connection status.
49
+ */
50
+ async isConnected(): Promise<boolean> {
51
+ return true;
52
+ };
53
+
54
+ /**
55
+ * Gets the Bitcoin account associated with the signer.
56
+ *
57
+ * @returns A promise that resolves to a string representing the Bitcoin account.
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * const account = await signer.getBtcAccount(); // Outputs the Bitcoin account
62
+ * ```
63
+ */
20
64
  async getBtcAccount(): Promise<string> {
21
65
  return this.account;
22
66
  }
23
67
 
68
+ /**
69
+ * Gets the Bitcoin public key associated with the signer.
70
+ *
71
+ * @returns A promise that resolves to a Hex string representing the Bitcoin public key.
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * const publicKey = await signer.getBtcPublicKey(); // Outputs the Bitcoin public key
76
+ * ```
77
+ */
24
78
  async getBtcPublicKey(): Promise<Hex> {
25
79
  return this.publicKey;
26
80
  }
@@ -3,21 +3,75 @@ import { Script, ScriptLike } from "../../ckb";
3
3
  import { Client } from "../../client";
4
4
  import { Signer } from "../signer";
5
5
 
6
+ /**
7
+ * A class extending Signer that provides read-only access to a CKB script.
8
+ * This class does not support signing operations.
9
+ */
6
10
  export class SignerCkbScriptReadonly extends Signer {
7
11
  private readonly script: Script;
8
12
 
13
+ /**
14
+ * Creates an instance of SignerCkbScriptReadonly.
15
+ *
16
+ * @param client - The client instance used for communication.
17
+ * @param script - The script associated with the signer.
18
+ */
9
19
  constructor(client: Client, script: ScriptLike) {
10
20
  super(client);
11
21
 
12
22
  this.script = Script.from(script);
13
23
  }
14
24
 
25
+ /**
26
+ * Construct a new signer with the client replaced.
27
+ *
28
+ * @returns A promise that resolves the new Signer.
29
+ */
30
+ async replaceClient(client: Client): Promise<SignerCkbScriptReadonly> {
31
+ return new SignerCkbScriptReadonly(client, this.script);
32
+ }
33
+
34
+ /**
35
+ * Connects to the client. This implementation does nothing as the class is read-only.
36
+ *
37
+ * @returns A promise that resolves when the connection is complete.
38
+ */
15
39
  async connect(): Promise<void> {}
16
40
 
41
+ /**
42
+ * Check if the signer is connected.
43
+ *
44
+ * @returns A promise that resolves the connection status.
45
+ */
46
+ async isConnected(): Promise<boolean> {
47
+ return true;
48
+ };
49
+
50
+ /**
51
+ * Gets the internal address for the script.
52
+ *
53
+ * @returns A promise that resolves to a string representing the internal address.
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * const internalAddress = await signer.getInternalAddress(); // Outputs the internal address
58
+ * ```
59
+ */
17
60
  async getInternalAddress(): Promise<string> {
18
61
  return this.getRecommendedAddress();
19
62
  }
20
63
 
64
+ /**
65
+ * Gets an array of Address objects representing the script address.
66
+ *
67
+ * @returns A promise that resolves to an array of Address objects.
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const addressObjs = await signer.getAddressObjs(); // Outputs the array of Address objects
72
+ * ```
73
+ */
74
+
21
75
  async getAddressObjs(): Promise<Address[]> {
22
76
  return [await Address.fromScript(this.script, this.client)];
23
77
  }
@@ -1,19 +1,38 @@
1
1
  import { Address } from "../../address";
2
2
  import { bytesConcat, bytesFrom } from "../../bytes";
3
- import { Transaction, WitnessArgs } from "../../ckb";
3
+ import { Transaction, TransactionLike, WitnessArgs } from "../../ckb";
4
4
  import { KnownScript } from "../../client";
5
5
  import { hexFrom } from "../../hex";
6
6
  import { numToBytes } from "../../num";
7
- import { getSignHashInfo } from "../helpers";
7
+ import { getSignHashInfo, prepareSighashAllWitness } from "../helpers";
8
8
  import { Signer } from "../signer";
9
9
 
10
+ /**
11
+ * An abstract class extending Signer for Ethereum Virtual Machine (EVM) based signing operations.
12
+ * This class provides methods to get EVM account, internal address, and signing transactions.
13
+ */
10
14
  export abstract class SignerEvm extends Signer {
15
+ /**
16
+ * Gets the EVM account associated with the signer.
17
+ *
18
+ * @returns A promise that resolves to a string representing the EVM account.
19
+ */
11
20
  abstract getEvmAccount(): Promise<string>;
12
21
 
22
+ /**
23
+ * Gets the internal address, which is the EVM account in this case.
24
+ *
25
+ * @returns A promise that resolves to a string representing the internal address.
26
+ */
13
27
  async getInternalAddress(): Promise<string> {
14
28
  return this.getEvmAccount();
15
29
  }
16
30
 
31
+ /**
32
+ * Gets an array of Address objects representing the known script addresses for the signer.
33
+ *
34
+ * @returns A promise that resolves to an array of Address objects.
35
+ */
17
36
  async getAddressObjs(): Promise<Address[]> {
18
37
  const account = await this.getEvmAccount();
19
38
  return [
@@ -25,9 +44,28 @@ export abstract class SignerEvm extends Signer {
25
44
  ];
26
45
  }
27
46
 
28
- async signOnlyTransaction(tx: Transaction): Promise<Transaction> {
47
+ /**
48
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
49
+ *
50
+ * @param txLike - The transaction to prepare, represented as a TransactionLike object.
51
+ * @returns A promise that resolves to the prepared Transaction object.
52
+ */
53
+ async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
29
54
  const { script } = await this.getRecommendedAddressObj();
30
- const info = await getSignHashInfo(tx, script);
55
+ return prepareSighashAllWitness(txLike, script, 85, this.client);
56
+ }
57
+
58
+ /**
59
+ * Signs a transaction without modifying it.
60
+ *
61
+ * @param txLike - The transaction to sign, represented as a TransactionLike object.
62
+ * @returns A promise that resolves to a signed Transaction object.
63
+ */
64
+ async signOnlyTransaction(txLike: TransactionLike): Promise<Transaction> {
65
+ const tx = Transaction.from(txLike);
66
+
67
+ const { script } = await this.getRecommendedAddressObj();
68
+ const info = await getSignHashInfo(tx, script, this.client);
31
69
  if (!info) {
32
70
  return tx;
33
71
  }
@@ -2,17 +2,66 @@ import { Client } from "../../client";
2
2
  import { Hex, HexLike, hexFrom } from "../../hex";
3
3
  import { SignerEvm } from "./signerEvm";
4
4
 
5
- export abstract class SignerEvmAddressReadonly extends SignerEvm {
5
+ /**
6
+ * A class extending SignerEvm that provides read-only access to an EVM address.
7
+ * This class does not support signing operations.
8
+ */
9
+ export class SignerEvmAddressReadonly extends SignerEvm {
6
10
  private readonly address: Hex;
7
11
 
12
+ /**
13
+ * Creates an instance of SignerEvmAddressReadonly.
14
+ *
15
+ * @param client - The client instance used for communication.
16
+ * @param address - The EVM address associated with the signer.
17
+ */
8
18
  constructor(client: Client, address: HexLike) {
9
19
  super(client);
10
20
 
11
21
  this.address = hexFrom(address);
12
22
  }
13
23
 
24
+ /**
25
+ * Construct a new signer with the client replaced.
26
+ *
27
+ * @returns A promise that resolves the new Signer.
28
+ */
29
+ async replaceClient(client: Client): Promise<SignerEvmAddressReadonly> {
30
+ return new SignerEvmAddressReadonly(client, this.address);
31
+ }
32
+
33
+ /**
34
+ * Connects to the client. This implementation does nothing as the class is read-only.
35
+ *
36
+ * @returns A promise that resolves when the connection is complete.
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * await signer.connect();
41
+ * ```
42
+ */
43
+
14
44
  async connect(): Promise<void> {}
15
45
 
46
+ /**
47
+ * Check if the signer is connected.
48
+ *
49
+ * @returns A promise that resolves the connection status.
50
+ */
51
+ async isConnected(): Promise<boolean> {
52
+ return true;
53
+ };
54
+
55
+ /**
56
+ * Gets the EVM account associated with the signer.
57
+ *
58
+ * @returns A promise that resolves to a string representing the EVM account.
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * const account = await signer.getEvmAccount(); // Outputs the EVM account
63
+ * ```
64
+ */
16
65
  async getEvmAccount(): Promise<string> {
17
66
  return this.address;
18
67
  }