@toruslabs/ethereum-controllers 4.5.2 → 4.7.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 +390 -13
- package/dist/ethereumControllers.cjs.js.map +1 -1
- package/dist/ethereumControllers.esm.js +368 -15
- package/dist/ethereumControllers.esm.js.map +1 -1
- package/dist/ethereumControllers.umd.min.js +1 -1
- package/dist/ethereumControllers.umd.min.js.LICENSE.txt +0 -2
- package/dist/ethereumControllers.umd.min.js.map +1 -1
- package/dist/types/Message/AddChainController.d.ts +20 -0
- package/dist/types/Message/DecryptMessageController.d.ts +2 -2
- package/dist/types/Message/EncryptionPublicKeyController.d.ts +2 -2
- package/dist/types/Message/MessageController.d.ts +2 -2
- package/dist/types/Message/PersonalMessageController.d.ts +2 -2
- package/dist/types/Message/SwitchChainController.d.ts +20 -0
- package/dist/types/Message/TypedMessageController.d.ts +2 -2
- package/dist/types/Message/utils.d.ts +3 -1
- package/dist/types/Network/createEthereumMiddleware.d.ts +23 -15
- package/dist/types/Preferences/PreferencesController.d.ts +9 -2
- package/dist/types/index.d.ts +2 -0
- package/dist/types/utils/constants.d.ts +2 -0
- package/dist/types/utils/interfaces.d.ts +22 -1
- package/package.json +10 -10
- package/src/Message/AddChainController.ts +73 -0
- package/src/Message/DecryptMessageController.ts +2 -5
- package/src/Message/EncryptionPublicKeyController.ts +2 -8
- package/src/Message/MessageController.ts +2 -2
- package/src/Message/PersonalMessageController.ts +3 -3
- package/src/Message/SwitchChainController.ts +74 -0
- package/src/Message/TypedMessageController.ts +2 -2
- package/src/Message/utils.ts +49 -1
- package/src/Network/createEthereumMiddleware.ts +190 -26
- package/src/Preferences/PreferencesController.ts +66 -1
- package/src/index.ts +2 -0
- package/src/utils/constants.ts +2 -0
- package/src/utils/interfaces.ts +26 -1
|
@@ -2,7 +2,7 @@ import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
|
2
2
|
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
3
|
import { BaseController, formatSmallNumbers, addressSlicer, ACTIVITY_ACTION_RECEIVE, ACTIVITY_ACTION_SEND, significantDigits, TransactionStatus, BaseBlockTracker, timeout, BaseCurrencyController, BaseKeyringController, randomId, PROVIDER_JRPC_METHODS, createFetchMiddleware, createSwappableProxy, createEventEmitterProxy, BasePreferencesController, ACTIVITY_ACTION_TOPUP, TX_EVENTS, TRANSACTION_TYPES, BaseTransactionStateManager, transactionMatchesNetwork as transactionMatchesNetwork$1 } from '@toruslabs/base-controllers';
|
|
4
4
|
import { Mutex } from 'async-mutex';
|
|
5
|
-
import { BrowserProvider, toQuantity, Contract, SigningKey, Wallet, Interface, keccak256 } from 'ethers';
|
|
5
|
+
import { BrowserProvider, toQuantity, Contract, SigningKey, Wallet, isHexString as isHexString$1, JsonRpcProvider, Interface, keccak256 } from 'ethers';
|
|
6
6
|
import log from 'loglevel';
|
|
7
7
|
import { addHexPrefix, isValidAddress, toChecksumAddress, stripHexPrefix, isHexString, ecsign, bigIntToBytes, bytesToHex } from '@ethereumjs/util';
|
|
8
8
|
import BigNumber from 'bignumber.js';
|
|
@@ -709,7 +709,9 @@ const METHOD_TYPES = {
|
|
|
709
709
|
WATCH_ASSET: "wallet_watchAsset",
|
|
710
710
|
ETH_GET_BLOCK_BY_HASH: "eth_getBlockByHash",
|
|
711
711
|
ETH_GET_CODE: "eth_getCode",
|
|
712
|
-
ETH_GET_GAS_PRICE: "eth_gasPrice"
|
|
712
|
+
ETH_GET_GAS_PRICE: "eth_gasPrice",
|
|
713
|
+
SWITCH_CHAIN: "wallet_switchEthereumChain",
|
|
714
|
+
ADD_CHAIN: "wallet_addEthereumChain"
|
|
713
715
|
};
|
|
714
716
|
const TRANSACTION_ENVELOPE_TYPES = {
|
|
715
717
|
LEGACY: "0x0",
|
|
@@ -2021,6 +2023,106 @@ function parseDecryptMessageData(data) {
|
|
|
2021
2023
|
const buffer = Buffer.from(stripped, "hex");
|
|
2022
2024
|
return JSON.parse(buffer.toString("utf8"));
|
|
2023
2025
|
}
|
|
2026
|
+
async function validateAddChainData(data) {
|
|
2027
|
+
const {
|
|
2028
|
+
chainId,
|
|
2029
|
+
rpcUrls,
|
|
2030
|
+
nativeCurrency
|
|
2031
|
+
} = data || {};
|
|
2032
|
+
if (!chainId) {
|
|
2033
|
+
throw new Error("Invalid add chain params: please pass chainId in params");
|
|
2034
|
+
}
|
|
2035
|
+
if (!isHexString$1(chainId)) {
|
|
2036
|
+
throw new Error("Invalid add chain params: please pass a valid hex chainId in params, for: ex: 0x1");
|
|
2037
|
+
}
|
|
2038
|
+
if (!rpcUrls || rpcUrls.length === 0) throw new Error("params.rpcUrls not provided");
|
|
2039
|
+
if (!nativeCurrency) throw new Error("params.nativeCurrency not provided");
|
|
2040
|
+
const {
|
|
2041
|
+
name,
|
|
2042
|
+
symbol,
|
|
2043
|
+
decimals
|
|
2044
|
+
} = nativeCurrency;
|
|
2045
|
+
if (!name) throw new Error("params.nativeCurrency.name not provided");
|
|
2046
|
+
if (!symbol) throw new Error("params.nativeCurrency.symbol not provided");
|
|
2047
|
+
if (decimals === undefined) throw new Error("params.nativeCurrency.decimals not provided");
|
|
2048
|
+
const _web3 = new JsonRpcProvider(rpcUrls[0], "any");
|
|
2049
|
+
const {
|
|
2050
|
+
chainId: networkChainID
|
|
2051
|
+
} = await _web3.getNetwork();
|
|
2052
|
+
if (Number.parseInt(networkChainID.toString()) !== Number.parseInt(chainId, 16)) {
|
|
2053
|
+
throw new Error(`Provided rpc url's chainId version is not matching with provided chainId, expected: ${toQuantity(networkChainID)}, received: ${chainId}`);
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
function validateSwitchChainData(data) {
|
|
2057
|
+
const {
|
|
2058
|
+
chainId
|
|
2059
|
+
} = data || {};
|
|
2060
|
+
if (!chainId) {
|
|
2061
|
+
throw new Error("Invalid switch chain params: please pass chainId in params");
|
|
2062
|
+
}
|
|
2063
|
+
if (!isHexString$1(chainId)) {
|
|
2064
|
+
throw new Error("Invalid switch chain params: please pass a valid hex chainId in params, for: ex: 0x1");
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
|
|
2068
|
+
class AddChainController extends AbstractMessageController {
|
|
2069
|
+
constructor(_ref) {
|
|
2070
|
+
let {
|
|
2071
|
+
config,
|
|
2072
|
+
state,
|
|
2073
|
+
getNetworkIdentifier,
|
|
2074
|
+
addChain
|
|
2075
|
+
} = _ref;
|
|
2076
|
+
super({
|
|
2077
|
+
config,
|
|
2078
|
+
state,
|
|
2079
|
+
getNetworkIdentifier
|
|
2080
|
+
});
|
|
2081
|
+
_defineProperty(this, "name", "AddChainController");
|
|
2082
|
+
_defineProperty(this, "addChain", void 0);
|
|
2083
|
+
this.addChain = addChain;
|
|
2084
|
+
this.initialize();
|
|
2085
|
+
}
|
|
2086
|
+
async processAddChain(messageId) {
|
|
2087
|
+
try {
|
|
2088
|
+
const msgObject = this.getMessage(messageId);
|
|
2089
|
+
await this.approveMessage(messageId, msgObject.messageParams);
|
|
2090
|
+
this.addChain(msgObject.messageParams);
|
|
2091
|
+
this.updateMessage(_objectSpread(_objectSpread({}, msgObject), {}, {
|
|
2092
|
+
rawSig: JSON.stringify(msgObject.messageParams)
|
|
2093
|
+
}));
|
|
2094
|
+
this.setMessageStatus(messageId, MessageStatus.SIGNED);
|
|
2095
|
+
return null;
|
|
2096
|
+
} catch (error) {
|
|
2097
|
+
log.error(error);
|
|
2098
|
+
this.setMessageStatus(messageId, MessageStatus.FAILED);
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
async addNewUnapprovedMessage(messageParams, req) {
|
|
2102
|
+
await this.addUnapprovedMessage(messageParams, req);
|
|
2103
|
+
return this.waitForFinishStatus(messageParams, this.name);
|
|
2104
|
+
}
|
|
2105
|
+
async addUnapprovedMessage(messageParams, req) {
|
|
2106
|
+
validateAddChainData(messageParams);
|
|
2107
|
+
if (req) {
|
|
2108
|
+
messageParams.origin = req.origin;
|
|
2109
|
+
}
|
|
2110
|
+
const messageId = messageParams.id || randomId();
|
|
2111
|
+
const messageData = {
|
|
2112
|
+
id: messageId,
|
|
2113
|
+
messageParams,
|
|
2114
|
+
status: MessageStatus.UNAPPROVED,
|
|
2115
|
+
time: Date.now(),
|
|
2116
|
+
type: METHOD_TYPES.ADD_CHAIN
|
|
2117
|
+
};
|
|
2118
|
+
await this.addMessage(messageData);
|
|
2119
|
+
this.emit(MESSAGE_EVENTS.UNAPPROVED_MESSAGE, messageData);
|
|
2120
|
+
return messageId;
|
|
2121
|
+
}
|
|
2122
|
+
prepMessageForSigning(messageParams) {
|
|
2123
|
+
return Promise.resolve(messageParams);
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2024
2126
|
|
|
2025
2127
|
class DecryptMessageController extends AbstractMessageController {
|
|
2026
2128
|
constructor(_ref) {
|
|
@@ -2257,6 +2359,70 @@ class PersonalMessageController extends AbstractMessageController {
|
|
|
2257
2359
|
type: METHOD_TYPES.PERSONAL_SIGN
|
|
2258
2360
|
};
|
|
2259
2361
|
await this.addMessage(messageData);
|
|
2362
|
+
this.emit(MESSAGE_EVENTS.UNAPPROVED_MESSAGE, {
|
|
2363
|
+
messageData,
|
|
2364
|
+
req
|
|
2365
|
+
});
|
|
2366
|
+
return messageId;
|
|
2367
|
+
}
|
|
2368
|
+
prepMessageForSigning(messageParams) {
|
|
2369
|
+
return Promise.resolve(messageParams);
|
|
2370
|
+
}
|
|
2371
|
+
}
|
|
2372
|
+
|
|
2373
|
+
class SwitchChainController extends AbstractMessageController {
|
|
2374
|
+
constructor(_ref) {
|
|
2375
|
+
let {
|
|
2376
|
+
config,
|
|
2377
|
+
state,
|
|
2378
|
+
getNetworkIdentifier,
|
|
2379
|
+
switchChain
|
|
2380
|
+
} = _ref;
|
|
2381
|
+
super({
|
|
2382
|
+
config,
|
|
2383
|
+
state,
|
|
2384
|
+
getNetworkIdentifier
|
|
2385
|
+
});
|
|
2386
|
+
_defineProperty(this, "name", "SwitchChainController");
|
|
2387
|
+
_defineProperty(this, "switchChain", void 0);
|
|
2388
|
+
this.switchChain = switchChain;
|
|
2389
|
+
this.initialize();
|
|
2390
|
+
}
|
|
2391
|
+
async processSwitchChain(messageId) {
|
|
2392
|
+
try {
|
|
2393
|
+
const msgObject = this.getMessage(messageId);
|
|
2394
|
+
await this.approveMessage(messageId, msgObject.messageParams);
|
|
2395
|
+
this.switchChain({
|
|
2396
|
+
chainId: msgObject.messageParams.chainId
|
|
2397
|
+
});
|
|
2398
|
+
this.updateMessage(_objectSpread(_objectSpread({}, msgObject), {}, {
|
|
2399
|
+
rawSig: JSON.stringify(msgObject.messageParams)
|
|
2400
|
+
}));
|
|
2401
|
+
this.setMessageStatus(messageId, MessageStatus.SIGNED);
|
|
2402
|
+
return null;
|
|
2403
|
+
} catch (error) {
|
|
2404
|
+
log.error(error);
|
|
2405
|
+
this.setMessageStatus(messageId, MessageStatus.FAILED);
|
|
2406
|
+
}
|
|
2407
|
+
}
|
|
2408
|
+
async addNewUnapprovedMessage(messageParams, req) {
|
|
2409
|
+
await this.addUnapprovedMessage(messageParams, req);
|
|
2410
|
+
return this.waitForFinishStatus(messageParams, this.name);
|
|
2411
|
+
}
|
|
2412
|
+
async addUnapprovedMessage(messageParams, req) {
|
|
2413
|
+
validateSwitchChainData(messageParams);
|
|
2414
|
+
if (req) {
|
|
2415
|
+
messageParams.origin = req.origin;
|
|
2416
|
+
}
|
|
2417
|
+
const messageId = messageParams.id || randomId();
|
|
2418
|
+
const messageData = {
|
|
2419
|
+
id: messageId,
|
|
2420
|
+
messageParams,
|
|
2421
|
+
status: MessageStatus.UNAPPROVED,
|
|
2422
|
+
time: Date.now(),
|
|
2423
|
+
type: METHOD_TYPES.SWITCH_CHAIN
|
|
2424
|
+
};
|
|
2425
|
+
await this.addMessage(messageData);
|
|
2260
2426
|
this.emit(MESSAGE_EVENTS.UNAPPROVED_MESSAGE, messageData);
|
|
2261
2427
|
return messageId;
|
|
2262
2428
|
}
|
|
@@ -2384,7 +2550,19 @@ function createProcessEthSignMessage(_ref3) {
|
|
|
2384
2550
|
} = request;
|
|
2385
2551
|
if (method !== METHOD_TYPES.ETH_SIGN) return next();
|
|
2386
2552
|
if (!processEthSignMessage) throw new Error("WalletMiddleware - opts.processEthSignMessage not provided");
|
|
2387
|
-
|
|
2553
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
2554
|
+
let msgParams = request.params;
|
|
2555
|
+
if (Array.isArray(request.params)) {
|
|
2556
|
+
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address, message]`);
|
|
2557
|
+
const params = request.params;
|
|
2558
|
+
const address = params[0];
|
|
2559
|
+
const message = params[1];
|
|
2560
|
+
msgParams = {
|
|
2561
|
+
from: address,
|
|
2562
|
+
data: message
|
|
2563
|
+
};
|
|
2564
|
+
}
|
|
2565
|
+
response.result = await processEthSignMessage(msgParams, request);
|
|
2388
2566
|
});
|
|
2389
2567
|
}
|
|
2390
2568
|
function createProcessTypedMessage(_ref4) {
|
|
@@ -2397,7 +2575,20 @@ function createProcessTypedMessage(_ref4) {
|
|
|
2397
2575
|
} = request;
|
|
2398
2576
|
if (method !== METHOD_TYPES.ETH_SIGN_TYPED_DATA) return next();
|
|
2399
2577
|
if (!processTypedMessage) throw new Error("WalletMiddleware - opts.processTypedMessage not provided");
|
|
2400
|
-
|
|
2578
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
2579
|
+
let msgParams = request.params;
|
|
2580
|
+
if (Array.isArray(request.params)) {
|
|
2581
|
+
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [typedData, address]`);
|
|
2582
|
+
const params = request.params;
|
|
2583
|
+
const message = params[0];
|
|
2584
|
+
const address = params[1];
|
|
2585
|
+
msgParams = {
|
|
2586
|
+
from: address,
|
|
2587
|
+
data: message,
|
|
2588
|
+
version: SignTypedDataVersion.V1
|
|
2589
|
+
};
|
|
2590
|
+
}
|
|
2591
|
+
response.result = await processTypedMessage(msgParams, request);
|
|
2401
2592
|
});
|
|
2402
2593
|
}
|
|
2403
2594
|
function createProcessTypedMessageV3(_ref5) {
|
|
@@ -2410,7 +2601,20 @@ function createProcessTypedMessageV3(_ref5) {
|
|
|
2410
2601
|
} = request;
|
|
2411
2602
|
if (method !== METHOD_TYPES.ETH_SIGN_TYPED_DATA_V3) return next();
|
|
2412
2603
|
if (!processTypedMessageV3) throw new Error("WalletMiddleware - opts.processTypedMessageV3 is not provided");
|
|
2413
|
-
|
|
2604
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
2605
|
+
let msgParams = request.params;
|
|
2606
|
+
if (Array.isArray(request.params)) {
|
|
2607
|
+
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address, typedData]`);
|
|
2608
|
+
const params = request.params;
|
|
2609
|
+
const address = params[0];
|
|
2610
|
+
const message = JSON.parse(params[1]);
|
|
2611
|
+
msgParams = {
|
|
2612
|
+
from: address,
|
|
2613
|
+
data: message,
|
|
2614
|
+
version: SignTypedDataVersion.V3
|
|
2615
|
+
};
|
|
2616
|
+
}
|
|
2617
|
+
response.result = await processTypedMessageV3(msgParams, request);
|
|
2414
2618
|
});
|
|
2415
2619
|
}
|
|
2416
2620
|
function createProcessTypedMessageV4(_ref6) {
|
|
@@ -2423,7 +2627,20 @@ function createProcessTypedMessageV4(_ref6) {
|
|
|
2423
2627
|
} = request;
|
|
2424
2628
|
if (method !== METHOD_TYPES.ETH_SIGN_TYPED_DATA_V4) return next();
|
|
2425
2629
|
if (!processTypedMessageV4) throw new Error("WalletMiddleware - opts.processTypedMessageV4 is not provided");
|
|
2426
|
-
|
|
2630
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
2631
|
+
let msgParams = request.params;
|
|
2632
|
+
if (Array.isArray(request.params)) {
|
|
2633
|
+
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address, typedData]`);
|
|
2634
|
+
const params = request.params;
|
|
2635
|
+
const address = params[0];
|
|
2636
|
+
const message = JSON.parse(params[1]);
|
|
2637
|
+
msgParams = {
|
|
2638
|
+
from: address,
|
|
2639
|
+
data: message,
|
|
2640
|
+
version: SignTypedDataVersion.V4
|
|
2641
|
+
};
|
|
2642
|
+
}
|
|
2643
|
+
response.result = await processTypedMessageV4(msgParams, request);
|
|
2427
2644
|
});
|
|
2428
2645
|
}
|
|
2429
2646
|
function createProcessPersonalMessage(_ref7) {
|
|
@@ -2436,7 +2653,19 @@ function createProcessPersonalMessage(_ref7) {
|
|
|
2436
2653
|
} = request;
|
|
2437
2654
|
if (method !== METHOD_TYPES.PERSONAL_SIGN) return next();
|
|
2438
2655
|
if (!processPersonalMessage) throw new Error("WalletMiddleware - opts.processPersonalMessage is not provided");
|
|
2439
|
-
|
|
2656
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
2657
|
+
let msgParams = request.params;
|
|
2658
|
+
if (Array.isArray(request.params)) {
|
|
2659
|
+
if (!(request.params.length >= 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [message, address]`);
|
|
2660
|
+
const params = request.params;
|
|
2661
|
+
const message = params[0];
|
|
2662
|
+
const address = params[1];
|
|
2663
|
+
msgParams = {
|
|
2664
|
+
from: address,
|
|
2665
|
+
data: message
|
|
2666
|
+
};
|
|
2667
|
+
}
|
|
2668
|
+
response.result = await processPersonalMessage(msgParams, request);
|
|
2440
2669
|
});
|
|
2441
2670
|
}
|
|
2442
2671
|
function createPendingNonceMiddleware(_ref8) {
|
|
@@ -2529,12 +2758,21 @@ function createProcessEncryptionPublicKeyMiddleware(_ref10) {
|
|
|
2529
2758
|
} = _ref10;
|
|
2530
2759
|
return createAsyncMiddleware(async (request, response, next) => {
|
|
2531
2760
|
const {
|
|
2532
|
-
params,
|
|
2533
2761
|
method
|
|
2534
2762
|
} = request;
|
|
2535
2763
|
if (method !== METHOD_TYPES.ETH_GET_ENCRYPTION_PUBLIC_KEY) return next();
|
|
2536
2764
|
if (!processEncryptionPublicKey) throw new Error("WalletMiddleware - opts.processEncryptionPublicKey not provided");
|
|
2537
|
-
|
|
2765
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
2766
|
+
let msgParams = request.params;
|
|
2767
|
+
if (Array.isArray(request.params)) {
|
|
2768
|
+
if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [address]`);
|
|
2769
|
+
const [address] = request.params;
|
|
2770
|
+
msgParams = {
|
|
2771
|
+
data: address,
|
|
2772
|
+
from: address
|
|
2773
|
+
};
|
|
2774
|
+
}
|
|
2775
|
+
response.result = await processEncryptionPublicKey(msgParams, request);
|
|
2538
2776
|
});
|
|
2539
2777
|
}
|
|
2540
2778
|
function createProcessDecryptMessageMiddleware(_ref11) {
|
|
@@ -2543,18 +2781,67 @@ function createProcessDecryptMessageMiddleware(_ref11) {
|
|
|
2543
2781
|
} = _ref11;
|
|
2544
2782
|
return createAsyncMiddleware(async (request, response, next) => {
|
|
2545
2783
|
const {
|
|
2546
|
-
params,
|
|
2547
2784
|
method
|
|
2548
2785
|
} = request;
|
|
2549
2786
|
if (method !== METHOD_TYPES.ETH_DECRYPT) return next();
|
|
2550
2787
|
if (!processDecryptMessage) throw new Error("WalletMiddleware - opts.processDecryptMessage not provided");
|
|
2551
|
-
|
|
2788
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
2789
|
+
let msgParams = request.params;
|
|
2790
|
+
if (Array.isArray(request.params)) {
|
|
2791
|
+
if (!(request.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [message, address]`);
|
|
2792
|
+
const [message, address] = request.params;
|
|
2793
|
+
msgParams = {
|
|
2794
|
+
data: message,
|
|
2795
|
+
from: address
|
|
2796
|
+
};
|
|
2797
|
+
}
|
|
2798
|
+
response.result = await processDecryptMessage(msgParams, request);
|
|
2552
2799
|
});
|
|
2553
2800
|
}
|
|
2554
|
-
function
|
|
2801
|
+
function createProcessSwitchEthereumChain(_ref12) {
|
|
2555
2802
|
let {
|
|
2556
|
-
|
|
2803
|
+
processSwitchEthereumChain
|
|
2557
2804
|
} = _ref12;
|
|
2805
|
+
return createAsyncMiddleware(async (request, response, next) => {
|
|
2806
|
+
const {
|
|
2807
|
+
method
|
|
2808
|
+
} = request;
|
|
2809
|
+
if (method !== METHOD_TYPES.SWITCH_CHAIN) return next();
|
|
2810
|
+
if (!processSwitchEthereumChain) throw new Error("WalletMiddleware - opts.processSwitchEthereumChain not provided");
|
|
2811
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
2812
|
+
let msgParams = request.params;
|
|
2813
|
+
if (Array.isArray(request.params)) {
|
|
2814
|
+
if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [data]`);
|
|
2815
|
+
const [message] = request.params;
|
|
2816
|
+
msgParams = message;
|
|
2817
|
+
}
|
|
2818
|
+
response.result = await processSwitchEthereumChain(msgParams, request);
|
|
2819
|
+
});
|
|
2820
|
+
}
|
|
2821
|
+
function createProcessAddEthereumChain(_ref13) {
|
|
2822
|
+
let {
|
|
2823
|
+
processAddEthereumChain
|
|
2824
|
+
} = _ref13;
|
|
2825
|
+
return createAsyncMiddleware(async (request, response, next) => {
|
|
2826
|
+
const {
|
|
2827
|
+
method
|
|
2828
|
+
} = request;
|
|
2829
|
+
if (method !== METHOD_TYPES.ADD_CHAIN) return next();
|
|
2830
|
+
if (!processAddEthereumChain) throw new Error("WalletMiddleware - opts.processAddEthereumChain not provided");
|
|
2831
|
+
if (!(request !== null && request !== void 0 && request.params)) throw new Error("WalletMiddleware - missing params");
|
|
2832
|
+
let msgParams = request.params;
|
|
2833
|
+
if (Array.isArray(request.params)) {
|
|
2834
|
+
if (!(request.params.length === 1)) throw new Error(`WalletMiddleware - incorrect params for ${method} method. expected [data]`);
|
|
2835
|
+
const [message] = request.params;
|
|
2836
|
+
msgParams = message;
|
|
2837
|
+
}
|
|
2838
|
+
response.result = await processAddEthereumChain(msgParams, request);
|
|
2839
|
+
});
|
|
2840
|
+
}
|
|
2841
|
+
function createRequestAccountsMiddleware(_ref14) {
|
|
2842
|
+
let {
|
|
2843
|
+
requestAccounts
|
|
2844
|
+
} = _ref14;
|
|
2558
2845
|
return createAsyncMiddleware(async (request, response, next) => {
|
|
2559
2846
|
const {
|
|
2560
2847
|
method
|
|
@@ -2581,6 +2868,8 @@ function createEthereumMiddleware(providerHandlers) {
|
|
|
2581
2868
|
getPendingTransactionByHash,
|
|
2582
2869
|
processEncryptionPublicKey,
|
|
2583
2870
|
processDecryptMessage,
|
|
2871
|
+
processSwitchEthereumChain,
|
|
2872
|
+
processAddEthereumChain,
|
|
2584
2873
|
getProviderState,
|
|
2585
2874
|
version
|
|
2586
2875
|
} = providerHandlers;
|
|
@@ -2611,6 +2900,10 @@ function createEthereumMiddleware(providerHandlers) {
|
|
|
2611
2900
|
processEncryptionPublicKey
|
|
2612
2901
|
}), createProcessDecryptMessageMiddleware({
|
|
2613
2902
|
processDecryptMessage
|
|
2903
|
+
}), createProcessSwitchEthereumChain({
|
|
2904
|
+
processSwitchEthereumChain
|
|
2905
|
+
}), createProcessAddEthereumChain({
|
|
2906
|
+
processAddEthereumChain
|
|
2614
2907
|
})]);
|
|
2615
2908
|
}
|
|
2616
2909
|
|
|
@@ -3234,7 +3527,8 @@ class PreferencesController extends BasePreferencesController {
|
|
|
3234
3527
|
provider,
|
|
3235
3528
|
blockTracker,
|
|
3236
3529
|
signAuthMessage,
|
|
3237
|
-
getProviderConfig
|
|
3530
|
+
getProviderConfig,
|
|
3531
|
+
setProviderConfig
|
|
3238
3532
|
} = _ref;
|
|
3239
3533
|
super({
|
|
3240
3534
|
config,
|
|
@@ -3249,10 +3543,12 @@ class PreferencesController extends BasePreferencesController {
|
|
|
3249
3543
|
_defineProperty(this, "_handle", void 0);
|
|
3250
3544
|
_defineProperty(this, "_mutex", new Mutex());
|
|
3251
3545
|
_defineProperty(this, "getProviderConfig", void 0);
|
|
3546
|
+
_defineProperty(this, "setProviderConfig", void 0);
|
|
3252
3547
|
_defineProperty(this, "provider", void 0);
|
|
3253
3548
|
_defineProperty(this, "blockTracker", void 0);
|
|
3254
3549
|
this.provider = provider;
|
|
3255
3550
|
this.getProviderConfig = getProviderConfig;
|
|
3551
|
+
this.setProviderConfig = setProviderConfig;
|
|
3256
3552
|
this.blockTracker = blockTracker;
|
|
3257
3553
|
log.info(this.blockTracker);
|
|
3258
3554
|
}
|
|
@@ -3451,6 +3747,34 @@ class PreferencesController extends BasePreferencesController {
|
|
|
3451
3747
|
var _this$getAddressState6, _this$getAddressState7;
|
|
3452
3748
|
return (_this$getAddressState6 = (_this$getAddressState7 = this.getAddressState(address)) === null || _this$getAddressState7 === void 0 ? void 0 : _this$getAddressState7.customNfts) !== null && _this$getAddressState6 !== void 0 ? _this$getAddressState6 : [];
|
|
3453
3749
|
}
|
|
3750
|
+
async addChain(network) {
|
|
3751
|
+
const approveChainOptions = this.getChainOptions();
|
|
3752
|
+
const providerConfig = approveChainOptions.find(x => x.chainId === network.chainId);
|
|
3753
|
+
if (providerConfig) {
|
|
3754
|
+
throw new Error(`chainId ${network.chainId} already exists`);
|
|
3755
|
+
}
|
|
3756
|
+
const newNetwork = {
|
|
3757
|
+
displayName: network.chainName,
|
|
3758
|
+
rpcTarget: network.rpcUrls[0],
|
|
3759
|
+
ticker: network.nativeCurrency.symbol,
|
|
3760
|
+
chainId: network.chainId,
|
|
3761
|
+
blockExplorerUrl: network.blockExplorerUrls[0]
|
|
3762
|
+
};
|
|
3763
|
+
const isSuccess = await this.addCustomNetwork({
|
|
3764
|
+
type: "rpc",
|
|
3765
|
+
network: newNetwork
|
|
3766
|
+
});
|
|
3767
|
+
if (!isSuccess) throw new Error("unable to add custom network");
|
|
3768
|
+
}
|
|
3769
|
+
switchChain(data) {
|
|
3770
|
+
const chainOptions = this.getChainOptions();
|
|
3771
|
+
const providerConfig = chainOptions.find(x => x.chainId === data.chainId);
|
|
3772
|
+
if (providerConfig) {
|
|
3773
|
+
this.setProviderConfig(providerConfig);
|
|
3774
|
+
} else {
|
|
3775
|
+
throw new Error(`chainId ${data.chainId} is not supported`);
|
|
3776
|
+
}
|
|
3777
|
+
}
|
|
3454
3778
|
|
|
3455
3779
|
// Custom Network methods
|
|
3456
3780
|
async addCustomNetwork(_ref2) {
|
|
@@ -3558,6 +3882,35 @@ class PreferencesController extends BasePreferencesController {
|
|
|
3558
3882
|
paymentTx: accumulator
|
|
3559
3883
|
}, address);
|
|
3560
3884
|
}
|
|
3885
|
+
getChainOptions() {
|
|
3886
|
+
var _identities$selectedA, _identities$selectedA2;
|
|
3887
|
+
const {
|
|
3888
|
+
selectedAddress,
|
|
3889
|
+
identities
|
|
3890
|
+
} = this.state;
|
|
3891
|
+
const customNetworks = (_identities$selectedA = (_identities$selectedA2 = identities[selectedAddress]) === null || _identities$selectedA2 === void 0 ? void 0 : _identities$selectedA2.customNetworks) !== null && _identities$selectedA !== void 0 ? _identities$selectedA : [];
|
|
3892
|
+
const custom = Object.values(customNetworks).reduce((chains, network) => {
|
|
3893
|
+
const networkItem = {
|
|
3894
|
+
blockExplorerUrl: network.block_explorer_url,
|
|
3895
|
+
chainId: network.chain_id,
|
|
3896
|
+
displayName: network.network_name,
|
|
3897
|
+
logo: "eth.svg",
|
|
3898
|
+
rpcTarget: network.rpc_url,
|
|
3899
|
+
ticker: network.symbol,
|
|
3900
|
+
tickerName: network.symbol.toUpperCase(),
|
|
3901
|
+
isCustom: true,
|
|
3902
|
+
id: network.id
|
|
3903
|
+
};
|
|
3904
|
+
if (Object.keys(SUPPORTED_NETWORKS).includes(networkItem.chainId)) return chains;
|
|
3905
|
+
chains.push(networkItem);
|
|
3906
|
+
return chains;
|
|
3907
|
+
}, []);
|
|
3908
|
+
const supported = Object.values(SUPPORTED_NETWORKS).reduce((chains, network) => {
|
|
3909
|
+
chains.push(network);
|
|
3910
|
+
return chains;
|
|
3911
|
+
}, []);
|
|
3912
|
+
return [...supported, ...custom];
|
|
3913
|
+
}
|
|
3561
3914
|
async calculatePastTx(txs, address) {
|
|
3562
3915
|
const pastTx = [];
|
|
3563
3916
|
const pendingTx = [];
|
|
@@ -5583,5 +5936,5 @@ class TransactionController extends TransactionStateManager {
|
|
|
5583
5936
|
}
|
|
5584
5937
|
}
|
|
5585
5938
|
|
|
5586
|
-
export { ARBITRUM_MAINNET_CHAIN_ID, ARBITRUM_TESTNET_CHAIN_ID, AVALANCHE_MAINNET_CHAIN_ID, AVALANCHE_TESTNET_CHAIN_ID, AccountTrackerController, BSC_MAINNET_CHAIN_ID, BSC_TESTNET_CHAIN_ID, CELO_MAINNET_CHAIN_ID, CHAIN_ID_TO_GAS_LIMIT_BUFFER_MAP, COINGECKO_PLATFORMS_CHAIN_CODE_MAP, COINGECKO_SUPPORTED_CURRENCIES, CONTRACT_TYPE_ERC1155, CONTRACT_TYPE_ERC20, CONTRACT_TYPE_ERC721, CONTRACT_TYPE_ETH, CurrencyController, DEFAULT_CURRENCY, DecryptMessageController, ERC1155_INTERFACE_ID, ERC721_ENUMERABLE_INTERFACE_ID, ERC721_INTERFACE_ID, ERC721_METADATA_INTERFACE_ID, ETHERSCAN_SUPPORTED_CHAINS, EncryptionPublicKeyController, GAS_ESTIMATE_TYPES, GAS_LIMITS, GOERLI_CHAIN_ID, GasFeeController, KeyringController, LOCALHOST, MAINNET_CHAIN_ID, MESSAGE_EVENTS, METHOD_TYPES, MessageController, MessageStatus, NetworkController, NftHandler, NftsController, NonceTracker, OLD_ERC721_LIST, OPTIMISM_MAINNET_CHAIN_ID, OPTIMISM_TESTNET_CHAIN_ID, POLYGON_CHAIN_ID, POLYGON_MUMBAI_CHAIN_ID, PendingTransactionTracker, PersonalMessageController, PollingBlockTracker, PreferencesController, SEPOLIA_CHAIN_ID, SIMPLEHASH_SUPPORTED_CHAINS, SUPPORTED_NETWORKS, TEST_CHAINS, TRANSACTION_ENVELOPE_TYPES, TokenHandler, TokenRatesController, TokensController, TransactionController, TransactionGasUtil, TransactionStateManager, TypedMessageController, XDAI_CHAIN_ID, bnLessThan, createChainIdMiddleware, createEthereumMiddleware, createGetAccountsMiddleware, createJsonRpcClient, createPendingNonceMiddleware, createPendingTxMiddleware, createProcessDecryptMessageMiddleware, createProcessEncryptionPublicKeyMiddleware, createProcessEthSignMessage, createProcessPersonalMessage, createProcessTransactionMiddleware, createProcessTypedMessage, createProcessTypedMessageV3, createProcessTypedMessageV4, createProviderConfigMiddleware, createRequestAccountsMiddleware, determineTransactionType, ensureFieldIsString, ensureMutuallyExclusiveFieldsNotProvided, formatDate, formatPastTx, formatTime, formatTxMetaForRpcResult, generateHistoryEntry, getChainType, getEthTxStatus, getEtherScanHashLink, getFinalStates, getIpfsEndpoint, idleTimeTracker, isAddressByChainId, isEIP1559Transaction, isLegacyTransaction, normalizeAndValidateTxParams, normalizeMessageData, normalizeTxParameters, parseDecryptMessageData, parseStandardTokenTransactionData, readAddressAsContract, replayHistory, sanitizeNftMetdataUrl, snapshotFromTxMeta, toChecksumAddressByChainId, transactionMatchesNetwork, validateAddress, validateDecryptedMessageData, validateEncryptionPublicKeyMessageData, validateFrom, validateRecipient, validateSignMessageData, validateTxParameters, validateTypedSignMessageDataV1, validateTypedSignMessageDataV3V4 };
|
|
5939
|
+
export { ARBITRUM_MAINNET_CHAIN_ID, ARBITRUM_TESTNET_CHAIN_ID, AVALANCHE_MAINNET_CHAIN_ID, AVALANCHE_TESTNET_CHAIN_ID, AccountTrackerController, AddChainController, BSC_MAINNET_CHAIN_ID, BSC_TESTNET_CHAIN_ID, CELO_MAINNET_CHAIN_ID, CHAIN_ID_TO_GAS_LIMIT_BUFFER_MAP, COINGECKO_PLATFORMS_CHAIN_CODE_MAP, COINGECKO_SUPPORTED_CURRENCIES, CONTRACT_TYPE_ERC1155, CONTRACT_TYPE_ERC20, CONTRACT_TYPE_ERC721, CONTRACT_TYPE_ETH, CurrencyController, DEFAULT_CURRENCY, DecryptMessageController, ERC1155_INTERFACE_ID, ERC721_ENUMERABLE_INTERFACE_ID, ERC721_INTERFACE_ID, ERC721_METADATA_INTERFACE_ID, ETHERSCAN_SUPPORTED_CHAINS, EncryptionPublicKeyController, GAS_ESTIMATE_TYPES, GAS_LIMITS, GOERLI_CHAIN_ID, GasFeeController, KeyringController, LOCALHOST, MAINNET_CHAIN_ID, MESSAGE_EVENTS, METHOD_TYPES, MessageController, MessageStatus, NetworkController, NftHandler, NftsController, NonceTracker, OLD_ERC721_LIST, OPTIMISM_MAINNET_CHAIN_ID, OPTIMISM_TESTNET_CHAIN_ID, POLYGON_CHAIN_ID, POLYGON_MUMBAI_CHAIN_ID, PendingTransactionTracker, PersonalMessageController, PollingBlockTracker, PreferencesController, SEPOLIA_CHAIN_ID, SIMPLEHASH_SUPPORTED_CHAINS, SUPPORTED_NETWORKS, SwitchChainController, TEST_CHAINS, TRANSACTION_ENVELOPE_TYPES, TokenHandler, TokenRatesController, TokensController, TransactionController, TransactionGasUtil, TransactionStateManager, TypedMessageController, XDAI_CHAIN_ID, bnLessThan, createChainIdMiddleware, createEthereumMiddleware, createGetAccountsMiddleware, createJsonRpcClient, createPendingNonceMiddleware, createPendingTxMiddleware, createProcessAddEthereumChain, createProcessDecryptMessageMiddleware, createProcessEncryptionPublicKeyMiddleware, createProcessEthSignMessage, createProcessPersonalMessage, createProcessSwitchEthereumChain, createProcessTransactionMiddleware, createProcessTypedMessage, createProcessTypedMessageV3, createProcessTypedMessageV4, createProviderConfigMiddleware, createRequestAccountsMiddleware, determineTransactionType, ensureFieldIsString, ensureMutuallyExclusiveFieldsNotProvided, formatDate, formatPastTx, formatTime, formatTxMetaForRpcResult, generateHistoryEntry, getChainType, getEthTxStatus, getEtherScanHashLink, getFinalStates, getIpfsEndpoint, idleTimeTracker, isAddressByChainId, isEIP1559Transaction, isLegacyTransaction, normalizeAndValidateTxParams, normalizeMessageData, normalizeTxParameters, parseDecryptMessageData, parseStandardTokenTransactionData, readAddressAsContract, replayHistory, sanitizeNftMetdataUrl, snapshotFromTxMeta, toChecksumAddressByChainId, transactionMatchesNetwork, validateAddChainData, validateAddress, validateDecryptedMessageData, validateEncryptionPublicKeyMessageData, validateFrom, validateRecipient, validateSignMessageData, validateSwitchChainData, validateTxParameters, validateTypedSignMessageDataV1, validateTypedSignMessageDataV3V4 };
|
|
5587
5940
|
//# sourceMappingURL=ethereumControllers.esm.js.map
|