1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/controllers/preferences.js

62 lines
1.4 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: [],
}, opts.initState)
this.store = new ObservableStore(initState)
}
//
// PUBLIC METHODS
//
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()
})
}
2017-02-21 21:32:13 +01:00
getSelectedAddress (_address) {
return this.store.getState().selectedAddress
}
updateFrequentRpcList (_url) {
return this.addToFrequentRpcList(_url)
.then((rpcList) => {
this.store.updateState({ frequentRpcList: rpcList })
return Promise.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
}
//
// PRIVATE METHODS
//
}
module.exports = PreferencesController