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

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 (110) hide show
  1. package/dist/ckb/transaction.d.ts +15 -4
  2. package/dist/ckb/transaction.d.ts.map +1 -1
  3. package/dist/ckb/transaction.js +37 -12
  4. package/dist/client/client.js +1 -1
  5. package/dist.commonjs/address/address.advanced.d.ts +24 -29
  6. package/dist.commonjs/address/address.advanced.js +92 -114
  7. package/dist.commonjs/address/advanced.d.ts +1 -1
  8. package/dist.commonjs/address/advanced.js +14 -30
  9. package/dist.commonjs/address/index.d.ts +44 -51
  10. package/dist.commonjs/address/index.js +68 -87
  11. package/dist.commonjs/advanced.d.ts +1 -1
  12. package/dist.commonjs/advanced.js +22 -47
  13. package/dist.commonjs/advancedBarrel.d.ts +1 -1
  14. package/dist.commonjs/advancedBarrel.js +14 -30
  15. package/dist.commonjs/barrel.d.ts +1 -1
  16. package/dist.commonjs/barrel.js +14 -30
  17. package/dist.commonjs/bytes/advanced.d.ts +2 -11
  18. package/dist.commonjs/bytes/index.d.ts +3 -9
  19. package/dist.commonjs/bytes/index.js +26 -28
  20. package/dist.commonjs/ckb/advanced.d.ts +1 -1
  21. package/dist.commonjs/ckb/advanced.js +22 -47
  22. package/dist.commonjs/ckb/index.d.ts +1 -1
  23. package/dist.commonjs/ckb/index.js +14 -30
  24. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts +260 -420
  25. package/dist.commonjs/ckb/molecule.advanced/generated.js +1332 -1536
  26. package/dist.commonjs/ckb/molecule.advanced/index.d.ts +3 -3
  27. package/dist.commonjs/ckb/molecule.advanced/index.js +18 -34
  28. package/dist.commonjs/ckb/script.advanced.d.ts +1 -1
  29. package/dist.commonjs/ckb/script.advanced.js +9 -12
  30. package/dist.commonjs/ckb/script.d.ts +76 -76
  31. package/dist.commonjs/ckb/script.js +126 -166
  32. package/dist.commonjs/ckb/transaction.advanced.d.ts +1 -1
  33. package/dist.commonjs/ckb/transaction.advanced.js +4 -4
  34. package/dist.commonjs/ckb/transaction.d.ts +543 -570
  35. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  36. package/dist.commonjs/ckb/transaction.js +747 -839
  37. package/dist.commonjs/client/advanced.d.ts +1 -1
  38. package/dist.commonjs/client/advanced.js +14 -30
  39. package/dist.commonjs/client/client.d.ts +21 -52
  40. package/dist.commonjs/client/client.js +57 -66
  41. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +2 -5
  42. package/dist.commonjs/client/clientPublicMainnet.advanced.js +24 -30
  43. package/dist.commonjs/client/clientPublicMainnet.d.ts +4 -6
  44. package/dist.commonjs/client/clientPublicMainnet.js +9 -9
  45. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +2 -5
  46. package/dist.commonjs/client/clientPublicTestnet.advanced.js +24 -30
  47. package/dist.commonjs/client/clientPublicTestnet.d.ts +4 -6
  48. package/dist.commonjs/client/clientPublicTestnet.js +9 -9
  49. package/dist.commonjs/client/clientTypes.d.ts +36 -51
  50. package/dist.commonjs/client/clientTypes.js +19 -34
  51. package/dist.commonjs/client/index.d.ts +1 -1
  52. package/dist.commonjs/client/index.js +14 -30
  53. package/dist.commonjs/client/jsonRpc/advanced.d.ts +1 -1
  54. package/dist.commonjs/client/jsonRpc/advanced.js +14 -30
  55. package/dist.commonjs/client/jsonRpc/index.d.ts +75 -92
  56. package/dist.commonjs/client/jsonRpc/index.js +108 -135
  57. package/dist.commonjs/client/jsonRpc/transformers.d.ts +38 -78
  58. package/dist.commonjs/client/jsonRpc/transformers.js +150 -175
  59. package/dist.commonjs/client/jsonRpc/types.d.ts +38 -38
  60. package/dist.commonjs/fixedPoint/index.d.ts +3 -9
  61. package/dist.commonjs/fixedPoint/index.js +20 -26
  62. package/dist.commonjs/fixedPoint/index.test.d.ts +1 -1
  63. package/dist.commonjs/fixedPoint/index.test.js +36 -42
  64. package/dist.commonjs/hasher/advanced.d.ts +1 -1
  65. package/dist.commonjs/hasher/index.d.ts +36 -36
  66. package/dist.commonjs/hasher/index.js +45 -52
  67. package/dist.commonjs/hex/index.d.ts +1 -1
  68. package/dist.commonjs/hex/index.js +1 -1
  69. package/dist.commonjs/index.d.ts +1 -1
  70. package/dist.commonjs/index.js +22 -47
  71. package/dist.commonjs/num/index.d.ts +1 -1
  72. package/dist.commonjs/num/index.js +19 -30
  73. package/dist.commonjs/signer/btc/index.d.ts +1 -1
  74. package/dist.commonjs/signer/btc/index.js +14 -30
  75. package/dist.commonjs/signer/btc/signerBtc.d.ts +47 -47
  76. package/dist.commonjs/signer/btc/signerBtc.js +65 -85
  77. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +45 -45
  78. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +52 -52
  79. package/dist.commonjs/signer/btc/verify.d.ts +2 -6
  80. package/dist.commonjs/signer/btc/verify.js +2 -5
  81. package/dist.commonjs/signer/ckb/index.d.ts +1 -1
  82. package/dist.commonjs/signer/ckb/index.js +14 -30
  83. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +45 -45
  84. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +56 -56
  85. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts +2 -6
  86. package/dist.commonjs/signer/ckb/verifyJoyId.js +8 -11
  87. package/dist.commonjs/signer/dummy/alwaysError.d.ts +4 -4
  88. package/dist.commonjs/signer/dummy/alwaysError.js +7 -7
  89. package/dist.commonjs/signer/dummy/dummy.d.ts +7 -7
  90. package/dist.commonjs/signer/dummy/dummy.js +16 -16
  91. package/dist.commonjs/signer/dummy/index.d.ts +1 -1
  92. package/dist.commonjs/signer/dummy/index.js +14 -30
  93. package/dist.commonjs/signer/dummy/openLink.d.ts +4 -4
  94. package/dist.commonjs/signer/dummy/openLink.js +7 -7
  95. package/dist.commonjs/signer/evm/index.d.ts +1 -1
  96. package/dist.commonjs/signer/evm/index.js +14 -30
  97. package/dist.commonjs/signer/evm/signerEvm.d.ts +38 -42
  98. package/dist.commonjs/signer/evm/signerEvm.js +68 -100
  99. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +37 -37
  100. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +42 -42
  101. package/dist.commonjs/signer/evm/verify.d.ts +2 -6
  102. package/dist.commonjs/signer/evm/verify.js +2 -7
  103. package/dist.commonjs/signer/index.d.ts +1 -1
  104. package/dist.commonjs/signer/index.js +14 -30
  105. package/dist.commonjs/signer/signer/index.d.ts +153 -159
  106. package/dist.commonjs/signer/signer/index.js +181 -204
  107. package/dist.commonjs/utils/index.d.ts +8 -36
  108. package/dist.commonjs/utils/index.js +12 -18
  109. package/package.json +2 -2
  110. package/src/ckb/transaction.ts +46 -13
