@toruslabs/ethereum-controllers 9.0.0 → 9.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/dist/lib.cjs/AccountAbstraction/AccountAbstractionController.js +18 -20
  2. package/dist/lib.cjs/Eip5792/walletGetCallsStatus.js +2 -2
  3. package/dist/lib.cjs/Eip7702/eip7702Utils.js +3 -3
  4. package/dist/lib.cjs/Gas/GasFeeController.js +2 -3
  5. package/dist/lib.cjs/Keyring/KeyringController.js +8 -10
  6. package/dist/lib.cjs/Message/utils.js +3 -6
  7. package/dist/lib.cjs/Network/NetworkController.js +7 -9
  8. package/dist/lib.cjs/Network/createEthereumMiddleware.js +373 -285
  9. package/dist/lib.cjs/Network/createJsonRpcClient.js +22 -21
  10. package/dist/lib.cjs/Preferences/PreferencesController.js +2 -2
  11. package/dist/lib.cjs/Transaction/TransactionController.js +13 -13
  12. package/dist/lib.cjs/Transaction/TransactionGasUtil.js +8 -8
  13. package/dist/lib.cjs/Transaction/TransactionUtils.js +20 -20
  14. package/dist/lib.cjs/index.js +9 -0
  15. package/dist/lib.cjs/types/AccountAbstraction/AccountAbstractionController.d.ts +3 -3
  16. package/dist/lib.cjs/types/Eip5792/walletSendCalls.d.ts +1 -1
  17. package/dist/lib.cjs/types/Eip7702/walletUpgradeAccount.d.ts +1 -1
  18. package/dist/lib.cjs/types/Network/NetworkController.d.ts +1 -1
  19. package/dist/lib.cjs/types/Network/createEthereumMiddleware.d.ts +28 -90
  20. package/dist/lib.cjs/types/Network/createJsonRpcClient.d.ts +4 -4
  21. package/dist/lib.cjs/types/Network/index.d.ts +5 -0
  22. package/dist/lib.cjs/types/Network/interfaces.d.ts +58 -0
  23. package/dist/lib.cjs/types/index.d.ts +1 -3
  24. package/dist/lib.cjs/types/utils/eip5792Types.d.ts +1 -1
  25. package/dist/lib.cjs/types/utils/eip7702Types.d.ts +1 -1
  26. package/dist/lib.cjs/types/utils/interfaces.d.ts +2 -2
  27. package/dist/lib.cjs/utils/conversionUtils.js +2 -2
  28. package/dist/lib.cjs/utils/helpers.js +4 -4
  29. package/dist/lib.cjs/utils/transaction.js +2 -2
  30. package/dist/lib.esm/AccountAbstraction/AccountAbstractionController.js +19 -21
  31. package/dist/lib.esm/Eip5792/walletGetCallsStatus.js +2 -2
  32. package/dist/lib.esm/Eip7702/eip7702Utils.js +3 -3
  33. package/dist/lib.esm/Gas/GasFeeController.js +2 -3
  34. package/dist/lib.esm/Keyring/KeyringController.js +9 -11
  35. package/dist/lib.esm/Message/utils.js +4 -7
  36. package/dist/lib.esm/Network/NetworkController.js +8 -10
  37. package/dist/lib.esm/Network/createEthereumMiddleware.js +387 -290
  38. package/dist/lib.esm/Network/createJsonRpcClient.js +23 -22
  39. package/dist/lib.esm/Preferences/PreferencesController.js +2 -2
  40. package/dist/lib.esm/Transaction/TransactionController.js +13 -13
  41. package/dist/lib.esm/Transaction/TransactionGasUtil.js +8 -8
  42. package/dist/lib.esm/Transaction/TransactionUtils.js +20 -20
  43. package/dist/lib.esm/index.js +4 -3
  44. package/dist/lib.esm/utils/conversionUtils.js +2 -2
  45. package/dist/lib.esm/utils/helpers.js +3 -3
  46. package/dist/lib.esm/utils/transaction.js +2 -2
  47. package/package.json +5 -5
  48. package/dist/lib.cjs/types/utils/hex.d.ts +0 -9
  49. package/dist/lib.cjs/utils/hex.js +0 -23
  50. package/dist/lib.esm/utils/hex.js +0 -21
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
3
4
  var baseControllers = require('@toruslabs/base-controllers');
4
5
  var auth = require('@web3auth/auth');
5
6
  var walletGetCallsStatus = require('../Eip5792/walletGetCallsStatus.js');
@@ -12,117 +13,219 @@ var eip5792Types = require('../utils/eip5792Types.js');
12
13
  var eip7702Types = require('../utils/eip7702Types.js');
13
14
  var transaction = require('../utils/transaction.js');
14
15
 
