@ckb-ccc/core 0.0.5-alpha.1 → 0.0.5-alpha.10

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 (229) hide show
  1. package/dist/address/index.d.ts +1 -1
  2. package/dist/address/index.d.ts.map +1 -1
  3. package/dist/address/index.js +4 -4
  4. package/dist/ckb/transaction.d.ts +2 -2
  5. package/dist/ckb/transaction.d.ts.map +1 -1
  6. package/dist/ckb/transaction.js +2 -3
  7. package/dist/client/client.d.ts +2 -2
  8. package/dist/client/client.d.ts.map +1 -1
  9. package/dist/client/client.js +2 -1
  10. package/dist/client/clientPublicMainnet.d.ts +1 -1
  11. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  12. package/dist/client/clientPublicMainnet.js +1 -1
  13. package/dist/client/clientPublicTestnet.d.ts +1 -1
  14. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  15. package/dist/client/clientPublicTestnet.js +1 -1
  16. package/dist/client/jsonRpc/index.d.ts +3 -3
  17. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  18. package/dist/client/jsonRpc/index.js +4 -4
  19. package/dist/signer/btc/index.d.ts +1 -0
  20. package/dist/signer/btc/index.d.ts.map +1 -1
  21. package/dist/signer/btc/index.js +1 -0
  22. package/dist/signer/btc/signerBtc.d.ts +9 -1
  23. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  24. package/dist/signer/btc/signerBtc.js +19 -3
  25. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +0 -6
  26. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
  27. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +0 -8
  28. package/dist/signer/btc/verify.d.ts +3 -0
  29. package/dist/signer/btc/verify.d.ts.map +1 -0
  30. package/dist/signer/btc/verify.js +6 -0
  31. package/dist/signer/ckb/index.d.ts +1 -0
  32. package/dist/signer/ckb/index.d.ts.map +1 -1
  33. package/dist/signer/ckb/index.js +1 -0
  34. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +3 -7
  35. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  36. package/dist/signer/ckb/signerCkbScriptReadonly.js +8 -10
  37. package/dist/signer/ckb/verifyJoyId.d.ts +3 -0
  38. package/dist/signer/ckb/verifyJoyId.d.ts.map +1 -0
  39. package/dist/signer/ckb/verifyJoyId.js +12 -0
  40. package/dist/signer/dummy/alwaysError.d.ts +9 -0
  41. package/dist/signer/dummy/alwaysError.d.ts.map +1 -0
  42. package/dist/signer/dummy/alwaysError.js +10 -0
  43. package/dist/signer/dummy/dummy.d.ts +12 -0
  44. package/dist/signer/dummy/dummy.d.ts.map +1 -0
  45. package/dist/signer/dummy/dummy.js +19 -0
  46. package/dist/signer/dummy/index.d.ts +4 -0
  47. package/dist/signer/dummy/index.d.ts.map +1 -0
  48. package/dist/signer/dummy/index.js +3 -0
  49. package/dist/signer/dummy/openLink.d.ts +9 -0
  50. package/dist/signer/dummy/openLink.d.ts.map +1 -0
  51. package/dist/signer/dummy/openLink.js +10 -0
  52. package/dist/signer/evm/index.d.ts +1 -0
  53. package/dist/signer/evm/index.d.ts.map +1 -1
  54. package/dist/signer/evm/index.js +1 -0
  55. package/dist/signer/evm/signerEvm.d.ts +3 -1
  56. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  57. package/dist/signer/evm/signerEvm.js +10 -4
  58. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +0 -6
  59. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
  60. package/dist/signer/evm/signerEvmAddressReadonly.js +0 -8
  61. package/dist/signer/evm/verify.d.ts +3 -0
  62. package/dist/signer/evm/verify.d.ts.map +1 -0
  63. package/dist/signer/evm/verify.js +6 -0
  64. package/dist/signer/index.d.ts +1 -0
  65. package/dist/signer/index.d.ts.map +1 -1
  66. package/dist/signer/index.js +1 -0
  67. package/{dist.commonjs/signer/signer.d.ts → dist/signer/signer/index.d.ts} +69 -25
  68. package/dist/signer/signer/index.d.ts.map +1 -0
  69. package/dist/signer/{signer.js → signer/index.js} +101 -16
  70. package/dist/utils/index.d.ts +60 -0
  71. package/dist/utils/index.d.ts.map +1 -1
  72. package/dist/utils/index.js +20 -0
  73. package/dist.commonjs/address/address.advanced.d.ts +29 -24
  74. package/dist.commonjs/address/address.advanced.js +114 -92
  75. package/dist.commonjs/address/advanced.d.ts +1 -1
  76. package/dist.commonjs/address/advanced.js +30 -14
  77. package/dist.commonjs/address/index.d.ts +51 -44
  78. package/dist.commonjs/address/index.d.ts.map +1 -1
  79. package/dist.commonjs/address/index.js +87 -68
  80. package/dist.commonjs/advanced.d.ts +1 -1
  81. package/dist.commonjs/advanced.js +47 -22
  82. package/dist.commonjs/advancedBarrel.d.ts +1 -1
  83. package/dist.commonjs/advancedBarrel.js +30 -14
  84. package/dist.commonjs/barrel.d.ts +1 -1
  85. package/dist.commonjs/barrel.js +30 -14
  86. package/dist.commonjs/bytes/advanced.d.ts +11 -2
  87. package/dist.commonjs/bytes/index.d.ts +9 -3
  88. package/dist.commonjs/bytes/index.js +28 -26
  89. package/dist.commonjs/ckb/advanced.d.ts +1 -1
  90. package/dist.commonjs/ckb/advanced.js +47 -22
  91. package/dist.commonjs/ckb/index.d.ts +1 -1
  92. package/dist.commonjs/ckb/index.js +30 -14
  93. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts +420 -260
  94. package/dist.commonjs/ckb/molecule.advanced/generated.js +1536 -1332
  95. package/dist.commonjs/ckb/molecule.advanced/index.d.ts +3 -3
  96. package/dist.commonjs/ckb/molecule.advanced/index.js +34 -18
  97. package/dist.commonjs/ckb/script.advanced.d.ts +1 -1
  98. package/dist.commonjs/ckb/script.advanced.js +12 -9
  99. package/dist.commonjs/ckb/script.d.ts +76 -76
  100. package/dist.commonjs/ckb/script.js +166 -126
  101. package/dist.commonjs/ckb/transaction.advanced.d.ts +1 -1
  102. package/dist.commonjs/ckb/transaction.advanced.js +4 -4
  103. package/dist.commonjs/ckb/transaction.d.ts +570 -532
  104. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  105. package/dist.commonjs/ckb/transaction.js +839 -723
  106. package/dist.commonjs/client/advanced.d.ts +1 -1
  107. package/dist.commonjs/client/advanced.js +30 -14
  108. package/dist.commonjs/client/client.d.ts +52 -21
  109. package/dist.commonjs/client/client.d.ts.map +1 -1
  110. package/dist.commonjs/client/client.js +67 -57
  111. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +5 -2
  112. package/dist.commonjs/client/clientPublicMainnet.advanced.js +30 -24
  113. package/dist.commonjs/client/clientPublicMainnet.d.ts +6 -4
  114. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
  115. package/dist.commonjs/client/clientPublicMainnet.js +9 -9
  116. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +5 -2
  117. package/dist.commonjs/client/clientPublicTestnet.advanced.js +30 -24
  118. package/dist.commonjs/client/clientPublicTestnet.d.ts +6 -4
  119. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
  120. package/dist.commonjs/client/clientPublicTestnet.js +9 -9
  121. package/dist.commonjs/client/clientTypes.d.ts +51 -36
  122. package/dist.commonjs/client/clientTypes.js +34 -19
  123. package/dist.commonjs/client/index.d.ts +1 -1
  124. package/dist.commonjs/client/index.js +30 -14
  125. package/dist.commonjs/client/jsonRpc/advanced.d.ts +1 -1
  126. package/dist.commonjs/client/jsonRpc/advanced.js +30 -14
  127. package/dist.commonjs/client/jsonRpc/index.d.ts +92 -75
  128. package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -1
  129. package/dist.commonjs/client/jsonRpc/index.js +135 -108
  130. package/dist.commonjs/client/jsonRpc/transformers.d.ts +78 -38
  131. package/dist.commonjs/client/jsonRpc/transformers.js +175 -150
  132. package/dist.commonjs/client/jsonRpc/types.d.ts +38 -38
  133. package/dist.commonjs/fixedPoint/index.d.ts +9 -3
  134. package/dist.commonjs/fixedPoint/index.js +26 -20
  135. package/dist.commonjs/fixedPoint/index.test.d.ts +1 -1
  136. package/dist.commonjs/fixedPoint/index.test.js +42 -36
  137. package/dist.commonjs/hasher/advanced.d.ts +1 -1
  138. package/dist.commonjs/hasher/index.d.ts +36 -36
  139. package/dist.commonjs/hasher/index.js +52 -45
  140. package/dist.commonjs/hex/index.d.ts +1 -1
  141. package/dist.commonjs/hex/index.js +1 -1
  142. package/dist.commonjs/index.d.ts +1 -1
  143. package/dist.commonjs/index.js +47 -22
  144. package/dist.commonjs/num/index.d.ts +1 -1
  145. package/dist.commonjs/num/index.js +30 -19
  146. package/dist.commonjs/signer/btc/index.d.ts +2 -1
  147. package/dist.commonjs/signer/btc/index.d.ts.map +1 -1
  148. package/dist.commonjs/signer/btc/index.js +31 -14
  149. package/dist.commonjs/signer/btc/signerBtc.d.ts +48 -40
  150. package/dist.commonjs/signer/btc/signerBtc.d.ts.map +1 -1
  151. package/dist.commonjs/signer/btc/signerBtc.js +85 -49
  152. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +45 -51
  153. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
  154. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +52 -60
  155. package/dist.commonjs/signer/btc/verify.d.ts +7 -0
  156. package/dist.commonjs/signer/btc/verify.d.ts.map +1 -0
  157. package/dist.commonjs/signer/btc/verify.js +13 -0
  158. package/dist.commonjs/signer/ckb/index.d.ts +2 -1
  159. package/dist.commonjs/signer/ckb/index.d.ts.map +1 -1
  160. package/dist.commonjs/signer/ckb/index.js +31 -14
  161. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +46 -50
  162. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  163. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +56 -58
  164. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts +7 -0
  165. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts.map +1 -0
  166. package/dist.commonjs/signer/ckb/verifyJoyId.js +19 -0
  167. package/dist.commonjs/signer/dummy/alwaysError.d.ts +9 -0
  168. package/dist.commonjs/signer/dummy/alwaysError.d.ts.map +1 -0
  169. package/dist.commonjs/signer/dummy/alwaysError.js +14 -0
  170. package/dist.commonjs/signer/dummy/dummy.d.ts +12 -0
  171. package/dist.commonjs/signer/dummy/dummy.d.ts.map +1 -0
  172. package/dist.commonjs/signer/dummy/dummy.js +23 -0
  173. package/dist.commonjs/signer/dummy/index.d.ts +4 -0
  174. package/dist.commonjs/signer/dummy/index.d.ts.map +1 -0
  175. package/dist.commonjs/signer/dummy/index.js +35 -0
  176. package/dist.commonjs/signer/dummy/openLink.d.ts +9 -0
  177. package/dist.commonjs/signer/dummy/openLink.d.ts.map +1 -0
  178. package/dist.commonjs/signer/dummy/openLink.js +14 -0
  179. package/dist.commonjs/signer/evm/index.d.ts +2 -1
  180. package/dist.commonjs/signer/evm/index.d.ts.map +1 -1
  181. package/dist.commonjs/signer/evm/index.js +31 -14
  182. package/dist.commonjs/signer/evm/signerEvm.d.ts +43 -37
  183. package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -1
  184. package/dist.commonjs/signer/evm/signerEvm.js +101 -63
  185. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +37 -43
  186. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
  187. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +42 -50
  188. package/dist.commonjs/signer/evm/verify.d.ts +7 -0
  189. package/dist.commonjs/signer/evm/verify.d.ts.map +1 -0
  190. package/dist.commonjs/signer/evm/verify.js +15 -0
  191. package/dist.commonjs/signer/index.d.ts +2 -1
  192. package/dist.commonjs/signer/index.d.ts.map +1 -1
  193. package/dist.commonjs/signer/index.js +31 -14
  194. package/dist.commonjs/signer/signer/index.d.ts +190 -0
  195. package/dist.commonjs/signer/signer/index.d.ts.map +1 -0
  196. package/dist.commonjs/signer/signer/index.js +234 -0
  197. package/dist.commonjs/utils/index.d.ts +94 -6
  198. package/dist.commonjs/utils/index.d.ts.map +1 -1
  199. package/dist.commonjs/utils/index.js +33 -5
  200. package/package.json +6 -3
  201. package/src/address/index.ts +4 -7
  202. package/src/ckb/transaction.ts +4 -5
  203. package/src/client/client.ts +7 -6
  204. package/src/client/clientPublicMainnet.ts +1 -1
  205. package/src/client/clientPublicTestnet.ts +1 -1
  206. package/src/client/jsonRpc/index.ts +3 -3
  207. package/src/signer/btc/index.ts +1 -0
  208. package/src/signer/btc/signerBtc.ts +22 -7
  209. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +0 -9
  210. package/src/signer/btc/verify.ts +14 -0
  211. package/src/signer/ckb/index.ts +1 -0
  212. package/src/signer/ckb/signerCkbScriptReadonly.ts +10 -11
  213. package/src/signer/ckb/verifyJoyId.ts +20 -0
  214. package/src/signer/dummy/alwaysError.ts +17 -0
  215. package/src/signer/dummy/dummy.ts +28 -0
  216. package/src/signer/dummy/index.ts +3 -0
  217. package/src/signer/dummy/openLink.ts +17 -0
  218. package/src/signer/evm/index.ts +1 -0
  219. package/src/signer/evm/signerEvm.ts +16 -10
  220. package/src/signer/evm/signerEvmAddressReadonly.ts +0 -9
  221. package/src/signer/evm/verify.ts +16 -0
  222. package/src/signer/index.ts +1 -0
  223. package/src/signer/{signer.ts → signer/index.ts} +135 -25
  224. package/src/utils/index.ts +108 -0
  225. package/dist/signer/signer.d.ts +0 -140
  226. package/dist/signer/signer.d.ts.map +0 -1
  227. package/dist.commonjs/signer/signer.d.ts.map +0 -1
  228. package/dist.commonjs/signer/signer.js +0 -125
  229. package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +0 -1
