@toruslabs/ethereum-controllers 9.0.0 → 9.1.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.
- package/dist/lib.cjs/AccountAbstraction/AccountAbstractionController.js +18 -20
- package/dist/lib.cjs/Eip5792/walletGetCallsStatus.js +2 -2
- package/dist/lib.cjs/Eip7702/eip7702Utils.js +3 -3
- package/dist/lib.cjs/Gas/GasFeeController.js +2 -3
- package/dist/lib.cjs/Keyring/KeyringController.js +8 -10
- package/dist/lib.cjs/Message/utils.js +3 -6
- package/dist/lib.cjs/Network/NetworkController.js +7 -9
- package/dist/lib.cjs/Network/createEthereumMiddleware.js +373 -285
- package/dist/lib.cjs/Network/createJsonRpcClient.js +21 -21
- package/dist/lib.cjs/Preferences/PreferencesController.js +2 -2
- package/dist/lib.cjs/Transaction/TransactionController.js +13 -13
- package/dist/lib.cjs/Transaction/TransactionGasUtil.js +8 -8
- package/dist/lib.cjs/Transaction/TransactionUtils.js +20 -20
- package/dist/lib.cjs/index.js +9 -0
- package/dist/lib.cjs/types/AccountAbstraction/AccountAbstractionController.d.ts +3 -3
- package/dist/lib.cjs/types/Eip5792/walletSendCalls.d.ts +1 -1
- package/dist/lib.cjs/types/Eip7702/walletUpgradeAccount.d.ts +1 -1
- package/dist/lib.cjs/types/Network/NetworkController.d.ts +1 -1
- package/dist/lib.cjs/types/Network/createEthereumMiddleware.d.ts +28 -90
- package/dist/lib.cjs/types/Network/createJsonRpcClient.d.ts +4 -4
- package/dist/lib.cjs/types/Network/index.d.ts +5 -0
- package/dist/lib.cjs/types/Network/interfaces.d.ts +58 -0
- package/dist/lib.cjs/types/index.d.ts +1 -3
- package/dist/lib.cjs/types/utils/eip5792Types.d.ts +1 -1
- package/dist/lib.cjs/types/utils/eip7702Types.d.ts +1 -1
- package/dist/lib.cjs/types/utils/interfaces.d.ts +2 -2
- package/dist/lib.cjs/utils/conversionUtils.js +2 -2
- package/dist/lib.cjs/utils/helpers.js +4 -4
- package/dist/lib.cjs/utils/transaction.js +2 -2
- package/dist/lib.esm/AccountAbstraction/AccountAbstractionController.js +19 -21
- package/dist/lib.esm/Eip5792/walletGetCallsStatus.js +2 -2
- package/dist/lib.esm/Eip7702/eip7702Utils.js +3 -3
- package/dist/lib.esm/Gas/GasFeeController.js +2 -3
- package/dist/lib.esm/Keyring/KeyringController.js +9 -11
- package/dist/lib.esm/Message/utils.js +4 -7
- package/dist/lib.esm/Network/NetworkController.js +8 -10
- package/dist/lib.esm/Network/createEthereumMiddleware.js +387 -290
- package/dist/lib.esm/Network/createJsonRpcClient.js +22 -22
- package/dist/lib.esm/Preferences/PreferencesController.js +2 -2
- package/dist/lib.esm/Transaction/TransactionController.js +13 -13
- package/dist/lib.esm/Transaction/TransactionGasUtil.js +8 -8
- package/dist/lib.esm/Transaction/TransactionUtils.js +20 -20
- package/dist/lib.esm/index.js +4 -3
- package/dist/lib.esm/utils/conversionUtils.js +2 -2
- package/dist/lib.esm/utils/helpers.js +3 -3
- package/dist/lib.esm/utils/transaction.js +2 -2
- package/package.json +5 -5
- package/dist/lib.cjs/types/utils/hex.d.ts +0 -9
- package/dist/lib.cjs/utils/hex.js +0 -23
- 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
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
25
|
-
|
|
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
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
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
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
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
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
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
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
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 (
|
|
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:
|
|
82
|
-
data:
|
|
174
|
+
from: params[0],
|
|
175
|
+
data: params[1]
|
|
83
176
|
};
|
|
84
177
|
}
|
|
85
|
-
|
|
86
|
-
}
|
|
178
|
+
return processEthSignMessage(msgParams, mutableRequestClone);
|
|
179
|
+
};
|
|
87
180
|
}
|
|
88
181
|
function createProcessTypedMessageV4({
|
|
89
182
|
processTypedMessageV4
|
|
90
183
|
}) {
|
|
91
|
-
return
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
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:
|
|
106
|
-
data:
|
|
202
|
+
from: params[0],
|
|
203
|
+
data: params[1]
|
|
107
204
|
};
|
|
108
205
|
}
|
|
109
|
-
|
|
110
|
-
}
|
|
206
|
+
return processTypedMessageV4(msgParams, mutableRequestClone);
|
|
207
|
+
};
|
|
111
208
|
}
|
|
112
209
|
function createProcessPersonalMessage({
|
|
113
210
|
processPersonalMessage
|
|
114
211
|
}) {
|
|
115
|
-
return
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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:
|
|
140
|
-
data:
|
|
240
|
+
from: params[1],
|
|
241
|
+
data: params[0]
|
|
141
242
|
};
|
|
142
243
|
}
|
|
143
244
|
}
|
|
144
|
-
|
|
145
|
-
}
|
|
245
|
+
return processPersonalMessage(msgParams, mutableRequestClone);
|
|
246
|
+
};
|
|
146
247
|
}
|
|
147
248
|
function createPendingNonceMiddleware({
|
|
148
249
|
getPendingNonce
|
|
149
250
|
}) {
|
|
150
|
-
return
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
if (method !== constants.METHOD_TYPES.ETH_GET_TRANSACTION_COUNT)
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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 (
|
|
242
|
-
|
|
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
|
-
|
|
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
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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
|
-
|
|
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
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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 (
|
|
280
|
-
|
|
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
|
-
|
|
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
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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
|
-
//
|
|
296
|
-
const accounts = await requestAccounts(request);
|
|
297
|
-
|
|
298
|
-
|
|
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
|
|
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
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
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
|
-
|
|
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.
|
|
370
|
-
[eip7702Types.EIP_7702_METHODS.WALLET_UPGRADE_ACCOUNT]:
|
|
371
|
-
|
|
372
|
-
|
|
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
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
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
|
-
|
|
408
|
-
|
|
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.
|
|
426
|
-
[eip5792Types.EIP_5792_METHODS.WALLET_GET_CAPABILITIES]:
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
const
|
|
445
|
-
|
|
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.
|
|
475
|
-
version,
|
|
476
|
-
[baseControllers.PROVIDER_JRPC_METHODS.GET_PROVIDER_STATE]:
|
|
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
|
-
|
|
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;
|