16
+ function formatTxMetaForRpcResult(txMeta) {
17
+ const {
18
+ r,
19
+ s,
20
+ v,
21
+ txReceipt,
22
+ transaction,
23
+ transactionHash,
24
+ accessList
25
+ } = txMeta;
26
+ const {
27
+ to,
28
+ data,
29
+ nonce,
30
+ gas,
31
+ from,
32
+ value,
33
+ gasPrice,
34
+ maxFeePerGas,
35
+ maxPriorityFeePerGas
36
+ } = transaction;
37
+ const formattedTxMeta = {
38
+ v,
39
+ r,
40
+ s,
41
+ to,
42
+ gas,
43
+ from,
44
+ hash: transactionHash,
45
+ nonce,
46
+ input: data || "0x",
47
+ value: value || "0x0",
48
+ accessList: accessList || null,
49
+ blockHash: (txReceipt === null || txReceipt === void 0 ? void 0 : txReceipt.blockHash) || null,
50
+ blockNumber: (txReceipt === null || txReceipt === void 0 ? void 0 : txReceipt.blockNumber) || null,
51
+ transactionIndex: (txReceipt === null || txReceipt === void 0 ? void 0 : txReceipt.transactionIndex) || null,
52
+ type: null
53
+ };
54
+ if (maxFeePerGas && maxPriorityFeePerGas) {
55
+ formattedTxMeta.maxFeePerGas = maxFeePerGas;
56
+ formattedTxMeta.maxPriorityFeePerGas = maxPriorityFeePerGas;
57
+ formattedTxMeta.type = constants.TRANSACTION_ENVELOPE_TYPES.FEE_MARKET;
58
+ } else {
59
+ formattedTxMeta.gasPrice = gasPrice;
60
+ formattedTxMeta.type = constants.TRANSACTION_ENVELOPE_TYPES.LEGACY;
61
+ }
62
+ return formattedTxMeta;
63
+ }
64
+ function createGetProviderStateMiddleware({
65
+ getProviderState
66
+ }) {
67
+ return async ({
68
+ request,
69
+ next
70
+ }) => {
71
+ if (request.method !== baseControllers.PROVIDER_JRPC_METHODS.GET_PROVIDER_STATE) {
72
+ await next();
73
+ return undefined;
74
+ }
75
+ const result = await getProviderState();
76
+ return result;
77
+ };
78
+ }
15
79
  function createGetAccountsMiddleware({
16
80
  getAccounts
17
81
  }) {
18
- return auth.createAsyncMiddleware(async (request, response, next) => {
19
- const {
20
- method
21
- } = request;
22
- if (method !== constants.METHOD_TYPES.GET_ACCOUNTS) return next();
82
+ return async ({
83
+ request,
84
+ context,
85
+ next
86
+ }) => {
87
+ if (request.method !== constants.METHOD_TYPES.GET_ACCOUNTS) {
88
+ await next();
89
+ return undefined;
90
+ }
23
91
  if (!getAccounts) throw new Error("WalletMiddleware - opts.getAccounts not provided");
24
- const accounts = await getAccounts(request);
25
- response.result = accounts;
26
- });
92
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
93
+ const accounts = await getAccounts(auth.propagateToMutableRequest(request, context));
94
+ return accounts;
95
+ };
27
96
  }
28
97
  function createProcessTransactionMiddleware({
29
98
  processTransaction
30
99
  }) {
31
- return auth.createAsyncMiddleware(async (request, response, next) => {
32
- const {
33
- method
34
- } = request;
35
- if (method !== constants.METHOD_TYPES.ETH_TRANSACTION) return next();
100
+ return async ({
101
+ request,
102
+ context,
103
+ next
104
+ }) => {
105
+ if (request.method !== constants.METHOD_TYPES.ETH_TRANSACTION) {
106
+ await next();
107
+ return undefined;
108
+ }
36
109
  if (!processTransaction) throw new Error("WalletMiddleware - opts.processTransaction not provided");
37
- response.result = await processTransaction(request.params, request);
38
- });
110
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
111
+ const mutableRequestClone = auth.propagateToMutableRequest(request, context);
112
+ const result = await processTransaction(mutableRequestClone.params, mutableRequestClone);
113
+ return result;
114
+ };
39
115
  }
40
116
  function createProcessSignTransactionMiddleware({
41
117
  processSignTransaction
42
118
  }) {
43
- return auth.createAsyncMiddleware(async (request, response, next) => {
44
- const {
45
- method
46
- } = request;
47
- if (method !== constants.METHOD_TYPES.ETH_SIGN_TRANSACTION) return next();
119
+ return async ({
120
+ request,
121
+ context,
122
+ next
123
+ }) => {
124
+ if (request.method !== constants.METHOD_TYPES.ETH_SIGN_TRANSACTION) {
125
+ await next();
126
+ return undefined;
127
+ }
48
128
  if (!processSignTransaction) throw new Error("WalletMiddleware - opts.processSignTransaction not provided");
49
- response.result = await processSignTransaction(request.params, request);
50
- });
129
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
130
+ const mutableRequestClone = auth.propagateToMutableRequest(request, context);
131
+ return await processSignTransaction(mutableRequestClone.params, mutableRequestClone);
132
+ };
51
133
  }
