2017-01-30 21:42:24 +01:00
|
|
|
const ObservableStore = require('obs-store')
|
2017-02-21 23:25:47 +01:00
|
|
|
const normalizeAddress = require('eth-sig-util').normalize
|
2017-02-23 00:12:56 +01:00
|
|
|
const extend = require('xtend')
|
2018-06-04 23:59:46 +02:00
|
|
|
const notifier = require('../lib/bug-notifier')
|
|
|
|
const { version } = require('../../manifest.json')
|
2017-01-30 21:42:24 +01:00
|
|
|
|
|
|
|
class PreferencesController {
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
*
|
2018-04-16 19:08:04 +02:00
|
|
|
* @typedef {Object} PreferencesController
|
2018-04-23 18:41:02 +02:00
|
|
|
* @param {object} opts Overrides the defaults for the initial state of this.store
|
2018-04-19 17:38:56 +02:00
|
|
|
* @property {object} store The stored object containing a users preferences, stored in local storage
|
2018-04-16 19:08:04 +02:00
|
|
|
* @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 {boolean} store.useBlockie The users preference for blockie identicons 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
|
|
|
|
* @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
|
|
|
*/
|
2017-01-30 21:42:24 +01:00
|
|
|
constructor (opts = {}) {
|
2017-03-09 22:07:38 +01:00
|
|
|
const initState = extend({
|
|
|
|
frequentRpcList: [],
|
2017-06-14 23:21:50 +02:00
|
|
|
currentAccountTab: 'history',
|
2017-06-16 01:22:53 +02:00
|
|
|
tokens: [],
|
2017-11-27 16:11:48 +01:00
|
|
|
useBlockie: false,
|
2017-11-14 17:04:55 +01:00
|
|
|
featureFlags: {},
|
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: {},
|
2017-03-09 22:07:38 +01:00
|
|
|
}, opts.initState)
|
2018-06-04 23:21:46 +02:00
|
|
|
|
2017-01-30 21:42:24 +01:00
|
|
|
this.store = new ObservableStore(initState)
|
|
|
|
}
|
2017-06-16 01:22:53 +02:00
|
|
|
// PUBLIC METHODS
|
2017-01-30 21:42:24 +01:00
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Setter for the `useBlockie` property
|
|
|
|
*
|
|
|
|
* @param {boolean} val Whether or not the user prefers blockie indicators
|
|
|
|
*
|
|
|
|
*/
|
2017-11-24 18:35:17 +01:00
|
|
|
setUseBlockie (val) {
|
|
|
|
this.store.updateState({ useBlockie: val })
|
2017-11-24 02:33:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Getter for the `useBlockie` property
|
|
|
|
*
|
|
|
|
* @returns {boolean} this.store.useBlockie
|
|
|
|
*
|
|
|
|
*/
|
2017-11-24 02:33:44 +01:00
|
|
|
getUseBlockie () {
|
|
|
|
return this.store.getState().useBlockie
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Setter for the `currentLocale` property
|
2018-04-16 19:08:04 +02:00
|
|
|
*
|
|
|
|
* @param {string} key he preferred language locale key
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2018-03-16 01:29:45 +01:00
|
|
|
setCurrentLocale (key) {
|
|
|
|
this.store.updateState({ currentLocale: key })
|
|
|
|
}
|
|
|
|
|
2018-06-03 20:27:25 +02:00
|
|
|
/**
|
|
|
|
* Updates identities to only include specified addresses. Removes identities
|
|
|
|
* not included in addresses array
|
|
|
|
*
|
2018-06-03 20:30:11 +02:00
|
|
|
* @param {string[]} addresses An array of hex addresses
|
2018-06-03 20:27:25 +02:00
|
|
|
*
|
|
|
|
*/
|
2018-04-19 05:33:51 +02:00
|
|
|
setAddresses (addresses) {
|
|
|
|
const oldIdentities = this.store.getState().identities
|
|
|
|
const identities = addresses.reduce((ids, address, index) => {
|
|
|
|
const oldId = oldIdentities[address] || {}
|
|
|
|
ids[address] = {name: `Account ${index + 1}`, address, ...oldId}
|
|
|
|
return ids
|
|
|
|
}, {})
|
|
|
|
this.store.updateState({ identities })
|
|
|
|
}
|
|
|
|
|
2018-06-03 20:27:25 +02:00
|
|
|
/**
|
|
|
|
* Adds addresses to the identities object without removing identities
|
|
|
|
*
|
2018-06-03 20:30:11 +02:00
|
|
|
* @param {string[]} addresses An array of hex addresses
|
2018-06-03 20:27:25 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
addAddresses (addresses) {
|
|
|
|
const identities = this.store.getState().identities
|
|
|
|
addresses.forEach((address) => {
|
|
|
|
// skip if already exists
|
|
|
|
if (identities[address]) return
|
|
|
|
// add missing identity
|
|
|
|
const identityCount = Object.keys(identities).length
|
|
|
|
identities[address] = { name: `Account ${identityCount + 1}`, address }
|
|
|
|
})
|
|
|
|
this.store.updateState({ identities })
|
|
|
|
}
|
|
|
|
|
2018-06-04 22:43:26 +02:00
|
|
|
/*
|
|
|
|
* 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) {
|
2018-06-04 23:05:56 +02:00
|
|
|
let { identities, lostIdentities } = this.store.getState()
|
2018-06-04 23:24:45 +02:00
|
|
|
|
|
|
|
let newlyLost = {}
|
2018-06-04 22:43:26 +02:00
|
|
|
Object.keys(identities).forEach((identity) => {
|
|
|
|
if (!addresses.includes(identity)) {
|
|
|
|
delete identities[identity]
|
2018-06-04 23:24:45 +02:00
|
|
|
newlyLost[identity] = identities[identity]
|
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-04 23:21:46 +02:00
|
|
|
|
|
|
|
// timeout to prevent blocking the thread:
|
|
|
|
setTimeout(() => {
|
|
|
|
alert('Error 4486: MetaMask has encountered a very strange error. Please open a support issue immediately at support@metamask.io.')
|
|
|
|
}, 10)
|
|
|
|
|
|
|
|
// Notify our servers:
|
2018-06-04 23:22:34 +02:00
|
|
|
const uri = 'https://diagnostics.metamask.io/v1/orphanedAccounts'
|
2018-06-04 23:59:46 +02:00
|
|
|
notifier.notify(uri, { accounts: Object.keys(newlyLost), version })
|
2018-06-04 23:21:46 +02:00
|
|
|
.catch(log.error)
|
2018-06-04 23:24:45 +02:00
|
|
|
|
|
|
|
for (let key in newlyLost) {
|
|
|
|
lostIdentities[key] = newlyLost[key]
|
|
|
|
}
|
2018-06-04 23:21:46 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 23:05:56 +02:00
|
|
|
this.store.updateState({ identities, lostIdentities })
|
2018-06-04 22:43:26 +02:00
|
|
|
this.addAddresses(addresses)
|
|
|
|
|
|
|
|
let selected = this.getSelectedAddress()
|
|
|
|
if (!addresses.includes(selected)) {
|
|
|
|
selected = addresses[0]
|
|
|
|
this.setSelectedAddress(selected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return selected
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Setter for the `selectedAddress` property
|
|
|
|
*
|
|
|
|
* @param {string} _address A new hex address for an account
|
|
|
|
* @returns {Promise<void>} Promise resolves with undefined
|
|
|
|
*
|
|
|
|
*/
|
2017-02-21 21:32:13 +01:00
|
|
|
setSelectedAddress (_address) {
|
2017-01-31 00:08:31 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const address = normalizeAddress(_address)
|
|
|
|
this.store.updateState({ selectedAddress: address })
|
|
|
|
resolve()
|
|
|
|
})
|
2017-01-30 21:42:24 +01:00
|
|
|
}
|
|
|
|
|
2018-04-16 19:08:04 +02:00
|
|
|
/**
|
|
|
|
* Getter for the `selectedAddress` property
|
|
|
|
*
|
|
|
|
* @returns {string} The hex address for the currently selected account
|
|
|
|
*
|
|
|
|
*/
|
2017-09-27 23:43:23 +02:00
|
|
|
getSelectedAddress () {
|
2017-01-30 21:42:24 +01:00
|
|
|
return this.store.getState().selectedAddress
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2018-06-03 20:27:25 +02:00
|
|
|
* @see AddedToken {@link AddedToken}
|
2018-04-16 19:08:04 +02: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
|
|
|
|
* @param {number} decimals The number of decimals the token uses.
|
|
|
|
* @returns {Promise<array>} Promises the new array of AddedToken objects.
|
|
|
|
*
|
|
|
|
*/
|
2017-12-19 00:57:04 +01:00
|
|
|
async addToken (rawAddress, symbol, decimals) {
|
2017-06-16 01:22:53 +02:00
|
|
|
const address = normalizeAddress(rawAddress)
|
|
|
|
const newEntry = { address, symbol, decimals }
|
|
|
|
|
|
|
|
const tokens = this.store.getState().tokens
|
2017-12-19 00:57:04 +01:00
|
|
|
const previousEntry = tokens.find((token, index) => {
|
2017-06-16 01:22:53 +02:00
|
|
|
return token.address === address
|
|
|
|
})
|
2017-12-19 00:57:04 +01:00
|
|
|
const previousIndex = tokens.indexOf(previousEntry)
|
2017-06-16 01:22:53 +02:00
|
|
|
|
2017-12-19 00:57:04 +01:00
|
|
|
if (previousEntry) {
|
2017-06-16 01:22:53 +02:00
|
|
|
tokens[previousIndex] = newEntry
|
|
|
|
} else {
|
|
|
|
tokens.push(newEntry)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.updateState({ tokens })
|
2017-12-20 23:46:12 +01:00
|
|
|
|
2017-10-03 19:25:52 +02:00
|
|
|
return Promise.resolve(tokens)
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Removes a specified token from the tokens array.
|
|
|
|
*
|
|
|
|
* @param {string} rawAddress Hex address of the token contract to remove.
|
2018-04-18 20:47:06 +02:00
|
|
|
* @returns {Promise<array>} The new array of AddedToken objects
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2017-10-03 19:25:52 +02:00
|
|
|
removeToken (rawAddress) {
|
|
|
|
const tokens = this.store.getState().tokens
|
|
|
|
|
|
|
|
const updatedTokens = tokens.filter(token => token.address !== rawAddress)
|
|
|
|
|
|
|
|
this.store.updateState({ tokens: updatedTokens })
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @returns {array} The current array of AddedToken objects
|
|
|
|
*
|
|
|
|
*/
|
2017-06-16 01:22:53 +02:00
|
|
|
getTokens () {
|
|
|
|
return this.store.getState().tokens
|
|
|
|
}
|
|
|
|
|
2018-04-19 05:33:51 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
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-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Gets an updated rpc list from this.addToFrequentRpcList() and sets the `frequentRpcList` to this update list.
|
|
|
|
*
|
|
|
|
* @param {string} _url The the new rpc url to add to the updated list
|
|
|
|
* @returns {Promise<void>} Promise resolves with undefined
|
|
|
|
*
|
|
|
|
*/
|
2017-02-23 22:56:58 +01:00
|
|
|
updateFrequentRpcList (_url) {
|
|
|
|
return this.addToFrequentRpcList(_url)
|
|
|
|
.then((rpcList) => {
|
|
|
|
this.store.updateState({ frequentRpcList: rpcList })
|
2017-03-08 01:01:51 +01:00
|
|
|
return Promise.resolve()
|
2017-02-23 22:56:58 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
2018-06-03 20:27:25 +02:00
|
|
|
* Setter for the `currentAccountTab` property
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
* @param {string} currentAccountTab Specifies the new tab to be marked as current
|
|
|
|
* @returns {Promise<void>} Promise resolves with undefined
|
|
|
|
*
|
|
|
|
*/
|
2017-06-14 23:21:50 +02:00
|
|
|
setCurrentAccountTab (currentAccountTab) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.store.updateState({ currentAccountTab })
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Returns an updated rpcList based on the passed url and the current list.
|
2018-04-16 19:08:04 +02:00
|
|
|
* The returned list will have a max length of 2. If the _url currently exists it the list, it will be moved to the
|
|
|
|
* end of the list. The current list is modified and returned as a promise.
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
2018-06-03 20:27:25 +02:00
|
|
|
* @param {string} _url The rpc url to add to the frequentRpcList.
|
2018-04-19 05:33:51 +02:00
|
|
|
* @returns {Promise<array>} The updated frequentRpcList.
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2017-02-21 21:51:46 +01:00
|
|
|
addToFrequentRpcList (_url) {
|
2017-04-27 06:05:45 +02:00
|
|
|
const rpcList = this.getFrequentRpcList()
|
|
|
|
const index = rpcList.findIndex((element) => { return element === _url })
|
2017-02-23 00:12:56 +01:00
|
|
|
if (index !== -1) {
|
|
|
|
rpcList.splice(index, 1)
|
|
|
|
}
|
|
|
|
if (_url !== 'http://localhost:8545') {
|
2017-02-21 21:32:13 +01:00
|
|
|
rpcList.push(_url)
|
2017-02-23 00:12:56 +01:00
|
|
|
}
|
|
|
|
if (rpcList.length > 2) {
|
|
|
|
rpcList.shift()
|
|
|
|
}
|
2017-02-23 22:56:58 +01:00
|
|
|
return Promise.resolve(rpcList)
|
2017-02-21 21:32:13 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* Getter for the `frequentRpcList` property.
|
|
|
|
*
|
|
|
|
* @returns {array<string>} An array of one or two rpc urls.
|
|
|
|
*
|
|
|
|
*/
|
2017-02-21 21:51:46 +01:00
|
|
|
getFrequentRpcList () {
|
2017-02-23 00:12:56 +01:00
|
|
|
return this.store.getState().frequentRpcList
|
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.
|
|
|
|
*
|
|
|
|
* @param {string} feature A key that corresponds to a UI feature.
|
2018-04-16 19:08:04 +02:00
|
|
|
* @param {boolean} activated Indicates whether or not the UI feature should be displayed
|
2018-04-18 20:41:39 +02:00
|
|
|
* @returns {Promise<object>} Promises a new object; the updated featureFlags object.
|
|
|
|
*
|
|
|
|
*/
|
2017-11-14 17:04:55 +01:00
|
|
|
setFeatureFlag (feature, activated) {
|
|
|
|
const currentFeatureFlags = this.store.getState().featureFlags
|
|
|
|
const updatedFeatureFlags = {
|
|
|
|
...currentFeatureFlags,
|
|
|
|
[feature]: activated,
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.updateState({ featureFlags: updatedFeatureFlags })
|
2017-11-16 20:28:59 +01:00
|
|
|
|
2017-11-14 17:04:55 +01:00
|
|
|
return Promise.resolve(updatedFeatureFlags)
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:41:39 +02:00
|
|
|
/**
|
|
|
|
* A getter for the `featureFlags` property
|
|
|
|
*
|
|
|
|
* @returns {object} A key-boolean map, where keys refer to features and booleans to whether the
|
2018-04-16 19:08:04 +02:00
|
|
|
* user wishes to see that feature
|
2018-04-18 20:41:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2017-11-14 17:04:55 +01:00
|
|
|
getFeatureFlags () {
|
|
|
|
return this.store.getState().featureFlags
|
|
|
|
}
|
2017-01-30 21:42:24 +01:00
|
|
|
//
|
|
|
|
// PRIVATE METHODS
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PreferencesController
|