@@ -0,0 +1,234 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SignerInfo =
4
+ exports.Signer =
5
+ exports.Signature =
6
+ exports.SignerType =
7
+ exports.SignerSignType =
8
+ void 0;
9
+ const btc_1 = require("../btc");
10
+ const verifyJoyId_1 = require("../ckb/verifyJoyId");
11
+ const verify_1 = require("../evm/verify");
12
+ var SignerSignType;
13
+ (function (SignerSignType) {
14
+ SignerSignType["Unknown"] = "Unknown";
15
+ SignerSignType["BtcEcdsa"] = "BtcEcdsa";
16
+ SignerSignType["EvmPersonal"] = "EvmPersonal";
17
+ SignerSignType["JoyId"] = "JoyId";
18
+ })(SignerSignType || (exports.SignerSignType = SignerSignType = {}));
19
+ /**
20
+ * An enumeration of signer display types in wallet.
21
+ */
22
+ var SignerType;
23
+ (function (SignerType) {
24
+ SignerType["EVM"] = "EVM";
25
+ SignerType["BTC"] = "BTC";
26
+ SignerType["CKB"] = "CKB";
27
+ })(SignerType || (exports.SignerType = SignerType = {}));
28
+ class Signature {
29
+ constructor(signature, identity, signType) {
30
+ this.signature = signature;
31
+ this.identity = identity;
32
+ this.signType = signType;
33
+ }
34
+ }
35
+ exports.Signature = Signature;
36
+ /**
37
+ * An abstract class representing a generic signer.
38
+ * This class provides methods to connect, get addresses, and sign transactions.
39
+ */
40
+ class Signer {
41
+ constructor(client_) {
42
+ this.client_ = client_;
43
+ }
44
+ get client() {
45
+ return this.client_;
46
+ }
47
+ static async verifyMessage(message, signature) {
48
+ switch (signature.signType) {
49
+ case SignerSignType.EvmPersonal:
50
+ return (0, verify_1.verifyMessageEvmPersonal)(
51
+ message,
52
+ signature.signature,
53
+ signature.identity,
54
+ );
55
+ case SignerSignType.BtcEcdsa:
56
+ return (0, btc_1.verifyMessageBtcEcdsa)(
57
+ message,
58
+ signature.signature,
59
+ signature.identity,
60
+ );
61
+ case SignerSignType.JoyId:
62
+ return (0, verifyJoyId_1.verifyMessageJoyId)(
63
+ message,
64
+ signature.signature,
65
+ signature.identity,
66
+ );
67
+ case SignerSignType.Unknown:
68
+ throw new Error("Unknown signer sign type");
69
+ }
70
+ }
71
+ /**
72
+ * Replace the current client.
73
+ */
74
+ async replaceClient(client) {
75
+ this.client_ = client;
76
+ }
77
+ /**
78
+ * Disconnects to the signer.
79
+ *
80
+ * @returns A promise that resolves when disconnected.
81
+ */
82
+ async disconnect() {}
83
+ /**
84
+ * Gets the identity for verifying signature, usually it's address
85
+ *
86
+ * @returns A promise that resolves to a string representing the identity
87
+ */
88
+ async getIdentity() {
89
+ return this.getInternalAddress();
90
+ }
91
+ /**
92
+ * Gets the recommended Address object for the signer.
93
+ *
94
+ * @param _preference - Optional preference parameter.
95
+ * @returns A promise that resolves to the recommended Address object.
96
+ */
97
+ async getRecommendedAddressObj(_preference) {
98
+ return (await this.getAddressObjs())[0];
99
+ }
100
+ /**
101
+ * Gets the recommended address for the signer as a string.
102
+ *
103
+ * @param preference - Optional preference parameter.
104
+ * @returns A promise that resolves to the recommended address as a string.
105
+ */
106
+ async getRecommendedAddress(preference) {
107
+ return (await this.getRecommendedAddressObj(preference)).toString();
108
+ }
109
+ /**
110
+ * Gets an array of addresses associated with the signer as strings.
111
+ *
112
+ * @returns A promise that resolves to an array of addresses as strings.
113
+ */
114
+ async getAddresses() {
115
+ return this.getAddressObjs().then((addresses) =>
116
+ addresses.map((address) => address.toString()),
117
+ );
118
+ }
119
+ /**
120
+ * Gets balance of all addresses
121
+ *
122
+ * @returns A promise that resolves to the balance
123
+ */
124
+ async getBalance() {
125
+ return this.client.getBalance(
126
+ (await this.getAddressObjs()).map(({ script }) => script),
127
+ );
128
+ }
129
+ /**
130
+ * Signs a message.
131
+ *
132
+ * @param _ - The message to sign, as a string or BytesLike object.
133
+ * @returns A promise that resolves to the signature info.
134
+ * @throws Will throw an error if not implemented.
135
+ */
136
+ async signMessage(message) {
137
+ return {
138
+ signature: await this.signMessageRaw(message),
139
+ identity: await this.getIdentity(),
140
+ signType: this.signType,
141
+ };
142
+ }
143
+ /**
144
+ * Signs a message and returns signature only. This method is not implemented and should be overridden by subclasses.
145
+ *
146
+ * @param _ - The message to sign, as a string or BytesLike object.
147
+ * @returns A promise that resolves to the signature as a string.
148
+ * @throws Will throw an error if not implemented.
149
+ */
150
+ signMessageRaw(_) {
151
+ throw Error("Signer.signMessageRaw not implemented");
152
+ }
153
+ /**
154
+ * Verify a signature.
155
+ *
156
+ * @param _ - The original message.
157
+ * @param _ - The signature to verify.
158
+ * @returns A promise that resolves to the verification result.
159
+ * @throws Will throw an error if not implemented.
160
+ */
161
+ async verifyMessage(message, signature) {
162
+ if (typeof signature === "string") {
163
+ return this.verifyMessageRaw(message, signature);
164
+ }
165
+ if (
166
+ signature.identity !== (await this.getIdentity()) ||
167
+ ![SignerSignType.Unknown, this.signType].includes(signature.signType)
168
+ ) {
169
+ return false;
170
+ }
171
+ return this.verifyMessageRaw(message, signature.signature);
172
+ }
173
+ /**
174
+ * Verify a string signature. This method is not implemented and should be overridden by subclasses.
175
+ *
176
+ * @param _0 - The original message.
177
+ * @param _1 - The signature to verify.
178
+ * @returns A promise that resolves to the verification result.
179
+ * @throws Will throw an error if not implemented.
180
+ */
181
+ verifyMessageRaw(_0, _1) {
182
+ throw Error("Signer.verifyMessageRaw not implemented");
183
+ }
184
+ /**
185
+ * Sends a transaction after signing it.
186
+ *
187
+ * @param tx - The transaction to send, represented as a TransactionLike object.
188
+ * @returns A promise that resolves to the transaction hash as a Hex string.
189
+ */
190
+ async sendTransaction(tx) {
191
+ return this.client.sendTransaction(await this.signTransaction(tx));
192
+ }
193
+ /**
194
+ * Signs a transaction.
195
+ *
196
+ * @param tx - The transaction to sign, represented as a TransactionLike object.
197
+ * @returns A promise that resolves to the signed Transaction object.
198
+ */
199
+ async signTransaction(tx) {
200
+ const preparedTx = await this.prepareTransaction(tx);
201
+ return this.signOnlyTransaction(preparedTx);
202
+ }
203
+ /**
204
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
205
+ *
206
+ * @param _ - The transaction to prepare, represented as a TransactionLike object.
207
+ * @returns A promise that resolves to the prepared Transaction object.
208
+ * @throws Will throw an error if not implemented.
209
+ */
210
+ prepareTransaction(_) {
211
+ throw Error("Signer.prepareTransaction not implemented");
212
+ }
213
+ /**
214
+ * Signs a transaction without preparing information for it. This method is not implemented and should be overridden by subclasses.
215
+ *
216
+ * @param _ - The transaction to sign, represented as a TransactionLike object.
217
+ * @returns A promise that resolves to the signed Transaction object.
218
+ * @throws Will throw an error if not implemented.
219
+ */
220
+ signOnlyTransaction(_) {
221
+ throw Error("Signer.signOnlyTransaction not implemented");
222
+ }
223
+ }
224
+ exports.Signer = Signer;
225
+ /**
226
+ * A class representing information about a signer, including its type and the signer instance.
227
+ */
228
+ class SignerInfo {
229
+ constructor(name, signer) {
230
+ this.name = name;
231
+ this.signer = signer;
232
+ }
233
+ }
234
+ exports.SignerInfo = SignerInfo;
@@ -1,7 +1,95 @@
1
- export declare function apply<T, R>(transformer: (val: T) => R, value: undefined): undefined;
2
- export declare function apply<T, R>(transformer: (val: T) => R, value: null): null;
1
+ /**
2
+ * A type safe way to apply a transformer on a value if it's not empty.
3
+ *
4
+ * @param transformer - The transformer.
5
+ * @param value - The value to be transformed.
6
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
7
+ */
8
+ export declare function apply<T, R>(
9
+ transformer: (val: T) => R,
10
+ value: undefined,
11
+ ): undefined;
12
+ /**
13
+ * A type safe way to apply a transformer on a value if it's not empty.
14
+ *
15
+ * @param transformer - The transformer.
16
+ * @param value - The value to be transformed.
17
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
18
+ */
19
+ export declare function apply<T, R>(
20
+ transformer: (val: T) => R,
21
+ value: null,
22
+ ): null;
23
+ /**
24
+ * A type safe way to apply a transformer on a value if it's not empty.
25
+ *
26
+ * @param transformer - The transformer.
27
+ * @param value - The value to be transformed.
28
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
29
+ */
3
30
  export declare function apply<T, R>(transformer: (val: T) => R, value: T): R;
