1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 12:56:01 +01:00
metamask-extension/app/scripts/controllers/preferences.js

133 lines
3.1 KiB
JavaScript
Raw Normal View History

const ObservableStore = require('obs-store')
const normalizeAddress = require('eth-sig-util').normalize
const extend = require('xtend')
class PreferencesController {
constructor (opts = {}) {
const initState = extend({
frequentRpcList: [],
currentAccountTab: 'history',
tokens: [],
useBlockie: false,
2017-11-14 17:04:55 +01:00
featureFlags: {},
currentLocale: opts.initLangCode,
}, opts.initState)
this.store = new ObservableStore(initState)
}
// PUBLIC METHODS
setUseBlockie (val) {
this.store.updateState({ useBlockie: val })
2017-11-24 02:33:44 +01:00
}
getUseBlockie () {
return this.store.getState().useBlockie
}
2018-03-16 01:29:45 +01:00
setCurrentLocale (key) {
this.store.updateState({ currentLocale: key })
}
2017-02-21 21:32:13 +01:00
setSelectedAddress (_address) {
return new Promise((resolve, reject) => {
const address = normalizeAddress(_address)
this.store.updateState({ selectedAddress: address })
resolve()
})
}
getSelectedAddress () {
return this.store.getState().selectedAddress
}
async addToken (rawAddress, symbol, decimals) {
const address = normalizeAddress(rawAddress)
const newEntry = { address, symbol, decimals }
const tokens = this.store.getState().tokens
const previousEntry = tokens.find((token, index) => {
return token.address === address
})
const previousIndex = tokens.indexOf(previousEntry)
if (previousEntry) {
tokens[previousIndex] = newEntry
} else {
tokens.push(newEntry)
}
this.store.updateState({ tokens })
2017-12-20 23:46:12 +01:00
return Promise.resolve(tokens)
}
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)
}
getTokens () {
return this.store.getState().tokens
}
updateFrequentRpcList (_url) {
return this.addToFrequentRpcList(_url)
.then((rpcList) => {
this.store.updateState({ frequentRpcList: rpcList })
return Promise.resolve()
})
}
setCurrentAccountTab (currentAccountTab) {
return new Promise((resolve, reject) => {
this.store.updateState({ currentAccountTab })
resolve()
})
}
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 })
if (index !== -1) {
rpcList.splice(index, 1)
}
if (_url !== 'http://localhost:8545') {
2017-02-21 21:32:13 +01:00
rpcList.push(_url)
}
if (rpcList.length > 2) {
rpcList.shift()
}
return Promise.resolve(rpcList)
2017-02-21 21:32:13 +01:00
}
2017-02-21 21:51:46 +01:00
getFrequentRpcList () {
return this.store.getState().frequentRpcList
2017-02-21 21:32:13 +01:00
}
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)
}
getFeatureFlags () {
return this.store.getState().featureFlags
}
//
// PRIVATE METHODS
//
}
module.exports = PreferencesController