@toruslabs/ethereum-controllers 5.10.1 → 6.0.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/ethereumControllers.cjs.js +114 -432
- package/dist/ethereumControllers.esm.js +63 -351
- package/dist/ethereumControllers.umd.min.js +1 -1
- package/dist/ethereumControllers.umd.min.js.LICENSE.txt +7 -2
- package/dist/lib.cjs/Account/AccountTrackerController.js +160 -0
- package/dist/lib.cjs/Block/PollingBlockTracker.js +85 -0
- package/dist/lib.cjs/Currency/CurrencyController.js +111 -0
- package/dist/lib.cjs/Gas/GasFeeController.js +214 -0
- package/dist/lib.cjs/Gas/gasUtil.js +148 -0
- package/dist/lib.cjs/Keyring/KeyringController.js +93 -0
- package/dist/lib.cjs/Message/AbstractMessageController.js +107 -0
- package/dist/lib.cjs/Message/AddChainController.js +78 -0
- package/dist/lib.cjs/Message/MessageController.js +77 -0
- package/dist/lib.cjs/Message/PersonalMessageController.js +77 -0
- package/dist/lib.cjs/Message/SwitchChainController.js +78 -0
- package/dist/lib.cjs/Message/TypedMessageController.js +81 -0
- package/dist/lib.cjs/Message/utils.js +112 -0
- package/dist/lib.cjs/Network/NetworkController.js +201 -0
- package/dist/lib.cjs/Network/cacheIdentifier.js +112 -0
- package/dist/lib.cjs/Network/createEthereumMiddleware.js +302 -0
- package/dist/lib.cjs/Network/createJsonRpcClient.js +64 -0
- package/dist/lib.cjs/Nfts/NftHandler.js +180 -0
- package/dist/lib.cjs/Nfts/NftsController.js +213 -0
- package/dist/lib.cjs/Preferences/PreferencesController.js +476 -0
- package/dist/lib.cjs/Tokens/TokenHandler.js +51 -0
- package/dist/lib.cjs/Tokens/TokenRatesController.js +112 -0
- package/dist/lib.cjs/Tokens/TokensController.js +259 -0
- package/dist/lib.cjs/Transaction/NonceTracker.js +150 -0
- package/dist/lib.cjs/Transaction/PendingTransactionTracker.js +222 -0
- package/dist/lib.cjs/Transaction/TransactionController.js +515 -0
- package/dist/lib.cjs/Transaction/TransactionGasUtil.js +81 -0
- package/dist/lib.cjs/Transaction/TransactionStateHistoryHelper.js +42 -0
- package/dist/lib.cjs/Transaction/TransactionStateManager.js +296 -0
- package/dist/lib.cjs/Transaction/TransactionUtils.js +341 -0
- package/dist/lib.cjs/index.js +171 -0
- package/dist/lib.cjs/utils/abis.js +510 -0
- package/dist/lib.cjs/utils/constants.js +362 -0
- package/dist/lib.cjs/utils/contractAddresses.js +16 -0
- package/dist/lib.cjs/utils/conversionUtils.js +232 -0
- package/dist/lib.cjs/utils/helpers.js +244 -0
- package/dist/lib.cjs/utils/lodashUtils.js +25 -0
- package/dist/lib.esm/Account/AccountTrackerController.js +158 -0
- package/dist/lib.esm/Block/PollingBlockTracker.js +83 -0
- package/dist/lib.esm/Currency/CurrencyController.js +109 -0
- package/dist/lib.esm/Gas/GasFeeController.js +212 -0
- package/dist/lib.esm/Gas/gasUtil.js +141 -0
- package/dist/lib.esm/Keyring/KeyringController.js +91 -0
- package/dist/lib.esm/Message/AbstractMessageController.js +105 -0
- package/dist/lib.esm/Message/AddChainController.js +76 -0
- package/dist/lib.esm/Message/MessageController.js +75 -0
- package/dist/lib.esm/Message/PersonalMessageController.js +75 -0
- package/dist/lib.esm/Message/SwitchChainController.js +76 -0
- package/dist/lib.esm/Message/TypedMessageController.js +79 -0
- package/dist/lib.esm/Message/utils.js +105 -0
- package/dist/lib.esm/Network/NetworkController.js +199 -0
- package/dist/lib.esm/Network/cacheIdentifier.js +107 -0
- package/dist/lib.esm/Network/createEthereumMiddleware.js +289 -0
- package/dist/lib.esm/Network/createJsonRpcClient.js +60 -0
- package/dist/lib.esm/Nfts/NftHandler.js +178 -0
- package/dist/lib.esm/Nfts/NftsController.js +211 -0
- package/dist/lib.esm/Preferences/PreferencesController.js +474 -0
- package/dist/lib.esm/Tokens/TokenHandler.js +49 -0
- package/dist/lib.esm/Tokens/TokenRatesController.js +109 -0
- package/dist/lib.esm/Tokens/TokensController.js +257 -0
- package/dist/lib.esm/Transaction/NonceTracker.js +148 -0
- package/dist/lib.esm/Transaction/PendingTransactionTracker.js +220 -0
- package/dist/lib.esm/Transaction/TransactionController.js +513 -0
- package/dist/lib.esm/Transaction/TransactionGasUtil.js +79 -0
- package/dist/lib.esm/Transaction/TransactionStateHistoryHelper.js +38 -0
- package/dist/lib.esm/Transaction/TransactionStateManager.js +294 -0
- package/dist/lib.esm/Transaction/TransactionUtils.js +326 -0
- package/dist/lib.esm/index.js +33 -0
- package/dist/lib.esm/utils/abis.js +505 -0
- package/dist/lib.esm/utils/constants.js +323 -0
- package/dist/lib.esm/utils/contractAddresses.js +14 -0
- package/dist/lib.esm/utils/conversionUtils.js +218 -0
- package/dist/lib.esm/utils/helpers.js +227 -0
- package/dist/lib.esm/utils/lodashUtils.js +21 -0
- package/dist/types/Account/AccountTrackerController.d.ts +5 -5
- package/dist/types/Block/PollingBlockTracker.d.ts +1 -2
- package/dist/types/Currency/CurrencyController.d.ts +1 -1
- package/dist/types/Gas/GasFeeController.d.ts +3 -3
- package/dist/types/Gas/gasUtil.d.ts +1 -1
- package/dist/types/Keyring/KeyringController.d.ts +3 -5
- package/dist/types/Message/AbstractMessageController.d.ts +5 -6
- package/dist/types/Message/AddChainController.d.ts +4 -4
- package/dist/types/Message/MessageController.d.ts +4 -4
- package/dist/types/Message/PersonalMessageController.d.ts +4 -4
- package/dist/types/Message/SwitchChainController.d.ts +4 -4
- package/dist/types/Message/TypedMessageController.d.ts +6 -7
- package/dist/types/Message/utils.d.ts +2 -7
- package/dist/types/Network/NetworkController.d.ts +4 -4
- package/dist/types/Network/cacheIdentifier.d.ts +1 -1
- package/dist/types/Network/createEthereumMiddleware.d.ts +2 -18
- package/dist/types/Network/createJsonRpcClient.d.ts +2 -2
- package/dist/types/Nfts/NftsController.d.ts +2 -2
- package/dist/types/Preferences/PreferencesController.d.ts +4 -4
- package/dist/types/Tokens/TokensController.d.ts +3 -3
- package/dist/types/Transaction/NonceTracker.d.ts +5 -5
- package/dist/types/Transaction/PendingTransactionTracker.d.ts +5 -5
- package/dist/types/Transaction/TransactionController.d.ts +12 -12
- package/dist/types/Transaction/TransactionGasUtil.d.ts +4 -4
- package/dist/types/Transaction/TransactionStateManager.d.ts +3 -3
- package/dist/types/Transaction/TransactionUtils.d.ts +1 -1
- package/dist/types/index.d.ts +12 -14
- package/dist/types/utils/constants.d.ts +1 -5
- package/dist/types/utils/helpers.d.ts +7 -4
- package/dist/types/utils/interfaces.d.ts +43 -23
- package/package.json +22 -10
- package/dist/types/Message/DecryptMessageController.d.ts +0 -20
- package/dist/types/Message/EncryptionPublicKeyController.d.ts +0 -20
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var baseControllers = require('@toruslabs/base-controllers');
|
|
4
|
+
var auth = require('@web3auth/auth');
|
|
5
|
+
var constants = require('../utils/constants.js');
|
|
6
|
+
|
|
7
|
+
function createGetAccountsMiddleware({
|
|
8
|
+
getAccounts
|
|
9
|
+
}) {
|
|
10
|
+
return auth.createAsyncMiddleware(async (request, response, next) => {
|
|
11
|
+
const {
|
|
12
|
+
method
|
|
13
|
+
} = request;
|
|
14
|
+
if (method !== constants.METHOD_TYPES.GET_ACCOUNTS) return next();
|
|
15
|
+
if (!getAccounts) throw new Error("WalletMiddleware - opts.getAccounts not provided");
|
|
16
|
+
const accounts = await getAccounts(request);
|
|
17
|
+
response.result = accounts;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function createProcessTransactionMiddleware({
|
|
21
|
+
processTransaction
|
|
22
|
+
}) {
|
|
23
|
+
return auth.createAsyncMiddleware(async (request, response, next) => {
|
|
24
|
+
const {
|
|
25
|
+
method
|
|
26
|
+
} = request;
|
|
27
|
+
if (method !== constants.METHOD_TYPES.ETH_TRANSACTION) return next();
|
|
28
|
+
if (!processTransaction) throw new Error("WalletMiddleware - opts.processTransaction not provided");
|
|
29
|
+
response.result = await processTransaction(request.params, request);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function createProcessEthSignMessage({
|
|
33
|
+
processEthSignMessage
|
|
34
|
+
}) {
|
|
35
|
+
return auth.createAsyncMiddleware(async (request, response, next) => {
|
|
36
|
+
const {
|
|
37
|
+
method
|
|
38
|
+
} = request;
|
|
39
|
+
if (method !== constants.METHOD_TYPES.ETH_SIGN) return next();
|
|
40
|
+
if (!processEthSignMessage) throw new Error("WalletMiddleware - opts.processEthSignMessage not provided");
|
|
41
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
42
|
+
let msgParams = request.params;
|
|
43
|
+
if (Array.isArray(request.params)) {
|
|
44
|
+
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address, message]`);
|
|
45
|
+
const params = request.params;
|
|
46
|
+
const address = params[0];
|
|
47
|
+
const message = params[1];
|
|
48
|
+
msgParams = {
|
|
49
|
+
from: address,
|
|
50
|
+
data: message
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
response.result = await processEthSignMessage(msgParams, request);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function createProcessTypedMessageV4({
|
|
57
|
+
processTypedMessageV4
|
|
58
|
+
}) {
|
|
59
|
+
return auth.createAsyncMiddleware(async (request, response, next) => {
|
|
60
|
+
const {
|
|
61
|
+
method
|
|
62
|
+
} = request;
|
|
63
|
+
if (method !== constants.METHOD_TYPES.ETH_SIGN_TYPED_DATA_V4) return next();
|
|
64
|
+
if (!processTypedMessageV4) throw new Error("WalletMiddleware - opts.processTypedMessageV4 is not provided");
|
|
65
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
66
|
+
let msgParams = request.params;
|
|
67
|
+
if (Array.isArray(request.params)) {
|
|
68
|
+
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address, typedData]`);
|
|
69
|
+
const params = request.params;
|
|
70
|
+
const address = params[0];
|
|
71
|
+
const message = params[1];
|
|
72
|
+
msgParams = {
|
|
73
|
+
from: address,
|
|
74
|
+
data: message
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
response.result = await processTypedMessageV4(msgParams, request);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function createProcessPersonalMessage({
|
|
81
|
+
processPersonalMessage
|
|
82
|
+
}) {
|
|
83
|
+
return auth.createAsyncMiddleware(async (request, response, next) => {
|
|
84
|
+
const {
|
|
85
|
+
method
|
|
86
|
+
} = request;
|
|
87
|
+
if (method !== constants.METHOD_TYPES.PERSONAL_SIGN) return next();
|
|
88
|
+
if (!processPersonalMessage) throw new Error("WalletMiddleware - opts.processPersonalMessage is not provided");
|
|
89
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
90
|
+
let msgParams = request.params;
|
|
91
|
+
if (Array.isArray(request.params)) {
|
|
92
|
+
if (!(request.params.length >= 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [message, address]`);
|
|
93
|
+
const params = request.params;
|
|
94
|
+
if (typeof params[0] === "object") {
|
|
95
|
+
const {
|
|
96
|
+
challenge,
|
|
97
|
+
address
|
|
98
|
+
} = params[0];
|
|
99
|
+
msgParams = {
|
|
100
|
+
from: address,
|
|
101
|
+
data: challenge
|
|
102
|
+
};
|
|
103
|
+
} else {
|
|
104
|
+
const message = params[0];
|
|
105
|
+
const address = params[1];
|
|
106
|
+
msgParams = {
|
|
107
|
+
from: address,
|
|
108
|
+
data: message
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
response.result = await processPersonalMessage(msgParams, request);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function createPendingNonceMiddleware({
|
|
116
|
+
getPendingNonce
|
|
117
|
+
}) {
|
|
118
|
+
return auth.createAsyncMiddleware(async (request, response, next) => {
|
|
119
|
+
const {
|
|
120
|
+
params,
|
|
121
|
+
method
|
|
122
|
+
} = request;
|
|
123
|
+
if (method !== constants.METHOD_TYPES.ETH_GET_TRANSACTION_COUNT) return next();
|
|
124
|
+
const {
|
|
125
|
+
blockReference
|
|
126
|
+
} = params;
|
|
127
|
+
if (blockReference !== "pending") return next();
|
|
128
|
+
response.result = await getPendingNonce(params, request);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
function formatTxMetaForRpcResult(txMeta) {
|
|
132
|
+
const {
|
|
133
|
+
r,
|
|
134
|
+
s,
|
|
135
|
+
v,
|
|
136
|
+
txReceipt,
|
|
137
|
+
transaction,
|
|
138
|
+
transactionHash,
|
|
139
|
+
accessList
|
|
140
|
+
} = txMeta;
|
|
141
|
+
const {
|
|
142
|
+
to,
|
|
143
|
+
data,
|
|
144
|
+
nonce,
|
|
145
|
+
gas,
|
|
146
|
+
from,
|
|
147
|
+
value,
|
|
148
|
+
gasPrice,
|
|
149
|
+
maxFeePerGas,
|
|
150
|
+
maxPriorityFeePerGas
|
|
151
|
+
} = transaction;
|
|
152
|
+
const formattedTxMeta = {
|
|
153
|
+
v,
|
|
154
|
+
r,
|
|
155
|
+
s,
|
|
156
|
+
to,
|
|
157
|
+
gas,
|
|
158
|
+
from,
|
|
159
|
+
hash: transactionHash,
|
|
160
|
+
nonce,
|
|
161
|
+
input: data || "0x",
|
|
162
|
+
value: value || "0x0",
|
|
163
|
+
accessList: accessList || null,
|
|
164
|
+
blockHash: (txReceipt === null || txReceipt === void 0 ? void 0 : txReceipt.blockHash) || null,
|
|
165
|
+
blockNumber: (txReceipt === null || txReceipt === void 0 ? void 0 : txReceipt.blockNumber) || null,
|
|
166
|
+
transactionIndex: (txReceipt === null || txReceipt === void 0 ? void 0 : txReceipt.transactionIndex) || null,
|
|
167
|
+
type: null
|
|
168
|
+
};
|
|
169
|
+
if (maxFeePerGas && maxPriorityFeePerGas) {
|
|
170
|
+
formattedTxMeta.maxFeePerGas = maxFeePerGas;
|
|
171
|
+
formattedTxMeta.maxPriorityFeePerGas = maxPriorityFeePerGas;
|
|
172
|
+
formattedTxMeta.type = constants.TRANSACTION_ENVELOPE_TYPES.FEE_MARKET;
|
|
173
|
+
} else {
|
|
174
|
+
formattedTxMeta.gasPrice = gasPrice;
|
|
175
|
+
formattedTxMeta.type = constants.TRANSACTION_ENVELOPE_TYPES.LEGACY;
|
|
176
|
+
}
|
|
177
|
+
return formattedTxMeta;
|
|
178
|
+
}
|
|
179
|
+
function createPendingTxMiddleware({
|
|
180
|
+
getPendingTransactionByHash
|
|
181
|
+
}) {
|
|
182
|
+
return auth.createAsyncMiddleware(async (request, response, next) => {
|
|
183
|
+
const {
|
|
184
|
+
params,
|
|
185
|
+
method
|
|
186
|
+
} = request;
|
|
187
|
+
if (method !== constants.METHOD_TYPES.ETH_GET_TRANSACTION_BY_HASH) return next();
|
|
188
|
+
if (!getPendingTransactionByHash) throw new Error("WalletMiddleware - opts.getPendingTransactionByHash not provided");
|
|
189
|
+
const txMeta = await getPendingTransactionByHash(params, request);
|
|
190
|
+
if (!txMeta) {
|
|
191
|
+
return next();
|
|
192
|
+
}
|
|
193
|
+
response.result = formatTxMetaForRpcResult(txMeta);
|
|
194
|
+
return undefined;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
function createProcessSwitchEthereumChain({
|
|
198
|
+
processSwitchEthereumChain
|
|
199
|
+
}) {
|
|
200
|
+
return auth.createAsyncMiddleware(async (request, response, next) => {
|
|
201
|
+
const {
|
|
202
|
+
method
|
|
203
|
+
} = request;
|
|
204
|
+
if (method !== constants.METHOD_TYPES.SWITCH_CHAIN) return next();
|
|
205
|
+
if (!processSwitchEthereumChain) throw new Error("WalletMiddleware - opts.processSwitchEthereumChain not provided");
|
|
206
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
207
|
+
let msgParams = request.params;
|
|
208
|
+
if (Array.isArray(request.params)) {
|
|
209
|
+
if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [data]`);
|
|
210
|
+
const [message] = request.params;
|
|
211
|
+
msgParams = message;
|
|
212
|
+
}
|
|
213
|
+
response.result = await processSwitchEthereumChain(msgParams, request);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
function createProcessAddEthereumChain({
|
|
217
|
+
processAddEthereumChain
|
|
218
|
+
}) {
|
|
219
|
+
return auth.createAsyncMiddleware(async (request, response, next) => {
|
|
220
|
+
const {
|
|
221
|
+
method
|
|
222
|
+
} = request;
|
|
223
|
+
if (method !== constants.METHOD_TYPES.ADD_CHAIN) return next();
|
|
224
|
+
if (!processAddEthereumChain) throw new Error("WalletMiddleware - opts.processAddEthereumChain not provided");
|
|
225
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
226
|
+
let msgParams = request.params;
|
|
227
|
+
if (Array.isArray(request.params)) {
|
|
228
|
+
if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [data]`);
|
|
229
|
+
const [message] = request.params;
|
|
230
|
+
msgParams = message;
|
|
231
|
+
}
|
|
232
|
+
response.result = await processAddEthereumChain(msgParams, request);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function createRequestAccountsMiddleware({
|
|
236
|
+
requestAccounts
|
|
237
|
+
}) {
|
|
238
|
+
return auth.createAsyncMiddleware(async (request, response, next) => {
|
|
239
|
+
const {
|
|
240
|
+
method
|
|
241
|
+
} = request;
|
|
242
|
+
if (method !== "eth_requestAccounts") return next();
|
|
243
|
+
if (!requestAccounts) throw new Error("WalletMiddleware - opts.requestAccounts not provided");
|
|
244
|
+
// This calls the UI login function
|
|
245
|
+
const accounts = await requestAccounts(request);
|
|
246
|
+
response.result = accounts;
|
|
247
|
+
return undefined;
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
function createEthereumMiddleware(providerHandlers) {
|
|
251
|
+
const {
|
|
252
|
+
requestAccounts,
|
|
253
|
+
getAccounts,
|
|
254
|
+
processTransaction,
|
|
255
|
+
processEthSignMessage,
|
|
256
|
+
processTypedMessageV4,
|
|
257
|
+
processPersonalMessage,
|
|
258
|
+
getPendingNonce,
|
|
259
|
+
getPendingTransactionByHash,
|
|
260
|
+
processSwitchEthereumChain,
|
|
261
|
+
processAddEthereumChain,
|
|
262
|
+
getProviderState,
|
|
263
|
+
version
|
|
264
|
+
} = providerHandlers;
|
|
265
|
+
return auth.mergeMiddleware([auth.createScaffoldMiddleware({
|
|
266
|
+
version,
|
|
267
|
+
[baseControllers.PROVIDER_JRPC_METHODS.GET_PROVIDER_STATE]: getProviderState
|
|
268
|
+
}), createRequestAccountsMiddleware({
|
|
269
|
+
requestAccounts
|
|
270
|
+
}), createGetAccountsMiddleware({
|
|
271
|
+
getAccounts
|
|
272
|
+
}), createProcessTransactionMiddleware({
|
|
273
|
+
processTransaction
|
|
274
|
+
}), createProcessEthSignMessage({
|
|
275
|
+
processEthSignMessage
|
|
276
|
+
}), createProcessTypedMessageV4({
|
|
277
|
+
processTypedMessageV4
|
|
278
|
+
}), createProcessPersonalMessage({
|
|
279
|
+
processPersonalMessage
|
|
280
|
+
}), createPendingNonceMiddleware({
|
|
281
|
+
getPendingNonce
|
|
282
|
+
}), createPendingTxMiddleware({
|
|
283
|
+
getPendingTransactionByHash
|
|
284
|
+
}), createProcessSwitchEthereumChain({
|
|
285
|
+
processSwitchEthereumChain
|
|
286
|
+
}), createProcessAddEthereumChain({
|
|
287
|
+
processAddEthereumChain
|
|
288
|
+
})]);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
exports.createEthereumMiddleware = createEthereumMiddleware;
|
|
292
|
+
exports.createGetAccountsMiddleware = createGetAccountsMiddleware;
|
|
293
|
+
exports.createPendingNonceMiddleware = createPendingNonceMiddleware;
|
|
294
|
+
exports.createPendingTxMiddleware = createPendingTxMiddleware;
|
|
295
|
+
exports.createProcessAddEthereumChain = createProcessAddEthereumChain;
|
|
296
|
+
exports.createProcessEthSignMessage = createProcessEthSignMessage;
|
|
297
|
+
exports.createProcessPersonalMessage = createProcessPersonalMessage;
|
|
298
|
+
exports.createProcessSwitchEthereumChain = createProcessSwitchEthereumChain;
|
|
299
|
+
exports.createProcessTransactionMiddleware = createProcessTransactionMiddleware;
|
|
300
|
+
exports.createProcessTypedMessageV4 = createProcessTypedMessageV4;
|
|
301
|
+
exports.createRequestAccountsMiddleware = createRequestAccountsMiddleware;
|
|
302
|
+
exports.formatTxMetaForRpcResult = formatTxMetaForRpcResult;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
4
|
+
var baseControllers = require('@toruslabs/base-controllers');
|
|
5
|
+
var auth = require('@web3auth/auth');
|
|
6
|
+
var PollingBlockTracker = require('../Block/PollingBlockTracker.js');
|
|
7
|
+
var cacheIdentifier = require('./cacheIdentifier.js');
|
|
8
|
+
|
|
9
|
+
function createChainIdMiddleware(chainId) {
|
|
10
|
+
return (req, res, next, end) => {
|
|
11
|
+
if (req.method === "eth_chainId") {
|
|
12
|
+
res.result = chainId;
|
|
13
|
+
return end();
|
|
14
|
+
}
|
|
15
|
+
if (req.method === "net_version") {
|
|
16
|
+
// convert to decimal
|
|
17
|
+
res.result = Number.parseInt(chainId, 16).toString(10);
|
|
18
|
+
return end();
|
|
19
|
+
}
|
|
20
|
+
return next();
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function createProviderConfigMiddleware(providerConfig) {
|
|
24
|
+
return (req, res, next, end) => {
|
|
25
|
+
if (req.method === "eth_provider_config") {
|
|
26
|
+
res.result = providerConfig;
|
|
27
|
+
return end();
|
|
28
|
+
}
|
|
29
|
+
return next();
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function createJsonRpcClient(providerConfig, networkConfig) {
|
|
33
|
+
const {
|
|
34
|
+
chainId,
|
|
35
|
+
rpcTarget
|
|
36
|
+
} = providerConfig;
|
|
37
|
+
const fetchMiddleware = baseControllers.createFetchMiddleware({
|
|
38
|
+
rpcTarget
|
|
39
|
+
});
|
|
40
|
+
const blockProvider = auth.providerFromMiddleware(fetchMiddleware);
|
|
41
|
+
const blockTracker = new PollingBlockTracker.PollingBlockTracker({
|
|
42
|
+
config: _objectSpread(_objectSpread({}, networkConfig), {}, {
|
|
43
|
+
provider: blockProvider
|
|
44
|
+
}),
|
|
45
|
+
state: {}
|
|
46
|
+
});
|
|
47
|
+
const networkMiddleware = auth.mergeMiddleware([createChainIdMiddleware(chainId), createProviderConfigMiddleware(providerConfig),
|
|
48
|
+
// 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
|
|
49
|
+
// createBlockRefRewriteMiddleware({ blockTracker }),
|
|
50
|
+
// createBlockCacheMiddleware({ blockTracker }),
|
|
51
|
+
baseControllers.createInflightCacheMiddleware({
|
|
52
|
+
cacheIdentifierForRequest: cacheIdentifier.cacheIdentifierForRequest
|
|
53
|
+
}),
|
|
54
|
+
// createBlockTrackerInspectorMiddleware({ blockTracker }),
|
|
55
|
+
fetchMiddleware]);
|
|
56
|
+
return {
|
|
57
|
+
networkMiddleware,
|
|
58
|
+
blockTracker
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
exports.createChainIdMiddleware = createChainIdMiddleware;
|
|
63
|
+
exports.createJsonRpcClient = createJsonRpcClient;
|
|
64
|
+
exports.createProviderConfigMiddleware = createProviderConfigMiddleware;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
4
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
5
|
+
var httpHelpers = require('@toruslabs/http-helpers');
|
|
6
|
+
var ethers = require('ethers');
|
|
7
|
+
var log = require('loglevel');
|
|
8
|
+
var abis = require('../utils/abis.js');
|
|
9
|
+
var constants = require('../utils/constants.js');
|
|
10
|
+
var helpers = require('../utils/helpers.js');
|
|
11
|
+
|
|
12
|
+
class NftHandler {
|
|
13
|
+
constructor({
|
|
14
|
+
chainId,
|
|
15
|
+
contractAddress,
|
|
16
|
+
contractImage,
|
|
17
|
+
contractName,
|
|
18
|
+
contractSymbol,
|
|
19
|
+
nftStandard,
|
|
20
|
+
provider,
|
|
21
|
+
contractDescription,
|
|
22
|
+
contractFallbackLogo,
|
|
23
|
+
contractSupply
|
|
24
|
+
}) {
|
|
25
|
+
_defineProperty(this, "contractAddress", void 0);
|
|
26
|
+
_defineProperty(this, "contractName", void 0);
|
|
27
|
+
_defineProperty(this, "contractSymbol", void 0);
|
|
28
|
+
_defineProperty(this, "contractImage", void 0);
|
|
29
|
+
_defineProperty(this, "contractSupply", void 0);
|
|
30
|
+
_defineProperty(this, "contractFallbackLogo", void 0);
|
|
31
|
+
_defineProperty(this, "nftStandard", void 0);
|
|
32
|
+
_defineProperty(this, "contractDescription", void 0);
|
|
33
|
+
_defineProperty(this, "chainId", void 0);
|
|
34
|
+
_defineProperty(this, "provider", void 0);
|
|
35
|
+
_defineProperty(this, "isSpecial", void 0);
|
|
36
|
+
this.chainId = chainId;
|
|
37
|
+
this.contractAddress = contractAddress;
|
|
38
|
+
this.contractImage = contractImage;
|
|
39
|
+
this.contractName = contractName;
|
|
40
|
+
this.contractSymbol = contractSymbol;
|
|
41
|
+
this.nftStandard = nftStandard;
|
|
42
|
+
this.provider = provider;
|
|
43
|
+
this.contractDescription = contractDescription;
|
|
44
|
+
this.contractFallbackLogo = contractFallbackLogo;
|
|
45
|
+
this.contractSupply = contractSupply;
|
|
46
|
+
}
|
|
47
|
+
async getNftMetadata(userAddress, tokenInfo) {
|
|
48
|
+
const returnNftItem = _objectSpread({
|
|
49
|
+
description: "",
|
|
50
|
+
image: "",
|
|
51
|
+
name: "",
|
|
52
|
+
tokenBalance: "",
|
|
53
|
+
tokenId: "",
|
|
54
|
+
decimals: "1"
|
|
55
|
+
}, tokenInfo);
|
|
56
|
+
const [tokenURI, balance] = await Promise.all([this.getCollectibleTokenURI(returnNftItem.tokenId, this.nftStandard), !returnNftItem.tokenBalance ? this.fetchNftBalance(userAddress, returnNftItem.tokenId) : Promise.resolve("0")]);
|
|
57
|
+
returnNftItem.tokenBalance = returnNftItem.tokenBalance || balance;
|
|
58
|
+
// some people put full json object in uri
|
|
59
|
+
try {
|
|
60
|
+
const object = JSON.parse(tokenURI);
|
|
61
|
+
returnNftItem.image = returnNftItem.image || helpers.sanitizeNftMetdataUrl(object.image);
|
|
62
|
+
returnNftItem.name = returnNftItem.name || object.name;
|
|
63
|
+
returnNftItem.description = returnNftItem.description || object.description;
|
|
64
|
+
returnNftItem.decimals = returnNftItem.decimals || object.decimals;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
log.warn("Token uri is not a valid json object", error);
|
|
67
|
+
}
|
|
68
|
+
const finalTokenMetaUri = helpers.sanitizeNftMetdataUrl(tokenURI);
|
|
69
|
+
try {
|
|
70
|
+
if (!returnNftItem.description || !returnNftItem.image || !returnNftItem.name) {
|
|
71
|
+
// this call might fail, if metadata url available in smart contract is not reachable
|
|
72
|
+
const object = await httpHelpers.get(finalTokenMetaUri);
|
|
73
|
+
returnNftItem.image = returnNftItem.image || helpers.sanitizeNftMetdataUrl(object.image);
|
|
74
|
+
returnNftItem.name = returnNftItem.name || object.name;
|
|
75
|
+
returnNftItem.description = returnNftItem.description || object.description;
|
|
76
|
+
returnNftItem.decimals = returnNftItem.decimals || object.decimals;
|
|
77
|
+
}
|
|
78
|
+
} catch (error) {
|
|
79
|
+
log.error("Failed to fetch nft metadata", error);
|
|
80
|
+
}
|
|
81
|
+
return returnNftItem;
|
|
82
|
+
}
|
|
83
|
+
async getContractMetadata() {
|
|
84
|
+
const returnNft = {
|
|
85
|
+
chainId: this.chainId,
|
|
86
|
+
contractAddress: this.contractAddress,
|
|
87
|
+
contractName: this.contractName,
|
|
88
|
+
contractSymbol: this.contractSymbol,
|
|
89
|
+
nftStandard: this.nftStandard,
|
|
90
|
+
contractImage: this.contractImage,
|
|
91
|
+
contractDescription: this.contractDescription,
|
|
92
|
+
contractFallbackLogo: this.contractFallbackLogo,
|
|
93
|
+
contractSupply: this.contractSupply
|
|
94
|
+
};
|
|
95
|
+
if (!this.nftStandard) {
|
|
96
|
+
const {
|
|
97
|
+
standard,
|
|
98
|
+
isSpecial
|
|
99
|
+
} = await this.checkNftStandard();
|
|
100
|
+
returnNft.nftStandard = standard;
|
|
101
|
+
this.nftStandard = standard;
|
|
102
|
+
this.isSpecial = isSpecial;
|
|
103
|
+
}
|
|
104
|
+
if (!this.contractName || !this.contractSymbol || !this.contractDescription) {
|
|
105
|
+
const abi = this.nftStandard === constants.CONTRACT_TYPE_ERC721 ? abis.erc721Abi : abis.erc1155Abi;
|
|
106
|
+
const contract = new ethers.Contract(this.contractAddress, abi, this.provider);
|
|
107
|
+
const [name, symbol] = await Promise.all([contract.name(), contract.symbol()]);
|
|
108
|
+
returnNft.contractName = name;
|
|
109
|
+
returnNft.contractSymbol = symbol;
|
|
110
|
+
if (!this.contractName) this.contractName = name;
|
|
111
|
+
if (!this.contractSymbol) this.contractSymbol = symbol;
|
|
112
|
+
}
|
|
113
|
+
return returnNft;
|
|
114
|
+
}
|
|
115
|
+
async fetchNftBalance(userAddress, tokenId) {
|
|
116
|
+
const {
|
|
117
|
+
standard
|
|
118
|
+
} = await this.checkNftStandard();
|
|
119
|
+
const abi = standard === constants.CONTRACT_TYPE_ERC721 ? abis.erc721Abi : abis.erc1155Abi;
|
|
120
|
+
const contract = new ethers.Contract(this.contractAddress, abi, this.provider);
|
|
121
|
+
if (standard === constants.CONTRACT_TYPE_ERC1155) {
|
|
122
|
+
const balance = await contract.balanceOf(userAddress, tokenId);
|
|
123
|
+
return balance;
|
|
124
|
+
}
|
|
125
|
+
let owner = "";
|
|
126
|
+
try {
|
|
127
|
+
owner = await contract.ownerOf(tokenId);
|
|
128
|
+
} catch {
|
|
129
|
+
throw new Error("Token id doesn't exists");
|
|
130
|
+
}
|
|
131
|
+
if (owner.toLowerCase() === userAddress.toLowerCase()) {
|
|
132
|
+
return "1";
|
|
133
|
+
}
|
|
134
|
+
return "0";
|
|
135
|
+
}
|
|
136
|
+
async checkNftStandard() {
|
|
137
|
+
// For Cryptokitties
|
|
138
|
+
if (this.nftStandard && this.isSpecial !== undefined) return;
|
|
139
|
+
if (Object.prototype.hasOwnProperty.call(constants.OLD_ERC721_LIST, this.contractAddress.toLowerCase())) {
|
|
140
|
+
this.nftStandard = constants.CONTRACT_TYPE_ERC721;
|
|
141
|
+
this.isSpecial = true;
|
|
142
|
+
return {
|
|
143
|
+
standard: constants.CONTRACT_TYPE_ERC721,
|
|
144
|
+
isSpecial: true
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const isErc721 = await this.contractSupportsInterface(constants.CONTRACT_TYPE_ERC721, constants.ERC721_INTERFACE_ID);
|
|
148
|
+
if (isErc721) {
|
|
149
|
+
this.nftStandard = constants.CONTRACT_TYPE_ERC721;
|
|
150
|
+
this.isSpecial = false;
|
|
151
|
+
return {
|
|
152
|
+
standard: constants.CONTRACT_TYPE_ERC721,
|
|
153
|
+
isSpecial: false
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const isErc1155 = await this.contractSupportsInterface(constants.CONTRACT_TYPE_ERC1155, constants.ERC1155_INTERFACE_ID);
|
|
157
|
+
if (isErc1155) {
|
|
158
|
+
this.nftStandard = constants.CONTRACT_TYPE_ERC1155;
|
|
159
|
+
this.isSpecial = false;
|
|
160
|
+
return {
|
|
161
|
+
standard: constants.CONTRACT_TYPE_ERC1155,
|
|
162
|
+
isSpecial: false
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
throw new Error("Unsupported nft standard");
|
|
166
|
+
}
|
|
167
|
+
async contractSupportsInterface(standard, interfaceId) {
|
|
168
|
+
const abi = standard === constants.CONTRACT_TYPE_ERC721 ? abis.erc721Abi : abis.erc1155Abi;
|
|
169
|
+
const contract = new ethers.Contract(this.contractAddress, abi, this.provider);
|
|
170
|
+
return contract.supportsInterface(interfaceId);
|
|
171
|
+
}
|
|
172
|
+
async getCollectibleTokenURI(tokenId, standard = constants.CONTRACT_TYPE_ERC721) {
|
|
173
|
+
const method = standard === constants.CONTRACT_TYPE_ERC721 ? "tokenURI" : "uri";
|
|
174
|
+
const abi = standard === constants.CONTRACT_TYPE_ERC721 ? abis.erc721Abi : abis.erc1155Abi;
|
|
175
|
+
const contract = new ethers.Contract(this.contractAddress, abi, this.provider);
|
|
176
|
+
return contract[method](tokenId);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
exports.NftHandler = NftHandler;
|