4
- export declare function apply<T, R>(transformer: (val: T) => R, value: T | undefined): R | undefined;
5
- export declare function apply<T, R>(transformer: (val: T) => R, value: T | null): R | null;
6
- export declare function apply<T, R>(transformer: (val: T) => R, value: undefined | null): undefined | null;
7
- //# sourceMappingURL=index.d.ts.map
31
+ /**
32
+ * A type safe way to apply a transformer on a value if it's not empty.
33
+ *
34
+ * @param transformer - The transformer.
35
+ * @param value - The value to be transformed.
36
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
37
+ */
38
+ export declare function apply<T, R>(
39
+ transformer: (val: T) => R,
40
+ value: T | undefined,
41
+ ): R | undefined;
42
+ /**
43
+ * A type safe way to apply a transformer on a value if it's not empty.
44
+ *
45
+ * @param transformer - The transformer.
46
+ * @param value - The value to be transformed.
47
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
48
+ */
49
+ export declare function apply<T, R>(
50
+ transformer: (val: T) => R,
51
+ value: T | null,
52
+ ): R | null;
53
+ /**
54
+ * A type safe way to apply a transformer on a value if it's not empty.
55
+ *
56
+ * @param transformer - The transformer.
57
+ * @param value - The value to be transformed.
58
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
59
+ */
60
+ export declare function apply<T, R>(
61
+ transformer: (val: T) => R,
62
+ value: undefined | null,
63
+ ): undefined | null;
64
+ /**
65
+ * Similar to Array.reduce, but the accumulator can returns Promise.
66
+ *
67
+ * @param values - The array to be reduced.
68
+ * @param accumulator - A callback to be called for each value. If it returns null, the previous result will be kept.
69
+ * @returns The accumulated result.
70
+ */
71
+ export declare function reduceAsync<T, V>(
72
+ values: T[],
73
+ accumulator: (
74
+ a: T,
75
+ b: T,
76
+ ) => Promise<T | undefined | null | void> | T | undefined | null | void,
77
+ ): Promise<T>;
78
+ /**
79
+ * Similar to Array.reduce, but the accumulator can returns Promise.
80
+ *
81
+ * @param values - The array to be reduced.
82
+ * @param accumulator - A callback to be called for each value. If it returns null, the previous result will be kept.
83
+ * @param init - The initial value.
84
+ * @returns The accumulated result.
85
+ */
86
+ export declare function reduceAsync<T, V>(
87
+ values: V[],
88
+ accumulator: (
89
+ a: T,
90
+ b: V,
91
+ ) => Promise<T | undefined | null | void> | T | undefined | null | void,
92
+ init: T,
93
+ ): Promise<T>;
94
+ export declare function isWebview(userAgent: string): boolean;
95
+ //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,SAAS,GACf,SAAS,CAAC;AACb,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,IAAI,CAAC;AAC3E,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;AACrE,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,CAAC,GAAG,SAAS,GACnB,CAAC,GAAG,SAAS,CAAC;AACjB,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,CAAC,GAAG,IAAI,GACd,CAAC,GAAG,IAAI,CAAC;AACZ,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,SAAS,GAAG,IAAI,GACtB,SAAS,GAAG,IAAI,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,SAAS,GACf,SAAS,CAAC;AACb;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,IAAI,CAAC;AAC3E;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;AACrE;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,CAAC,GAAG,SAAS,GACnB,CAAC,GAAG,SAAS,CAAC;AACjB;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,CAAC,GAAG,IAAI,GACd,CAAC,GAAG,IAAI,CAAC;AACZ;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,SAAS,GAAG,IAAI,GACtB,SAAS,GAAG,IAAI,CAAC;AAmBpB;;;;;;GAMG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,CAAC,EACpC,MAAM,EAAE,CAAC,EAAE,EACX,WAAW,EAAE,CACX,CAAC,EAAE,CAAC,EACJ,CAAC,EAAE,CAAC,KACD,OAAO,CAAC,CAAC,GAAG,SAAS,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,SAAS,GAAG,IAAI,GAAG,IAAI,GACtE,OAAO,CAAC,CAAC,CAAC,CAAC;AACd;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,CAAC,EACpC,MAAM,EAAE,CAAC,EAAE,EACX,WAAW,EAAE,CACX,CAAC,EAAE,CAAC,EACJ,CAAC,EAAE,CAAC,KACD,OAAO,CAAC,CAAC,GAAG,SAAS,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,SAAS,GAAG,IAAI,GAAG,IAAI,EACvE,IAAI,EAAE,CAAC,GACN,OAAO,CAAC,CAAC,CAAC,CAAC;AA0Bd,wBAAgB,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAEpD"}
@@ -1,10 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.apply = void 0;
3
+ exports.isWebview = exports.reduceAsync = exports.apply = void 0;
4
+ /**
5
+ * A type safe way to apply a transformer on a value if it's not empty.
6
+ *
7
+ * @param transformer - The transformer.
8
+ * @param value - The value to be transformed.
9
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
10
+ */
4
11
  function apply(transformer, value) {
5
- if (value == null) {
6
- return value;
7
- }
8
- return transformer(value);
12
+ if (value == null) {
13
+ return value;
14
+ }
15
+ return transformer(value);
9
16
  }
