@ckb-ccc/core 0.0.4-alpha.8 → 0.0.5-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/README.md +6 -0
  2. package/dist/ckb/transaction.d.ts +85 -2
  3. package/dist/ckb/transaction.d.ts.map +1 -1
  4. package/dist/ckb/transaction.js +152 -7
  5. package/dist/client/client.d.ts +8 -3
  6. package/dist/client/client.d.ts.map +1 -1
  7. package/dist/client/client.js +29 -2
  8. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  9. package/dist/client/clientPublicMainnet.advanced.js +4 -0
  10. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  11. package/dist/client/clientPublicTestnet.advanced.js +4 -0
  12. package/dist/client/clientTypes.d.ts +48 -1
  13. package/dist/client/clientTypes.d.ts.map +1 -1
  14. package/dist/client/clientTypes.js +27 -1
  15. package/dist/client/jsonRpc/index.d.ts +9 -1
  16. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  17. package/dist/client/jsonRpc/index.js +15 -1
  18. package/dist/client/jsonRpc/transformers.d.ts +17 -3
  19. package/dist/client/jsonRpc/transformers.d.ts.map +1 -1
  20. package/dist/client/jsonRpc/transformers.js +37 -3
  21. package/dist/client/jsonRpc/types.d.ts +15 -0
  22. package/dist/client/jsonRpc/types.d.ts.map +1 -1
  23. package/dist/fixedPoint/index.d.ts.map +1 -1
  24. package/dist/fixedPoint/index.js +4 -1
  25. package/dist/fixedPoint/index.test.d.ts +2 -0
  26. package/dist/fixedPoint/index.test.d.ts.map +1 -0
  27. package/dist/fixedPoint/index.test.js +43 -0
  28. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  29. package/dist/signer/btc/signerBtc.js +2 -3
  30. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +12 -0
  31. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
  32. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +16 -0
  33. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +12 -0
  34. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  35. package/dist/signer/ckb/signerCkbScriptReadonly.js +16 -0
  36. package/dist/signer/evm/signerEvm.d.ts +5 -1
  37. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  38. package/dist/signer/evm/signerEvm.js +21 -8
  39. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +13 -1
  40. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
  41. package/dist/signer/evm/signerEvmAddressReadonly.js +16 -0
  42. package/dist/signer/index.d.ts +0 -1
  43. package/dist/signer/index.d.ts.map +1 -1
  44. package/dist/signer/index.js +0 -1
  45. package/dist/signer/signer.d.ts +26 -6
  46. package/dist/signer/signer.d.ts.map +1 -1
  47. package/dist/signer/signer.js +14 -3
  48. package/dist.commonjs/address/address.advanced.d.ts +65 -0
  49. package/dist.commonjs/address/address.advanced.d.ts.map +1 -0
  50. package/dist.commonjs/address/address.advanced.js +149 -0
  51. package/dist.commonjs/address/advanced.d.ts +2 -0
  52. package/dist.commonjs/address/advanced.d.ts.map +1 -0
  53. package/dist.commonjs/address/advanced.js +17 -0
  54. package/dist.commonjs/address/index.d.ts +54 -0
  55. package/dist.commonjs/address/index.d.ts.map +1 -0
  56. package/dist.commonjs/address/index.js +84 -0
  57. package/dist.commonjs/advanced.d.ts +3 -0
  58. package/dist.commonjs/advanced.d.ts.map +1 -0
  59. package/dist.commonjs/advanced.js +31 -0
  60. package/dist.commonjs/advancedBarrel.d.ts +5 -0
  61. package/dist.commonjs/advancedBarrel.d.ts.map +1 -0
  62. package/dist.commonjs/advancedBarrel.js +20 -0
  63. package/dist.commonjs/barrel.d.ts +11 -0
  64. package/dist.commonjs/barrel.d.ts.map +1 -0
  65. package/dist.commonjs/barrel.js +26 -0
  66. package/dist.commonjs/bytes/advanced.d.ts +5 -0
  67. package/dist.commonjs/bytes/advanced.d.ts.map +1 -0
  68. package/dist.commonjs/bytes/advanced.js +2 -0
  69. package/dist.commonjs/bytes/index.d.ts +64 -0
  70. package/dist.commonjs/bytes/index.d.ts.map +1 -0
  71. package/dist.commonjs/bytes/index.js +97 -0
  72. package/dist.commonjs/ckb/advanced.d.ts +4 -0
  73. package/dist.commonjs/ckb/advanced.d.ts.map +1 -0
  74. package/dist.commonjs/ckb/advanced.js +32 -0
  75. package/dist.commonjs/ckb/index.d.ts +3 -0
  76. package/dist.commonjs/ckb/index.d.ts.map +1 -0
  77. package/dist.commonjs/ckb/index.js +18 -0
  78. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts +355 -0
  79. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts.map +1 -0
  80. package/dist.commonjs/ckb/molecule.advanced/generated.js +1536 -0
  81. package/dist.commonjs/ckb/molecule.advanced/index.d.ts +6 -0
  82. package/dist.commonjs/ckb/molecule.advanced/index.d.ts.map +1 -0
  83. package/dist.commonjs/ckb/molecule.advanced/index.js +25 -0
  84. package/dist.commonjs/ckb/script.advanced.d.ts +5 -0
  85. package/dist.commonjs/ckb/script.advanced.d.ts.map +1 -0
  86. package/dist.commonjs/ckb/script.advanced.js +16 -0
  87. package/dist.commonjs/ckb/script.d.ts +126 -0
  88. package/dist.commonjs/ckb/script.d.ts.map +1 -0
  89. package/dist.commonjs/ckb/script.js +188 -0
  90. package/dist.commonjs/ckb/transaction.advanced.d.ts +5 -0
  91. package/dist.commonjs/ckb/transaction.advanced.d.ts.map +1 -0
  92. package/dist.commonjs/ckb/transaction.advanced.js +12 -0
  93. package/dist.commonjs/ckb/transaction.d.ts +616 -0
  94. package/dist.commonjs/ckb/transaction.d.ts.map +1 -0
  95. package/dist.commonjs/ckb/transaction.js +810 -0
  96. package/dist.commonjs/client/advanced.d.ts +4 -0
  97. package/dist.commonjs/client/advanced.d.ts.map +1 -0
  98. package/dist.commonjs/client/advanced.js +19 -0
  99. package/dist.commonjs/client/client.d.ts +24 -0
  100. package/dist.commonjs/client/client.d.ts.map +1 -0
  101. package/dist.commonjs/client/client.js +57 -0
  102. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +4 -0
  103. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -0
  104. package/dist.commonjs/client/clientPublicMainnet.advanced.js +30 -0
  105. package/dist.commonjs/client/clientPublicMainnet.d.ts +9 -0
  106. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -0
  107. package/dist.commonjs/client/clientPublicMainnet.js +17 -0
  108. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +4 -0
  109. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -0
  110. package/dist.commonjs/client/clientPublicTestnet.advanced.js +30 -0
  111. package/dist.commonjs/client/clientPublicTestnet.d.ts +9 -0
  112. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -0
  113. package/dist.commonjs/client/clientPublicTestnet.js +17 -0
  114. package/dist.commonjs/client/clientTypes.d.ts +55 -0
  115. package/dist.commonjs/client/clientTypes.d.ts.map +1 -0
  116. package/dist.commonjs/client/clientTypes.js +31 -0
  117. package/dist.commonjs/client/index.d.ts +6 -0
  118. package/dist.commonjs/client/index.d.ts.map +1 -0
  119. package/dist.commonjs/client/index.js +21 -0
  120. package/dist.commonjs/client/jsonRpc/advanced.d.ts +3 -0
  121. package/dist.commonjs/client/jsonRpc/advanced.d.ts.map +1 -0
  122. package/dist.commonjs/client/jsonRpc/advanced.js +18 -0
  123. package/dist.commonjs/client/jsonRpc/index.d.ts +76 -0
  124. package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -0
  125. package/dist.commonjs/client/jsonRpc/index.js +142 -0
  126. package/dist.commonjs/client/jsonRpc/transformers.d.ts +42 -0
  127. package/dist.commonjs/client/jsonRpc/transformers.d.ts.map +1 -0
  128. package/dist.commonjs/client/jsonRpc/transformers.js +160 -0
  129. package/dist.commonjs/client/jsonRpc/types.d.ts +56 -0
  130. package/dist.commonjs/client/jsonRpc/types.d.ts.map +1 -0
  131. package/dist.commonjs/client/jsonRpc/types.js +2 -0
  132. package/dist.commonjs/fixedPoint/index.d.ts +49 -0
  133. package/dist.commonjs/fixedPoint/index.d.ts.map +1 -0
  134. package/dist.commonjs/fixedPoint/index.js +68 -0
  135. package/dist.commonjs/fixedPoint/index.test.d.ts +2 -0
  136. package/dist.commonjs/fixedPoint/index.test.d.ts.map +1 -0
  137. package/dist.commonjs/fixedPoint/index.test.js +45 -0
  138. package/dist.commonjs/hasher/advanced.d.ts +2 -0
  139. package/dist.commonjs/hasher/advanced.d.ts.map +1 -0
  140. package/dist.commonjs/hasher/advanced.js +4 -0
  141. package/dist.commonjs/hasher/index.d.ts +52 -0
  142. package/dist.commonjs/hasher/index.d.ts.map +1 -0
  143. package/dist.commonjs/hasher/index.js +68 -0
  144. package/dist.commonjs/hex/index.d.ts +24 -0
  145. package/dist.commonjs/hex/index.d.ts.map +1 -0
  146. package/dist.commonjs/hex/index.js +20 -0
  147. package/dist.commonjs/index.d.ts +3 -0
  148. package/dist.commonjs/index.d.ts.map +1 -0
  149. package/dist.commonjs/index.js +31 -0
  150. package/dist.commonjs/num/index.d.ts +112 -0
  151. package/dist.commonjs/num/index.d.ts.map +1 -0
  152. package/dist.commonjs/num/index.js +139 -0
  153. package/dist.commonjs/signer/btc/index.d.ts +3 -0
  154. package/dist.commonjs/signer/btc/index.d.ts.map +1 -0
  155. package/dist.commonjs/signer/btc/index.js +18 -0
  156. package/dist.commonjs/signer/btc/signerBtc.d.ts +50 -0
  157. package/dist.commonjs/signer/btc/signerBtc.d.ts.map +1 -0
  158. package/dist.commonjs/signer/btc/signerBtc.js +70 -0
  159. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +60 -0
  160. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -0
  161. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +72 -0
  162. package/dist.commonjs/signer/ckb/index.d.ts +2 -0
  163. package/dist.commonjs/signer/ckb/index.d.ts.map +1 -0
  164. package/dist.commonjs/signer/ckb/index.js +17 -0
  165. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +59 -0
  166. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -0
  167. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +71 -0
  168. package/dist.commonjs/signer/evm/index.d.ts +3 -0
  169. package/dist.commonjs/signer/evm/index.d.ts.map +1 -0
  170. package/dist.commonjs/signer/evm/index.js +18 -0
  171. package/dist.commonjs/signer/evm/signerEvm.d.ts +46 -0
  172. package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -0
  173. package/dist.commonjs/signer/evm/signerEvm.js +82 -0
  174. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +52 -0
  175. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -0
  176. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +62 -0
  177. package/dist.commonjs/signer/index.d.ts +5 -0
  178. package/dist.commonjs/signer/index.d.ts.map +1 -0
  179. package/dist.commonjs/signer/index.js +20 -0
  180. package/dist.commonjs/signer/signer.d.ts +133 -0
  181. package/dist.commonjs/signer/signer.d.ts.map +1 -0
  182. package/dist.commonjs/signer/signer.js +117 -0
  183. package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +1 -0
  184. package/dist.commonjs/utils/index.d.ts +7 -0
  185. package/dist.commonjs/utils/index.d.ts.map +1 -0
  186. package/dist.commonjs/utils/index.js +10 -0
  187. package/jest.config.js +5 -0
  188. package/package.json +24 -7
  189. package/src/ckb/transaction.ts +198 -3
  190. package/src/client/client.ts +64 -3
  191. package/src/client/clientPublicMainnet.advanced.ts +5 -0
  192. package/src/client/clientPublicTestnet.advanced.ts +5 -0
  193. package/src/client/clientTypes.ts +70 -1
  194. package/src/client/jsonRpc/index.ts +38 -2
  195. package/src/client/jsonRpc/transformers.ts +73 -4
  196. package/src/client/jsonRpc/types.ts +16 -0
  197. package/src/fixedPoint/index.test.ts +55 -0
  198. package/src/fixedPoint/index.ts +7 -1
  199. package/src/signer/btc/signerBtc.ts +6 -3
  200. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +18 -0
  201. package/src/signer/ckb/signerCkbScriptReadonly.ts +18 -0
  202. package/src/signer/evm/signerEvm.ts +56 -14
  203. package/src/signer/evm/signerEvmAddressReadonly.ts +19 -1
  204. package/src/signer/index.ts +0 -1
  205. package/src/signer/signer.ts +29 -3
  206. package/dist/signer/helpers.d.ts +0 -40
  207. package/dist/signer/helpers.d.ts.map +0 -1
  208. package/dist/signer/helpers.js +0 -90
  209. package/src/signer/helpers.ts +0 -122