52
134
  function createProcessEstimateUserOperationGasMiddleware({
53
135
  processEstimateUserOperationGas
54
136
  }) {
55
- return auth.createAsyncMiddleware(async (request, response, next) => {
56
- const {
57
- method
58
- } = request;
59
- if (method !== constants.METHOD_TYPES.ETH_ESTIMATE_AA_TRANSACTION_GAS) return next();
137
+ return async ({
138
+ request,
139
+ context,
140
+ next
141
+ }) => {
142
+ if (request.method !== constants.METHOD_TYPES.ETH_ESTIMATE_AA_TRANSACTION_GAS) {
143
+ await next();
144
+ return undefined;
145
+ }
60
146
  if (!processEstimateUserOperationGas) throw new Error("WalletMiddleware - opts.processEstimateUserOperationGas not provided");
61
- response.result = await processEstimateUserOperationGas(request.params, request);
62
- });
147
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
148
+ const mutableReq = auth.propagateToMutableRequest(request, context);
149
+ const txParams = mutableReq.params;
150
+ return processEstimateUserOperationGas(txParams, mutableReq);
151
+ };
63
152
  }
64
153
  function createProcessEthSignMessage({
65
154
  processEthSignMessage
66
155
  }) {
67
- return auth.createAsyncMiddleware(async (request, response, next) => {
68
- const {
69
- method
70
- } = request;
71
- if (method !== constants.METHOD_TYPES.ETH_SIGN) return next();
156
+ return async ({
157
+ request,
158
+ context,
159
+ next
160
+ }) => {
161
+ if (request.method !== constants.METHOD_TYPES.ETH_SIGN) {
162
+ await next();
163
+ return undefined;
164
+ }
72
165
  if (!processEthSignMessage) throw new Error("WalletMiddleware - opts.processEthSignMessage not provided");
73
166
  if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
74
- let msgParams = request.params;
167
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
168
+ const mutableRequestClone = auth.propagateToMutableRequest(request, context);
169
+ let msgParams = mutableRequestClone.params;
75
170
  if (Array.isArray(request.params)) {
76
- if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address, message]`);
171
+ if (request.params.length !== 2) throw new Error(`WalletMiddleware - incorrect params for ${request.method} method. expected [address, message]`);
77
172
  const params = request.params;
78
- const address = params[0];
79
- const message = params[1];
80
173
  msgParams = {
81
- from: address,
82
- data: message
174
+ from: params[0],
175
+ data: params[1]
83
176
  };
84
177
  }
85
- response.result = await processEthSignMessage(msgParams, request);
86
- });
178
+ return processEthSignMessage(msgParams, mutableRequestClone);
179
+ };
87
180
  }
88
181
  function createProcessTypedMessageV4({
89
182
  processTypedMessageV4
90
183
  }) {
91
- return auth.createAsyncMiddleware(async (request, response, next) => {
92
- const {
93
- method
94
- } = request;
95
- if (method !== constants.METHOD_TYPES.ETH_SIGN_TYPED_DATA_V4) return next();
184
+ return async ({
185
+ request,
186
+ context,
187
+ next
188
+ }) => {
189
+ if (request.method !== constants.METHOD_TYPES.ETH_SIGN_TYPED_DATA_V4) {
190
+ await next();
191
+ return undefined;
192
+ }
96
193
  if (!processTypedMessageV4) throw new Error("WalletMiddleware - opts.processTypedMessageV4 is not provided");
97
194
  if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
98
- let msgParams = request.params;
99
- if (Array.isArray(request.params)) {
100
- if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address, typedData]`);
101
- const params = request.params;
102
- const address = params[0];
103
- const message = params[1];
195
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
196
+ const mutableRequestClone = auth.propagateToMutableRequest(request, context);
197
+ let msgParams = mutableRequestClone.params;
198
+ if (Array.isArray(mutableRequestClone.params)) {
199
+ if (mutableRequestClone.params.length !== 2) throw new Error(`WalletMiddleware - incorrect params for ${request.method} method. expected [address, typedData]`);
200
+ const params = mutableRequestClone.params;
104
201
  msgParams = {
105
- from: address,
106
- data: message
202
+ from: params[0],
203
+ data: params[1]
107
204
  };
108
205
  }
109
- response.result = await processTypedMessageV4(msgParams, request);
110
- });
206
+ return processTypedMessageV4(msgParams, mutableRequestClone);
207
+ };
111
208
  }
