@toruslabs/ethereum-controllers 5.5.2 → 5.5.4
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/ethereumControllers.cjs.js +22 -21
- package/dist/ethereumControllers.esm.js +22 -20
- package/dist/ethereumControllers.umd.min.js +1 -2
- package/dist/ethereumControllers.umd.min.js.LICENSE.txt +0 -2
- package/dist/types/Account/AccountTrackerController.d.ts +1 -1
- package/dist/types/utils/interfaces.d.ts +3 -0
- package/package.json +14 -15
- package/dist/ethereumControllers.cjs.js.map +0 -1
- package/dist/ethereumControllers.esm.js.map +0 -1
- package/dist/ethereumControllers.umd.min.js.map +0 -1
- package/src/Account/AccountTrackerController.ts +0 -172
- package/src/Block/PollingBlockTracker.ts +0 -89
- package/src/Currency/CurrencyController.ts +0 -117
- package/src/Gas/GasFeeController.ts +0 -261
- package/src/Gas/IGasFeeController.ts +0 -56
- package/src/Gas/gasUtil.ts +0 -163
- package/src/Keyring/KeyringController.ts +0 -117
- package/src/Message/AbstractMessageController.ts +0 -136
- package/src/Message/AddChainController.ts +0 -73
- package/src/Message/DecryptMessageController.ts +0 -76
- package/src/Message/EncryptionPublicKeyController.ts +0 -75
- package/src/Message/MessageController.ts +0 -74
- package/src/Message/PersonalMessageController.ts +0 -74
- package/src/Message/SwitchChainController.ts +0 -74
- package/src/Message/TypedMessageController.ts +0 -109
- package/src/Message/utils.ts +0 -155
- package/src/Network/NetworkController.ts +0 -184
- package/src/Network/createEthereumMiddleware.ts +0 -475
- package/src/Network/createJsonRpcClient.ts +0 -63
- package/src/Nfts/INftsController.ts +0 -13
- package/src/Nfts/NftHandler.ts +0 -191
- package/src/Nfts/NftsController.ts +0 -216
- package/src/Preferences/PreferencesController.ts +0 -473
- package/src/Tokens/ITokensController.ts +0 -13
- package/src/Tokens/TokenHandler.ts +0 -60
- package/src/Tokens/TokenRatesController.ts +0 -134
- package/src/Tokens/TokensController.ts +0 -273
- package/src/Transaction/NonceTracker.ts +0 -152
- package/src/Transaction/PendingTransactionTracker.ts +0 -235
- package/src/Transaction/TransactionController.ts +0 -558
- package/src/Transaction/TransactionGasUtil.ts +0 -74
- package/src/Transaction/TransactionStateHistoryHelper.ts +0 -41
- package/src/Transaction/TransactionStateManager.ts +0 -315
- package/src/Transaction/TransactionUtils.ts +0 -333
- package/src/index.ts +0 -49
- package/src/utils/abis.ts +0 -677
- package/src/utils/constants.ts +0 -438
- package/src/utils/contractAddresses.ts +0 -19
- package/src/utils/conversionUtils.ts +0 -269
- package/src/utils/helpers.ts +0 -234
- package/src/utils/interfaces.ts +0 -516
|
@@ -1,475 +0,0 @@
|
|
|
1
|
-
import { SignTypedDataVersion } from "@metamask/eth-sig-util";
|
|
2
|
-
import { InPageWalletProviderState, PROVIDER_JRPC_METHODS } from "@toruslabs/base-controllers";
|
|
3
|
-
import {
|
|
4
|
-
createAsyncMiddleware,
|
|
5
|
-
createScaffoldMiddleware,
|
|
6
|
-
JRPCEngineEndCallback,
|
|
7
|
-
JRPCEngineNextCallback,
|
|
8
|
-
JRPCMiddleware,
|
|
9
|
-
JRPCRequest,
|
|
10
|
-
JRPCResponse,
|
|
11
|
-
mergeMiddleware,
|
|
12
|
-
} from "@toruslabs/openlogin-jrpc";
|
|
13
|
-
|
|
14
|
-
import { METHOD_TYPES, TRANSACTION_ENVELOPE_TYPES } from "../utils/constants";
|
|
15
|
-
import {
|
|
16
|
-
AddChainMessageParams,
|
|
17
|
-
BlockParams,
|
|
18
|
-
EncryptionPublicKeyParams,
|
|
19
|
-
EthereumTransactionMeta,
|
|
20
|
-
MessageParams,
|
|
21
|
-
SwitchChainMessageParams,
|
|
22
|
-
TransactionParams,
|
|
23
|
-
TransactionRPCMeta,
|
|
24
|
-
TypedMessageParams,
|
|
25
|
-
UserRequestApprovalParams,
|
|
26
|
-
} from "../utils/interfaces";
|
|
27
|
-
|
|
28
|
-
export interface IProviderHandlers {
|
|
29
|
-
version: string;
|
|
30
|
-
requestAccounts?: (req: JRPCRequest<string[]>) => Promise<string[]>;
|
|
31
|
-
getAccounts: (req: JRPCRequest<string[]>) => Promise<string[]>;
|
|
32
|
-
|
|
33
|
-
// All operations which open popup window or modal should operate with a windowId parameter
|
|
34
|
-
processTransaction?: (txParams: TransactionParams, req: JRPCRequest<TransactionParams> & UserRequestApprovalParams) => Promise<string>;
|
|
35
|
-
|
|
36
|
-
processEthSignMessage?: (msgParams: MessageParams, req: JRPCRequest<unknown> & UserRequestApprovalParams) => Promise<string>;
|
|
37
|
-
processTypedMessage?: (msgParams: TypedMessageParams, req: JRPCRequest<unknown> & UserRequestApprovalParams) => Promise<string>;
|
|
38
|
-
processTypedMessageV3?: (msgParams: TypedMessageParams, req: JRPCRequest<unknown> & UserRequestApprovalParams) => Promise<string>;
|
|
39
|
-
processTypedMessageV4?: (msgParams: TypedMessageParams, req: JRPCRequest<unknown> & UserRequestApprovalParams) => Promise<string>;
|
|
40
|
-
processPersonalMessage?: (msgParams: MessageParams, req: JRPCRequest<unknown> & UserRequestApprovalParams) => Promise<string>;
|
|
41
|
-
|
|
42
|
-
processEncryptionPublicKey?: (msgParams: EncryptionPublicKeyParams, req: JRPCRequest<unknown> & UserRequestApprovalParams) => Promise<string>;
|
|
43
|
-
processDecryptMessage?: (msgParams: MessageParams, req: JRPCRequest<unknown> & UserRequestApprovalParams) => Promise<string>;
|
|
44
|
-
|
|
45
|
-
processSwitchEthereumChain?: (msgParams: SwitchChainMessageParams, req: JRPCRequest<unknown> & UserRequestApprovalParams) => Promise<string>;
|
|
46
|
-
|
|
47
|
-
processAddEthereumChain?: (msgParams: AddChainMessageParams, req: JRPCRequest<unknown> & UserRequestApprovalParams) => Promise<string>;
|
|
48
|
-
|
|
49
|
-
getPendingNonce?: (
|
|
50
|
-
nonceParams: { address: string; blockReference: string },
|
|
51
|
-
req: JRPCRequest<{ address: string; blockReference: string }>
|
|
52
|
-
) => Promise<string>;
|
|
53
|
-
getPendingTransactionByHash?: (hash: string, req: JRPCRequest<string>) => Promise<EthereumTransactionMeta>;
|
|
54
|
-
|
|
55
|
-
getProviderState: (
|
|
56
|
-
req: JRPCRequest<[]>,
|
|
57
|
-
res: JRPCResponse<InPageWalletProviderState>,
|
|
58
|
-
next: JRPCEngineNextCallback,
|
|
59
|
-
end: JRPCEngineEndCallback
|
|
60
|
-
) => void;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function createGetAccountsMiddleware({ getAccounts }: { getAccounts: IProviderHandlers["getAccounts"] }): JRPCMiddleware<never, string[]> {
|
|
64
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
65
|
-
const { method } = request;
|
|
66
|
-
if (method !== METHOD_TYPES.GET_ACCOUNTS) return next();
|
|
67
|
-
|
|
68
|
-
if (!getAccounts) throw new Error("WalletMiddleware - opts.getAccounts not provided");
|
|
69
|
-
|
|
70
|
-
const accounts = await getAccounts(request);
|
|
71
|
-
response.result = accounts;
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function createProcessTransactionMiddleware({
|
|
76
|
-
processTransaction,
|
|
77
|
-
}: {
|
|
78
|
-
processTransaction: IProviderHandlers["processTransaction"];
|
|
79
|
-
}): JRPCMiddleware<TransactionParams, string> {
|
|
80
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
81
|
-
const { method } = request;
|
|
82
|
-
if (method !== METHOD_TYPES.ETH_TRANSACTION) return next();
|
|
83
|
-
|
|
84
|
-
if (!processTransaction) throw new Error("WalletMiddleware - opts.processTransaction not provided");
|
|
85
|
-
|
|
86
|
-
response.result = await processTransaction(request.params, request);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export function createProcessEthSignMessage({
|
|
91
|
-
processEthSignMessage,
|
|
92
|
-
}: {
|
|
93
|
-
processEthSignMessage: IProviderHandlers["processEthSignMessage"];
|
|
94
|
-
}): JRPCMiddleware<unknown, string> {
|
|
95
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
96
|
-
const { method } = request;
|
|
97
|
-
if (method !== METHOD_TYPES.ETH_SIGN) return next();
|
|
98
|
-
|
|
99
|
-
if (!processEthSignMessage) throw new Error("WalletMiddleware - opts.processEthSignMessage not provided");
|
|
100
|
-
if (!request?.params) throw new Error("WalletMiddleware - missing params");
|
|
101
|
-
|
|
102
|
-
let msgParams: MessageParams = request.params as MessageParams;
|
|
103
|
-
|
|
104
|
-
if (Array.isArray(request.params)) {
|
|
105
|
-
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address, message]`);
|
|
106
|
-
|
|
107
|
-
const params = request.params as [string, string];
|
|
108
|
-
const address = params[0];
|
|
109
|
-
const message = params[1];
|
|
110
|
-
|
|
111
|
-
msgParams = {
|
|
112
|
-
from: address,
|
|
113
|
-
data: message,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
response.result = await processEthSignMessage(msgParams, request);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function createProcessTypedMessage({
|
|
122
|
-
processTypedMessage,
|
|
123
|
-
}: {
|
|
124
|
-
processTypedMessage: IProviderHandlers["processTypedMessage"];
|
|
125
|
-
}): JRPCMiddleware<unknown, string> {
|
|
126
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
127
|
-
const { method } = request;
|
|
128
|
-
if (method !== METHOD_TYPES.ETH_SIGN_TYPED_DATA) return next();
|
|
129
|
-
|
|
130
|
-
if (!processTypedMessage) throw new Error("WalletMiddleware - opts.processTypedMessage not provided");
|
|
131
|
-
if (!request?.params) throw new Error("WalletMiddleware - missing params");
|
|
132
|
-
|
|
133
|
-
let msgParams: TypedMessageParams = request.params as TypedMessageParams;
|
|
134
|
-
|
|
135
|
-
if (Array.isArray(request.params)) {
|
|
136
|
-
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [typedData, address]`);
|
|
137
|
-
|
|
138
|
-
const params = request.params as [Record<string, unknown>[], string];
|
|
139
|
-
const message = params[0];
|
|
140
|
-
const address = params[1];
|
|
141
|
-
|
|
142
|
-
msgParams = {
|
|
143
|
-
from: address,
|
|
144
|
-
data: message,
|
|
145
|
-
version: SignTypedDataVersion.V1,
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
response.result = await processTypedMessage(msgParams, request);
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export function createProcessTypedMessageV3({
|
|
154
|
-
processTypedMessageV3,
|
|
155
|
-
}: {
|
|
156
|
-
processTypedMessageV3: IProviderHandlers["processTypedMessageV3"];
|
|
157
|
-
}): JRPCMiddleware<unknown, string> {
|
|
158
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
159
|
-
const { method } = request;
|
|
160
|
-
if (method !== METHOD_TYPES.ETH_SIGN_TYPED_DATA_V3) return next();
|
|
161
|
-
|
|
162
|
-
if (!processTypedMessageV3) throw new Error("WalletMiddleware - opts.processTypedMessageV3 is not provided");
|
|
163
|
-
if (!request?.params) throw new Error("WalletMiddleware - missing params");
|
|
164
|
-
|
|
165
|
-
let msgParams: TypedMessageParams = request.params as TypedMessageParams;
|
|
166
|
-
|
|
167
|
-
if (Array.isArray(request.params)) {
|
|
168
|
-
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address, typedData]`);
|
|
169
|
-
|
|
170
|
-
const params = request.params as [string, string];
|
|
171
|
-
const address = params[0];
|
|
172
|
-
const message = JSON.parse(params[1]);
|
|
173
|
-
|
|
174
|
-
msgParams = {
|
|
175
|
-
from: address,
|
|
176
|
-
data: message,
|
|
177
|
-
version: SignTypedDataVersion.V3,
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
response.result = await processTypedMessageV3(msgParams, request);
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export function createProcessTypedMessageV4({
|
|
186
|
-
processTypedMessageV4,
|
|
187
|
-
}: {
|
|
188
|
-
processTypedMessageV4: IProviderHandlers["processTypedMessageV4"];
|
|
189
|
-
}): JRPCMiddleware<unknown, string> {
|
|
190
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
191
|
-
const { method } = request;
|
|
192
|
-
if (method !== METHOD_TYPES.ETH_SIGN_TYPED_DATA_V4) return next();
|
|
193
|
-
|
|
194
|
-
if (!processTypedMessageV4) throw new Error("WalletMiddleware - opts.processTypedMessageV4 is not provided");
|
|
195
|
-
if (!request?.params) throw new Error("WalletMiddleware - missing params");
|
|
196
|
-
|
|
197
|
-
let msgParams: TypedMessageParams = request.params as TypedMessageParams;
|
|
198
|
-
|
|
199
|
-
if (Array.isArray(request.params)) {
|
|
200
|
-
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address, typedData]`);
|
|
201
|
-
|
|
202
|
-
const params = request.params as [string, string];
|
|
203
|
-
const address = params[0];
|
|
204
|
-
const message = JSON.parse(params[1]);
|
|
205
|
-
|
|
206
|
-
msgParams = {
|
|
207
|
-
from: address,
|
|
208
|
-
data: message,
|
|
209
|
-
version: SignTypedDataVersion.V4,
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
response.result = await processTypedMessageV4(msgParams, request);
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export function createProcessPersonalMessage({
|
|
218
|
-
processPersonalMessage,
|
|
219
|
-
}: {
|
|
220
|
-
processPersonalMessage: IProviderHandlers["processPersonalMessage"];
|
|
221
|
-
}): JRPCMiddleware<unknown, string> {
|
|
222
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
223
|
-
const { method } = request;
|
|
224
|
-
if (method !== METHOD_TYPES.PERSONAL_SIGN) return next();
|
|
225
|
-
|
|
226
|
-
if (!processPersonalMessage) throw new Error("WalletMiddleware - opts.processPersonalMessage is not provided");
|
|
227
|
-
if (!request?.params) throw new Error("WalletMiddleware - missing params");
|
|
228
|
-
|
|
229
|
-
let msgParams: MessageParams = request.params as MessageParams;
|
|
230
|
-
|
|
231
|
-
if (Array.isArray(request.params)) {
|
|
232
|
-
if (!(request.params.length >= 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [message, address]`);
|
|
233
|
-
|
|
234
|
-
const params = request.params as [string, string];
|
|
235
|
-
const message = params[0];
|
|
236
|
-
const address = params[1];
|
|
237
|
-
|
|
238
|
-
msgParams = {
|
|
239
|
-
from: address,
|
|
240
|
-
data: message,
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
response.result = await processPersonalMessage(msgParams, request);
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
export function createPendingNonceMiddleware({
|
|
249
|
-
getPendingNonce,
|
|
250
|
-
}: {
|
|
251
|
-
getPendingNonce: IProviderHandlers["getPendingNonce"];
|
|
252
|
-
}): JRPCMiddleware<{ address: string; blockReference: BlockParams }, string> {
|
|
253
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
254
|
-
const { params, method } = request;
|
|
255
|
-
if (method !== METHOD_TYPES.ETH_GET_TRANSACTION_COUNT) return next();
|
|
256
|
-
|
|
257
|
-
const { blockReference } = params;
|
|
258
|
-
if (blockReference !== "pending") return next();
|
|
259
|
-
response.result = await getPendingNonce(params, request);
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
export function formatTxMetaForRpcResult(txMeta: EthereumTransactionMeta): TransactionRPCMeta {
|
|
264
|
-
const { r, s, v, txReceipt, transaction, transactionHash, accessList } = txMeta;
|
|
265
|
-
const { to, data, nonce, gas, from, value, gasPrice, maxFeePerGas, maxPriorityFeePerGas } = transaction;
|
|
266
|
-
|
|
267
|
-
const formattedTxMeta: TransactionRPCMeta = {
|
|
268
|
-
v,
|
|
269
|
-
r,
|
|
270
|
-
s,
|
|
271
|
-
to,
|
|
272
|
-
gas,
|
|
273
|
-
from,
|
|
274
|
-
hash: transactionHash,
|
|
275
|
-
nonce,
|
|
276
|
-
input: data || "0x",
|
|
277
|
-
value: value || "0x0",
|
|
278
|
-
accessList: accessList || null,
|
|
279
|
-
blockHash: txReceipt?.blockHash || null,
|
|
280
|
-
blockNumber: txReceipt?.blockNumber || null,
|
|
281
|
-
transactionIndex: txReceipt?.transactionIndex || null,
|
|
282
|
-
type: null,
|
|
283
|
-
};
|
|
284
|
-
|
|
285
|
-
if (maxFeePerGas && maxPriorityFeePerGas) {
|
|
286
|
-
formattedTxMeta.maxFeePerGas = maxFeePerGas;
|
|
287
|
-
formattedTxMeta.maxPriorityFeePerGas = maxPriorityFeePerGas;
|
|
288
|
-
formattedTxMeta.type = TRANSACTION_ENVELOPE_TYPES.FEE_MARKET;
|
|
289
|
-
} else {
|
|
290
|
-
formattedTxMeta.gasPrice = gasPrice;
|
|
291
|
-
formattedTxMeta.type = TRANSACTION_ENVELOPE_TYPES.LEGACY;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
return formattedTxMeta;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
export function createPendingTxMiddleware({
|
|
298
|
-
getPendingTransactionByHash,
|
|
299
|
-
}: {
|
|
300
|
-
getPendingTransactionByHash: IProviderHandlers["getPendingTransactionByHash"];
|
|
301
|
-
}): JRPCMiddleware<string, TransactionRPCMeta> {
|
|
302
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
303
|
-
const { params, method } = request;
|
|
304
|
-
if (method !== METHOD_TYPES.ETH_GET_TRANSACTION_BY_HASH) return next();
|
|
305
|
-
|
|
306
|
-
if (!getPendingTransactionByHash) throw new Error("WalletMiddleware - opts.getPendingTransactionByHash not provided");
|
|
307
|
-
|
|
308
|
-
const txMeta = await getPendingTransactionByHash(params, request);
|
|
309
|
-
if (!txMeta) {
|
|
310
|
-
return next();
|
|
311
|
-
}
|
|
312
|
-
response.result = formatTxMetaForRpcResult(txMeta);
|
|
313
|
-
return undefined;
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
export function createProcessEncryptionPublicKeyMiddleware({
|
|
318
|
-
processEncryptionPublicKey,
|
|
319
|
-
}: {
|
|
320
|
-
processEncryptionPublicKey: IProviderHandlers["processEncryptionPublicKey"];
|
|
321
|
-
}): JRPCMiddleware<unknown, string> {
|
|
322
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
323
|
-
const { method } = request;
|
|
324
|
-
if (method !== METHOD_TYPES.ETH_GET_ENCRYPTION_PUBLIC_KEY) return next();
|
|
325
|
-
|
|
326
|
-
if (!processEncryptionPublicKey) throw new Error("WalletMiddleware - opts.processEncryptionPublicKey not provided");
|
|
327
|
-
|
|
328
|
-
if (!request?.params) throw new Error("WalletMiddleware - missing params");
|
|
329
|
-
|
|
330
|
-
let msgParams: EncryptionPublicKeyParams = request.params as EncryptionPublicKeyParams;
|
|
331
|
-
|
|
332
|
-
if (Array.isArray(request.params)) {
|
|
333
|
-
if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address]`);
|
|
334
|
-
|
|
335
|
-
const [address] = request.params as [string];
|
|
336
|
-
msgParams = { data: address, from: address };
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
response.result = await processEncryptionPublicKey(msgParams, request);
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
export function createProcessDecryptMessageMiddleware({
|
|
344
|
-
processDecryptMessage,
|
|
345
|
-
}: {
|
|
346
|
-
processDecryptMessage: IProviderHandlers["processDecryptMessage"];
|
|
347
|
-
}): JRPCMiddleware<unknown, string> {
|
|
348
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
349
|
-
const { method } = request;
|
|
350
|
-
if (method !== METHOD_TYPES.ETH_DECRYPT) return next();
|
|
351
|
-
|
|
352
|
-
if (!processDecryptMessage) throw new Error("WalletMiddleware - opts.processDecryptMessage not provided");
|
|
353
|
-
|
|
354
|
-
if (!request?.params) throw new Error("WalletMiddleware - missing params");
|
|
355
|
-
|
|
356
|
-
let msgParams: MessageParams = request.params as MessageParams;
|
|
357
|
-
|
|
358
|
-
if (Array.isArray(request.params)) {
|
|
359
|
-
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [message, address]`);
|
|
360
|
-
|
|
361
|
-
const [message, address] = request.params as [string, string];
|
|
362
|
-
msgParams = { data: message, from: address };
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
response.result = await processDecryptMessage(msgParams, request);
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
export function createProcessSwitchEthereumChain({
|
|
370
|
-
processSwitchEthereumChain,
|
|
371
|
-
}: {
|
|
372
|
-
processSwitchEthereumChain: IProviderHandlers["processSwitchEthereumChain"];
|
|
373
|
-
}): JRPCMiddleware<unknown, string> {
|
|
374
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
375
|
-
const { method } = request;
|
|
376
|
-
if (method !== METHOD_TYPES.SWITCH_CHAIN) return next();
|
|
377
|
-
|
|
378
|
-
if (!processSwitchEthereumChain) throw new Error("WalletMiddleware - opts.processSwitchEthereumChain not provided");
|
|
379
|
-
if (!request?.params) throw new Error("WalletMiddleware - missing params");
|
|
380
|
-
|
|
381
|
-
let msgParams: SwitchChainMessageParams = request.params as SwitchChainMessageParams;
|
|
382
|
-
|
|
383
|
-
if (Array.isArray(request.params)) {
|
|
384
|
-
if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [data]`);
|
|
385
|
-
|
|
386
|
-
const [message] = request.params as [SwitchChainMessageParams];
|
|
387
|
-
msgParams = message;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
response.result = await processSwitchEthereumChain(msgParams, request);
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
export function createProcessAddEthereumChain({
|
|
395
|
-
processAddEthereumChain,
|
|
396
|
-
}: {
|
|
397
|
-
processAddEthereumChain: IProviderHandlers["processAddEthereumChain"];
|
|
398
|
-
}): JRPCMiddleware<unknown, string> {
|
|
399
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
400
|
-
const { method } = request;
|
|
401
|
-
if (method !== METHOD_TYPES.ADD_CHAIN) return next();
|
|
402
|
-
|
|
403
|
-
if (!processAddEthereumChain) throw new Error("WalletMiddleware - opts.processAddEthereumChain not provided");
|
|
404
|
-
if (!request?.params) throw new Error("WalletMiddleware - missing params");
|
|
405
|
-
|
|
406
|
-
let msgParams: AddChainMessageParams = request.params as AddChainMessageParams;
|
|
407
|
-
|
|
408
|
-
if (Array.isArray(request.params)) {
|
|
409
|
-
if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [data]`);
|
|
410
|
-
|
|
411
|
-
const [message] = request.params as [AddChainMessageParams];
|
|
412
|
-
msgParams = message;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
response.result = await processAddEthereumChain(msgParams, request);
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
export function createRequestAccountsMiddleware({
|
|
420
|
-
requestAccounts,
|
|
421
|
-
}: {
|
|
422
|
-
requestAccounts: IProviderHandlers["requestAccounts"];
|
|
423
|
-
}): JRPCMiddleware<string[], unknown> {
|
|
424
|
-
return createAsyncMiddleware(async (request, response, next) => {
|
|
425
|
-
const { method } = request;
|
|
426
|
-
if (method !== "eth_requestAccounts") return next();
|
|
427
|
-
|
|
428
|
-
if (!requestAccounts) throw new Error("WalletMiddleware - opts.requestAccounts not provided");
|
|
429
|
-
// This calls the UI login function
|
|
430
|
-
const accounts = await requestAccounts(request);
|
|
431
|
-
response.result = accounts;
|
|
432
|
-
return undefined;
|
|
433
|
-
});
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
export function createEthereumMiddleware(providerHandlers: IProviderHandlers): JRPCMiddleware<unknown, unknown> {
|
|
437
|
-
const {
|
|
438
|
-
requestAccounts,
|
|
439
|
-
getAccounts,
|
|
440
|
-
processTransaction,
|
|
441
|
-
processEthSignMessage,
|
|
442
|
-
processTypedMessage,
|
|
443
|
-
processTypedMessageV3,
|
|
444
|
-
processTypedMessageV4,
|
|
445
|
-
processPersonalMessage,
|
|
446
|
-
getPendingNonce,
|
|
447
|
-
getPendingTransactionByHash,
|
|
448
|
-
processEncryptionPublicKey,
|
|
449
|
-
processDecryptMessage,
|
|
450
|
-
processSwitchEthereumChain,
|
|
451
|
-
processAddEthereumChain,
|
|
452
|
-
getProviderState,
|
|
453
|
-
version,
|
|
454
|
-
} = providerHandlers;
|
|
455
|
-
return mergeMiddleware([
|
|
456
|
-
createScaffoldMiddleware({
|
|
457
|
-
version,
|
|
458
|
-
[PROVIDER_JRPC_METHODS.GET_PROVIDER_STATE]: getProviderState as JRPCMiddleware<unknown, unknown>,
|
|
459
|
-
}),
|
|
460
|
-
createRequestAccountsMiddleware({ requestAccounts }) as JRPCMiddleware<unknown, unknown>,
|
|
461
|
-
createGetAccountsMiddleware({ getAccounts }) as JRPCMiddleware<unknown, unknown>,
|
|
462
|
-
createProcessTransactionMiddleware({ processTransaction }) as JRPCMiddleware<unknown, unknown>,
|
|
463
|
-
createProcessEthSignMessage({ processEthSignMessage }) as JRPCMiddleware<unknown, unknown>,
|
|
464
|
-
createProcessTypedMessage({ processTypedMessage }) as JRPCMiddleware<unknown, unknown>,
|
|
465
|
-
createProcessTypedMessageV3({ processTypedMessageV3 }) as JRPCMiddleware<unknown, unknown>,
|
|
466
|
-
createProcessTypedMessageV4({ processTypedMessageV4 }) as JRPCMiddleware<unknown, unknown>,
|
|
467
|
-
createProcessPersonalMessage({ processPersonalMessage }) as JRPCMiddleware<unknown, unknown>,
|
|
468
|
-
createPendingNonceMiddleware({ getPendingNonce }) as JRPCMiddleware<unknown, unknown>,
|
|
469
|
-
createPendingTxMiddleware({ getPendingTransactionByHash }) as JRPCMiddleware<unknown, unknown>,
|
|
470
|
-
createProcessEncryptionPublicKeyMiddleware({ processEncryptionPublicKey }) as JRPCMiddleware<unknown, unknown>,
|
|
471
|
-
createProcessDecryptMessageMiddleware({ processDecryptMessage }) as JRPCMiddleware<unknown, unknown>,
|
|
472
|
-
createProcessSwitchEthereumChain({ processSwitchEthereumChain }) as JRPCMiddleware<unknown, unknown>,
|
|
473
|
-
createProcessAddEthereumChain({ processAddEthereumChain }) as JRPCMiddleware<unknown, unknown>,
|
|
474
|
-
]);
|
|
475
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { createFetchMiddleware, ProviderConfig } from "@toruslabs/base-controllers";
|
|
2
|
-
import {
|
|
3
|
-
JRPCEngineEndCallback,
|
|
4
|
-
JRPCEngineNextCallback,
|
|
5
|
-
JRPCMiddleware,
|
|
6
|
-
JRPCRequest,
|
|
7
|
-
JRPCResponse,
|
|
8
|
-
mergeMiddleware,
|
|
9
|
-
providerFromMiddleware,
|
|
10
|
-
} from "@toruslabs/openlogin-jrpc";
|
|
11
|
-
|
|
12
|
-
import PollingBlockTracker from "../Block/PollingBlockTracker";
|
|
13
|
-
import { EthereumNetworkConfig } from "../utils/interfaces";
|
|
14
|
-
|
|
15
|
-
export function createChainIdMiddleware(chainId: string): JRPCMiddleware<unknown, string> {
|
|
16
|
-
return (req: JRPCRequest<unknown>, res: JRPCResponse<string>, next: JRPCEngineNextCallback, end: JRPCEngineEndCallback) => {
|
|
17
|
-
if (req.method === "eth_chainId") {
|
|
18
|
-
res.result = chainId;
|
|
19
|
-
return end();
|
|
20
|
-
}
|
|
21
|
-
if (req.method === "net_version") {
|
|
22
|
-
// convert to decimal
|
|
23
|
-
res.result = Number.parseInt(chainId, 16).toString(10);
|
|
24
|
-
return end();
|
|
25
|
-
}
|
|
26
|
-
return next();
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function createProviderConfigMiddleware(providerConfig: ProviderConfig): JRPCMiddleware<unknown, ProviderConfig> {
|
|
31
|
-
return (req: JRPCRequest<unknown>, res: JRPCResponse<ProviderConfig>, next: JRPCEngineNextCallback, end: JRPCEngineEndCallback) => {
|
|
32
|
-
if (req.method === "eth_provider_config") {
|
|
33
|
-
res.result = providerConfig;
|
|
34
|
-
return end();
|
|
35
|
-
}
|
|
36
|
-
return next();
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function createJsonRpcClient(
|
|
41
|
-
providerConfig: ProviderConfig,
|
|
42
|
-
networkConfig: EthereumNetworkConfig
|
|
43
|
-
): {
|
|
44
|
-
networkMiddleware: JRPCMiddleware<unknown, unknown>;
|
|
45
|
-
blockTracker: PollingBlockTracker;
|
|
46
|
-
} {
|
|
47
|
-
const { chainId, rpcTarget } = providerConfig;
|
|
48
|
-
const fetchMiddleware = createFetchMiddleware({ rpcTarget });
|
|
49
|
-
const blockProvider = providerFromMiddleware(fetchMiddleware as JRPCMiddleware<unknown, unknown>);
|
|
50
|
-
const blockTracker = new PollingBlockTracker({ config: { ...networkConfig, provider: blockProvider }, state: {} });
|
|
51
|
-
|
|
52
|
-
const networkMiddleware = mergeMiddleware([
|
|
53
|
-
createChainIdMiddleware(chainId) as JRPCMiddleware<unknown, unknown>,
|
|
54
|
-
createProviderConfigMiddleware(providerConfig) as JRPCMiddleware<unknown, unknown>,
|
|
55
|
-
// No need for the following middlewares for web because all browser sessions are quite short lived and each session is limited to scope of a window/tab
|
|
56
|
-
// createBlockRefRewriteMiddleware({ blockTracker }),
|
|
57
|
-
// createBlockCacheMiddleware({ blockTracker }),
|
|
58
|
-
// createInflightCacheMiddleware(),
|
|
59
|
-
// createBlockTrackerInspectorMiddleware({ blockTracker }),
|
|
60
|
-
fetchMiddleware as JRPCMiddleware<unknown, unknown>,
|
|
61
|
-
]);
|
|
62
|
-
return { networkMiddleware, blockTracker };
|
|
63
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { BaseConfig, BaseState } from "@toruslabs/base-controllers";
|
|
2
|
-
|
|
3
|
-
import { CustomNftInfo } from "../utils/interfaces";
|
|
4
|
-
|
|
5
|
-
export interface NftsControllerConfig extends BaseConfig {
|
|
6
|
-
interval?: number;
|
|
7
|
-
selectedAddress: string;
|
|
8
|
-
chainId: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface NftsControllerState extends BaseState {
|
|
12
|
-
nfts: Record<string, CustomNftInfo[]>;
|
|
13
|
-
}
|