@@ -1,4 +1,7 @@
1
- import { Transaction } from "../ckb";
1
+ import { Cell, Script, ScriptLike, Transaction } from "../ckb";
2
+ import { Hex, HexLike, hexFrom } from "../hex";
3
+ import { Num, NumLike, numFrom } from "../num";
4
+ import { apply } from "../utils";
2
5
 
3
6
  export type OutputsValidator = "passthrough" | "well_known_scripts_only";
4
7
 
@@ -11,4 +14,70 @@ export type TransactionStatus =
11
14
  export type ClientTransactionResponse = {
12
15
  transaction: Transaction;
13
16
  status: TransactionStatus;
17
+ blockNumber: Num;
18
+ };
19
+
20
+ export type ClientIndexerSearchKeyLike = {
21
+ script: ScriptLike;
22
+ scriptType: "lock" | "type";
23
+ scriptSearchMode?: "prefix" | "exact" | "partial";
24
+ filter?: {
25
+ script?: ScriptLike;
26
+ scriptLenRange?: [NumLike, NumLike];
27
+ outputData?: HexLike;
28
+ outputDataSearchMode?: "prefix" | "exact" | "partial";
29
+ outputDataLenRange?: [NumLike, NumLike];
30
+ outputCapacityRange?: [NumLike, NumLike];
31
+ blockRange?: [NumLike, NumLike];
32
+ };
33
+ withData?: boolean;
34
+ };
35
+
36
+ function rangeFrom([a, b]: [NumLike, NumLike]): [Num, Num] {
37
+ return [numFrom(a), numFrom(b)];
38
+ }
39
+ export class ClientIndexerSearchKey {
40
+ constructor(
41
+ public script: Script,
42
+ public scriptType: "lock" | "type",
43
+ public scriptSearchMode: "prefix" | "exact" | "partial" | undefined,
44
+ public filter:
45
+ | {
46
+ script?: Script;
47
+ scriptLenRange?: [Num, Num];
48
+ outputData?: Hex;
49
+ outputDataSearchMode?: "prefix" | "exact" | "partial";
50
+ outputDataLenRange?: [Num, Num];
51
+ outputCapacityRange?: [Num, Num];
52
+ blockRange?: [Num, Num];
53
+ }
54
+ | undefined,
55
+ public withData: boolean | undefined,
56
+ ) {}
57
+
58
+ static from(keyLike: ClientIndexerSearchKeyLike): ClientIndexerSearchKey {
59
+ return new ClientIndexerSearchKey(
60
+ Script.from(keyLike.script),
61
+ keyLike.scriptType,
62
+ keyLike.scriptSearchMode,
63
+ apply(
64
+ (filter: NonNullable<ClientIndexerSearchKeyLike["filter"]>) => ({
65
+ script: apply(Script.from, filter.script),
66
+ scriptLenRange: apply(rangeFrom, filter.scriptLenRange),
67
+ outputData: apply(hexFrom, filter.outputData),
68
+ outputDataSearchMode: filter.outputDataSearchMode,
69
+ outputDataLenRange: apply(rangeFrom, filter.outputDataLenRange),
70
+ outputCapacityRange: apply(rangeFrom, filter.outputCapacityRange),
71
+ blockRange: apply(rangeFrom, filter.blockRange),
72
+ }),
73
+ keyLike.filter,
74
+ ),
75
+ keyLike.withData,
76
+ );
77
+ }
78
+ }
79
+
80
+ export type ClientFindCellsResponse = {
81
+ lastCursor: string;
82
+ cells: Cell[];
14
83
  };