112
209
  function createProcessPersonalMessage({
113
210
  processPersonalMessage
114
211
  }) {
115
- return auth.createAsyncMiddleware(async (request, response, next) => {
116
- const {
117
- method
118
- } = request;
119
- if (method !== constants.METHOD_TYPES.PERSONAL_SIGN) return next();
212
+ return async ({
213
+ request,
214
+ context,
215
+ next
216
+ }) => {
217
+ if (request.method !== constants.METHOD_TYPES.PERSONAL_SIGN) {
218
+ await next();
219
+ return undefined;
220
+ }
120
221
  if (!processPersonalMessage) throw new Error("WalletMiddleware - opts.processPersonalMessage is not provided");
121
222
  if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
122
- let msgParams = request.params;
123
- if (Array.isArray(request.params)) {
124
- if (!(request.params.length >= 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [message, address]`);
125
- const params = request.params;
223
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
224
+ const mutableRequestClone = auth.propagateToMutableRequest(request, context);
225
+ let msgParams = mutableRequestClone.params;
226
+ if (Array.isArray(mutableRequestClone.params)) {
227
+ if (mutableRequestClone.params.length < 2) throw new Error(`WalletMiddleware - incorrect params for ${mutableRequestClone.method} method. expected [message, address]`);
228
+ const params = mutableRequestClone.params;
126
229
  if (typeof params[0] === "object") {
127
230
  const {
128
231
  challenge,
@@ -133,173 +236,149 @@ function createProcessPersonalMessage({
133
236
  data: challenge
134
237
  };
135
238
  } else {
136
- const message = params[0];
137
- const address = params[1];
138
239
  msgParams = {
139
- from: address,
140
- data: message
240
+ from: params[1],
241
+ data: params[0]
141
242
  };
142
243
  }
143
244
  }
144
- response.result = await processPersonalMessage(msgParams, request);
145
- });
245
+ return processPersonalMessage(msgParams, mutableRequestClone);
246
+ };
146
247
  }
147
248
  function createPendingNonceMiddleware({
148
249
  getPendingNonce
149
250
  }) {
150
- return auth.createAsyncMiddleware(async (request, response, next) => {
151
- const {
152
- params,
153
- method
154
- } = request;
155
- if (method !== constants.METHOD_TYPES.ETH_GET_TRANSACTION_COUNT) return next();
156
- const {
157
- blockReference
158
- } = params;
159
- if (blockReference !== "pending") return next();
160
- response.result = await getPendingNonce(params, request);
161
- });
162
- }
163
- function formatTxMetaForRpcResult(txMeta) {
164
- const {
165
- r,
166
- s,
167
- v,
168
- txReceipt,
169
- transaction,
170
- transactionHash,
171
- accessList
172
- } = txMeta;
173
- const {
174
- to,
175
- data,
176
- nonce,
177
- gas,
178
- from,
179
- value,
180
- gasPrice,
181
- maxFeePerGas,
182
- maxPriorityFeePerGas
183
- } = transaction;
184
- const formattedTxMeta = {
185
- v,
186
- r,
187
- s,
188
- to,
189
- gas,
190
- from,
191
- hash: transactionHash,
192
- nonce,
193
- input: data || "0x",
194
- value: value || "0x0",
195
- accessList: accessList || null,
196
- blockHash: (txReceipt === null || txReceipt === void 0 ? void 0 : txReceipt.blockHash) || null,
197
- blockNumber: (txReceipt === null || txReceipt === void 0 ? void 0 : txReceipt.blockNumber) || null,
198
- transactionIndex: (txReceipt === null || txReceipt === void 0 ? void 0 : txReceipt.transactionIndex) || null,
199
- type: null
251
+ return async ({
252
+ request,
253
+ context,
254
+ next
255
+ }) => {
256
+ if (request.method !== constants.METHOD_TYPES.ETH_GET_TRANSACTION_COUNT) {
257
+ await next();
258
+ return undefined;
259
+ }
260
+ const params = request.params;
261
+ if (params.blockReference !== "pending") {
262
+ await next();
263
+ return undefined;
264
+ }
265
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
266
+ const mutableReq = auth.propagateToMutableRequest(request, context);
267
+ return getPendingNonce(params, mutableReq);
200
268
  };
201
- if (maxFeePerGas && maxPriorityFeePerGas) {
202
- formattedTxMeta.maxFeePerGas = maxFeePerGas;
203
- formattedTxMeta.maxPriorityFeePerGas = maxPriorityFeePerGas;
204
- formattedTxMeta.type = constants.TRANSACTION_ENVELOPE_TYPES.FEE_MARKET;
205
- } else {
206
- formattedTxMeta.gasPrice = gasPrice;
207
- formattedTxMeta.type = constants.TRANSACTION_ENVELOPE_TYPES.LEGACY;
208
- }
209
- return formattedTxMeta;
210
269
  }
211
270
  function createPendingTxMiddleware({
212
271
  getPendingTransactionByHash
213
272
  }) {
214
- return auth.createAsyncMiddleware(async (request, response, next) => {
215
- const {
216
- params,
217
- method
218
- } = request;
219
- if (method !== constants.METHOD_TYPES.ETH_GET_TRANSACTION_BY_HASH) return next();
273
+ return async ({
274
+ request,
275
+ context,
276
+ next
277
+ }) => {
278
+ if (request.method !== constants.METHOD_TYPES.ETH_GET_TRANSACTION_BY_HASH) return next();
220
279
  if (!getPendingTransactionByHash) throw new Error("WalletMiddleware - opts.getPendingTransactionByHash not provided");
221
- const txMeta = await getPendingTransactionByHash(params, request);
222
- if (!txMeta) {
223
- return next();
224
- }
225
- response.result = formatTxMetaForRpcResult(txMeta);
226
- return undefined;
227
- });
280
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
281
+ const mutableReq = auth.propagateToMutableRequest(request, context);
282
+ const txMeta = await getPendingTransactionByHash(request.params, mutableReq);
283
+ if (!txMeta) return next();
284
+ return formatTxMetaForRpcResult(txMeta);
285
+ };
228
286
  }
229
287
  function createProcessSwitchEthereumChain({
230
288
  processSwitchEthereumChain
231
289
  }) {
232
- return auth.createAsyncMiddleware(async (request, response, next) => {
233
- const {
234
- method
235
- } = request;
236
- if (method !== constants.METHOD_TYPES.SWITCH_CHAIN) return next();
290
+ return async ({
291
+ request,
292
+ context,
293
+ next
294
+ }) => {
295
+ if (request.method !== constants.METHOD_TYPES.SWITCH_CHAIN) {
296
+ await next();
297
+ return undefined;
298
+ }
237
299
  if (!processSwitchEthereumChain) throw new Error("WalletMiddleware - opts.processSwitchEthereumChain not provided");
238
300
  if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
239
301
  let msgParams = request.params;
240
302
  if (Array.isArray(request.params)) {
241
- if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [data]`);
242
- const [message] = request.params;
243
- msgParams = message;
303
+ if (request.params.length !== 1) throw new Error(`WalletMiddleware - incorrect params for ${request.method} method. expected [data]`);
304
+ [msgParams] = request.params;
244
305
  }
245
- response.result = await processSwitchEthereumChain(msgParams, request);
246
- });
306
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
307
+ const mutableRequestClone = auth.propagateToMutableRequest(request, context);
308
+ const result = await processSwitchEthereumChain(msgParams, mutableRequestClone);
309
+ return result;
310
+ };
247
311
  }
248
312
  function createProcessWalletSwitchChain({
249
313
  processWalletSwitchChain
250
314
  }) {
251
- return auth.createAsyncMiddleware(async (request, response, next) => {
252
- const {
253
- method
254
- } = request;
255
- if (method !== baseControllers.PROVIDER_JRPC_METHODS.WALLET_SWITCH_CHAIN) return next();
315
+ return async ({
316
+ request,
317
+ context,
318
+ next
319
+ }) => {
320
+ if (request.method !== baseControllers.PROVIDER_JRPC_METHODS.WALLET_SWITCH_CHAIN) {
321
+ await next();
322
+ return undefined;
323
+ }
256
324
  if (!processWalletSwitchChain) throw new Error("WalletMiddleware - opts.processWalletSwitchChain not provided");
257
325
  if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
258
- let msgParams = request.params;
259
- if (Array.isArray(request.params)) {
260
- if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [data]`);
261
- const [message] = request.params;
262
- msgParams = message;
326
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
327
+ const mutableRequestClone = auth.propagateToMutableRequest(request, context);
328
+ let msgParams = mutableRequestClone.params;
329
+ if (Array.isArray(mutableRequestClone.params)) {
330
+ if (mutableRequestClone.params.length !== 1) throw new Error(`WalletMiddleware - incorrect params for ${request.method} method. expected [data]`);
331
+ [msgParams] = mutableRequestClone.params;
263
332
  }
264
- response.result = await processWalletSwitchChain(msgParams, request);
265
- });
333
+ const result = await processWalletSwitchChain(msgParams, mutableRequestClone);
334
+ return result;
335
+ };
266
336
  }
267
337
  function createProcessAddEthereumChain({
268
338
  processAddEthereumChain
269
339
  }) {
270
- return auth.createAsyncMiddleware(async (request, response, next) => {
271
- const {
272
- method
273
- } = request;
274
- if (method !== constants.METHOD_TYPES.ADD_CHAIN) return next();
340
+ return async ({
341
+ request,
342
+ context,
343
+ next
344
+ }) => {
345
+ if (request.method !== constants.METHOD_TYPES.ADD_CHAIN) {
346
+ await next();
347
+ return undefined;
348
+ }
275
349
  if (!processAddEthereumChain) throw new Error("WalletMiddleware - opts.processAddEthereumChain not provided");
276
350
  if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
277
351
  let msgParams = request.params;
278
352
  if (Array.isArray(request.params)) {
279
- if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [data]`);
280
- const [message] = request.params;
281
- msgParams = message;
353
+ if (request.params.length !== 1) throw new Error(`WalletMiddleware - incorrect params for ${request.method} method. expected [data]`);
354
+ [msgParams] = request.params;
282
355
  }
283
- response.result = await processAddEthereumChain(msgParams, request);
284
- });
356
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
357
+ const mutableRequestClone = auth.propagateToMutableRequest(request, context);
358
+ const result = await processAddEthereumChain(msgParams, mutableRequestClone);
359
+ return result;
360
+ };
285
361
  }