@@ -1,36 +1,31 @@
1
1
  "use strict";
2
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;
3
+ exports.SignerInfo = exports.Signer = exports.Signature = exports.SignerType = exports.SignerSignType = void 0;
9
4
  const btc_1 = require("../btc");
10
5
  const verifyJoyId_1 = require("../ckb/verifyJoyId");
11
6
  const verify_1 = require("../evm/verify");
12
7
  var SignerSignType;
13
8
  (function (SignerSignType) {
14
- SignerSignType["Unknown"] = "Unknown";
15
- SignerSignType["BtcEcdsa"] = "BtcEcdsa";
16
- SignerSignType["EvmPersonal"] = "EvmPersonal";
17
- SignerSignType["JoyId"] = "JoyId";
9
+ SignerSignType["Unknown"] = "Unknown";
10
+ SignerSignType["BtcEcdsa"] = "BtcEcdsa";
11
+ SignerSignType["EvmPersonal"] = "EvmPersonal";
12
+ SignerSignType["JoyId"] = "JoyId";
18
13
  })(SignerSignType || (exports.SignerSignType = SignerSignType = {}));
19
14
  /**
20
15
  * An enumeration of signer display types in wallet.
21
16
  */
22
17
  var SignerType;
23
18
  (function (SignerType) {
24
- SignerType["EVM"] = "EVM";
25
- SignerType["BTC"] = "BTC";
26
- SignerType["CKB"] = "CKB";
19
+ SignerType["EVM"] = "EVM";
20
+ SignerType["BTC"] = "BTC";
21
+ SignerType["CKB"] = "CKB";
27
22
  })(SignerType || (exports.SignerType = SignerType = {}));