@@ -1,8 +1,14 @@
1
1
  import fetch from "cross-fetch";
2
2
  import { TransactionLike } from "../../ckb";
3
3
  import { Hex, HexLike, hexFrom } from "../../hex";
4
+ import { NumLike, numToHex } from "../../num";
4
5
  import { Client } from "../client";
5
- import { ClientTransactionResponse, OutputsValidator } from "../clientTypes";
6
+ import {
7
+ ClientFindCellsResponse,
8
+ ClientIndexerSearchKeyLike,
9
+ ClientTransactionResponse,
10
+ OutputsValidator,
11
+ } from "../clientTypes";
6
12
  import { JsonRpcPayload, JsonRpcTransformers } from "./advanced";
7
13
 
8
14
  /**
@@ -89,6 +95,28 @@ export abstract class ClientJsonRpc extends Client {
89
95
  JsonRpcTransformers.transactionResponseTo,
90
96
  ) as (txHash: HexLike) => Promise<ClientTransactionResponse>;
91
97
 
98
+ /**
99
+ * find cells from node.
100
+ *
101
+ * @param txHash - The hash of the transaction.
102
+ * @returns The found cells.
103
+ */
104
+
105
+ findCellsPaged = this.buildSender(
106
+ "get_cells",
107
+ [
108
+ JsonRpcTransformers.indexerSearchKeyFrom,
109
+ (order) => order ?? "asc",
110
+ (limit) => numToHex(limit ?? 10),
111
+ ],
112
+ JsonRpcTransformers.findCellsResponseTo,
113
+ ) as (
114
+ key: ClientIndexerSearchKeyLike,
115
+ order?: "asc" | "desc",
116
+ limit?: NumLike,
117
+ after?: string,
118
+ ) => Promise<ClientFindCellsResponse>;
119
+
92
120
  /**
93
121
  * Builds a sender function for a JSON-RPC method.
94
122
  *
@@ -106,7 +134,15 @@ export abstract class ClientJsonRpc extends Client {
106
134
  return async (...req: unknown[]) => {
107
135
  const payload = ClientJsonRpc.buildPayload(
108
136
  rpcMethod,
109
- await Promise.all(req.map((v, i) => transform(v, inTransformers[i]))),
137
+ await Promise.all(
138
+ req
139
+ .concat(
140
+ Array.from(
141
+ new Array(Math.max(inTransformers.length - req.length, 0)),
142
+ ),
143
+ )
144
+ .map((v, i) => transform(v, inTransformers[i])),
145
+ ),
110
146
  );
111
147
 
112
148
  return transform(await this.send(payload), outTransformer);
@@ -1,4 +1,5 @@
1
1
  import {
2
+ Cell,
2
3
  CellDep,
3
4
  CellDepLike,
4
5
  CellInput,
@@ -18,15 +19,23 @@ import {
18
19
  depTypeFrom,
19
20
  hashTypeFrom,
20
21
  } from "../../ckb";
21
- import { numToHex } from "../../num";
22
+ import { Hex } from "../../hex";
23
+ import { NumLike, numFrom, numToHex } from "../../num";
22
24
  import { apply } from "../../utils";
23
- import { ClientTransactionResponse, TransactionStatus } from "../clientTypes";
25
+ import {
26
+ ClientFindCellsResponse,
27
+ ClientIndexerSearchKey,
28
+ ClientIndexerSearchKeyLike,
29
+ ClientTransactionResponse,
30
+ TransactionStatus,
31
+ } from "../clientTypes";
24
32
  import {
25
33
  JsonRpcCellDep,
26
34
  JsonRpcCellInput,
27
35
  JsonRpcCellOutput,
28
36
  JsonRpcDepType,
29
37
  JsonRpcHashType,
38
+ JsonRpcIndexerSearchKey,
30
39
  JsonRpcOutPoint,
31
40
  JsonRpcScript,
32
41
  JsonRpcTransaction,
@@ -147,15 +156,75 @@ export class JsonRpcTransformers {
147
156
  });
148
157
  }
149
158
  static transactionResponseTo({
150
- tx_status: { status },
159
+ tx_status: { status, block_number },
151
160
  transaction,
152
161
  }: {
153
- tx_status: { status: TransactionStatus };
162
+ tx_status: { status: TransactionStatus; block_number: Hex };
154
163
  transaction: JsonRpcTransaction;
155
164
  }): ClientTransactionResponse {
156
165
  return {
157
166
  transaction: JsonRpcTransformers.transactionTo(transaction),
158
167
  status,
168
+ blockNumber: numFrom(block_number),
169
+ };
170
+ }
171
+ static rangeFrom([a, b]: [NumLike, NumLike]): [Hex, Hex] {
172
+ return [numToHex(a), numToHex(b)];
173
+ }
174
+ static indexerSearchKeyFrom(
175
+ keyLike: ClientIndexerSearchKeyLike,
176
+ ): JsonRpcIndexerSearchKey {
177
+ const key = ClientIndexerSearchKey.from(keyLike);
178
+ return {
179
+ script: JsonRpcTransformers.scriptFrom(key.script),
180
+ script_type: key.scriptType,
181
+ script_search_mode: key.scriptSearchMode,
182
+ filter: apply(
183
+ (filter: NonNullable<ClientIndexerSearchKey["filter"]>) => ({
184
+ script: apply(JsonRpcTransformers.scriptFrom, filter.script),
185
+ script_len_range: apply(
186
+ JsonRpcTransformers.rangeFrom,
187
+ filter.scriptLenRange,
188
+ ),
189
+ output_data: filter.outputData,
190
+ output_data_filter_mode: filter.outputDataSearchMode,
191
+ output_data_len_range: apply(
192
+ JsonRpcTransformers.rangeFrom,
193
+ filter.outputDataLenRange,
194
+ ),
195
+ output_capacity_range: apply(
196
+ JsonRpcTransformers.rangeFrom,
197
+ filter.outputCapacityRange,
198
+ ),
199
+ block_range: apply(JsonRpcTransformers.rangeFrom, filter.blockRange),
200
+ }),
201
+ key.filter,
202
+ ),
203
+ with_data: key.withData,
204
+ };
205
+ }
206
+ static findCellsResponseTo({
207
+ last_cursor,
208
+ objects,
209
+ }: {
210
+ last_cursor: string;
211
+ objects: {
212
+ block_number: Hex;
213
+ out_point: JsonRpcOutPoint;
214
+ output: JsonRpcCellOutput;
215
+ output_data: Hex;
216
+ }[];
217
+ }): ClientFindCellsResponse {
218
+ return {
219
+ lastCursor: last_cursor,
220
+ cells: objects.map((cell) =>
221
+ Cell.from({
222
+ blockNumber: cell.block_number,
223
+ outPoint: JsonRpcTransformers.outPointTo(cell.out_point),
224
+ cellOutput: JsonRpcTransformers.cellOutputTo(cell.output),
225
+ outputData: cell.output_data,
226
+ }),
227
+ ),
159
228
  };
160
229
  }
161
230
  }
@@ -46,3 +46,19 @@ export type JsonRpcTransaction = {
46
46
  outputs_data: Hex[];
47
47
  witnesses: Hex[];
48
48
  };
49
+
50
+ export type JsonRpcIndexerSearchKey = {
51
+ script: JsonRpcScript;
52
+ script_type: "lock" | "type";
53
+ script_search_mode?: "prefix" | "exact" | "partial";
54
+ filter?: {
55
+ script?: JsonRpcScript;
56
+ script_len_range?: [Hex, Hex];
57
+ output_data?: Hex;
58
+ output_data_filter_mode?: "prefix" | "exact" | "partial";
59
+ output_data_len_range?: [Hex, Hex];
60
+ output_capacity_range?: [Hex, Hex];
61
+ block_range?: [Hex, Hex];
62
+ };
63
+ with_data?: boolean;
64
+ };
@@ -0,0 +1,55 @@
1
+ import { fixedPointFrom, fixedPointToString } from ".";
2
+
3
+ describe("fixedPointToString", () => {
4
+ const cases: [bigint, string, number?][] = [
5
+ [0n, "0"],
6
+ [1n, "0.00000001"],
7
+ [100000000n, "1"],
8
+ [1000000000n, "10"],
9
+ [1010100000n, "10.101"],
10
+ [11n, "1.1", 1],
11
+ [0n, "0", 1],
12
+ [1n, "1", 0],
13
+ [0n, "0", 0],
14
+ ];
15
+
16
+ cases.forEach(([i, o, decimals]) =>
17
+ test(`${i} with ${decimals ?? "default"} decimals = "${o}"`, () => {
18
+ expect(fixedPointToString(i, decimals)).toBe(o);
19
+ }),
20
+ );
21
+ });
22
+
23
+ describe("fixedPointFrom string", () => {
24
+ const cases: [string, bigint, number?][] = [
25
+ ["0", 0n],
26
+ ["0.00000001", 1n],
27
+ ["1", 100000000n],
28
+ ["10", 1000000000n],
29
+ ["10.101", 1010100000n],
30
+ ["1.1", 11n, 1],
31
+ ["0", 0n, 1],
32
+ ["1", 1n, 0],
33
+ ["0", 0n, 0],
34
+ ];
35
+
36
+ cases.forEach(([i, o, decimals]) =>
37
+ test(`"${i}" = ${o} with ${decimals ?? "default"} decimals `, () => {
38
+ expect(fixedPointFrom(i, decimals)).toBe(o);
39
+ }),
40
+ );
41
+ });
42
+
43
+ describe("fixedPointFrom number", () => {
44
+ const cases: [number, bigint, number?][] = [
45
+ [0.00000001, 1n],
46
+ [10.101, 1010100000n],
47
+ [1.1, 11n, 1],
48
+ ];
49
+
50
+ cases.forEach(([i, o, decimals]) =>
51
+ test(`${i} = ${o} with ${decimals ?? "default"} decimals `, () => {
52
+ expect(fixedPointFrom(i, decimals)).toBe(o);
53
+ }),
54
+ );
55
+ });
@@ -28,6 +28,10 @@ export type FixedPointLike = bigint | string | number;
28
28
 
29
29
  export function fixedPointToString(val: FixedPointLike, decimals = 8): string {
30
30
  const str = fixedPointFrom(val).toString();
31
+ if (decimals === 0) {
32
+ return str;
33
+ }
34
+
31
35
  const l = str.length <= decimals ? "0" : str.slice(0, -decimals);
32
36
  const r = str.slice(-decimals).padStart(decimals, "0").replace(/0*$/, "");
33
37
  if (r === "") {
@@ -57,7 +61,9 @@ export function fixedPointFrom(val: FixedPointLike, decimals = 8): FixedPoint {
57
61
  return val;
58
62
  }
59
63
 
60
- const [l, r] = val.toString().split(".");
64
+ const [l, r] = (
65
+ typeof val === "number" ? val.toFixed(decimals) : val.toString()
66
+ ).split(".");
61
67
  const lVal = BigInt(l.padEnd(l.length + decimals, "0"));
62
68
  if (r === undefined) {
63
69
  return lVal;
@@ -6,7 +6,6 @@ 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, prepareSighashAllWitness } from "../helpers";
10
9
  import { Signer } from "../signer";
11
10
 
12
11
  /**
@@ -64,7 +63,11 @@ export abstract class SignerBtc extends Signer {
64
63
  */
