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