mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-27 21:00:13 +01:00
f519fa1ed3
* add PermissionsController remove provider approval controller integrate rpc-cap create PermissionsController move provider approval functionality to permissions controller add permissions approval ui, settings page add permissions activity and history move some functionality to metamask-inpage-provider rename siteMetadata -> domainMetadata add accountsChange notification to inpage provider move functionality to inpage provider update inpage provider Remove 'Connections' settings page (#7369) add hooks for exposing accounts in settings rename unused messages in non-English locales Add external extension id to metadata (#7396) update inpage provider, rpc-cap add eth_requestAccounts handling to background prevent notifying connections if extension is locked update inpage provider Fix lint errors add migration review fixes transaction controller review updates removed unused messages * Login Per Site UI (#7368) * LoginPerSite original UI changes to keep * First commit * Get necessary connected tab info for redirect and icon display for permissioned sites * Fix up designs and add missing features * Some lint fixes * More lint fixes * Ensures the tx controller + tx-state-manager orders transactions in the order they are received * Code cleanup for LoginPerSite-ui * Update e2e tests to use new connection flow * Fix display of connect screen and app header after login when connect request present * Update metamask-responsive-ui.spec for new item in accounts dropdown * Fix approve container by replacing approvedOrigins with domainMetaData * Adds test/e2e/permissions.spec.js * Correctly handle cancellation of a permissions request * Redirect to home after disconnecting all sites / cancelling all permissions * Fix display of site icons in menu * Fix height of permissions page container * Remove unused locale messages * Set default values for openExternalTabs and tabIdOrigins in account-menu.container * More code cleanup for LoginPerSite-ui * Use extensions api to close tab in permissions-connect * Remove unnecessary change in domIsReady() in contentscript * Remove unnecessary private function markers and class methods (for background tab info) in metamask-controller. * Adds getOriginOfCurrentTab selector * Adds IconWithFallback component and substitutes for appropriate cases * Add and utilize font mixins * Remove unused method in disconnect-all.container.js * Simplify buttonSizeLarge code in page-container-footer.component.js * Add and utilize getAccountsWithLabels selector * Remove console.log in ui/app/store/actions.js * Change last connected time format to yyyy-M-d * Fix css associated with IconWithFallback change * Ensure tracked openNonMetamaskTabsIDs are correctly set to inactive on tab changes * Code cleanup for LoginPerSite-ui * Use reusable function for modifying openNonMetamaskTabsIDs in background.js * Enables automatic switching to connected account when connected domain is open * Prevent exploit of tabIdOriginMap in background.js * Remove unneeded code from contentscript.js * Simplify current tab origin and window opener logic using remotePort listener tabs.queryTabs * Design and styling fixes for LoginPerSite-ui * Fix permissionHistory and permission logging for eth_requestAccounts and eth_accounts * Front end changes to support display of lastConnected time in connected and permissions screens * Fix lint errors * Refactor structure of permissionsHistory * Fix default values and object modifications for domain and permissionsHistory related data * Fix connecting to new accounts from modal * Replace retweet.svg with connect-white.svg * Fix signature-request.spec * Update metamask-inpage-provider version * Fix permissions e2e tests * Remove unneeded delay from test/e2e/signature-request.spec.js * Add delay before attempting to retrieve network id in dapp in ethereum-on=.spec * Use requestAccountTabIds strategy for determining tab id that opened a given window * Improve default values for permissions requests * Add some message descriptions to app/_locales/en/messages.json * Code clean up in permission controller * Stopped deep cloning object in mapObjectValues * Bump metamask-inpage-provider version * Add missing description in app/_locales/en/messages.json * Return promises from queryTabs and switchToTab of extension.js * Remove unused getAllPermissions function * Use default props in icon-with-fallback.component.js * Stop passing to permissions controller * Delete no longer used clear-approved-origins modal code * Remove duplicate imports in ui/app/components/app/index.scss * Use URL instead of regex in getOriginFromUrl() * Add runtime error checking to platform, promise based extension.tab methods * Support permission requests from external extensions * Improve font size and colour of the domain origin on the permission confirmation screen * Add support for toggling permissions * Ensure getRenderablePermissionsDomains only returns domains with exposedAccount caveat permissions * Remove unused code from LoginPerSite-ui branch * Ensure modal closes on Enter press for new-account-modal.component.js * Lint fix * fixup! Login Per Site UI (#7368) * Some code cleanup for LoginPerSite * Adds UX for connecting to dapps via the connected sites screen (#7593) * Adds UX for connecting to dapps via the connected sites screen * Use openMetaMaskTabIds from background.js to determine if current active tab is MetaMask * Delete unused permissions controller methods * Fixes two small bugs in the LoginPerSite ui (#7595) * Restore `providerRequest` message translations (#7600) This message was removed, but it was replaced with a very similar message called `likeToConnect`. The only difference is that the new message has "MetaMask" in it. Preserving these messages without "MetaMask" is probably better than deleting them, so these messages have all been restored and renamed to `likeToConnect`. * Login per site no sitemetadata fix (#7610) * Support connected sites for which we have no site metadata. * Change property containing subtitle info often populated by origin to a more accurate of purpose name * Lint fix * Improve disconnection modal messages (#7612) * Improve disconnectAccountModalDescription and disconnectAllModalDescription messages * Update disconnectAccountModalDescription app/_locales/en/messages.json Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * Improve disconnectAccount modal message clarity * Adds cancel button to the account selection screen of the permissions request flow (#7613) * Fix eth_accounts permission language & selectability (#7614) * fix eth_accounts language & selectability * fix MetaMask capitalization in all messages * Close sidebar when opening connected sites (#7611) The 'Connected Sites' button in the accounts details now closes the sidebar, if it is open. This was accomplished by pulling the click handler for that button up to the wallet view component, where another button already followed a similar pattern of closing the sidebar. It seemed confusing to me that one handler was in the `AccountsDetails` container component, and one was handed down from above, so I added PropTypes to the container component. I'm not sure that the WalletView component is the best place for this logic, but I've put it there for now to be consistent with the add token button. * Reject permissions request upon tab close (#7618) Permissions requests are now rejected when the page is closed. This only applies to the full-screen view, as that is the view permission requests should be handled in. The case where the user deals with the request through a different view is handled in #7617 * Handle tab update failure (#7619) `extension.tabs.update` can sometimes fail if the user interacts with the tabs directly around the same time. The redirect flow has been updated to ensure that the permissions tab is still closed in that case. The user is on their own to find the dapp tab again in that case. * Login per site tab popup fixes (#7617) * Handle redirect in response to state update in permissions-connect * Ensure origin is available to permissions-connect subcomponents during redirect * Hide app bar whenever on redirect route * Improvements to handling of redirects in permissions-connect * Ensure permission request id change handling only happens when page is not null * Lint fix * Decouple confirm transaction screen from the selected address (#7622) * Avoid race condtion that could prevent contextual account switching (#7623) There was a race condition in the logic responsible for switching the selected account based upon the active tab. It was asynchronously querying the active tab, then assuming it had been retrieved later. The active tab info itself was already in the redux store in another spot, one that is guaranteed to be set before the UI renders. The race condition was avoided by deleting the duplicate state, and using the other active tab state. * Only redirect back to dapp if current tab is active (#7621) The "redirect back to dapp" behaviour can be disruptive when the permissions connect tab is not active. The purpose of the redirect was to maintain context between the dapp and the permissions request, but if the user has already moved to another tab, that no longer applies. * Fix JSX style lint errors * Remove unused state
761 lines
24 KiB
JavaScript
761 lines
24 KiB
JavaScript
const ObservableStore = require('obs-store')
|
|
const { addInternalMethodPrefix } = require('./permissions')
|
|
const normalizeAddress = require('eth-sig-util').normalize
|
|
const { isValidAddress, sha3, bufferToHex } = require('ethereumjs-util')
|
|
const extend = require('xtend')
|
|
|
|
|
|
class PreferencesController {
|
|
|
|
/**
|
|
*
|
|
* @typedef {Object} PreferencesController
|
|
* @param {object} opts Overrides the defaults for the initial state of this.store
|
|
* @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 {string} store.currentAccountTab Indicates the selected tab in the ui
|
|
* @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
|
|
* @property {boolean} store.useBlockie The users preference for blockie identicons within the UI
|
|
* @property {boolean} store.useNonceField The users preference for nonce field within the UI
|
|
* @property {object} store.featureFlags A key-boolean map, where keys refer to features and booleans to whether the
|
|
* 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.
|
|
* @property {object} store.knownMethodData Contains all data methods known by the user
|
|
* @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
|
|
*
|
|
*/
|
|
constructor (opts = {}) {
|
|
const initState = extend({
|
|
frequentRpcListDetail: [],
|
|
currentAccountTab: 'history',
|
|
accountTokens: {},
|
|
assetImages: {},
|
|
tokens: [],
|
|
suggestedTokens: {},
|
|
useBlockie: false,
|
|
useNonceField: false,
|
|
|
|
// 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.
|
|
featureFlags: {
|
|
showIncomingTransactions: true,
|
|
transactionTime: false,
|
|
},
|
|
knownMethodData: {},
|
|
participateInMetaMetrics: null,
|
|
firstTimeFlowType: null,
|
|
currentLocale: opts.initLangCode,
|
|
identities: {},
|
|
lostIdentities: {},
|
|
forgottenPassword: false,
|
|
preferences: {
|
|
useNativeCurrencyAsPrimaryCurrency: true,
|
|
},
|
|
completedOnboarding: false,
|
|
metaMetricsId: null,
|
|
metaMetricsSendCount: 0,
|
|
}, opts.initState)
|
|
|
|
this.diagnostics = opts.diagnostics
|
|
this.network = opts.network
|
|
this.store = new ObservableStore(initState)
|
|
this.openPopup = opts.openPopup
|
|
this._subscribeProviderType()
|
|
|
|
global.setPreference = (key, value) => {
|
|
return this.setFeatureFlag(key, value)
|
|
}
|
|
}
|
|
// PUBLIC METHODS
|
|
|
|
/**
|
|
* Sets the {@code forgottenPassword} state property
|
|
* @param {boolean} forgottenPassword whether or not the user has forgotten their password
|
|
*/
|
|
setPasswordForgotten (forgottenPassword) {
|
|
this.store.updateState({ forgottenPassword })
|
|
}
|
|
|
|
/**
|
|
* Setter for the `useBlockie` property
|
|
*
|
|
* @param {boolean} val Whether or not the user prefers blockie indicators
|
|
*
|
|
*/
|
|
setUseBlockie (val) {
|
|
this.store.updateState({ useBlockie: val })
|
|
}
|
|
|
|
/**
|
|
* Setter for the `useNonceField` property
|
|
*
|
|
* @param {boolean} val Whether or not the user prefers to set nonce
|
|
*
|
|
*/
|
|
setUseNonceField (val) {
|
|
this.store.updateState({ useNonceField: val })
|
|
}
|
|
|
|
/**
|
|
* Setter for the `participateInMetaMetrics` property
|
|
*
|
|
* @param {boolean} bool Whether or not the user wants to participate in MetaMetrics
|
|
* @returns {string|null} the string of the new metametrics id, or null if not set
|
|
*
|
|
*/
|
|
setParticipateInMetaMetrics (bool) {
|
|
this.store.updateState({ participateInMetaMetrics: bool })
|
|
let metaMetricsId = null
|
|
if (bool && !this.store.getState().metaMetricsId) {
|
|
metaMetricsId = bufferToHex(sha3(String(Date.now()) + String(Math.round(Math.random() * Number.MAX_SAFE_INTEGER))))
|
|
this.store.updateState({ metaMetricsId })
|
|
} else if (bool === false) {
|
|
this.store.updateState({ metaMetricsId })
|
|
}
|
|
return metaMetricsId
|
|
}
|
|
|
|
getMetaMetricsId () {
|
|
return this.store.getState().metaMetricsId
|
|
}
|
|
|
|
getParticipateInMetaMetrics () {
|
|
return this.store.getState().participateInMetaMetrics
|
|
}
|
|
|
|
setMetaMetricsSendCount (val) {
|
|
this.store.updateState({ metaMetricsSendCount: val })
|
|
}
|
|
|
|
getMetaMetricsSendCount () {
|
|
return this.store.getState().metaMetricsSendCount
|
|
}
|
|
|
|
/**
|
|
* Setter for the `firstTimeFlowType` property
|
|
*
|
|
* @param {String} type Indicates the type of first time flow - create or import - the user wishes to follow
|
|
*
|
|
*/
|
|
setFirstTimeFlowType (type) {
|
|
this.store.updateState({ firstTimeFlowType: type })
|
|
}
|
|
|
|
|
|
getSuggestedTokens () {
|
|
return this.store.getState().suggestedTokens
|
|
}
|
|
|
|
getAssetImages () {
|
|
return this.store.getState().assetImages
|
|
}
|
|
|
|
addSuggestedERC20Asset (tokenOpts) {
|
|
this._validateERC20AssetParams(tokenOpts)
|
|
const suggested = this.getSuggestedTokens()
|
|
const { rawAddress, symbol, decimals, image } = tokenOpts
|
|
const address = normalizeAddress(rawAddress)
|
|
const newEntry = { address, symbol, decimals, image }
|
|
suggested[address] = newEntry
|
|
this.store.updateState({ suggestedTokens: suggested })
|
|
}
|
|
|
|
/**
|
|
* Add new methodData to state, to avoid requesting this information again through Infura
|
|
*
|
|
* @param {string} fourBytePrefix Four-byte method signature
|
|
* @param {string} methodData Corresponding data method
|
|
*/
|
|
addKnownMethodData (fourBytePrefix, methodData) {
|
|
const knownMethodData = this.store.getState().knownMethodData
|
|
knownMethodData[fourBytePrefix] = methodData
|
|
this.store.updateState({ knownMethodData })
|
|
}
|
|
|
|
/**
|
|
* RPC engine middleware for requesting new asset added
|
|
*
|
|
* @param req
|
|
* @param res
|
|
* @param {Function} - next
|
|
* @param {Function} - end
|
|
*/
|
|
async requestWatchAsset (req, res, next, end) {
|
|
if (
|
|
req.method === 'metamask_watchAsset' ||
|
|
req.method === addInternalMethodPrefix('watchAsset')
|
|
) {
|
|
const { type, options } = req.params
|
|
switch (type) {
|
|
case 'ERC20':
|
|
const result = await this._handleWatchAssetERC20(options)
|
|
if (result instanceof Error) {
|
|
end(result)
|
|
} else {
|
|
res.result = result
|
|
end()
|
|
}
|
|
break
|
|
default:
|
|
end(new Error(`Asset of type ${type} not supported`))
|
|
}
|
|
} else {
|
|
next()
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Getter for the `useBlockie` property
|
|
*
|
|
* @returns {boolean} this.store.useBlockie
|
|
*
|
|
*/
|
|
getUseBlockie () {
|
|
return this.store.getState().useBlockie
|
|
}
|
|
|
|
/**
|
|
* Getter for the `getUseNonceField` property
|
|
*
|
|
* @returns {boolean} this.store.getUseNonceField
|
|
*
|
|
*/
|
|
getUseNonceField () {
|
|
return this.store.getState().useNonceField
|
|
}
|
|
|
|
/**
|
|
* Setter for the `currentLocale` property
|
|
*
|
|
* @param {string} key he preferred language locale key
|
|
*
|
|
*/
|
|
setCurrentLocale (key) {
|
|
const textDirection = (['ar', 'dv', 'fa', 'he', 'ku'].includes(key)) ? 'rtl' : 'auto'
|
|
this.store.updateState({
|
|
currentLocale: key,
|
|
textDirection: textDirection,
|
|
})
|
|
return textDirection
|
|
}
|
|
|
|
/**
|
|
* Updates identities to only include specified addresses. Removes identities
|
|
* not included in addresses array
|
|
*
|
|
* @param {string[]} addresses An array of hex addresses
|
|
*
|
|
*/
|
|
setAddresses (addresses) {
|
|
const oldIdentities = this.store.getState().identities
|
|
const oldAccountTokens = this.store.getState().accountTokens
|
|
|
|
const identities = addresses.reduce((ids, address, index) => {
|
|
const oldId = oldIdentities[address] || {}
|
|
ids[address] = {name: `Account ${index + 1}`, address, ...oldId}
|
|
return ids
|
|
}, {})
|
|
const accountTokens = addresses.reduce((tokens, address) => {
|
|
const oldTokens = oldAccountTokens[address] || {}
|
|
tokens[address] = oldTokens
|
|
return tokens
|
|
}, {})
|
|
this.store.updateState({ identities, accountTokens })
|
|
}
|
|
|
|
/**
|
|
* Removes an address from state
|
|
*
|
|
* @param {string} address A hex address
|
|
* @returns {string} the address that was removed
|
|
*/
|
|
removeAddress (address) {
|
|
const identities = this.store.getState().identities
|
|
const accountTokens = this.store.getState().accountTokens
|
|
if (!identities[address]) {
|
|
throw new Error(`${address} can't be deleted cause it was not found`)
|
|
}
|
|
delete identities[address]
|
|
delete accountTokens[address]
|
|
this.store.updateState({ identities, accountTokens })
|
|
|
|
// If the selected account is no longer valid,
|
|
// select an arbitrary other account:
|
|
if (address === this.getSelectedAddress()) {
|
|
const selected = Object.keys(identities)[0]
|
|
this.setSelectedAddress(selected)
|
|
}
|
|
return address
|
|
}
|
|
|
|
|
|
/**
|
|
* Adds addresses to the identities object without removing identities
|
|
*
|
|
* @param {string[]} addresses An array of hex addresses
|
|
*
|
|
*/
|
|
addAddresses (addresses) {
|
|
const identities = this.store.getState().identities
|
|
const accountTokens = this.store.getState().accountTokens
|
|
addresses.forEach((address) => {
|
|
// skip if already exists
|
|
if (identities[address]) {
|
|
return
|
|
}
|
|
// add missing identity
|
|
const identityCount = Object.keys(identities).length
|
|
|
|
accountTokens[address] = {}
|
|
identities[address] = { name: `Account ${identityCount + 1}`, address }
|
|
})
|
|
this.store.updateState({ identities, accountTokens })
|
|
}
|
|
|
|
/*
|
|
* Synchronizes identity entries with known accounts.
|
|
* Removes any unknown identities, and returns the resulting selected address.
|
|
*
|
|
* @param {Array<string>} addresses known to the vault.
|
|
* @returns {Promise<string>} selectedAddress the selected address.
|
|
*/
|
|
syncAddresses (addresses) {
|
|
const { identities, lostIdentities } = this.store.getState()
|
|
|
|
const newlyLost = {}
|
|
Object.keys(identities).forEach((identity) => {
|
|
if (!addresses.includes(identity)) {
|
|
newlyLost[identity] = identities[identity]
|
|
delete identities[identity]
|
|
}
|
|
})
|
|
|
|
// Identities are no longer present.
|
|
if (Object.keys(newlyLost).length > 0) {
|
|
|
|
// Notify our servers:
|
|
if (this.diagnostics) {
|
|
this.diagnostics.reportOrphans(newlyLost)
|
|
}
|
|
|
|
// store lost accounts
|
|
for (const key in newlyLost) {
|
|
lostIdentities[key] = newlyLost[key]
|
|
}
|
|
}
|
|
|
|
this.store.updateState({ identities, lostIdentities })
|
|
this.addAddresses(addresses)
|
|
|
|
// If the selected account is no longer valid,
|
|
// select an arbitrary other account:
|
|
let selected = this.getSelectedAddress()
|
|
if (!addresses.includes(selected)) {
|
|
selected = addresses[0]
|
|
this.setSelectedAddress(selected)
|
|
}
|
|
|
|
return selected
|
|
}
|
|
|
|
removeSuggestedTokens () {
|
|
return new Promise((resolve) => {
|
|
this.store.updateState({ suggestedTokens: {} })
|
|
resolve({})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Setter for the `selectedAddress` property
|
|
*
|
|
* @param {string} _address A new hex address for an account
|
|
* @returns {Promise<void>} Promise resolves with tokens
|
|
*
|
|
*/
|
|
setSelectedAddress (_address) {
|
|
const address = normalizeAddress(_address)
|
|
this._updateTokens(address)
|
|
this.store.updateState({ selectedAddress: address })
|
|
const tokens = this.store.getState().tokens
|
|
return Promise.resolve(tokens)
|
|
}
|
|
|
|
/**
|
|
* Getter for the `selectedAddress` property
|
|
*
|
|
* @returns {string} The hex address for the currently selected account
|
|
*
|
|
*/
|
|
getSelectedAddress () {
|
|
return this.store.getState().selectedAddress
|
|
}
|
|
|
|
/**
|
|
* 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}
|
|
*/
|
|
|
|
/**
|
|
* Adds a new token to the token array, or updates the token if passed an address that already exists.
|
|
* Modifies the existing tokens array from the store. All objects in the tokens array array AddedToken objects.
|
|
* @see AddedToken {@link AddedToken}
|
|
*
|
|
* @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
|
|
* @param {number} decimals The number of decimals the token uses.
|
|
* @returns {Promise<array>} Promises the new array of AddedToken objects.
|
|
*
|
|
*/
|
|
async addToken (rawAddress, symbol, decimals, image) {
|
|
const address = normalizeAddress(rawAddress)
|
|
const newEntry = { address, symbol, decimals }
|
|
const tokens = this.store.getState().tokens
|
|
const assetImages = this.getAssetImages()
|
|
const previousEntry = tokens.find((token) => {
|
|
return token.address === address
|
|
})
|
|
const previousIndex = tokens.indexOf(previousEntry)
|
|
|
|
if (previousEntry) {
|
|
tokens[previousIndex] = newEntry
|
|
} else {
|
|
tokens.push(newEntry)
|
|
}
|
|
assetImages[address] = image
|
|
this._updateAccountTokens(tokens, assetImages)
|
|
return Promise.resolve(tokens)
|
|
}
|
|
|
|
/**
|
|
* Removes a specified token from the tokens array.
|
|
*
|
|
* @param {string} rawAddress Hex address of the token contract to remove.
|
|
* @returns {Promise<array>} The new array of AddedToken objects
|
|
*
|
|
*/
|
|
removeToken (rawAddress) {
|
|
const tokens = this.store.getState().tokens
|
|
const assetImages = this.getAssetImages()
|
|
const updatedTokens = tokens.filter(token => token.address !== rawAddress)
|
|
delete assetImages[rawAddress]
|
|
this._updateAccountTokens(updatedTokens, assetImages)
|
|
return Promise.resolve(updatedTokens)
|
|
}
|
|
|
|
/**
|
|
* A getter for the `tokens` property
|
|
*
|
|
* @returns {array} The current array of AddedToken objects
|
|
*
|
|
*/
|
|
getTokens () {
|
|
return this.store.getState().tokens
|
|
}
|
|
|
|
/**
|
|
* Sets a custom label for an account
|
|
* @param {string} account the account to set a label for
|
|
* @param {string} label the custom label for the account
|
|
* @return {Promise<string>}
|
|
*/
|
|
setAccountLabel (account, label) {
|
|
if (!account) {
|
|
throw new Error('setAccountLabel requires a valid address, got ' + String(account))
|
|
}
|
|
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)
|
|
}
|
|
|
|
/**
|
|
* Setter for the `currentAccountTab` property
|
|
*
|
|
* @param {string} currentAccountTab Specifies the new tab to be marked as current
|
|
* @returns {Promise<void>} Promise resolves with undefined
|
|
*
|
|
*/
|
|
setCurrentAccountTab (currentAccountTab) {
|
|
return new Promise((resolve) => {
|
|
this.store.updateState({ currentAccountTab })
|
|
resolve()
|
|
})
|
|
}
|
|
|
|
/**
|
|
* updates custom RPC details
|
|
*
|
|
* @param {string} url The RPC url to add to frequentRpcList.
|
|
* @param {number} chainId Optional chainId of the selected network.
|
|
* @param {string} ticker Optional ticker symbol of the selected network.
|
|
* @param {string} nickname Optional nickname of the selected network.
|
|
* @returns {Promise<array>} Promise resolving to updated frequentRpcList.
|
|
*
|
|
*/
|
|
|
|
|
|
updateRpc (newRpcDetails) {
|
|
const rpcList = this.getFrequentRpcListDetail()
|
|
const index = rpcList.findIndex((element) => {
|
|
return element.rpcUrl === newRpcDetails.rpcUrl
|
|
})
|
|
if (index > -1) {
|
|
const rpcDetail = rpcList[index]
|
|
const updatedRpc = extend(rpcDetail, newRpcDetails)
|
|
rpcList[index] = updatedRpc
|
|
this.store.updateState({ frequentRpcListDetail: rpcList })
|
|
} else {
|
|
const { rpcUrl, chainId, ticker, nickname, rpcPrefs = {} } = newRpcDetails
|
|
return this.addToFrequentRpcList(rpcUrl, chainId, ticker, nickname, rpcPrefs)
|
|
}
|
|
return Promise.resolve(rpcList)
|
|
}
|
|
/**
|
|
* Adds custom RPC url to state.
|
|
*
|
|
* @param {string} url The RPC url to add to frequentRpcList.
|
|
* @param {number} chainId Optional chainId of the selected network.
|
|
* @param {string} ticker Optional ticker symbol of the selected network.
|
|
* @param {string} nickname Optional nickname of the selected network.
|
|
* @returns {Promise<array>} Promise resolving to updated frequentRpcList.
|
|
*
|
|
*/
|
|
addToFrequentRpcList (url, chainId, ticker = 'ETH', nickname = '', rpcPrefs = {}) {
|
|
const rpcList = this.getFrequentRpcListDetail()
|
|
const index = rpcList.findIndex((element) => {
|
|
return element.rpcUrl === url
|
|
})
|
|
if (index !== -1) {
|
|
rpcList.splice(index, 1)
|
|
}
|
|
if (url !== 'http://localhost:8545') {
|
|
let checkedChainId
|
|
if (!!chainId && !Number.isNaN(parseInt(chainId))) {
|
|
checkedChainId = chainId
|
|
}
|
|
rpcList.push({ rpcUrl: url, chainId: checkedChainId, ticker, nickname, rpcPrefs })
|
|
}
|
|
this.store.updateState({ frequentRpcListDetail: rpcList })
|
|
return Promise.resolve(rpcList)
|
|
}
|
|
|
|
/**
|
|
* Removes custom RPC url from state.
|
|
*
|
|
* @param {string} url The RPC url to remove from frequentRpcList.
|
|
* @returns {Promise<array>} Promise resolving to updated frequentRpcList.
|
|
*
|
|
*/
|
|
removeFromFrequentRpcList (url) {
|
|
const rpcList = this.getFrequentRpcListDetail()
|
|
const index = rpcList.findIndex((element) => {
|
|
return element.rpcUrl === url
|
|
})
|
|
if (index !== -1) {
|
|
rpcList.splice(index, 1)
|
|
}
|
|
this.store.updateState({ frequentRpcListDetail: rpcList })
|
|
return Promise.resolve(rpcList)
|
|
}
|
|
|
|
/**
|
|
* Getter for the `frequentRpcListDetail` property.
|
|
*
|
|
* @returns {array<array>} An array of rpc urls.
|
|
*
|
|
*/
|
|
getFrequentRpcListDetail () {
|
|
return this.store.getState().frequentRpcListDetail
|
|
}
|
|
|
|
/**
|
|
* Updates the `featureFlags` property, which is an object. One property within that object will be set to a boolean.
|
|
*
|
|
* @param {string} feature A key that corresponds to a UI feature.
|
|
* @param {boolean} activated Indicates whether or not the UI feature should be displayed
|
|
* @returns {Promise<object>} Promises a new object; the updated featureFlags object.
|
|
*
|
|
*/
|
|
setFeatureFlag (feature, activated) {
|
|
const currentFeatureFlags = this.store.getState().featureFlags
|
|
const updatedFeatureFlags = {
|
|
...currentFeatureFlags,
|
|
[feature]: activated,
|
|
}
|
|
|
|
this.store.updateState({ featureFlags: updatedFeatureFlags })
|
|
|
|
return Promise.resolve(updatedFeatureFlags)
|
|
}
|
|
|
|
/**
|
|
* A getter for the `featureFlags` property
|
|
*
|
|
* @returns {object} A key-boolean map, where keys refer to features and booleans to whether the
|
|
* user wishes to see that feature
|
|
*
|
|
*/
|
|
getFeatureFlags () {
|
|
return this.store.getState().featureFlags
|
|
}
|
|
|
|
/**
|
|
* Updates the `preferences` property, which is an object. These are user-controlled features
|
|
* found in the settings page.
|
|
* @param {string} preference The preference to enable or disable.
|
|
* @param {boolean} value Indicates whether or not the preference should be enabled or disabled.
|
|
* @returns {Promise<object>} Promises a new object; the updated preferences object.
|
|
*/
|
|
setPreference (preference, value) {
|
|
const currentPreferences = this.getPreferences()
|
|
const updatedPreferences = {
|
|
...currentPreferences,
|
|
[preference]: value,
|
|
}
|
|
|
|
this.store.updateState({ preferences: updatedPreferences })
|
|
return Promise.resolve(updatedPreferences)
|
|
}
|
|
|
|
/**
|
|
* A getter for the `preferences` property
|
|
* @returns {object} A key-boolean map of user-selected preferences.
|
|
*/
|
|
getPreferences () {
|
|
return this.store.getState().preferences
|
|
}
|
|
|
|
/**
|
|
* Sets the completedOnboarding state to true, indicating that the user has completed the
|
|
* onboarding process.
|
|
*/
|
|
completeOnboarding () {
|
|
this.store.updateState({ completedOnboarding: true })
|
|
return Promise.resolve(true)
|
|
}
|
|
|
|
//
|
|
// PRIVATE METHODS
|
|
//
|
|
|
|
/**
|
|
* Subscription to network provider type.
|
|
*
|
|
*
|
|
*/
|
|
_subscribeProviderType () {
|
|
this.network.providerStore.subscribe(() => {
|
|
const { tokens } = this._getTokenRelatedStates()
|
|
this.store.updateState({ tokens })
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Updates `accountTokens` and `tokens` of current account and network according to it.
|
|
*
|
|
* @param {array} tokens Array of tokens to be updated.
|
|
*
|
|
*/
|
|
_updateAccountTokens (tokens, assetImages) {
|
|
const { accountTokens, providerType, selectedAddress } = this._getTokenRelatedStates()
|
|
accountTokens[selectedAddress][providerType] = tokens
|
|
this.store.updateState({ accountTokens, tokens, assetImages })
|
|
}
|
|
|
|
/**
|
|
* Updates `tokens` of current account and network.
|
|
*
|
|
* @param {string} selectedAddress Account address to be updated with.
|
|
*
|
|
*/
|
|
_updateTokens (selectedAddress) {
|
|
const { tokens } = this._getTokenRelatedStates(selectedAddress)
|
|
this.store.updateState({ tokens })
|
|
}
|
|
|
|
/**
|
|
* A getter for `tokens` and `accountTokens` related states.
|
|
*
|
|
* @param {string} selectedAddress A new hex address for an account
|
|
* @returns {Object.<array, object, string, string>} States to interact with tokens in `accountTokens`
|
|
*
|
|
*/
|
|
_getTokenRelatedStates (selectedAddress) {
|
|
const accountTokens = this.store.getState().accountTokens
|
|
if (!selectedAddress) {
|
|
selectedAddress = this.store.getState().selectedAddress
|
|
}
|
|
const providerType = this.network.providerStore.getState().type
|
|
if (!(selectedAddress in accountTokens)) {
|
|
accountTokens[selectedAddress] = {}
|
|
}
|
|
if (!(providerType in accountTokens[selectedAddress])) {
|
|
accountTokens[selectedAddress][providerType] = []
|
|
}
|
|
const tokens = accountTokens[selectedAddress][providerType]
|
|
return { tokens, accountTokens, providerType, selectedAddress }
|
|
}
|
|
|
|
/**
|
|
* Handle the suggestion of an ERC20 asset through `watchAsset`
|
|
* *
|
|
* @param {Promise} promise Promise according to addition of ERC20 token
|
|
*
|
|
*/
|
|
async _handleWatchAssetERC20 (options) {
|
|
const { address, symbol, decimals, image } = options
|
|
const rawAddress = address
|
|
try {
|
|
this._validateERC20AssetParams({ rawAddress, symbol, decimals })
|
|
} catch (err) {
|
|
return err
|
|
}
|
|
const tokenOpts = { rawAddress, decimals, symbol, image }
|
|
this.addSuggestedERC20Asset(tokenOpts)
|
|
return this.openPopup().then(() => {
|
|
const tokenAddresses = this.getTokens().filter(token => token.address === normalizeAddress(rawAddress))
|
|
return tokenAddresses.length > 0
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Validates that the passed options for suggested token have all required properties.
|
|
*
|
|
* @param {Object} opts The options object to validate
|
|
* @throws {string} Throw a custom error indicating that address, symbol and/or decimals
|
|
* doesn't fulfill requirements
|
|
*
|
|
*/
|
|
_validateERC20AssetParams (opts) {
|
|
const { rawAddress, symbol, decimals } = opts
|
|
if (!rawAddress || !symbol || typeof decimals === 'undefined') {
|
|
throw new Error(`Cannot suggest token without address, symbol, and decimals`)
|
|
}
|
|
if (!(symbol.length < 7)) {
|
|
throw new Error(`Invalid symbol ${symbol} more than six characters`)
|
|
}
|
|
const numDecimals = parseInt(decimals, 10)
|
|
if (isNaN(numDecimals) || numDecimals > 36 || numDecimals < 0) {
|
|
throw new Error(`Invalid decimals ${decimals} must be at least 0, and not over 36`)
|
|
}
|
|
if (!isValidAddress(rawAddress)) {
|
|
throw new Error(`Invalid address ${rawAddress}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = PreferencesController
|