28
23
  class Signature {
29
- constructor(signature, identity, signType) {
30
- this.signature = signature;
31
- this.identity = identity;
32
- this.signType = signType;
33
- }
24
+ constructor(signature, identity, signType) {
25
+ this.signature = signature;
26
+ this.identity = identity;
27
+ this.signType = signType;
28
+ }
34
29
  }
35
30
  exports.Signature = Signature;
36
31
  /**
@@ -38,197 +33,179 @@ exports.Signature = Signature;
38
33
  * This class provides methods to connect, get addresses, and sign transactions.
39
34
  */
40
35
  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
- }
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
+ }
223
200
  }
224
201
  exports.Signer = Signer;
225
202
  /**
226
203
  * A class representing information about a signer, including its type and the signer instance.
227
204
  */
228
205
  class SignerInfo {
229
- constructor(name, signer) {
230
- this.name = name;
231
- this.signer = signer;
232
- }
206
+ constructor(name, signer) {
207
+ this.name = name;
208
+ this.signer = signer;
209
+ }
233
210
  }
234
211
  exports.SignerInfo = SignerInfo;
@@ -5,10 +5,7 @@
5
5
  * @param value - The value to be transformed.
6
6
  * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
7
7
  */
8
- export declare function apply<T, R>(
9
- transformer: (val: T) => R,
10
- value: undefined,
11
- ): undefined;
8
+ export declare function apply<T, R>(transformer: (val: T) => R, value: undefined): undefined;
12
9
  /**
13
10
  * A type safe way to apply a transformer on a value if it's not empty.
14
11
  *
@@ -16,10 +13,7 @@ export declare function apply<T, R>(
16
13
  * @param value - The value to be transformed.
17
14
  * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
18
15
  */
19
- export declare function apply<T, R>(
20
- transformer: (val: T) => R,
21
- value: null,
22
- ): null;
16
+ export declare function apply<T, R>(transformer: (val: T) => R, value: null): null;
23
17
  /**
24
18
  * A type safe way to apply a transformer on a value if it's not empty.
25
19
  *
@@ -35,10 +29,7 @@ export declare function apply<T, R>(transformer: (val: T) => R, value: T): R;
35
29
  * @param value - The value to be transformed.
36
30
  * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
37
31
  */
38
- export declare function apply<T, R>(
39
- transformer: (val: T) => R,
40
- value: T | undefined,
41
- ): R | undefined;
32
+ export declare function apply<T, R>(transformer: (val: T) => R, value: T | undefined): R | undefined;
42
33
  /**
43
34
  * A type safe way to apply a transformer on a value if it's not empty.
44
35
  *
@@ -46,10 +37,7 @@ export declare function apply<T, R>(
46
37
  * @param value - The value to be transformed.
47
38
  * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
48
39
  */