286
362
  function createRequestAccountsMiddleware({
287
363
  requestAccounts
288
364
  }) {
289
- return auth.createAsyncMiddleware(async (request, response, next) => {
290
- const {
291
- method
292
- } = request;
293
- if (method !== "eth_requestAccounts") return next();
365
+ return async ({
366
+ request,
367
+ context,
368
+ next
369
+ }) => {
370
+ if (request.method !== "eth_requestAccounts") {
371
+ await next();
372
+ return undefined;
373
+ }
294
374
  if (!requestAccounts) throw new Error("WalletMiddleware - opts.requestAccounts not provided");
295
- // This calls the UI login function
296
- const accounts = await requestAccounts(request);
297
- response.result = accounts;
298
- return undefined;
299
- });
375
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
376
+ const accounts = await requestAccounts(auth.propagateToMutableRequest(request, context));
377
+ return accounts;
378
+ };
300
379
  }
301
380
  /**
302
- * Middleware to handle bundler/paymaster rpc method
381
+ * Middleware to handle bundler/paymaster rpc methods.
303
382
  */
304
383
  function createAAMiddleware({
305
384
  bundlerUrl,
@@ -307,94 +386,86 @@ function createAAMiddleware({
307
386
  providerConfig,
308
387
  analytics
309
388
  }) {
310
- // forward request to bundler/paymaster
311
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
312
- const middlewares = [];
313
- // NOTE: bundler/paymaster rpc only accept number as request id, we need to handle that here if client doesn't handle request id themselves
314
- const idNumberRemapMiddleware = (req, res, next, _end) => {
315
- // skip if not bundler/paymaster methods
316
- if (!constants.BUNDLER_METHOD_TYPES.includes(req.method) && !constants.PAYMASTER_METHOD_TYPES.includes(req.method)) {
317
- return next();
318
- }
319
- const originalId = req.id;
320
- const newId = Math.floor(Math.random() * 1000000);
321
- req.id = newId;
322
- res.id = newId;
323
- next(done => {
324
- req.id = originalId;
325
- res.id = originalId;
326
- done();
327
- });
328
- };
329
389
  const bundlerFetchMiddleware = baseControllers.createFetchMiddleware({
330
390
  rpcTarget: bundlerUrl,
331
391
  providerConfig,
332
392
  analytics,
333
393
  transactionDecoder: transaction.transactionDecoder
334
394
  });
335
- middlewares.push(idNumberRemapMiddleware);
336
- middlewares.push((request, response, next, end) => {
337
- if (!constants.BUNDLER_METHOD_TYPES.includes(request.method)) {
338
- return next();
339
- }
340
- bundlerFetchMiddleware(request, response, next, end);
341
- });
342
- if (paymasterUrl) {
343
- const paymasterFetchMiddleware = baseControllers.createFetchMiddleware({
344
- rpcTarget: paymasterUrl,
345
- providerConfig,
346
- analytics,
347
- transactionDecoder: transaction.transactionDecoder
348
- });
349
- middlewares.push((request, response, next, end) => {
350
- if (!constants.PAYMASTER_METHOD_TYPES.includes(request.method)) {
351
- return next();
352
- }
353
- paymasterFetchMiddleware(request, response, next, end);
395
+ const paymasterFetchMiddleware = paymasterUrl ? baseControllers.createFetchMiddleware({
396
+ rpcTarget: paymasterUrl,
397
+ providerConfig,
398
+ analytics,
399
+ transactionDecoder: transaction.transactionDecoder
400
+ }) : null;
401
+ const idRemapAndBundlerMiddleware = async ({
402
+ request,
403
+ next,
404
+ context
405
+ }) => {
406
+ const isBundlerMethod = constants.BUNDLER_METHOD_TYPES.includes(request.method);
407
+ const isPaymasterMethod = constants.PAYMASTER_METHOD_TYPES.includes(request.method);
408
+ if (!isBundlerMethod && !isPaymasterMethod) return next();
409
+ // Bundler/paymaster RPCs only accept numeric request IDs.
410
+ // Request is Readonly, so we create a shallow copy with a remapped ID.
411
+ const remappedRequest = _objectSpread(_objectSpread({}, request), {}, {
412
+ id: auth.getUniqueId()
354
413
  });
355
- }
356
- return auth.mergeMiddleware(middlewares);
414
+ if (isBundlerMethod) {
415
+ return bundlerFetchMiddleware({
416
+ request: remappedRequest,
417
+ next,
418
+ context
419
+ });
420
+ }
421
+ if (isPaymasterMethod && paymasterFetchMiddleware) {
422
+ return paymasterFetchMiddleware({
423
+ request: remappedRequest,
424
+ next,
425
+ context
426
+ });
427
+ }
428
+ return next();
429
+ };
430
+ return idRemapAndBundlerMiddleware;
357
431
  }
358
432
  /**
359
433
  * Middleware to handle EIP-7702 methods.
360
- *
361
- * @param getEthCode - Function to get the deployment bytecode of an address. (eth_getCode)
362
- * @param processTransaction - Function to process a transaction.
363
- * @returns JRPCMiddleware to handle EIP-7702 methods
364
434
  */
365
435
  function createEip7702Middleware({
366
436
  getEthCode,
367
437
  processTransaction
368
438
  }) {
369
- return auth.createScaffoldMiddleware({
370
- [eip7702Types.EIP_7702_METHODS.WALLET_UPGRADE_ACCOUNT]: auth.createAsyncMiddleware(async (request, response, next) => {
371
- if (request.method !== eip7702Types.EIP_7702_METHODS.WALLET_UPGRADE_ACCOUNT) {
372
- return next();
373
- }
439
+ return auth.createScaffoldMiddlewareV2({
440
+ [eip7702Types.EIP_7702_METHODS.WALLET_UPGRADE_ACCOUNT]: async ({
441
+ request,
442
+ context,
443
+ next
444
+ }) => {
445
+ if (request.method !== eip7702Types.EIP_7702_METHODS.WALLET_UPGRADE_ACCOUNT) return next();
374
446
  if (!getEthCode) throw new Error("WalletMiddleware - opts.getEthCode not provided");
375
447
  if (!processTransaction) throw new Error("WalletMiddleware - opts.processTransaction not provided");
376
- const result = await walletUpgradeAccount.walletUpgradeAccount(request, getEthCode, processTransaction);
377
- response.result = result;
378
- }),
379
- [eip7702Types.EIP_7702_METHODS.WALLET_GET_ACCOUNT_UPGRADE_STATUS]: auth.createAsyncMiddleware(async (request, response, next) => {
380
- if (request.method !== eip7702Types.EIP_7702_METHODS.WALLET_GET_ACCOUNT_UPGRADE_STATUS) {
381
- return next();
382
- }
383
- if (!getEthCode) {
384
- throw new Error("getEthCode not provided");
385
- }
386
- const result = await walletGetUpgradeStatus.walletGetUpgradeStatus(request, getEthCode);
387
- response.result = result;
388
- })
448
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
449
+ const mutableReq = auth.propagateToMutableRequest(request, context);
450
+ const result = await walletUpgradeAccount.walletUpgradeAccount(mutableReq, getEthCode, processTransaction);
451
+ return result;
452
+ },
453
+ [eip7702Types.EIP_7702_METHODS.WALLET_GET_ACCOUNT_UPGRADE_STATUS]: async ({
454
+ request,
455
+ context,
456
+ next
457
+ }) => {
458
+ if (request.method !== eip7702Types.EIP_7702_METHODS.WALLET_GET_ACCOUNT_UPGRADE_STATUS) return next();
459
+ if (!getEthCode) throw new Error("getEthCode not provided");
460
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
461
+ const mutableReq = auth.propagateToMutableRequest(request, context);
462
+ const result = await walletGetUpgradeStatus.walletGetUpgradeStatus(mutableReq, getEthCode);
463
+ return result;
464
+ }
389
465
  });
390
466
  }
391
467
  /**
392
468
  * Middleware to handle EIP-5792 methods.
393
- * Supports wallet capabilities and batch call operations for EIP-7702 upgraded accounts.
394
- *
395
- * @param eip5792Config - Configuration for EIP-5792 handlers.
396
- * @param processTransaction - Function to process a transaction.
397
- * @returns JRPCMiddleware to handle EIP-5792 methods
398
469
  */
399
470
  function createEip5792Middleware({
400
471
  eip5792Config,
@@ -404,46 +475,57 @@ function createEip5792Middleware({
404
475
  getEthCode
405
476
  }) {
406
477
  if (!eip5792Config) {
407
- // Return a pass-through middleware if EIP-5792 is not configured
408
- return (_req, _res, next) => next();
478
+ return async ({
479
+ next
480
+ }) => next();
409
481
  }
410
482
  const {
411
483
  getSupportedChains,
412
484
  getCachedDelegations,
413
485
  updateDelegationCache
414
486
  } = eip5792Config;
415
- // Context for wallet_getCapabilities
416
487
  const getCapabilitiesContext = {
417
488
  getSupportedChains,
418
489
  getCachedDelegations,
419
490
  updateDelegationCache
420
491
  };
421
- // Context for wallet_sendCalls
422
492
  const sendCallsContext = {
423
493
  processTransactionBatch
424
494
  };
425
- return auth.createScaffoldMiddleware({
426
- [eip5792Types.EIP_5792_METHODS.WALLET_GET_CAPABILITIES]: auth.createAsyncMiddleware(async (request, response, next) => {
427
- if (request.method !== eip5792Types.EIP_5792_METHODS.WALLET_GET_CAPABILITIES) {
428
- return next();
429
- }
430
- const result = await walletGetCapabilities.walletGetCapabilities(request, getEthCode, getCapabilitiesContext);
431
- response.result = result;
432
- }),
433
- [eip5792Types.EIP_5792_METHODS.WALLET_SEND_CALLS]: auth.createAsyncMiddleware(async (request, response, next) => {
434
- if (request.method !== eip5792Types.EIP_5792_METHODS.WALLET_SEND_CALLS) {
435
- return next();
436
- }
437
- const result = await walletSendCalls.walletSendCalls(request, getEthCode, sendCallsContext);
438
- response.result = result;
439
- }),
440
- [eip5792Types.EIP_5792_METHODS.WALLET_GET_CALLS_STATUS]: auth.createAsyncMiddleware(async (request, response, next) => {
441
- if (request.method !== eip5792Types.EIP_5792_METHODS.WALLET_GET_CALLS_STATUS) {
442
- return next();
443
- }
444
- const result = walletGetCallsStatus.walletGetCallsStatus(request, getTransactionByBatchId);
445
- response.result = result;
446
- })
495
+ return auth.createScaffoldMiddlewareV2({
496
+ [eip5792Types.EIP_5792_METHODS.WALLET_GET_CAPABILITIES]: async ({
497
+ request,
498
+ context,
499
+ next
500
+ }) => {
501
+ if (request.method !== eip5792Types.EIP_5792_METHODS.WALLET_GET_CAPABILITIES) return next();
502
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
503
+ const mutableReq = auth.propagateToMutableRequest(request, context);
504
+ const result = await walletGetCapabilities.walletGetCapabilities(mutableReq, getEthCode, getCapabilitiesContext);
505
+ return result;
506
+ },
507
+ [eip5792Types.EIP_5792_METHODS.WALLET_SEND_CALLS]: async ({
508
+ request,
509
+ context,
510
+ next
511
+ }) => {
512
+ if (request.method !== eip5792Types.EIP_5792_METHODS.WALLET_SEND_CALLS) return next();
513
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
514
+ const mutableReq = auth.propagateToMutableRequest(request, context);
515
+ const result = await walletSendCalls.walletSendCalls(mutableReq, getEthCode, sendCallsContext);
516
+ return result;
517
+ },
518
+ [eip5792Types.EIP_5792_METHODS.WALLET_GET_CALLS_STATUS]: async ({
519
+ request,
520
+ context,
521
+ next
522
+ }) => {
523
+ if (request.method !== eip5792Types.EIP_5792_METHODS.WALLET_GET_CALLS_STATUS) return next();
524
+ // Propagate V2 request and context into a mutable V1-like request for handler consumption.
525
+ const mutableReq = auth.propagateToMutableRequest(request, context);
526
+ const result = walletGetCallsStatus.walletGetCallsStatus(mutableReq, getTransactionByBatchId);
527
+ return result;
528
+ }
447
529
  });
448
530
  }
449
531
  function createEthereumMiddleware(providerHandlers, providerConfig, analytics) {
@@ -471,9 +553,11 @@ function createEthereumMiddleware(providerHandlers, providerConfig, analytics) {
471
553
  getEthCode,
472
554
  eip5792Config
473
555
  } = providerHandlers;
474
- const middlewares = [auth.createScaffoldMiddleware({
475
- version,
476
- [baseControllers.PROVIDER_JRPC_METHODS.GET_PROVIDER_STATE]: getProviderState
556
+ const middlewares = [auth.createScaffoldMiddlewareV2({
557
+ version: async () => version,
558
+ [baseControllers.PROVIDER_JRPC_METHODS.GET_PROVIDER_STATE]: createGetProviderStateMiddleware({
559
+ getProviderState
560
+ })
477
561
  }), createEip7702Middleware({
478
562
  getEthCode,
479
563
  processTransaction
@@ -510,7 +594,7 @@ function createEthereumMiddleware(providerHandlers, providerConfig, analytics) {
510
594
  }), createProcessAddEthereumChain({
511
595
  processAddEthereumChain
512
596
  })];
513
- if (aaConfig) {
597
+ if (aaConfig && providerConfig) {
514
598
  middlewares.push(createAAMiddleware({
515
599
  bundlerUrl: aaConfig.bundlerUrl,
516
600
  paymasterUrl: aaConfig.paymasterUrl,
@@ -518,7 +602,10 @@ function createEthereumMiddleware(providerHandlers, providerConfig, analytics) {
518
602
  analytics
519
603
  }));
520
604
  }
521
- return auth.mergeMiddleware(middlewares);
605
+ const engine = auth.JRPCEngineV2.create({
606
+ middleware: middlewares
607
+ });
608
+ return engine.asMiddleware();
522
609
  }
523
610
 
524
611
  exports.createAAMiddleware = createAAMiddleware;
@@ -526,6 +613,7 @@ exports.createEip5792Middleware = createEip5792Middleware;
526
613
  exports.createEip7702Middleware = createEip7702Middleware;
527
614
  exports.createEthereumMiddleware = createEthereumMiddleware;
528
615
  exports.createGetAccountsMiddleware = createGetAccountsMiddleware;
616
+ exports.createGetProviderStateMiddleware = createGetProviderStateMiddleware;
529
617
  exports.createPendingNonceMiddleware = createPendingNonceMiddleware;
530
618
  exports.createPendingTxMiddleware = createPendingTxMiddleware;
531
619
  exports.createProcessAddEthereumChain = createProcessAddEthereumChain;