65
64
  async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
66
65
  const { script } = await this.getRecommendedAddressObj();
67
- return prepareSighashAllWitness(txLike, script, 85, this.client);
66
+ return Transaction.from(txLike).prepareSighashAllWitness(
67
+ script,
68
+ 85,
69
+ this.client,
70
+ );
68
71
  }
69
72
 
70
73
  /**
@@ -76,7 +79,7 @@ export abstract class SignerBtc extends Signer {
76
79
  async signOnlyTransaction(txLike: TransactionLike): Promise<Transaction> {
77
80
  const tx = Transaction.from(txLike);
78
81
  const { script } = await this.getRecommendedAddressObj();
79
- const info = await getSignHashInfo(tx, script, this.client);
82
+ const info = await tx.getSignHashInfo(script, this.client);
80
83
  if (!info) {
81
84
  return tx;
82
85
  }
@@ -26,6 +26,15 @@ export class SignerBtcPublicKeyReadonly extends SignerBtc {
26
26
  this.publicKey = hexFrom(publicKey);
27
27
  }
28
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
+
29
38
  /**
30
39
  * Connects to the client. This implementation does nothing as the class is read-only.
31
40
  *
@@ -33,6 +42,15 @@ export class SignerBtcPublicKeyReadonly extends SignerBtc {
33
42
  */