49
- export declare function apply<T, R>(
50
- transformer: (val: T) => R,
51
- value: T | null,
52
- ): R | null;
40
+ export declare function apply<T, R>(transformer: (val: T) => R, value: T | null): R | null;
53
41
  /**
54
42
  * A type safe way to apply a transformer on a value if it's not empty.
55
43
  *
@@ -57,10 +45,7 @@ export declare function apply<T, R>(
57
45
  * @param value - The value to be transformed.
58
46
  * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
59
47
  */
60
- export declare function apply<T, R>(
61
- transformer: (val: T) => R,
62
- value: undefined | null,
63
- ): undefined | null;
48
+ export declare function apply<T, R>(transformer: (val: T) => R, value: undefined | null): undefined | null;
64
49
  /**
65
50
  * Similar to Array.reduce, but the accumulator can returns Promise.
66
51
  *
@@ -68,13 +53,7 @@ export declare function apply<T, R>(
68
53
  * @param accumulator - A callback to be called for each value. If it returns null, the previous result will be kept.
69
54
  * @returns The accumulated result.
70
55
  */
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>;
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>;
78
57
  /**
79
58
  * Similar to Array.reduce, but the accumulator can returns Promise.
80
59
  *
@@ -83,13 +62,6 @@ export declare function reduceAsync<T, V>(
83
62
  * @param init - The initial value.
84
63
  * @returns The accumulated result.
85
64
  */
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>;
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>;
94
66
  export declare function isWebview(userAgent: string): boolean;
95
- //# sourceMappingURL=index.d.ts.map
67
+ //# sourceMappingURL=index.d.ts.map
@@ -9,30 +9,24 @@ exports.isWebview = exports.reduceAsync = exports.apply = void 0;
9
9
  * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
10
10
  */
11
11
  function apply(transformer, value) {
12
- if (value == null) {
13
- return value;
14
- }
15
- return transformer(value);
12
+ if (value == null) {
13
+ return value;
14
+ }
15
+ return transformer(value);
16
16
  }
17
17
  exports.apply = apply;
18
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");
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);
22
25
  }
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
- );
26
+ return values.reduce((current, b) => current.then((v) => Promise.resolve(accumulator(v, b)).then((r) => r ?? v)), Promise.resolve(init));
33
27
  }
34
28
  exports.reduceAsync = reduceAsync;
35
29
  function isWebview(userAgent) {
36
- return /webview|wv|ip((?!.*Safari)|(?=.*like Safari))/i.test(userAgent);
30
+ return /webview|wv|ip((?!.*Safari)|(?=.*like Safari))/i.test(userAgent);
37
31
  }
38
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.10",
3
+ "version": "0.0.5-alpha.13",
4
4
  "description": "Common Chains Connector Core",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -67,5 +67,5 @@
67
67
  "cross-fetch": "^4.0.0",
68
68
  "ethers": "^6.13.1"
69
69
  },
70
- "gitHead": "91ea3a92b02117905449ce4d2afef88bd3ffcc60"
70
+ "gitHead": "ea98fe4d33d85079871803fcf055ab3f7dc2a87c"
71
71
  }
@@ -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,40 @@ 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);
924
+
925
+ if (!input.cellOutput) {
926
+ throw Error("Unable to resolve inputs info");
927
+ }
928
+
929
+ if (script.eq(input.cellOutput.lock)) {
930
+ return i;
931
+ }
932
+ }
933
+ }
934
+
935
+ /**
936
+ * Find the last occurrence of a input with the specified lock
937
+ *
938
+ * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
939
+ * @param client - The client for complete extra infos in the transaction.
940
+ * @returns A promise that resolves to the prepared transaction
941
+ *
942
+ * @example
943
+ * ```typescript
944
+ * const index = await tx.findLastInputIndexByLock(scriptLike, client);
945
+ * ```
946
+ */
947
+ async findLastInputIndexByLock(
948
+ scriptLike: ScriptLike,
949
+ client: Client,
950
+ ): Promise<number | undefined> {
951
+ const script = Script.from(scriptLike);
952
+
953
+ for (let i = this.inputs.length - 1; i >= 0; i -= 1) {
954
+ const input = this.inputs[i];
955
+ await input.completeExtraInfos(client);
923
956
 
924
957
  if (!input.cellOutput) {
925
958
  throw Error("Unable to resolve inputs info");