10
17
  exports.apply = apply;
18
+ async function reduceAsync(values, accumulator, init) {
19
+ if (init === undefined) {
20
+ if (values.length === 0) {
21
+ throw new TypeError("Reduce of empty array with no initial value");
22
+ }
23
+ init = values[0];
24
+ values = values.slice(1);
25
+ }
26
+ return values.reduce(
27
+ (current, b) =>
28
+ current.then((v) =>
29
+ Promise.resolve(accumulator(v, b)).then((r) => r ?? v),
30
+ ),
31
+ Promise.resolve(init),
32
+ );
33
+ }
34
+ exports.reduceAsync = reduceAsync;
35
+ function isWebview(userAgent) {
36
+ return /webview|wv|ip((?!.*Safari)|(?=.*like Safari))/i.test(userAgent);
37
+ }
38
+ exports.isWebview = isWebview;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/core",
3
- "version": "0.0.5-alpha.1",
3
+ "version": "0.0.5-alpha.10",
4
4
  "description": "Common Chains Connector Core",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -57,12 +57,15 @@
57
57
  },
58
58
  "dependencies": {
59
59
  "@ckb-lumos/helpers": "^0.22.2",
60
+ "@joyid/ckb": "^0.0.11",
60
61
  "@noble/hashes": "^1.4.0",
62
+ "@unisat/wallet-utils": "^1.0.0",
61
63
  "abort-controller": "^3.0.0",
62
64
  "bech32": "^2.0.0",
63
65
  "blake2b": "^2.1.4",
64
66
  "buffer": "^6.0.3",
65
- "cross-fetch": "^4.0.0"
67
+ "cross-fetch": "^4.0.0",
68
+ "ethers": "^6.13.1"
66
69
  },