34
43
  async connect(): Promise<void> {}
35
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
+
36
54
  /**
37
55
  * Gets the Bitcoin account associated with the signer.
38
56
  *
@@ -22,6 +22,15 @@ export class SignerCkbScriptReadonly extends Signer {
22
22
  this.script = Script.from(script);
23
23
  }
24
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
+
25
34
  /**
26
35
  * Connects to the client. This implementation does nothing as the class is read-only.
27
36
  *
@@ -29,6 +38,15 @@ export class SignerCkbScriptReadonly extends Signer {
29
38
  */
30
39
  async connect(): Promise<void> {}
31
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
+
32
50
  /**
33
51
  * Gets the internal address for the script.
34
52
  *
@@ -1,10 +1,9 @@
1
1
  import { Address } from "../../address";
2
- import { bytesConcat, bytesFrom } from "../../bytes";
3
- import { Transaction, TransactionLike, WitnessArgs } from "../../ckb";
2
+ import { BytesLike, bytesConcat, bytesFrom } from "../../bytes";
3
+ import { Script, 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, prepareSighashAllWitness } from "../helpers";
8
7
  import { Signer } from "../signer";
9
8
 
10
9
  /**
@@ -36,14 +35,27 @@ export abstract class SignerEvm extends Signer {
36
35
  async getAddressObjs(): Promise<Address[]> {
37
36
  const account = await this.getEvmAccount();
38
37
  return [
39
- await Address.fromKnownScript(
40
- KnownScript.OmniLock,
41
- hexFrom([0x12, ...bytesFrom(account), 0x00]),
42
- this.client,
43
- ),
38
+ await this._getOmniLockEvmAddressObj(account),
39
+ await this._getOmniLockOldEvmAddressObj(account),
44
40
  ];
45
41
  }
46
42
 
43
+ async _getOmniLockEvmAddressObj(account: string): Promise<Address> {
44
+ return Address.fromKnownScript(
45
+ KnownScript.OmniLock,
46
+ hexFrom([0x12, ...bytesFrom(account), 0x00]),
47
+ this.client,
48
+ );
49
+ }
50
+
51
+ async _getOmniLockOldEvmAddressObj(account: string): Promise<Address> {
52
+ return Address.fromKnownScript(
53
+ KnownScript.OmniLock,
54
+ hexFrom([0x1, ...bytesFrom(account), 0x00]),
55
+ this.client,
56
+ );
57
+ }
58
+
47
59
  /**
48
60
  * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
49
61
  *
@@ -51,8 +63,15 @@ export abstract class SignerEvm extends Signer {
51
63
  * @returns A promise that resolves to the prepared Transaction object.
52
64
  */
