2021-02-04 19:15:23 +01:00
|
|
|
import { strict as assert } from 'assert';
|
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
|
|
|
import { ethErrors } from 'eth-rpc-errors';
|
|
|
|
import { normalize as normalizeAddress } from 'eth-sig-util';
|
2021-06-22 19:39:44 +02:00
|
|
|
import { ethers } from 'ethers';
|
2021-02-04 19:15:23 +01:00
|
|
|
import log from 'loglevel';
|
2021-06-22 19:39:44 +02:00
|
|
|
import abiERC721 from 'human-standard-collectible-abi';
|
|
|
|
import contractsMap from '@metamask/contract-metadata';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { LISTED_CONTRACT_ADDRESSES } from '../../../shared/constants/tokens';
|
|
|
|
import { NETWORK_TYPE_TO_ID_MAP } from '../../../shared/constants/network';
|
2021-02-12 16:25:58 +01:00
|
|
|
import { isPrefixedFormattedHexString } from '../../../shared/modules/network.utils';
|
2021-06-22 19:39:44 +02:00
|
|
|
import {
|
|
|
|
isValidHexAddress,
|
|
|
|
toChecksumHexAddress,
|
|
|
|
} from '../../../shared/modules/hexstring-utils';
|
2021-02-26 16:40:25 +01:00
|
|
|
import { NETWORK_EVENTS } from './network';
|
2017-01-30 21:42:24 +01:00
|
|
|
|
2021-06-22 19:39:44 +02:00
|
|
|
const ERC721METADATA_INTERFACE_ID = '0x5b5e139f';
|
|
|
|
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class PreferencesController {
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
*
|
2018-04-16 19:08:04 +02:00
|
|
|
* @typedef {Object} PreferencesController
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Object} opts - Overrides the defaults for the initial state of this.store
|
2020-11-10 18:30:41 +01:00
|
|
|
* @property {Object} store The stored object containing a users preferences, stored in local storage
|
|
|
|
* @property {Array} store.frequentRpcList A list of custom rpcs to provide the user
|
|
|
|
* @property {Array} store.tokens The tokens the user wants display in their token lists
|
|
|
|
* @property {Object} store.accountTokens The tokens stored per account and then per network type
|
|
|
|
* @property {Object} store.assetImages Contains assets objects related to assets added
|
2018-04-16 19:08:04 +02:00
|
|
|
* @property {boolean} store.useBlockie The users preference for blockie identicons within the UI
|
2019-09-27 06:30:36 +02:00
|
|
|
* @property {boolean} store.useNonceField The users preference for nonce field within the UI
|
2020-11-10 18:30:41 +01:00
|
|
|
* @property {Object} store.featureFlags A key-boolean map, where keys refer to features and booleans to whether the
|
2019-02-25 20:10:13 +01:00
|
|
|
* user wishes to see that feature.
|
|
|
|
*
|
|
|
|
* Feature flags can be set by the global function `setPreference(feature, enabled)`, and so should not expose any sensitive behavior.
|
2020-11-10 18:30:41 +01:00
|
|
|
* @property {Object} store.knownMethodData Contains all data methods known by the user
|
2018-04-16 19:08:04 +02:00
|
|
|
* @property {string} store.currentLocale The preferred language locale key
|
|
|
|
* @property {string} store.selectedAddress A hex string that matches the currently selected address in the app
|
|
|
|
*
|
2018-04-18 20:41:39 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(opts = {}) {
|
2020-08-19 18:27:05 +02:00
|
|
|
const initState = {
|
2018-10-26 10:26:43 +02:00
|
|
|
frequentRpcListDetail: [],
|
2018-07-27 22:05:12 +02:00
|
|
|
accountTokens: {},
|
2020-12-08 21:38:00 +01:00
|
|
|
accountHiddenTokens: {},
|
2018-08-21 17:59:42 +02:00
|
|
|
assetImages: {},
|
2017-06-16 01:22:53 +02:00
|
|
|
tokens: [],
|
2020-12-08 21:38:00 +01:00
|
|
|
hiddenTokens: [],
|
2018-06-19 00:33:50 +02:00
|
|
|
suggestedTokens: {},
|
2017-11-27 16:11:48 +01:00
|
|
|
useBlockie: false,
|
2019-09-27 06:30:36 +02:00
|
|
|
useNonceField: false,
|
2020-02-27 07:29:41 +01:00
|
|
|
usePhishDetect: true,
|
2021-05-05 15:58:29 +02:00
|
|
|
dismissSeedBackUpReminder: false,
|
2019-02-25 20:10:13 +01:00
|
|
|
|
|
|
|
// WARNING: Do not use feature flags for security-sensitive things.
|
|
|
|
// Feature flag toggling is available in the global namespace
|
|
|
|
// for convenient testing of pre-release features, and should never
|
|
|
|
// perform sensitive operations.
|
2019-03-26 16:07:53 +01:00
|
|
|
featureFlags: {
|
2019-08-21 20:42:14 +02:00
|
|
|
showIncomingTransactions: true,
|
2019-03-26 16:07:53 +01:00
|
|
|
},
|
2019-01-22 19:22:56 +01:00
|
|
|
knownMethodData: {},
|
2019-03-05 16:45:01 +01:00
|
|
|
firstTimeFlowType: null,
|
2018-03-22 16:09:16 +01:00
|
|
|
currentLocale: opts.initLangCode,
|
2018-04-19 05:33:51 +02:00
|
|
|
identities: {},
|
2018-06-04 23:05:56 +02:00
|
|
|
lostIdentities: {},
|
2018-09-11 02:01:15 +02:00
|
|
|
forgottenPassword: false,
|
2018-10-17 01:03:29 +02:00
|
|
|
preferences: {
|
2020-05-13 16:53:00 +02:00
|
|
|
autoLockTimeLimit: undefined,
|
|
|
|
showFiatInTestnets: false,
|
2018-10-26 10:26:43 +02:00
|
|
|
useNativeCurrencyAsPrimaryCurrency: true,
|
2021-03-09 20:35:55 +01:00
|
|
|
hideZeroBalanceTokens: false,
|
2018-10-17 01:03:29 +02:00
|
|
|
},
|
2019-01-23 16:25:34 +01:00
|
|
|
completedOnboarding: false,
|
2019-12-12 20:28:07 +01:00
|
|
|
// ENS decentralized website resolution
|
2020-11-03 00:41:28 +01:00
|
|
|
ipfsGateway: 'dweb.link',
|
2021-04-15 19:41:40 +02:00
|
|
|
infuraBlocked: null,
|
2021-04-26 20:05:48 +02:00
|
|
|
useLedgerLive: false,
|
2020-11-03 00:41:28 +01:00
|
|
|
...opts.initState,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-04 23:21:46 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.network = opts.network;
|
2021-06-22 19:39:44 +02:00
|
|
|
this.ethersProvider = new ethers.providers.Web3Provider(opts.provider);
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store = new ObservableStore(initState);
|
|
|
|
this.store.setMaxListeners(12);
|
|
|
|
this.openPopup = opts.openPopup;
|
|
|
|
this.migrateAddressBookState = opts.migrateAddressBookState;
|
2021-06-22 19:39:44 +02:00
|
|
|
|
|
|
|
this.network.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, () => {
|
|
|
|
const { tokens, hiddenTokens } = this._getTokenRelatedStates();
|
|
|
|
this.ethersProvider = new ethers.providers.Web3Provider(opts.provider);
|
|
|
|
this._updateAccountTokens(tokens, this.getAssetImages(), hiddenTokens);
|
|
|
|
});
|
|
|
|
|
2021-04-15 19:41:40 +02:00
|
|
|
this._subscribeToInfuraAvailability();
|
2019-02-25 20:10:13 +01:00
|
|
|
|
|
|
|
global.setPreference = (key, value) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.setFeatureFlag(key, value);
|
|
|
|
};
|
2017-01-30 21:42:24 +01:00
|
|
|
}
|
2019-07-31 22:17:11 +02:00
|
|
|
// PUBLIC METHODS
|
2017-01-30 21:42:24 +01:00
|
|
|
|
2018-09-11 02:01:15 +02:00
|
|
|
/**
|
|
|
|
* Sets the {@code forgottenPassword} state property
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {boolean} forgottenPassword - whether or not the user has forgotten their password
|
2018-09-11 02:01:15 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setPasswordForgotten(forgottenPassword) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ forgottenPassword });
|
2018-09-11 02:01:15 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Setter for the `useBlockie` property
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {boolean} val - Whether or not the user prefers blockie indicators
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setUseBlockie(val) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ useBlockie: val });
|
2017-11-24 02:33:44 +01:00
|
|
|
}
|
|
|
|
|
2019-09-27 06:30:36 +02:00
|
|
|
/**
|
|
|
|
* Setter for the `useNonceField` property
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {boolean} val - Whether or not the user prefers to set nonce
|
2019-09-27 06:30:36 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setUseNonceField(val) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ useNonceField: val });
|
2019-09-27 06:30:36 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 07:29:41 +01:00
|
|
|
/**
|
|
|
|
* Setter for the `usePhishDetect` property
|
|
|
|
*
|
|
|
|
* @param {boolean} val - Whether or not the user prefers phishing domain protection
|
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setUsePhishDetect(val) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ usePhishDetect: val });
|
2020-02-27 07:29:41 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 16:45:01 +01:00
|
|
|
/**
|
|
|
|
* Setter for the `firstTimeFlowType` property
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} type - Indicates the type of first time flow - create or import - the user wishes to follow
|
2019-03-05 16:45:01 +01:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setFirstTimeFlowType(type) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ firstTimeFlowType: type });
|
2019-07-31 22:17:11 +02:00
|
|
|
}
|
2019-03-05 16:45:01 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
getSuggestedTokens() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.store.getState().suggestedTokens;
|
2018-06-19 00:05:41 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
getAssetImages() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.store.getState().assetImages;
|
2018-08-15 23:34:57 +02:00
|
|
|
}
|
|
|
|
|
2019-01-22 19:22:56 +01:00
|
|
|
/**
|
|
|
|
* Add new methodData to state, to avoid requesting this information again through Infura
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} fourBytePrefix - Four-byte method signature
|
|
|
|
* @param {string} methodData - Corresponding data method
|
2019-01-22 19:22:56 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
addKnownMethodData(fourBytePrefix, methodData) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { knownMethodData } = this.store.getState();
|
|
|
|
knownMethodData[fourBytePrefix] = methodData;
|
|
|
|
this.store.updateState({ knownMethodData });
|
2019-01-22 19:22:56 +01:00
|
|
|
}
|
|
|
|
|
2018-06-19 00:05:41 +02:00
|
|
|
/**
|
2020-12-02 17:49:49 +01:00
|
|
|
* wallet_watchAsset request handler.
|
|
|
|
*
|
|
|
|
* @param {Object} req - The watchAsset JSON-RPC request object.
|
|
|
|
*/
|
|
|
|
async requestWatchAsset(req) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { type, options } = req.params;
|
2020-07-18 00:49:52 +02:00
|
|
|
|
2020-12-02 17:49:49 +01:00
|
|
|
switch (type) {
|
|
|
|
case 'ERC20':
|
2021-02-04 19:15:23 +01:00
|
|
|
return await this._handleWatchAssetERC20(options);
|
2020-12-02 17:49:49 +01:00
|
|
|
default:
|
|
|
|
throw ethErrors.rpc.invalidParams(
|
|
|
|
`Asset of type "${type}" not supported.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-12-02 17:49:49 +01:00
|
|
|
}
|
2018-06-19 00:05:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Setter for the `currentLocale` property
|
2018-04-16 19:08:04 +02:00
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} key - he preferred language locale key
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setCurrentLocale(key) {
|
|
|
|
const textDirection = ['ar', 'dv', 'fa', 'he', 'ku'].includes(key)
|
|
|
|
? 'rtl'
|
2021-02-04 19:15:23 +01:00
|
|
|
: 'auto';
|
2019-09-03 19:47:54 +02:00
|
|
|
this.store.updateState({
|
|
|
|
currentLocale: key,
|
2020-08-19 18:27:05 +02:00
|
|
|
textDirection,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
return textDirection;
|
2018-03-16 01:29:45 +01:00
|
|
|
}
|
|
|
|
|
2018-06-03 20:27:25 +02:00
|
|
|
/**
|
|
|
|
* Updates identities to only include specified addresses. Removes identities
|
|
|
|
* not included in addresses array
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string[]} addresses - An array of hex addresses
|
2018-06-03 20:27:25 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setAddresses(addresses) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const oldIdentities = this.store.getState().identities;
|
|
|
|
const oldAccountTokens = this.store.getState().accountTokens;
|
|
|
|
const oldAccountHiddenTokens = this.store.getState().accountHiddenTokens;
|
2018-07-27 22:05:12 +02:00
|
|
|
|
2018-04-19 05:33:51 +02:00
|
|
|
const identities = addresses.reduce((ids, address, index) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const oldId = oldIdentities[address] || {};
|
|
|
|
ids[address] = { name: `Account ${index + 1}`, address, ...oldId };
|
|
|
|
return ids;
|
|
|
|
}, {});
|
2018-07-31 01:09:17 +02:00
|
|
|
const accountTokens = addresses.reduce((tokens, address) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const oldTokens = oldAccountTokens[address] || {};
|
|
|
|
tokens[address] = oldTokens;
|
|
|
|
return tokens;
|
|
|
|
}, {});
|
2020-12-08 21:38:00 +01:00
|
|
|
const accountHiddenTokens = addresses.reduce((hiddenTokens, address) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const oldHiddenTokens = oldAccountHiddenTokens[address] || {};
|
|
|
|
hiddenTokens[address] = oldHiddenTokens;
|
|
|
|
return hiddenTokens;
|
|
|
|
}, {});
|
|
|
|
this.store.updateState({ identities, accountTokens, accountHiddenTokens });
|
2018-04-19 05:33:51 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 06:20:40 +02:00
|
|
|
/**
|
|
|
|
* Removes an address from state
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} address - A hex address
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {string} the address that was removed
|
2018-07-11 06:20:40 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
removeAddress(address) {
|
2020-12-08 21:38:00 +01:00
|
|
|
const {
|
|
|
|
identities,
|
|
|
|
accountTokens,
|
|
|
|
accountHiddenTokens,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.store.getState();
|
2020-12-08 21:38:00 +01:00
|
|
|
|
2018-07-11 06:20:40 +02:00
|
|
|
if (!identities[address]) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(`${address} can't be deleted cause it was not found`);
|
2018-07-11 06:20:40 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
delete identities[address];
|
|
|
|
delete accountTokens[address];
|
|
|
|
delete accountHiddenTokens[address];
|
|
|
|
this.store.updateState({ identities, accountTokens, accountHiddenTokens });
|
2018-07-11 06:20:40 +02:00
|
|
|
|
|
|
|
// If the selected account is no longer valid,
|
|
|
|
// select an arbitrary other account:
|
|
|
|
if (address === this.getSelectedAddress()) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const selected = Object.keys(identities)[0];
|
|
|
|
this.setSelectedAddress(selected);
|
2018-07-11 06:20:40 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return address;
|
2018-07-11 06:20:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-03 20:27:25 +02:00
|
|
|
/**
|
|
|
|
* Adds addresses to the identities object without removing identities
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string[]} addresses - An array of hex addresses
|
2018-06-03 20:27:25 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
addAddresses(addresses) {
|
2020-12-08 21:38:00 +01:00
|
|
|
const {
|
|
|
|
identities,
|
|
|
|
accountTokens,
|
|
|
|
accountHiddenTokens,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.store.getState();
|
2018-06-03 20:27:25 +02:00
|
|
|
addresses.forEach((address) => {
|
|
|
|
// skip if already exists
|
2019-11-20 01:03:20 +01:00
|
|
|
if (identities[address]) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-06-03 20:27:25 +02:00
|
|
|
// add missing identity
|
2021-02-04 19:15:23 +01:00
|
|
|
const identityCount = Object.keys(identities).length;
|
2018-07-27 00:04:34 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
accountTokens[address] = {};
|
|
|
|
accountHiddenTokens[address] = {};
|
|
|
|
identities[address] = { name: `Account ${identityCount + 1}`, address };
|
|
|
|
});
|
|
|
|
this.store.updateState({ identities, accountTokens, accountHiddenTokens });
|
2018-06-03 20:27:25 +02:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:37:27 +01:00
|
|
|
/**
|
2018-06-04 22:43:26 +02:00
|
|
|
* Synchronizes identity entries with known accounts.
|
|
|
|
* Removes any unknown identities, and returns the resulting selected address.
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Array<string>} addresses - known to the vault.
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Promise<string>} selectedAddress the selected address.
|
2018-06-04 22:43:26 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
syncAddresses(addresses) {
|
2020-03-20 20:37:27 +01:00
|
|
|
if (!Array.isArray(addresses) || addresses.length === 0) {
|
2021-06-29 00:38:20 +02:00
|
|
|
throw new Error('Expected non-empty array of addresses. Error #11201');
|
2020-03-20 20:37:27 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { identities, lostIdentities } = this.store.getState();
|
2018-06-04 23:24:45 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const newlyLost = {};
|
2018-06-04 22:43:26 +02:00
|
|
|
Object.keys(identities).forEach((identity) => {
|
|
|
|
if (!addresses.includes(identity)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
newlyLost[identity] = identities[identity];
|
|
|
|
delete identities[identity];
|
2018-06-04 22:43:26 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-04 22:43:26 +02:00
|
|
|
|
2018-06-04 23:21:46 +02:00
|
|
|
// Identities are no longer present.
|
2018-06-04 23:24:45 +02:00
|
|
|
if (Object.keys(newlyLost).length > 0) {
|
2018-06-05 20:51:27 +02:00
|
|
|
// store lost accounts
|
2020-07-21 23:10:45 +02:00
|
|
|
Object.keys(newlyLost).forEach((key) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
lostIdentities[key] = newlyLost[key];
|
|
|
|
});
|
2018-06-04 23:21:46 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ identities, lostIdentities });
|
|
|
|
this.addAddresses(addresses);
|
2018-06-04 22:43:26 +02:00
|
|
|
|
2018-06-05 00:18:12 +02:00
|
|
|
// If the selected account is no longer valid,
|
|
|
|
// select an arbitrary other account:
|
2021-02-04 19:15:23 +01:00
|
|
|
let selected = this.getSelectedAddress();
|
2018-06-04 22:43:26 +02:00
|
|
|
if (!addresses.includes(selected)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
selected = addresses[0];
|
|
|
|
this.setSelectedAddress(selected);
|
2018-06-04 22:43:26 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return selected;
|
2018-06-04 22:43:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
removeSuggestedTokens() {
|
2019-05-08 21:51:33 +02:00
|
|
|
return new Promise((resolve) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ suggestedTokens: {} });
|
|
|
|
resolve({});
|
|
|
|
});
|
2018-08-04 01:24:12 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Setter for the `selectedAddress` property
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} _address - A new hex address for an account
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Promise<void>} Promise resolves with tokens
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setSelectedAddress(_address) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const address = normalizeAddress(_address);
|
|
|
|
this._updateTokens(address);
|
2020-04-16 20:20:01 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { identities, tokens } = this.store.getState();
|
|
|
|
const selectedIdentity = identities[address];
|
2020-04-16 20:20:01 +02:00
|
|
|
if (!selectedIdentity) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(`Identity for '${address} not found`);
|
2020-04-16 20:20:01 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
selectedIdentity.lastSelected = Date.now();
|
|
|
|
this.store.updateState({ identities, selectedAddress: address });
|
|
|
|
return Promise.resolve(tokens);
|
2017-01-30 21:42:24 +01:00
|
|
|
}
|
|
|
|
|
2018-04-16 19:08:04 +02:00
|
|
|
/**
|
|
|
|
* Getter for the `selectedAddress` property
|
|
|
|
*
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {string} The hex address for the currently selected account
|
2018-04-16 19:08:04 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getSelectedAddress() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.store.getState().selectedAddress;
|
2017-01-30 21:42:24 +01:00
|
|
|
}
|
|
|
|
|
2018-04-16 19:08:04 +02:00
|
|
|
/**
|
|
|
|
* Contains data about tokens users add to their account.
|
|
|
|
* @typedef {Object} AddedToken
|
|
|
|
* @property {string} address - The hex address for the token contract. Will be all lower cased and hex-prefixed.
|
|
|
|
* @property {string} symbol - The symbol of the token, usually 3 or 4 capitalized letters
|
|
|
|
* {@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md#symbol}
|
|
|
|
* @property {boolean} decimals - The number of decimals the token uses.
|
|
|
|
* {@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md#decimals}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-12-08 21:38:00 +01:00
|
|
|
* Adds a new token to the token array and removes it from the hiddenToken array, or updates the token if passed an address that already exists.
|
2018-04-16 19:08:04 +02:00
|
|
|
* Modifies the existing tokens array from the store. All objects in the tokens array array AddedToken objects.
|
2018-06-03 20:27:25 +02:00
|
|
|
* @see AddedToken {@link AddedToken}
|
2018-04-16 19:08:04 +02:00
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} rawAddress - Hex address of the token contract. May or may not be a checksum address.
|
|
|
|
* @param {string} symbol - The symbol of the token
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {number} decimals - The number of decimals the token uses.
|
|
|
|
* @returns {Promise<array>} Promises the new array of AddedToken objects.
|
2018-04-16 19:08:04 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async addToken(rawAddress, symbol, decimals, image) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const address = normalizeAddress(rawAddress);
|
2021-03-19 18:17:39 +01:00
|
|
|
const newEntry = { address, symbol, decimals: Number(decimals) };
|
2021-02-04 19:15:23 +01:00
|
|
|
const { tokens, hiddenTokens } = this.store.getState();
|
|
|
|
const assetImages = this.getAssetImages();
|
2020-12-08 21:38:00 +01:00
|
|
|
const updatedHiddenTokens = hiddenTokens.filter(
|
|
|
|
(tokenAddress) => tokenAddress !== rawAddress.toLowerCase(),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-05-08 21:51:33 +02:00
|
|
|
const previousEntry = tokens.find((token) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return token.address === address;
|
|
|
|
});
|
|
|
|
const previousIndex = tokens.indexOf(previousEntry);
|
2017-06-16 01:22:53 +02:00
|
|
|
|
2021-06-22 19:39:44 +02:00
|
|
|
newEntry.isERC721 = await this._detectIsERC721(newEntry.address);
|
|
|
|
|
2017-12-19 00:57:04 +01:00
|
|
|
if (previousEntry) {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokens[previousIndex] = newEntry;
|
2017-06-16 01:22:53 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokens.push(newEntry);
|
2017-06-16 01:22:53 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
assetImages[address] = image;
|
|
|
|
this._updateAccountTokens(tokens, assetImages, updatedHiddenTokens);
|
|
|
|
return Promise.resolve(tokens);
|
2017-10-03 19:25:52 +02:00
|
|
|
}
|
|
|
|
|
2021-06-22 19:39:44 +02:00
|
|
|
/**
|
|
|
|
* Adds isERC721 field to token object
|
|
|
|
* (Called when a user attempts to add tokens that were previously added which do not yet had isERC721 field)
|
|
|
|
*
|
|
|
|
* @param {string} tokenAddress - The contract address of the token requiring the isERC721 field added.
|
|
|
|
* @returns {Promise<object>} The new token object with the added isERC721 field.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
async updateTokenType(tokenAddress) {
|
|
|
|
const { tokens } = this.store.getState();
|
|
|
|
const tokenIndex = tokens.findIndex((token) => {
|
|
|
|
return token.address === tokenAddress;
|
|
|
|
});
|
|
|
|
tokens[tokenIndex].isERC721 = await this._detectIsERC721(tokenAddress);
|
|
|
|
this.store.updateState({ tokens });
|
|
|
|
return Promise.resolve(tokens[tokenIndex]);
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
2020-12-08 21:38:00 +01:00
|
|
|
* Removes a specified token from the tokens array and adds it to hiddenTokens array
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} rawAddress - Hex address of the token contract to remove.
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Promise<array>} The new array of AddedToken objects
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
removeToken(rawAddress) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { tokens, hiddenTokens } = this.store.getState();
|
|
|
|
const assetImages = this.getAssetImages();
|
|
|
|
const updatedTokens = tokens.filter(
|
|
|
|
(token) => token.address !== rawAddress,
|
|
|
|
);
|
|
|
|
const updatedHiddenTokens = [...hiddenTokens, rawAddress.toLowerCase()];
|
|
|
|
delete assetImages[rawAddress];
|
|
|
|
this._updateAccountTokens(updatedTokens, assetImages, updatedHiddenTokens);
|
|
|
|
return Promise.resolve(updatedTokens);
|
2017-06-16 01:22:53 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* A getter for the `tokens` property
|
|
|
|
*
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Array} The current array of AddedToken objects
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getTokens() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.store.getState().tokens;
|
2017-06-16 01:22:53 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 05:33:51 +02:00
|
|
|
/**
|
|
|
|
* Sets a custom label for an account
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} account - the account to set a label for
|
|
|
|
* @param {string} label - the custom label for the account
|
|
|
|
* @returns {Promise<string>}
|
2018-04-19 05:33:51 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setAccountLabel(account, label) {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!account) {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`setAccountLabel requires a valid address, got ${String(account)}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const address = normalizeAddress(account);
|
|
|
|
const { identities } = this.store.getState();
|
|
|
|
identities[address] = identities[address] || {};
|
|
|
|
identities[address].name = label;
|
|
|
|
this.store.updateState({ identities });
|
|
|
|
return Promise.resolve(label);
|
2018-04-19 05:33:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 19:13:51 +01:00
|
|
|
/**
|
|
|
|
* updates custom RPC details
|
|
|
|
*
|
Update address book state upon custom RPC chainId edit (#9493)
When the `chainId` for a custom RPC endpoint is edited, we now migrate
the corresponding address book entries to ensure they are not orphaned.
The address book entries are grouped by the `metamask.network` state,
which unfortunately was sometimes the `chainId`, and sometimes the
`networkId`. It was always the `networkId` for built-in Infura
networks, but for custom RPC endpoints it would be set to the user-set
`chainId` field, with a fallback to the `networkId` of the network.
A recent change will force users to enter valid `chainId`s on all
custom networks, which will be normalized to be hex-prefixed. As a
result, address book contacts will now be keyed by a different string.
The contact entries are now migrated when this edit takes place.
There are some edge cases where two separate entries share the same set
of contacts. For example, if two entries have the same `chainId`, or if
they had the same `networkId` and had no `chainId` set. When the
`chainId` is edited in such cases, the contacts are duplicated on both
networks. This is the best we can do, as we don't have any way to know
which network the contacts _should_ be on.
The `typed-message-manager` unit tests have also been updated as part
of this commit because the addition of `sinon.restore()` to the
preferences controller tests ended up clearing a test object in-between
individual tests in that file. The test object is now re-constructed
before each individual test.
2020-10-07 19:32:17 +02:00
|
|
|
* @param {Object} newRpcDetails - Options bag.
|
|
|
|
* @param {string} newRpcDetails.rpcUrl - The RPC url to add to frequentRpcList.
|
|
|
|
* @param {string} newRpcDetails.chainId - The chainId of the selected network.
|
|
|
|
* @param {string} [newRpcDetails.ticker] - Optional ticker symbol of the selected network.
|
|
|
|
* @param {string} [newRpcDetails.nickname] - Optional nickname of the selected network.
|
|
|
|
* @param {Object} [newRpcDetails.rpcPrefs] - Optional RPC preferences, such as the block explorer URL
|
2019-01-29 19:13:51 +01:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async updateRpc(newRpcDetails) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const rpcList = this.getFrequentRpcListDetail();
|
2019-11-20 01:03:20 +01:00
|
|
|
const index = rpcList.findIndex((element) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return element.rpcUrl === newRpcDetails.rpcUrl;
|
|
|
|
});
|
2019-01-29 19:13:51 +01:00
|
|
|
if (index > -1) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const rpcDetail = rpcList[index];
|
|
|
|
const updatedRpc = { ...rpcDetail, ...newRpcDetails };
|
Update address book state upon custom RPC chainId edit (#9493)
When the `chainId` for a custom RPC endpoint is edited, we now migrate
the corresponding address book entries to ensure they are not orphaned.
The address book entries are grouped by the `metamask.network` state,
which unfortunately was sometimes the `chainId`, and sometimes the
`networkId`. It was always the `networkId` for built-in Infura
networks, but for custom RPC endpoints it would be set to the user-set
`chainId` field, with a fallback to the `networkId` of the network.
A recent change will force users to enter valid `chainId`s on all
custom networks, which will be normalized to be hex-prefixed. As a
result, address book contacts will now be keyed by a different string.
The contact entries are now migrated when this edit takes place.
There are some edge cases where two separate entries share the same set
of contacts. For example, if two entries have the same `chainId`, or if
they had the same `networkId` and had no `chainId` set. When the
`chainId` is edited in such cases, the contacts are duplicated on both
networks. This is the best we can do, as we don't have any way to know
which network the contacts _should_ be on.
The `typed-message-manager` unit tests have also been updated as part
of this commit because the addition of `sinon.restore()` to the
preferences controller tests ended up clearing a test object in-between
individual tests in that file. The test object is now re-constructed
before each individual test.
2020-10-07 19:32:17 +02:00
|
|
|
if (rpcDetail.chainId !== updatedRpc.chainId) {
|
|
|
|
// When the chainId is changed, associated address book entries should
|
|
|
|
// also be migrated. The address book entries are keyed by the `network` state,
|
|
|
|
// which for custom networks is the chainId with a fallback to the networkId
|
|
|
|
// if the chainId is not set.
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let addressBookKey = rpcDetail.chainId;
|
Update address book state upon custom RPC chainId edit (#9493)
When the `chainId` for a custom RPC endpoint is edited, we now migrate
the corresponding address book entries to ensure they are not orphaned.
The address book entries are grouped by the `metamask.network` state,
which unfortunately was sometimes the `chainId`, and sometimes the
`networkId`. It was always the `networkId` for built-in Infura
networks, but for custom RPC endpoints it would be set to the user-set
`chainId` field, with a fallback to the `networkId` of the network.
A recent change will force users to enter valid `chainId`s on all
custom networks, which will be normalized to be hex-prefixed. As a
result, address book contacts will now be keyed by a different string.
The contact entries are now migrated when this edit takes place.
There are some edge cases where two separate entries share the same set
of contacts. For example, if two entries have the same `chainId`, or if
they had the same `networkId` and had no `chainId` set. When the
`chainId` is edited in such cases, the contacts are duplicated on both
networks. This is the best we can do, as we don't have any way to know
which network the contacts _should_ be on.
The `typed-message-manager` unit tests have also been updated as part
of this commit because the addition of `sinon.restore()` to the
preferences controller tests ended up clearing a test object in-between
individual tests in that file. The test object is now re-constructed
before each individual test.
2020-10-07 19:32:17 +02:00
|
|
|
if (!addressBookKey) {
|
|
|
|
// We need to find the networkId to determine what these addresses were keyed by
|
|
|
|
try {
|
2021-06-22 19:39:44 +02:00
|
|
|
addressBookKey = await this.ethersProvider.send('net_version');
|
2021-02-04 19:15:23 +01:00
|
|
|
assert(typeof addressBookKey === 'string');
|
Update address book state upon custom RPC chainId edit (#9493)
When the `chainId` for a custom RPC endpoint is edited, we now migrate
the corresponding address book entries to ensure they are not orphaned.
The address book entries are grouped by the `metamask.network` state,
which unfortunately was sometimes the `chainId`, and sometimes the
`networkId`. It was always the `networkId` for built-in Infura
networks, but for custom RPC endpoints it would be set to the user-set
`chainId` field, with a fallback to the `networkId` of the network.
A recent change will force users to enter valid `chainId`s on all
custom networks, which will be normalized to be hex-prefixed. As a
result, address book contacts will now be keyed by a different string.
The contact entries are now migrated when this edit takes place.
There are some edge cases where two separate entries share the same set
of contacts. For example, if two entries have the same `chainId`, or if
they had the same `networkId` and had no `chainId` set. When the
`chainId` is edited in such cases, the contacts are duplicated on both
networks. This is the best we can do, as we don't have any way to know
which network the contacts _should_ be on.
The `typed-message-manager` unit tests have also been updated as part
of this commit because the addition of `sinon.restore()` to the
preferences controller tests ended up clearing a test object in-between
individual tests in that file. The test object is now re-constructed
before each individual test.
2020-10-07 19:32:17 +02:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.debug(error);
|
2020-11-03 00:41:28 +01:00
|
|
|
log.warn(
|
|
|
|
`Failed to get networkId from ${rpcDetail.rpcUrl}; skipping address book migration`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
Update address book state upon custom RPC chainId edit (#9493)
When the `chainId` for a custom RPC endpoint is edited, we now migrate
the corresponding address book entries to ensure they are not orphaned.
The address book entries are grouped by the `metamask.network` state,
which unfortunately was sometimes the `chainId`, and sometimes the
`networkId`. It was always the `networkId` for built-in Infura
networks, but for custom RPC endpoints it would be set to the user-set
`chainId` field, with a fallback to the `networkId` of the network.
A recent change will force users to enter valid `chainId`s on all
custom networks, which will be normalized to be hex-prefixed. As a
result, address book contacts will now be keyed by a different string.
The contact entries are now migrated when this edit takes place.
There are some edge cases where two separate entries share the same set
of contacts. For example, if two entries have the same `chainId`, or if
they had the same `networkId` and had no `chainId` set. When the
`chainId` is edited in such cases, the contacts are duplicated on both
networks. This is the best we can do, as we don't have any way to know
which network the contacts _should_ be on.
The `typed-message-manager` unit tests have also been updated as part
of this commit because the addition of `sinon.restore()` to the
preferences controller tests ended up clearing a test object in-between
individual tests in that file. The test object is now re-constructed
before each individual test.
2020-10-07 19:32:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is an edge case where two separate RPC endpoints are keyed by the same
|
|
|
|
// value. In this case, the contact book entries are duplicated so that they remain
|
|
|
|
// on both networks, since we don't know which network each contact is intended for.
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let duplicate = false;
|
2020-11-03 00:41:28 +01:00
|
|
|
const builtInProviderNetworkIds = Object.values(
|
|
|
|
NETWORK_TYPE_TO_ID_MAP,
|
2021-02-04 19:15:23 +01:00
|
|
|
).map((ids) => ids.networkId);
|
2020-11-03 00:41:28 +01:00
|
|
|
const otherRpcEntries = rpcList.filter(
|
|
|
|
(entry) => entry.rpcUrl !== newRpcDetails.rpcUrl,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
Update address book state upon custom RPC chainId edit (#9493)
When the `chainId` for a custom RPC endpoint is edited, we now migrate
the corresponding address book entries to ensure they are not orphaned.
The address book entries are grouped by the `metamask.network` state,
which unfortunately was sometimes the `chainId`, and sometimes the
`networkId`. It was always the `networkId` for built-in Infura
networks, but for custom RPC endpoints it would be set to the user-set
`chainId` field, with a fallback to the `networkId` of the network.
A recent change will force users to enter valid `chainId`s on all
custom networks, which will be normalized to be hex-prefixed. As a
result, address book contacts will now be keyed by a different string.
The contact entries are now migrated when this edit takes place.
There are some edge cases where two separate entries share the same set
of contacts. For example, if two entries have the same `chainId`, or if
they had the same `networkId` and had no `chainId` set. When the
`chainId` is edited in such cases, the contacts are duplicated on both
networks. This is the best we can do, as we don't have any way to know
which network the contacts _should_ be on.
The `typed-message-manager` unit tests have also been updated as part
of this commit because the addition of `sinon.restore()` to the
preferences controller tests ended up clearing a test object in-between
individual tests in that file. The test object is now re-constructed
before each individual test.
2020-10-07 19:32:17 +02:00
|
|
|
if (
|
|
|
|
builtInProviderNetworkIds.includes(addressBookKey) ||
|
|
|
|
otherRpcEntries.some((entry) => entry.chainId === addressBookKey)
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
duplicate = true;
|
Update address book state upon custom RPC chainId edit (#9493)
When the `chainId` for a custom RPC endpoint is edited, we now migrate
the corresponding address book entries to ensure they are not orphaned.
The address book entries are grouped by the `metamask.network` state,
which unfortunately was sometimes the `chainId`, and sometimes the
`networkId`. It was always the `networkId` for built-in Infura
networks, but for custom RPC endpoints it would be set to the user-set
`chainId` field, with a fallback to the `networkId` of the network.
A recent change will force users to enter valid `chainId`s on all
custom networks, which will be normalized to be hex-prefixed. As a
result, address book contacts will now be keyed by a different string.
The contact entries are now migrated when this edit takes place.
There are some edge cases where two separate entries share the same set
of contacts. For example, if two entries have the same `chainId`, or if
they had the same `networkId` and had no `chainId` set. When the
`chainId` is edited in such cases, the contacts are duplicated on both
networks. This is the best we can do, as we don't have any way to know
which network the contacts _should_ be on.
The `typed-message-manager` unit tests have also been updated as part
of this commit because the addition of `sinon.restore()` to the
preferences controller tests ended up clearing a test object in-between
individual tests in that file. The test object is now re-constructed
before each individual test.
2020-10-07 19:32:17 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
this.migrateAddressBookState(
|
|
|
|
addressBookKey,
|
|
|
|
updatedRpc.chainId,
|
|
|
|
duplicate,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
Update address book state upon custom RPC chainId edit (#9493)
When the `chainId` for a custom RPC endpoint is edited, we now migrate
the corresponding address book entries to ensure they are not orphaned.
The address book entries are grouped by the `metamask.network` state,
which unfortunately was sometimes the `chainId`, and sometimes the
`networkId`. It was always the `networkId` for built-in Infura
networks, but for custom RPC endpoints it would be set to the user-set
`chainId` field, with a fallback to the `networkId` of the network.
A recent change will force users to enter valid `chainId`s on all
custom networks, which will be normalized to be hex-prefixed. As a
result, address book contacts will now be keyed by a different string.
The contact entries are now migrated when this edit takes place.
There are some edge cases where two separate entries share the same set
of contacts. For example, if two entries have the same `chainId`, or if
they had the same `networkId` and had no `chainId` set. When the
`chainId` is edited in such cases, the contacts are duplicated on both
networks. This is the best we can do, as we don't have any way to know
which network the contacts _should_ be on.
The `typed-message-manager` unit tests have also been updated as part
of this commit because the addition of `sinon.restore()` to the
preferences controller tests ended up clearing a test object in-between
individual tests in that file. The test object is now re-constructed
before each individual test.
2020-10-07 19:32:17 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
rpcList[index] = updatedRpc;
|
|
|
|
this.store.updateState({ frequentRpcListDetail: rpcList });
|
2019-01-29 19:13:51 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
const {
|
|
|
|
rpcUrl,
|
|
|
|
chainId,
|
|
|
|
ticker,
|
|
|
|
nickname,
|
|
|
|
rpcPrefs = {},
|
|
|
|
} = newRpcDetails;
|
|
|
|
this.addToFrequentRpcList(rpcUrl, chainId, ticker, nickname, rpcPrefs);
|
2019-01-29 19:13:51 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
2018-10-19 22:20:54 +02:00
|
|
|
* Adds custom RPC url to state.
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
2020-10-06 19:57:02 +02:00
|
|
|
* @param {string} rpcUrl - The RPC url to add to frequentRpcList.
|
|
|
|
* @param {string} chainId - The chainId of the selected network.
|
|
|
|
* @param {string} [ticker] - Ticker symbol of the selected network.
|
|
|
|
* @param {string} [nickname] - Nickname of the selected network.
|
Update address book state upon custom RPC chainId edit (#9493)
When the `chainId` for a custom RPC endpoint is edited, we now migrate
the corresponding address book entries to ensure they are not orphaned.
The address book entries are grouped by the `metamask.network` state,
which unfortunately was sometimes the `chainId`, and sometimes the
`networkId`. It was always the `networkId` for built-in Infura
networks, but for custom RPC endpoints it would be set to the user-set
`chainId` field, with a fallback to the `networkId` of the network.
A recent change will force users to enter valid `chainId`s on all
custom networks, which will be normalized to be hex-prefixed. As a
result, address book contacts will now be keyed by a different string.
The contact entries are now migrated when this edit takes place.
There are some edge cases where two separate entries share the same set
of contacts. For example, if two entries have the same `chainId`, or if
they had the same `networkId` and had no `chainId` set. When the
`chainId` is edited in such cases, the contacts are duplicated on both
networks. This is the best we can do, as we don't have any way to know
which network the contacts _should_ be on.
The `typed-message-manager` unit tests have also been updated as part
of this commit because the addition of `sinon.restore()` to the
preferences controller tests ended up clearing a test object in-between
individual tests in that file. The test object is now re-constructed
before each individual test.
2020-10-07 19:32:17 +02:00
|
|
|
* @param {Object} [rpcPrefs] - Optional RPC preferences, such as the block explorer URL
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
addToFrequentRpcList(
|
|
|
|
rpcUrl,
|
|
|
|
chainId,
|
|
|
|
ticker = 'ETH',
|
|
|
|
nickname = '',
|
|
|
|
rpcPrefs = {},
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const rpcList = this.getFrequentRpcListDetail();
|
2020-10-12 21:05:40 +02:00
|
|
|
|
2019-11-20 01:03:20 +01:00
|
|
|
const index = rpcList.findIndex((element) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return element.rpcUrl === rpcUrl;
|
|
|
|
});
|
2019-07-31 22:17:11 +02:00
|
|
|
if (index !== -1) {
|
2021-02-04 19:15:23 +01:00
|
|
|
rpcList.splice(index, 1);
|
2019-07-31 22:17:11 +02:00
|
|
|
}
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
if (!isPrefixedFormattedHexString(chainId)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(`Invalid chainId: "${chainId}"`);
|
2018-10-19 22:20:54 +02:00
|
|
|
}
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
rpcList.push({ rpcUrl, chainId, ticker, nickname, rpcPrefs });
|
|
|
|
this.store.updateState({ frequentRpcListDetail: rpcList });
|
2019-07-31 22:17:11 +02:00
|
|
|
}
|
2018-10-19 22:20:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes custom RPC url from state.
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} url - The RPC url to remove from frequentRpcList.
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Promise<array>} Promise resolving to updated frequentRpcList.
|
2018-10-19 22:20:54 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
removeFromFrequentRpcList(url) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const rpcList = this.getFrequentRpcListDetail();
|
2019-11-20 01:03:20 +01:00
|
|
|
const index = rpcList.findIndex((element) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return element.rpcUrl === url;
|
|
|
|
});
|
2018-10-19 22:20:54 +02:00
|
|
|
if (index !== -1) {
|
2021-02-04 19:15:23 +01:00
|
|
|
rpcList.splice(index, 1);
|
2017-02-23 00:12:56 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ frequentRpcListDetail: rpcList });
|
|
|
|
return Promise.resolve(rpcList);
|
2017-02-21 21:32:13 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
2018-10-26 10:26:43 +02:00
|
|
|
* Getter for the `frequentRpcListDetail` property.
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {array<array>} An array of rpc urls.
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getFrequentRpcListDetail() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.store.getState().frequentRpcListDetail;
|
2017-02-21 21:32:13 +01:00
|
|
|
}
|
2017-11-14 17:04:55 +01:00
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Updates the `featureFlags` property, which is an object. One property within that object will be set to a boolean.
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} feature - A key that corresponds to a UI feature.
|
|
|
|
* @param {boolean} activated - Indicates whether or not the UI feature should be displayed
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Promise<object>} Promises a new object; the updated featureFlags object.
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setFeatureFlag(feature, activated) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const currentFeatureFlags = this.store.getState().featureFlags;
|
2017-11-14 17:04:55 +01:00
|
|
|
const updatedFeatureFlags = {
|
|
|
|
...currentFeatureFlags,
|
|
|
|
[feature]: activated,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-11-14 17:04:55 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ featureFlags: updatedFeatureFlags });
|
2017-11-16 20:28:59 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return Promise.resolve(updatedFeatureFlags);
|
2017-11-14 17:04:55 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 01:03:29 +02:00
|
|
|
/**
|
|
|
|
* Updates the `preferences` property, which is an object. These are user-controlled features
|
|
|
|
* found in the settings page.
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} preference - The preference to enable or disable.
|
|
|
|
* @param {boolean} value - Indicates whether or not the preference should be enabled or disabled.
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Promise<object>} Promises a new object; the updated preferences object.
|
2018-10-17 01:03:29 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setPreference(preference, value) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const currentPreferences = this.getPreferences();
|
2018-10-17 01:03:29 +02:00
|
|
|
const updatedPreferences = {
|
|
|
|
...currentPreferences,
|
|
|
|
[preference]: value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ preferences: updatedPreferences });
|
|
|
|
return Promise.resolve(updatedPreferences);
|
2018-10-17 01:03:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A getter for the `preferences` property
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Object} A key-boolean map of user-selected preferences.
|
2018-10-17 01:03:29 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getPreferences() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.store.getState().preferences;
|
2018-10-17 01:03:29 +02:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:25:34 +01:00
|
|
|
/**
|
|
|
|
* Sets the completedOnboarding state to true, indicating that the user has completed the
|
|
|
|
* onboarding process.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
completeOnboarding() {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ completedOnboarding: true });
|
|
|
|
return Promise.resolve(true);
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
2019-12-12 20:28:07 +01:00
|
|
|
/**
|
|
|
|
* A getter for the `ipfsGateway` property
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {string} The current IPFS gateway domain
|
2019-12-12 20:28:07 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getIpfsGateway() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.store.getState().ipfsGateway;
|
2019-12-12 20:28:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A setter for the `ipfsGateway` property
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} domain - The new IPFS gateway domain
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Promise<string>} A promise of the update IPFS gateway domain
|
2019-12-12 20:28:07 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setIpfsGateway(domain) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ ipfsGateway: domain });
|
|
|
|
return Promise.resolve(domain);
|
2019-12-12 20:28:07 +01:00
|
|
|
}
|
|
|
|
|
2021-04-26 20:05:48 +02:00
|
|
|
/**
|
|
|
|
* A setter for the `useLedgerLive` property
|
2021-05-05 15:58:29 +02:00
|
|
|
* @param {bool} useLedgerLive - Value for ledger live support
|
2021-04-26 20:05:48 +02:00
|
|
|
* @returns {Promise<string>} A promise of the update to useLedgerLive
|
|
|
|
*/
|
|
|
|
async setLedgerLivePreference(useLedgerLive) {
|
|
|
|
this.store.updateState({ useLedgerLive });
|
|
|
|
return useLedgerLive;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A getter for the `useLedgerLive` property
|
|
|
|
* @returns {boolean} User preference of using Ledger Live
|
|
|
|
*/
|
|
|
|
getLedgerLivePreference() {
|
|
|
|
return this.store.getState().useLedgerLive;
|
|
|
|
}
|
|
|
|
|
2021-05-05 15:58:29 +02:00
|
|
|
/**
|
|
|
|
* A setter for the user preference to dismiss the seed phrase backup reminder
|
|
|
|
* @param {bool} dismissBackupReminder- User preference for dismissing the back up reminder
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
async setDismissSeedBackUpReminder(dismissSeedBackUpReminder) {
|
|
|
|
await this.store.updateState({
|
|
|
|
dismissSeedBackUpReminder,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-30 21:42:24 +01:00
|
|
|
//
|
|
|
|
// PRIVATE METHODS
|
|
|
|
//
|
2018-08-07 00:28:47 +02:00
|
|
|
|
2021-04-15 19:41:40 +02:00
|
|
|
_subscribeToInfuraAvailability() {
|
|
|
|
this.network.on(NETWORK_EVENTS.INFURA_IS_BLOCKED, () => {
|
|
|
|
this._setInfuraBlocked(true);
|
|
|
|
});
|
|
|
|
this.network.on(NETWORK_EVENTS.INFURA_IS_UNBLOCKED, () => {
|
|
|
|
this._setInfuraBlocked(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* A setter for the `infuraBlocked` property
|
|
|
|
* @param {boolean} isBlocked - Bool indicating whether Infura is blocked
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
_setInfuraBlocked(isBlocked) {
|
|
|
|
const { infuraBlocked } = this.store.getState();
|
|
|
|
|
|
|
|
if (infuraBlocked === isBlocked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.updateState({ infuraBlocked: isBlocked });
|
|
|
|
}
|
|
|
|
|
2018-07-27 00:04:34 +02:00
|
|
|
/**
|
2020-12-08 21:38:00 +01:00
|
|
|
* Updates `accountTokens`, `tokens`, `accountHiddenTokens` and `hiddenTokens` of current account and network according to it.
|
2018-07-27 00:04:34 +02:00
|
|
|
*
|
2020-12-08 21:38:00 +01:00
|
|
|
* @param {array} tokens - Array of tokens to be updated.
|
|
|
|
* @param {array} assetImages - Array of assets objects related to assets added
|
|
|
|
* @param {array} hiddenTokens - Array of tokens hidden by user
|
2018-07-27 00:04:34 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-12-08 21:38:00 +01:00
|
|
|
_updateAccountTokens(tokens, assetImages, hiddenTokens) {
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
accountTokens,
|
2021-02-26 16:40:25 +01:00
|
|
|
chainId,
|
2020-11-03 00:41:28 +01:00
|
|
|
selectedAddress,
|
2020-12-08 21:38:00 +01:00
|
|
|
accountHiddenTokens,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this._getTokenRelatedStates();
|
2021-02-26 16:40:25 +01:00
|
|
|
accountTokens[selectedAddress][chainId] = tokens;
|
|
|
|
accountHiddenTokens[selectedAddress][chainId] = hiddenTokens;
|
2020-12-08 21:38:00 +01:00
|
|
|
this.store.updateState({
|
|
|
|
accountTokens,
|
|
|
|
tokens,
|
|
|
|
assetImages,
|
|
|
|
accountHiddenTokens,
|
|
|
|
hiddenTokens,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-27 22:05:12 +02:00
|
|
|
}
|
2018-07-27 00:04:34 +02:00
|
|
|
|
2021-06-22 19:39:44 +02:00
|
|
|
/**
|
|
|
|
* Detects whether or not a token is ERC-721 compatible.
|
|
|
|
*
|
|
|
|
* @param {string} tokensAddress - the token contract address.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
async _detectIsERC721(tokenAddress) {
|
|
|
|
const checksumAddress = toChecksumHexAddress(tokenAddress);
|
|
|
|
// if this token is already in our contract metadata map we don't need
|
|
|
|
// to check against the contract
|
|
|
|
if (contractsMap[checksumAddress]?.erc721 === true) {
|
|
|
|
return Promise.resolve(true);
|
|
|
|
}
|
|
|
|
const tokenContract = await this._createEthersContract(
|
|
|
|
tokenAddress,
|
|
|
|
abiERC721,
|
|
|
|
this.ethersProvider,
|
|
|
|
);
|
|
|
|
|
|
|
|
return await tokenContract
|
|
|
|
.supportsInterface(ERC721METADATA_INTERFACE_ID)
|
|
|
|
.catch((error) => {
|
|
|
|
console.log('error', error);
|
|
|
|
log.debug(error);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async _createEthersContract(tokenAddress, abi, ethersProvider) {
|
|
|
|
const tokenContract = await new ethers.Contract(
|
|
|
|
tokenAddress,
|
|
|
|
abi,
|
|
|
|
ethersProvider,
|
|
|
|
);
|
|
|
|
return tokenContract;
|
|
|
|
}
|
|
|
|
|
2018-07-27 22:05:12 +02:00
|
|
|
/**
|
2020-12-08 21:38:00 +01:00
|
|
|
* Updates `tokens` and `hiddenTokens` of current account and network.
|
2018-07-27 22:05:12 +02:00
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} selectedAddress - Account address to be updated with.
|
2018-07-27 22:05:12 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_updateTokens(selectedAddress) {
|
2020-12-08 21:38:00 +01:00
|
|
|
const { tokens, hiddenTokens } = this._getTokenRelatedStates(
|
|
|
|
selectedAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
this.store.updateState({ tokens, hiddenTokens });
|
2018-07-31 21:59:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-08 21:38:00 +01:00
|
|
|
* A getter for `tokens`, `accountTokens`, `hiddenTokens` and `accountHiddenTokens` related states.
|
2018-07-31 21:59:19 +02:00
|
|
|
*
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {string} [selectedAddress] - A new hex address for an account
|
|
|
|
* @returns {Object.<array, object, string, string>} States to interact with tokens in `accountTokens`
|
2018-07-31 21:59:19 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_getTokenRelatedStates(selectedAddress) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { accountTokens, accountHiddenTokens } = this.store.getState();
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!selectedAddress) {
|
2020-08-15 13:58:11 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2021-02-04 19:15:23 +01:00
|
|
|
selectedAddress = this.store.getState().selectedAddress;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-26 16:40:25 +01:00
|
|
|
const chainId = this.network.getCurrentChainId();
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!(selectedAddress in accountTokens)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
accountTokens[selectedAddress] = {};
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2020-12-08 21:38:00 +01:00
|
|
|
if (!(selectedAddress in accountHiddenTokens)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
accountHiddenTokens[selectedAddress] = {};
|
2020-12-08 21:38:00 +01:00
|
|
|
}
|
2021-02-26 16:40:25 +01:00
|
|
|
if (!(chainId in accountTokens[selectedAddress])) {
|
|
|
|
accountTokens[selectedAddress][chainId] = [];
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-26 16:40:25 +01:00
|
|
|
if (!(chainId in accountHiddenTokens[selectedAddress])) {
|
|
|
|
accountHiddenTokens[selectedAddress][chainId] = [];
|
2020-12-08 21:38:00 +01:00
|
|
|
}
|
2021-02-26 16:40:25 +01:00
|
|
|
const tokens = accountTokens[selectedAddress][chainId];
|
|
|
|
const hiddenTokens = accountHiddenTokens[selectedAddress][chainId];
|
2020-12-08 21:38:00 +01:00
|
|
|
return {
|
|
|
|
tokens,
|
|
|
|
accountTokens,
|
|
|
|
hiddenTokens,
|
|
|
|
accountHiddenTokens,
|
2021-02-26 16:40:25 +01:00
|
|
|
chainId,
|
2020-12-08 21:38:00 +01:00
|
|
|
selectedAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-08-07 00:28:47 +02:00
|
|
|
}
|
2018-08-16 01:01:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the suggestion of an ERC20 asset through `watchAsset`
|
|
|
|
* *
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {Object} tokenMetadata - Token metadata
|
2018-08-16 01:01:59 +02:00
|
|
|
*
|
|
|
|
*/
|
2020-11-10 18:30:41 +01:00
|
|
|
async _handleWatchAssetERC20(tokenMetadata) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._validateERC20AssetParams(tokenMetadata);
|
2020-12-02 17:49:49 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const address = normalizeAddress(tokenMetadata.address);
|
|
|
|
const { symbol, decimals, image } = tokenMetadata;
|
|
|
|
this._addSuggestedERC20Asset(address, symbol, decimals, image);
|
2020-12-02 17:49:49 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await this.openPopup();
|
2020-12-02 17:49:49 +01:00
|
|
|
const tokenAddresses = this.getTokens().filter(
|
|
|
|
(token) => token.address === address,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return tokenAddresses.length > 0;
|
2018-08-16 01:01:59 +02:00
|
|
|
}
|
2018-08-21 17:59:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates that the passed options for suggested token have all required properties.
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Object} opts - The options object to validate
|
2018-08-21 17:59:42 +02:00
|
|
|
* @throws {string} Throw a custom error indicating that address, symbol and/or decimals
|
|
|
|
* doesn't fulfill requirements
|
|
|
|
*
|
|
|
|
*/
|
2020-12-02 17:49:49 +01:00
|
|
|
_validateERC20AssetParams({ address, symbol, decimals }) {
|
|
|
|
if (!address || !symbol || typeof decimals === 'undefined') {
|
|
|
|
throw ethErrors.rpc.invalidParams(
|
|
|
|
`Must specify address, symbol, and decimals.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2020-11-30 21:59:01 +01:00
|
|
|
if (typeof symbol !== 'string') {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw ethErrors.rpc.invalidParams(`Invalid symbol: not a string.`);
|
2020-11-30 21:59:01 +01:00
|
|
|
}
|
2021-03-26 14:10:17 +01:00
|
|
|
if (!(symbol.length > 0)) {
|
2020-12-02 17:49:49 +01:00
|
|
|
throw ethErrors.rpc.invalidParams(
|
2021-03-26 14:10:17 +01:00
|
|
|
`Invalid symbol "${symbol}": shorter than a character.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!(symbol.length < 12)) {
|
|
|
|
throw ethErrors.rpc.invalidParams(
|
|
|
|
`Invalid symbol "${symbol}": longer than 11 characters.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const numDecimals = parseInt(decimals, 10);
|
2018-08-21 17:59:42 +02:00
|
|
|
if (isNaN(numDecimals) || numDecimals > 36 || numDecimals < 0) {
|
2020-12-02 17:49:49 +01:00
|
|
|
throw ethErrors.rpc.invalidParams(
|
|
|
|
`Invalid decimals "${decimals}": must be 0 <= 36.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-08-21 17:59:42 +02:00
|
|
|
}
|
2021-05-17 21:00:59 +02:00
|
|
|
if (!isValidHexAddress(address, { allowNonPrefixed: false })) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw ethErrors.rpc.invalidParams(`Invalid address "${address}".`);
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-08-21 17:59:42 +02:00
|
|
|
}
|
2020-12-02 17:49:49 +01:00
|
|
|
|
|
|
|
_addSuggestedERC20Asset(address, symbol, decimals, image) {
|
|
|
|
const newEntry = {
|
|
|
|
address,
|
|
|
|
symbol,
|
|
|
|
decimals,
|
|
|
|
image,
|
|
|
|
unlisted: !LISTED_CONTRACT_ADDRESSES.includes(address),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const suggested = this.getSuggestedTokens();
|
|
|
|
suggested[address] = newEntry;
|
|
|
|
this.store.updateState({ suggestedTokens: suggested });
|
2020-12-02 17:49:49 +01:00
|
|
|
}
|
2017-01-30 21:42:24 +01:00
|
|
|
}
|