67
- "gitHead": "98d6d902fff0cc3d65f536cb85b32c867045d15d"
70
+ "gitHead": "91ea3a92b02117905449ce4d2afef88bd3ffcc60"
68
71
  }
@@ -66,7 +66,7 @@ export class Address {
66
66
  if (!client) {
67
67
  throw new Error(`Unknown address prefix ${prefix}`);
68
68
  }
69
- const expectedPrefix = await client.getAddressPrefix();
69
+ const expectedPrefix = client.addressPrefix;
70
70
  if (expectedPrefix !== prefix) {
71
71
  throw new Error(
72
72
  `Unknown address prefix ${prefix}, expected ${expectedPrefix}`,
@@ -86,11 +86,8 @@ export class Address {
86
86
  * @returns A promise that resolves to an Address instance.
87
87
  */
88
88
 
89
- static async fromScript(
90
- script: ScriptLike,
91
- client: Client,
92
- ): Promise<Address> {
93
- return Address.from({ script, prefix: await client.getAddressPrefix() });
89
+ static fromScript(script: ScriptLike, client: Client): Address {
90
+ return Address.from({ script, prefix: client.addressPrefix });
94
91
  }
95
92
 
96
93
  static async fromKnownScript(
@@ -103,7 +100,7 @@ export class Address {
103
100
  ...(await client.getKnownScript(script)),
104
101
  args,
105
102
  },
106
- prefix: await client.getAddressPrefix(),
103
+ prefix: client.addressPrefix,
107
104
  });
108
105
  }
109
106
 
@@ -961,7 +961,7 @@ export class Transaction {
961
961
  * await tx.setWitnessArgsAt(0, witnessArgs);
962
962
  * ```
963
963
  */
964
- setWitnessArgsAt(index: number, witness: WitnessArgs): Transaction {
964
+ setWitnessArgsAt(index: number, witness: WitnessArgs): void {
965
965
  if (this.witnesses.length < index) {
966
966
  this.witnesses.push(
967
967
  ...Array.from(
@@ -972,7 +972,6 @@ export class Transaction {
972
972
  }
973
973
 
974
974
  this.witnesses[index] = hexFrom(witness.toBytes());
975
- return this;
976
975
  }
977
976
 
978
977
  /**
@@ -992,14 +991,14 @@ export class Transaction {
992
991
  scriptLike: ScriptLike,
993
992
  lockLen: number,
994
993
  client: Client,
995
- ): Promise<Transaction> {
994
+ ): Promise<void> {
996
995
  const position = await this.findInputIndexByLock(scriptLike, client);
997
996
  if (position === undefined) {
998
- return this;
997
+ return;
999
998
  }
1000
999
 
1001
1000
  const witness = this.getWitnessArgsAt(position) ?? WitnessArgs.from({});
1002
1001
  witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
1003
- return this.setWitnessArgsAt(position, witness);
1002
+ this.setWitnessArgsAt(position, witness);
1004
1003
  }
1005
1004
  }
@@ -8,6 +8,7 @@ import {
8
8
  import { Zero } from "../fixedPoint";
9
9
  import { Hex, HexLike } from "../hex";
10
10
  import { Num, NumLike, numFrom } from "../num";
11
+ import { reduceAsync } from "../utils";
11
12
  import {
12
13
  ClientFindCellsResponse,
13
14
  ClientIndexerSearchKeyLike,
@@ -25,9 +26,9 @@ export enum KnownScript {
25
26
  }
26
27
 
27
28
  export abstract class Client {
28
- abstract getUrl(): string;
29
+ abstract get url(): string;
30
+ abstract get addressPrefix(): string;
29
31
 
30
- abstract getAddressPrefix(): Promise<string>;
31
32
  abstract getKnownScript(
32
33
  script: KnownScript,
33
34
  ): Promise<Pick<Script, "codeHash" | "hashType">>;
@@ -121,10 +122,10 @@ export abstract class Client {
121
122
  }
122
123
 
123
124
  async getBalance(locks: ScriptLike[]): Promise<Num> {
124
- return locks.reduce(
125
- (res: Promise<Num>, lock) =>
126
- this.getBalanceSingle(lock).then((a) => res.then((b) => a + b)),
127
- Promise.resolve(Zero),
125
+ return reduceAsync(
126
+ locks,
127
+ async (acc, lock) => acc + (await this.getBalanceSingle(lock)),
128
+ Zero,
128
129
  );
129
130
  }
130
131
  }
@@ -8,7 +8,7 @@ export class ClientPublicMainnet extends ClientJsonRpc {
8
8
  super(url, timeout);
9
9
  }
10
10
 
11
- async getAddressPrefix(): Promise<string> {
11
+ get addressPrefix(): string {
12
12
  return "ckb";
13
13
  }
14
14
 
@@ -8,7 +8,7 @@ export class ClientPublicTestnet extends ClientJsonRpc {
8
8
  super(url, timeout);
9
9
  }
10
10
 
11
- async getAddressPrefix(): Promise<string> {
11
+ get addressPrefix(): string {
12
12
  return "ckt";
13
13
  }
14
14
 
@@ -49,7 +49,7 @@ export abstract class ClientJsonRpc extends Client {
49
49
  */
50
50
 
51
51
  constructor(
52
- private readonly url: string,
52
+ private readonly url_: string,
53
53
  private readonly timeout = 30000,
54
54
  ) {
55
55
  super();
@@ -61,8 +61,8 @@ export abstract class ClientJsonRpc extends Client {
61
61
  * @returns The URL of the JSON-RPC server.
62
62
  */
63
63
 
64
- getUrl() {
65
- return this.url;
64
+ get url() {
65
+ return this.url_;
66
66
  }
67
67
 
68
68
  /**
@@ -1,2 +1,3 @@
1
1
  export * from "./signerBtc";
2
2
  export * from "./signerBtcPublicKeyReadonly";
3
+ export * from "./verify";
@@ -6,7 +6,7 @@ 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 { Signer } from "../signer";
9
+ import { Signer, SignerSignType, SignerType } from "../signer";
10
10
 
11
11
  /**
12
12
  * An abstract class extending the Signer class for Bitcoin-like signing operations.
@@ -14,6 +14,14 @@ import { Signer } from "../signer";
14
14
  * as well as signing transactions.
15
15
  */
16
16
  export abstract class SignerBtc extends Signer {
17
+ get type(): SignerType {
18
+ return SignerType.BTC;
19
+ }
20
+
21
+ get signType(): SignerSignType {
22
+ return SignerSignType.BtcEcdsa;
23
+ }
24
+
17
25
  /**
18
26
  * Gets the Bitcoin account associated with the signer.
19
27
  *
@@ -37,6 +45,15 @@ export abstract class SignerBtc extends Signer {
37
45
  return this.getBtcAccount();
38
46
  }
39
47
 
48
+ /**
49
+ * Gets the identity, which is the Bitcoin public key in this case.
50
+ *
51
+ * @returns A promise that resolves to a string representing the identity
52
+ */
53
+ async getIdentity(): Promise<string> {
54
+ return hexFrom(await this.getBtcPublicKey()).slice(2);
55
+ }
56
+
40
57
  /**
41
58
  * Gets an array of Address objects representing the known script addresses for the signer.
42
59
  *
@@ -62,12 +79,10 @@ export abstract class SignerBtc extends Signer {
62
79
  * @returns A promise that resolves to the prepared Transaction object.
63
80
  */
64
81
  async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
82
+ const tx = Transaction.from(txLike);
65
83
  const { script } = await this.getRecommendedAddressObj();
66
- return Transaction.from(txLike).prepareSighashAllWitness(
67
- script,
68
- 85,
69
- this.client,
70
- );
84
+ await tx.prepareSighashAllWitness(script, 85, this.client);
85
+ return tx;
71
86
  }
72
87
 
73
88
  /**
@@ -85,7 +100,7 @@ export abstract class SignerBtc extends Signer {
85
100
  }
86
101
 
87
102
  const signature = bytesFrom(
88
- await this.signMessage(
103
+ await this.signMessageRaw(
89
104
  `CKB (Bitcoin Layer) transaction: ${info.message}`,
90
105
  ),
91
106
  "base64",
@@ -26,15 +26,6 @@ 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
-
38
29
  /**
39
30
  * Connects to the client. This implementation does nothing as the class is read-only.
40
31
  *
@@ -0,0 +1,14 @@
1
+ import { verifyMessage } from "@unisat/wallet-utils";
2
+ import { BytesLike } from "../../bytes";
3
+ import { hexFrom } from "../../hex";
4
+
5
+ export function verifyMessageBtcEcdsa(
6
+ message: string | BytesLike,
7
+ signature: string,
8
+ publicKey: string,
9
+ ): boolean {
10
+ const challenge =
11
+ typeof message === "string" ? message : hexFrom(message).slice(2);
12
+
13
+ return verifyMessage(publicKey, challenge, signature);
14
+ }
@@ -1 +1,2 @@
1
1
  export * from "./signerCkbScriptReadonly";
2
+ export * from "./verifyJoyId";