53
65
  async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
54
- const { script } = await this.getRecommendedAddressObj();
55
- return prepareSighashAllWitness(txLike, script, 85, this.client);
66
+ const addresses = await this.getAddressObjs();
67
+
68
+ return addresses.reduce(
69
+ (txPromise, { script }) =>
70
+ txPromise.then((tx) =>
71
+ tx.prepareSighashAllWitness(script, 85, this.client),
72
+ ),
73
+ Promise.resolve(Transaction.from(txLike)),
74
+ );
56
75
  }
57
76
 
58
77
  /**
@@ -62,16 +81,39 @@ export abstract class SignerEvm extends Signer {
62
81
  * @returns A promise that resolves to a signed Transaction object.
63
82
  */
64
83
  async signOnlyTransaction(txLike: TransactionLike): Promise<Transaction> {
65
- const tx = Transaction.from(txLike);
84
+ let tx = Transaction.from(txLike);
85
+
86
+ const account = await this.getEvmAccount();
87
+ const { script: evmScript } = await this._getOmniLockEvmAddressObj(account);
88
+ const { script: oldEvmScript } =
89
+ await this._getOmniLockOldEvmAddressObj(account);
90
+
91
+ tx = await this._signOmniLockScriptForTransaction(
92
+ tx,
93
+ evmScript,
94
+ (hash) => `CKB transaction: ${hash}`,
95
+ );
96
+ tx = await this._signOmniLockScriptForTransaction(
97
+ tx,
98
+ oldEvmScript,
99
+ (hash) => bytesFrom(hash),
100
+ );
101
+
102
+ return tx;
103
+ }
66
104
 
