@ckb-ccc/core 0.0.5-alpha.1 → 0.0.5-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 (173) 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 +4 -6
  5. package/dist/ckb/transaction.d.ts.map +1 -1
  6. package/dist/ckb/transaction.js +14 -15
  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/signer/{signer.d.ts → 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/index.d.ts +1 -1
  74. package/dist.commonjs/address/index.d.ts.map +1 -1
  75. package/dist.commonjs/address/index.js +4 -4
  76. package/dist.commonjs/ckb/transaction.d.ts +4 -6
  77. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  78. package/dist.commonjs/ckb/transaction.js +14 -15
  79. package/dist.commonjs/client/client.d.ts +2 -2
  80. package/dist.commonjs/client/client.d.ts.map +1 -1
  81. package/dist.commonjs/client/client.js +2 -1
  82. package/dist.commonjs/client/clientPublicMainnet.d.ts +1 -1
  83. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
  84. package/dist.commonjs/client/clientPublicMainnet.js +1 -1
  85. package/dist.commonjs/client/clientPublicTestnet.d.ts +1 -1
  86. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
  87. package/dist.commonjs/client/clientPublicTestnet.js +1 -1
  88. package/dist.commonjs/client/jsonRpc/index.d.ts +3 -3
  89. package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -1
  90. package/dist.commonjs/client/jsonRpc/index.js +4 -4
  91. package/dist.commonjs/signer/btc/index.d.ts +1 -0
  92. package/dist.commonjs/signer/btc/index.d.ts.map +1 -1
  93. package/dist.commonjs/signer/btc/index.js +1 -0
  94. package/dist.commonjs/signer/btc/signerBtc.d.ts +9 -1
  95. package/dist.commonjs/signer/btc/signerBtc.d.ts.map +1 -1
  96. package/dist.commonjs/signer/btc/signerBtc.js +18 -2
  97. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +0 -6
  98. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
  99. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +0 -8
  100. package/dist.commonjs/signer/btc/verify.d.ts +3 -0
  101. package/dist.commonjs/signer/btc/verify.d.ts.map +1 -0
  102. package/dist.commonjs/signer/btc/verify.js +10 -0
  103. package/dist.commonjs/signer/ckb/index.d.ts +1 -0
  104. package/dist.commonjs/signer/ckb/index.d.ts.map +1 -1
  105. package/dist.commonjs/signer/ckb/index.js +1 -0
  106. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +3 -7
  107. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  108. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +7 -9
  109. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts +3 -0
  110. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts.map +1 -0
  111. package/dist.commonjs/signer/ckb/verifyJoyId.js +16 -0
  112. package/dist.commonjs/signer/dummy/alwaysError.d.ts +9 -0
  113. package/dist.commonjs/signer/dummy/alwaysError.d.ts.map +1 -0
  114. package/dist.commonjs/signer/dummy/alwaysError.js +14 -0
  115. package/dist.commonjs/signer/dummy/dummy.d.ts +12 -0
  116. package/dist.commonjs/signer/dummy/dummy.d.ts.map +1 -0
  117. package/dist.commonjs/signer/dummy/dummy.js +23 -0
  118. package/dist.commonjs/signer/dummy/index.d.ts +4 -0
  119. package/dist.commonjs/signer/dummy/index.d.ts.map +1 -0
  120. package/dist.commonjs/signer/dummy/index.js +19 -0
  121. package/dist.commonjs/signer/dummy/openLink.d.ts +9 -0
  122. package/dist.commonjs/signer/dummy/openLink.d.ts.map +1 -0
  123. package/dist.commonjs/signer/dummy/openLink.js +14 -0
  124. package/dist.commonjs/signer/evm/index.d.ts +1 -0
  125. package/dist.commonjs/signer/evm/index.d.ts.map +1 -1
  126. package/dist.commonjs/signer/evm/index.js +1 -0
  127. package/dist.commonjs/signer/evm/signerEvm.d.ts +3 -1
  128. package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -1
  129. package/dist.commonjs/signer/evm/signerEvm.js +9 -3
  130. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +0 -6
  131. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
  132. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +0 -8
  133. package/dist.commonjs/signer/evm/verify.d.ts +3 -0
  134. package/dist.commonjs/signer/evm/verify.d.ts.map +1 -0
  135. package/dist.commonjs/signer/evm/verify.js +10 -0
  136. package/dist.commonjs/signer/index.d.ts +1 -0
  137. package/dist.commonjs/signer/index.d.ts.map +1 -1
  138. package/dist.commonjs/signer/index.js +1 -0
  139. package/dist.commonjs/signer/{signer.d.ts → signer/index.d.ts} +69 -25
  140. package/dist.commonjs/signer/signer/index.d.ts.map +1 -0
  141. package/dist.commonjs/signer/signer/index.js +211 -0
  142. package/dist.commonjs/utils/index.d.ts +60 -0
  143. package/dist.commonjs/utils/index.d.ts.map +1 -1
  144. package/dist.commonjs/utils/index.js +23 -1
  145. package/package.json +6 -3
  146. package/src/address/index.ts +4 -7
  147. package/src/ckb/transaction.ts +18 -18
  148. package/src/client/client.ts +7 -6
  149. package/src/client/clientPublicMainnet.ts +1 -1
  150. package/src/client/clientPublicTestnet.ts +1 -1
  151. package/src/client/jsonRpc/index.ts +3 -3
  152. package/src/signer/btc/index.ts +1 -0
  153. package/src/signer/btc/signerBtc.ts +22 -7
  154. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +0 -9
  155. package/src/signer/btc/verify.ts +14 -0
  156. package/src/signer/ckb/index.ts +1 -0
  157. package/src/signer/ckb/signerCkbScriptReadonly.ts +10 -11
  158. package/src/signer/ckb/verifyJoyId.ts +20 -0
  159. package/src/signer/dummy/alwaysError.ts +17 -0
  160. package/src/signer/dummy/dummy.ts +28 -0
  161. package/src/signer/dummy/index.ts +3 -0
  162. package/src/signer/dummy/openLink.ts +17 -0
  163. package/src/signer/evm/index.ts +1 -0
  164. package/src/signer/evm/signerEvm.ts +16 -10
  165. package/src/signer/evm/signerEvmAddressReadonly.ts +0 -9
  166. package/src/signer/evm/verify.ts +16 -0
  167. package/src/signer/index.ts +1 -0
  168. package/src/signer/{signer.ts → signer/index.ts} +135 -25
  169. package/src/utils/index.ts +108 -0
  170. package/dist/signer/signer.d.ts.map +0 -1
  171. package/dist.commonjs/signer/signer.d.ts.map +0 -1
  172. package/dist.commonjs/signer/signer.js +0 -125
  173. package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +0 -1
@@ -0,0 +1,211 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SignerInfo = exports.Signer = exports.Signature = exports.SignerType = exports.SignerSignType = void 0;
4
+ const btc_1 = require("../btc");
5
+ const verifyJoyId_1 = require("../ckb/verifyJoyId");
6
+ const verify_1 = require("../evm/verify");
7
+ var SignerSignType;
8
+ (function (SignerSignType) {
9
+ SignerSignType["Unknown"] = "Unknown";
10
+ SignerSignType["BtcEcdsa"] = "BtcEcdsa";
11
+ SignerSignType["EvmPersonal"] = "EvmPersonal";
12
+ SignerSignType["JoyId"] = "JoyId";
13
+ })(SignerSignType || (exports.SignerSignType = SignerSignType = {}));
14
+ /**
15
+ * An enumeration of signer display types in wallet.
16
+ */
17
+ var SignerType;
18
+ (function (SignerType) {
19
+ SignerType["EVM"] = "EVM";
20
+ SignerType["BTC"] = "BTC";
21
+ SignerType["CKB"] = "CKB";
22
+ })(SignerType || (exports.SignerType = SignerType = {}));
23
+ class Signature {
24
+ constructor(signature, identity, signType) {
25
+ this.signature = signature;
26
+ this.identity = identity;
27
+ this.signType = signType;
28
+ }
29
+ }
30
+ exports.Signature = Signature;
31
+ /**
32
+ * An abstract class representing a generic signer.
33
+ * This class provides methods to connect, get addresses, and sign transactions.
34
+ */
35
+ class Signer {
36
+ constructor(client_) {
37
+ this.client_ = client_;
38
+ }
39
+ get client() {
40
+ return this.client_;
41
+ }
42
+ static async verifyMessage(message, signature) {
43
+ switch (signature.signType) {
44
+ case SignerSignType.EvmPersonal:
45
+ return (0, verify_1.verifyMessageEvmPersonal)(message, signature.signature, signature.identity);
46
+ case SignerSignType.BtcEcdsa:
47
+ return (0, btc_1.verifyMessageBtcEcdsa)(message, signature.signature, signature.identity);
48
+ case SignerSignType.JoyId:
49
+ return (0, verifyJoyId_1.verifyMessageJoyId)(message, signature.signature, signature.identity);
50
+ case SignerSignType.Unknown:
51
+ throw new Error("Unknown signer sign type");
52
+ }
53
+ }
54
+ /**
55
+ * Replace the current client.
56
+ */
57
+ async replaceClient(client) {
58
+ this.client_ = client;
59
+ }
60
+ /**
61
+ * Disconnects to the signer.
62
+ *
63
+ * @returns A promise that resolves when disconnected.
64
+ */
65
+ async disconnect() { }
66
+ /**
67
+ * Gets the identity for verifying signature, usually it's address
68
+ *
69
+ * @returns A promise that resolves to a string representing the identity
70
+ */
71
+ async getIdentity() {
72
+ return this.getInternalAddress();
73
+ }
74
+ /**
75
+ * Gets the recommended Address object for the signer.
76
+ *
77
+ * @param _preference - Optional preference parameter.
78
+ * @returns A promise that resolves to the recommended Address object.
79
+ */
80
+ async getRecommendedAddressObj(_preference) {
81
+ return (await this.getAddressObjs())[0];
82
+ }
83
+ /**
84
+ * Gets the recommended address for the signer as a string.
85
+ *
86
+ * @param preference - Optional preference parameter.
87
+ * @returns A promise that resolves to the recommended address as a string.
88
+ */
89
+ async getRecommendedAddress(preference) {
90
+ return (await this.getRecommendedAddressObj(preference)).toString();
91
+ }
92
+ /**
93
+ * Gets an array of addresses associated with the signer as strings.
94
+ *
95
+ * @returns A promise that resolves to an array of addresses as strings.
96
+ */
97
+ async getAddresses() {
98
+ return this.getAddressObjs().then((addresses) => addresses.map((address) => address.toString()));
99
+ }
100
+ /**
101
+ * Gets balance of all addresses
102
+ *
103
+ * @returns A promise that resolves to the balance
104
+ */
105
+ async getBalance() {
106
+ return this.client.getBalance((await this.getAddressObjs()).map(({ script }) => script));
107
+ }
108
+ /**
109
+ * Signs a message.
110
+ *
111
+ * @param _ - The message to sign, as a string or BytesLike object.
112
+ * @returns A promise that resolves to the signature info.
113
+ * @throws Will throw an error if not implemented.
114
+ */
115
+ async signMessage(message) {
116
+ return {
117
+ signature: await this.signMessageRaw(message),
118
+ identity: await this.getIdentity(),
119
+ signType: this.signType,
120
+ };
121
+ }
122
+ /**
123
+ * Signs a message and returns signature only. This method is not implemented and should be overridden by subclasses.
124
+ *
125
+ * @param _ - The message to sign, as a string or BytesLike object.
126
+ * @returns A promise that resolves to the signature as a string.
127
+ * @throws Will throw an error if not implemented.
128
+ */
129
+ signMessageRaw(_) {
130
+ throw Error("Signer.signMessageRaw not implemented");
131
+ }
132
+ /**
133
+ * Verify a signature.
134
+ *
135
+ * @param _ - The original message.
136
+ * @param _ - The signature to verify.
137
+ * @returns A promise that resolves to the verification result.
138
+ * @throws Will throw an error if not implemented.
139
+ */
140
+ async verifyMessage(message, signature) {
141
+ if (typeof signature === "string") {
142
+ return this.verifyMessageRaw(message, signature);
143
+ }
144
+ if (signature.identity !== (await this.getIdentity()) ||
145
+ ![SignerSignType.Unknown, this.signType].includes(signature.signType)) {
146
+ return false;
147
+ }
148
+ return this.verifyMessageRaw(message, signature.signature);
149
+ }
150
+ /**
151
+ * Verify a string signature. This method is not implemented and should be overridden by subclasses.
152
+ *
153
+ * @param _0 - The original message.
154
+ * @param _1 - The signature to verify.
155
+ * @returns A promise that resolves to the verification result.
156
+ * @throws Will throw an error if not implemented.
157
+ */
158
+ verifyMessageRaw(_0, _1) {
159
+ throw Error("Signer.verifyMessageRaw not implemented");
160
+ }
161
+ /**
162
+ * Sends a transaction after signing it.
163
+ *
164
+ * @param tx - The transaction to send, represented as a TransactionLike object.
165
+ * @returns A promise that resolves to the transaction hash as a Hex string.
166
+ */
167
+ async sendTransaction(tx) {
168
+ return this.client.sendTransaction(await this.signTransaction(tx));
169
+ }
170
+ /**
171
+ * Signs a transaction.
172
+ *
173
+ * @param tx - The transaction to sign, represented as a TransactionLike object.
174
+ * @returns A promise that resolves to the signed Transaction object.
175
+ */
176
+ async signTransaction(tx) {
177
+ const preparedTx = await this.prepareTransaction(tx);
178
+ return this.signOnlyTransaction(preparedTx);
179
+ }
180
+ /**
181
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
182
+ *
183
+ * @param _ - The transaction to prepare, represented as a TransactionLike object.
184
+ * @returns A promise that resolves to the prepared Transaction object.
185
+ * @throws Will throw an error if not implemented.
186
+ */
187
+ prepareTransaction(_) {
188
+ throw Error("Signer.prepareTransaction not implemented");
189
+ }
190
+ /**
191
+ * Signs a transaction without preparing information for it. This method is not implemented and should be overridden by subclasses.
192
+ *
193
+ * @param _ - The transaction to sign, represented as a TransactionLike object.
194
+ * @returns A promise that resolves to the signed Transaction object.
195
+ * @throws Will throw an error if not implemented.
196
+ */
197
+ signOnlyTransaction(_) {
198
+ throw Error("Signer.signOnlyTransaction not implemented");
199
+ }
200
+ }
201
+ exports.Signer = Signer;
202
+ /**
203
+ * A class representing information about a signer, including its type and the signer instance.
204
+ */
205
+ class SignerInfo {
206
+ constructor(name, signer) {
207
+ this.name = name;
208
+ this.signer = signer;
209
+ }
210
+ }
211
+ exports.SignerInfo = SignerInfo;
@@ -1,7 +1,67 @@
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
+ */
1
8
  export declare function apply<T, R>(transformer: (val: T) => R, value: undefined): undefined;
9
+ /**
10
+ * A type safe way to apply a transformer on a value if it's not empty.
11
+ *
12
+ * @param transformer - The transformer.
13
+ * @param value - The value to be transformed.
14
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
15
+ */
2
16
  export declare function apply<T, R>(transformer: (val: T) => R, value: null): null;
17
+ /**
18
+ * A type safe way to apply a transformer on a value if it's not empty.
19
+ *
20
+ * @param transformer - The transformer.
21
+ * @param value - The value to be transformed.
22
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
23
+ */
3
24
  export declare function apply<T, R>(transformer: (val: T) => R, value: T): R;
25
+ /**
26
+ * A type safe way to apply a transformer on a value if it's not empty.
27
+ *
28
+ * @param transformer - The transformer.
29
+ * @param value - The value to be transformed.
30
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
31
+ */
4
32
  export declare function apply<T, R>(transformer: (val: T) => R, value: T | undefined): R | undefined;
33
+ /**
34
+ * A type safe way to apply a transformer on a value if it's not empty.
35
+ *
36
+ * @param transformer - The transformer.
37
+ * @param value - The value to be transformed.
38
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
39
+ */
5
40
  export declare function apply<T, R>(transformer: (val: T) => R, value: T | null): R | null;
41
+ /**
42
+ * A type safe way to apply a transformer on a value if it's not empty.
43
+ *
44
+ * @param transformer - The transformer.
45
+ * @param value - The value to be transformed.
46
+ * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
47
+ */
6
48
  export declare function apply<T, R>(transformer: (val: T) => R, value: undefined | null): undefined | null;
49
+ /**
50
+ * Similar to Array.reduce, but the accumulator can returns Promise.
51
+ *
52
+ * @param values - The array to be reduced.
53
+ * @param accumulator - A callback to be called for each value. If it returns null, the previous result will be kept.
54
+ * @returns The accumulated result.
55
+ */
56
+ export declare function reduceAsync<T, V>(values: T[], accumulator: (a: T, b: T) => Promise<T | undefined | null | void> | T | undefined | null | void): Promise<T>;
57
+ /**
58
+ * Similar to Array.reduce, but the accumulator can returns Promise.
59
+ *
60
+ * @param values - The array to be reduced.
61
+ * @param accumulator - A callback to be called for each value. If it returns null, the previous result will be kept.
62
+ * @param init - The initial value.
63
+ * @returns The accumulated result.
64
+ */
65
+ export declare function reduceAsync<T, V>(values: V[], accumulator: (a: T, b: V) => Promise<T | undefined | null | void> | T | undefined | null | void, init: T): Promise<T>;
66
+ export declare function isWebview(userAgent: string): boolean;
7
67
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"A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E,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,6 +1,13 @@
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
12
  if (value == null) {
6
13
  return value;
@@ -8,3 +15,18 @@ function apply(transformer, value) {
8
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((current, b) => current.then((v) => Promise.resolve(accumulator(v, b)).then((r) => r ?? v)), Promise.resolve(init));
27
+ }
28
+ exports.reduceAsync = reduceAsync;
29
+ function isWebview(userAgent) {
30
+ return /webview|wv|ip((?!.*Safari)|(?=.*like Safari))/i.test(userAgent);
31
+ }
32
+ 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.11",
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": "91d312cf8246dbe772ed163a8d970e6af6c55368"
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
 
@@ -384,24 +384,23 @@ export class CellInput {
384
384
  * @returns The completed instance.
385
385
  * @example
386
386
  * ```typescript
387
- * if (!cellInput.cellOutput) {
388
- * await cellInput.completeExtraInfos();
389
- * }
387
+ * await cellInput.completeExtraInfos();
390
388
  * ```
391
389
  */
392
390
 
393
- async completeExtraInfos(client: Client): Promise<CellInput> {
391
+ async completeExtraInfos(client: Client): Promise<void> {
394
392
  if (this.cellOutput && this.outputData && this.blockNumber) {
395
- return this;
393
+ return;
396
394
  }
397
395
 
398
396
  const cell = await client.getCell(this.previousOutput);
399
- if (cell) {
400
- this.cellOutput = cell.cellOutput;
401
- this.outputData = cell.outputData;
402
- this.blockNumber = cell.blockNumber;
397
+ if (!cell) {
398
+ return;
403
399
  }
404
- return this;
400
+
401
+ this.cellOutput = cell.cellOutput;
402
+ this.outputData = cell.outputData;
403
+ this.blockNumber = cell.blockNumber;
405
404
  }
406
405
 
407
406
  /**
@@ -867,8 +866,9 @@ export class Transaction {
867
866
  hasher.update(this.hash());
868
867
 
869
868
  for (let i = 0; i < this.witnesses.length; i += 1) {
870
- if (this.inputs[i]) {
871
- const input = await this.inputs[i].completeExtraInfos(client);
869
+ const input = this.inputs[i];
870
+ if (input) {
871
+ await input.completeExtraInfos(client);
872
872
 
873
873
  if (!input.cellOutput) {
874
874
  throw Error("Unable to resolve inputs info");
@@ -919,7 +919,8 @@ export class Transaction {
919
919
  const script = Script.from(scriptLike);
920
920
 
921
921
  for (let i = 0; i < this.inputs.length; i += 1) {
922
- const input = await this.inputs[i].completeExtraInfos(client);
922
+ const input = this.inputs[i];
923
+ await input.completeExtraInfos(client);
923
924
 
924
925
  if (!input.cellOutput) {
925
926
  throw Error("Unable to resolve inputs info");
@@ -961,7 +962,7 @@ export class Transaction {
961
962
  * await tx.setWitnessArgsAt(0, witnessArgs);
962
963
  * ```
963
964
  */
964
- setWitnessArgsAt(index: number, witness: WitnessArgs): Transaction {
965
+ setWitnessArgsAt(index: number, witness: WitnessArgs): void {
965
966
  if (this.witnesses.length < index) {
966
967
  this.witnesses.push(
967
968
  ...Array.from(
@@ -972,7 +973,6 @@ export class Transaction {
972
973
  }
973
974
 
974
975
  this.witnesses[index] = hexFrom(witness.toBytes());
975
- return this;
976
976
  }
977
977
 
978
978
  /**
@@ -992,14 +992,14 @@ export class Transaction {
992
992
  scriptLike: ScriptLike,
993
993
  lockLen: number,
994
994
  client: Client,
995
- ): Promise<Transaction> {
995
+ ): Promise<void> {
996
996
  const position = await this.findInputIndexByLock(scriptLike, client);
997
997
  if (position === undefined) {
998
- return this;
998
+ return;
999
999
  }
1000
1000
 
1001
1001
  const witness = this.getWitnessArgsAt(position) ?? WitnessArgs.from({});
1002
1002
  witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
1003
- return this.setWitnessArgsAt(position, witness);
1003
+ this.setWitnessArgsAt(position, witness);
1004
1004
  }
1005
1005
  }
@@ -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";