67
- const { script } = await this.getRecommendedAddressObj();
68
- const info = await getSignHashInfo(tx, script, this.client);
105
+ async _signOmniLockScriptForTransaction(
106
+ tx: Transaction,
107
+ script: Script,
108
+ messageTransformer: (hash: string) => BytesLike,
109
+ ): Promise<Transaction> {
110
+ const info = await tx.getSignHashInfo(script, this.client);
69
111
  if (!info) {
70
112
  return tx;
71
113
  }
72
114
 
73
115
  const signature = bytesFrom(
74
- await this.signMessage(`CKB transaction: ${info.message}`),
116
+ await this.signMessage(messageTransformer(info.message)),
75
117
  );
76
118
  if (signature[signature.length - 1] >= 27) {
77
119
  signature[signature.length - 1] -= 27;
@@ -6,7 +6,7 @@ import { SignerEvm } from "./signerEvm";
6
6
  * A class extending SignerEvm that provides read-only access to an EVM address.
7
7
  * This class does not support signing operations.
8
8
  */
9
- export abstract class SignerEvmAddressReadonly extends SignerEvm {
9
+ export class SignerEvmAddressReadonly extends SignerEvm {
10
10
  private readonly address: Hex;
11
11
 
12
12
  /**
@@ -21,6 +21,15 @@ export abstract class SignerEvmAddressReadonly extends SignerEvm {
21
21
  this.address = hexFrom(address);
22
22
  }
23
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
+
24
33
  /**
25
34
  * Connects to the client. This implementation does nothing as the class is read-only.
26
35
  *
@@ -34,6 +43,15 @@ export abstract class SignerEvmAddressReadonly extends SignerEvm {
34
43
 
35
44
  async connect(): Promise<void> {}
36
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
+
37
55
  /**
38
56
  * Gets the EVM account associated with the signer.
39
57
  *
@@ -1,5 +1,4 @@
1
1
  export * from "./btc";
2
2
  export * from "./ckb";
3
3
  export * from "./evm";
4
- export * from "./helpers";
5
4
  export * from "./signer";
@@ -9,15 +9,40 @@ import { Hex } from "../hex";
9
9
  * This class provides methods to connect, get addresses, and sign transactions.
10
10
  */
11
11
  export abstract class Signer {
12
- constructor(public readonly client: Client) {}
12
+ constructor(protected client_: Client) {}
13
+
14
+ get client(): Client {
15
+ return this.client_;
16
+ }
13
17
 
14
18
  /**
15
- * Connects to the client.
19
+ * Construct a new signer with the client replaced.
20
+ *
21
+ * @returns A promise that resolves the new Signer.
22
+ */
23
+ abstract replaceClient(client: Client): Promise<Signer>;
24
+
25
+ /**
26
+ * Connects to the signer.
16
27
  *
17
28
  * @returns A promise that resolves when the connection is complete.
18
29
  */
19
30
  abstract connect(): Promise<void>;
20
31
 
32
+ /**
33
+ * Disconnects to the signer.
34
+ *
35
+ * @returns A promise that resolves when disconnected.
36
+ */
37
+ async disconnect(): Promise<void> {}
38
+
39
+ /**
40
+ * Check if the signer is connected.
41
+ *
42
+ * @returns A promise that resolves the connection status.
43
+ */
44
+ abstract isConnected(): Promise<boolean>;
45
+
21
46
  /**
22
47
  * Gets the internal address associated with the signer.
23
48
  *
@@ -124,6 +149,7 @@ export abstract class Signer {
124
149
  export enum SignerType {
125
150
  EVM = "EVM",
126
151
  BTC = "BTC",
152
+ CKB = "CKB",
127
153
  }
128
154
 
129
155
  /**
@@ -131,6 +157,7 @@ export enum SignerType {
131
157
  */
132
158
  export class SignerInfo {
133
159
  constructor(
160
+ public name: string,
134
161
  public type: SignerType,
135
162
  public signer: Signer,
136
163
  ) {}
@@ -142,5 +169,4 @@ export class SignerInfo {
142
169
  export type Wallet = {
143
170
  name: string;
144
171
  icon: string;
145
- signers: SignerInfo[];
146
172
  };