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

309 lines
7.3 KiB
JavaScript
Raw Normal View History

2016-05-20 02:13:33 +02:00
const MetamaskConfig = require('../config.js')
const ethUtil = require('ethereumjs-util')
const normalize = require('./sig-util').normalize
2016-05-20 01:53:16 +02:00
const TESTNET_RPC = MetamaskConfig.network.testnet
const MAINNET_RPC = MetamaskConfig.network.mainnet
const MORDEN_RPC = MetamaskConfig.network.morden
/* The config-manager is a convenience object
* wrapping a pojo-migrator.
*
* It exists mostly to allow the creation of
* convenience methods to access and persist
* particular portions of the state.
*/
module.exports = ConfigManager
function ConfigManager (opts) {
// ConfigManager is observable and will emit updates
this._subs = []
2017-01-12 04:04:19 +01:00
this.store = opts.store
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.setConfig = function (config) {
2017-01-25 04:47:00 +01:00
var data = this.getData()
data.config = config
this.setData(data)
this._emitUpdates(config)
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.getConfig = function () {
2017-01-25 04:47:00 +01:00
var data = this.getData()
return data.config
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.setRpcTarget = function (rpcUrl) {
2016-04-01 01:32:35 +02:00
var config = this.getConfig()
config.provider = {
type: 'rpc',
rpcTarget: rpcUrl,
}
this.setConfig(config)
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.setProviderType = function (type) {
var config = this.getConfig()
config.provider = {
type: type,
}
this.setConfig(config)
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.useEtherscanProvider = function () {
2016-04-01 01:32:35 +02:00
var config = this.getConfig()
config.provider = {
type: 'etherscan',
}
this.setConfig(config)
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.getProvider = function () {
2016-04-01 01:32:35 +02:00
var config = this.getConfig()
return config.provider
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.setData = function (data) {
2017-01-25 04:47:00 +01:00
this.store.putState(data)
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.getData = function () {
2017-01-25 04:47:00 +01:00
return this.store.getState()
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.setWallet = function (wallet) {
2017-01-25 04:47:00 +01:00
var data = this.getData()
data.wallet = wallet
this.setData(data)
}
2016-10-19 23:55:08 +02:00
ConfigManager.prototype.setVault = function (encryptedString) {
var data = this.getData()
data.vault = encryptedString
this.setData(data)
}
ConfigManager.prototype.getVault = function () {
var data = this.getData()
return data.vault
2016-10-19 23:55:08 +02:00
}
2016-10-15 19:48:12 +02:00
ConfigManager.prototype.getKeychains = function () {
2017-01-25 04:47:00 +01:00
return this.getData().keychains || []
2016-10-15 19:48:12 +02:00
}
ConfigManager.prototype.setKeychains = function (keychains) {
2017-01-25 04:47:00 +01:00
var data = this.getData()
2016-10-15 19:48:12 +02:00
data.keychains = keychains
this.setData(data)
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.getSelectedAccount = function () {
var config = this.getConfig()
return config.selectedAccount
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.setSelectedAccount = function (address) {
var config = this.getConfig()
config.selectedAccount = ethUtil.addHexPrefix(address)
this.setConfig(config)
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.getWallet = function () {
2017-01-25 04:47:00 +01:00
return this.getData().wallet
}
// Takes a boolean
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.setShowSeedWords = function (should) {
2017-01-25 04:47:00 +01:00
var data = this.getData()
data.showSeedWords = should
this.setData(data)
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.getShouldShowSeedWords = function () {
2017-01-25 04:47:00 +01:00
var data = this.getData()
return data.showSeedWords
}
ConfigManager.prototype.setSeedWords = function (words) {
var data = this.getData()
data.seedWords = words
this.setData(data)
}
ConfigManager.prototype.getSeedWords = function () {
var data = this.getData()
2017-01-25 04:47:00 +01:00
return data.seedWords
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.getCurrentRpcAddress = function () {
var provider = this.getProvider()
if (!provider) return null
2016-06-21 22:18:32 +02:00
switch (provider.type) {
2016-06-21 22:18:32 +02:00
case 'mainnet':
return MAINNET_RPC
2016-06-21 22:18:32 +02:00
case 'testnet':
return TESTNET_RPC
case 'morden':
return MORDEN_RPC
2016-06-21 22:18:32 +02:00
default:
return provider && provider.rpcTarget ? provider.rpcTarget : TESTNET_RPC
}
}
//
// Tx
//
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.getTxList = function () {
2017-01-25 04:47:00 +01:00
var data = this.getData()
if (data.transactions !== undefined) {
return data.transactions
} else {
return []
}
}
2016-12-14 21:55:41 +01:00
ConfigManager.prototype.setTxList = function (txList) {
2017-01-25 04:47:00 +01:00
var data = this.getData()
data.transactions = txList
this.setData(data)
}
2016-04-20 02:32:09 +02:00
// wallet nickname methods
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.getWalletNicknames = function () {
var data = this.getData()
2016-06-21 22:18:32 +02:00
const nicknames = ('walletNicknames' in data) ? data.walletNicknames : {}
return nicknames
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.nicknameForWallet = function (account) {
const address = normalize(account)
2016-06-21 22:18:32 +02:00
const nicknames = this.getWalletNicknames()
return nicknames[address]
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.setNicknameForWallet = function (account, nickname) {
const address = normalize(account)
2016-06-21 22:18:32 +02:00
const nicknames = this.getWalletNicknames()
nicknames[address] = nickname
var data = this.getData()
data.walletNicknames = nicknames
this.setData(data)
}
// observable
2016-10-15 19:48:12 +02:00
ConfigManager.prototype.getSalt = function () {
var data = this.getData()
2017-01-25 04:47:00 +01:00
return data.salt
2016-10-15 19:48:12 +02:00
}
2016-11-11 19:26:12 +01:00
ConfigManager.prototype.setSalt = function (salt) {
2016-10-15 19:48:12 +02:00
var data = this.getData()
data.salt = salt
this.setData(data)
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.subscribe = function (fn) {
this._subs.push(fn)
var unsubscribe = this.unsubscribe.bind(this, fn)
return unsubscribe
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype.unsubscribe = function (fn) {
var index = this._subs.indexOf(fn)
if (index !== -1) this._subs.splice(index, 1)
}
2016-06-21 22:18:32 +02:00
ConfigManager.prototype._emitUpdates = function (state) {
this._subs.forEach(function (handler) {
handler(state)
})
}
ConfigManager.prototype.setConfirmedDisclaimer = function (confirmed) {
var data = this.getData()
data.isDisclaimerConfirmed = confirmed
this.setData(data)
}
ConfigManager.prototype.getConfirmedDisclaimer = function () {
var data = this.getData()
2017-01-25 04:47:00 +01:00
return data.isDisclaimerConfirmed
}
2016-10-07 10:41:27 +02:00
ConfigManager.prototype.setTOSHash = function (hash) {
var data = this.getData()
2016-10-07 10:41:27 +02:00
data.TOSHash = hash
this.setData(data)
}
ConfigManager.prototype.getTOSHash = function () {
var data = this.getData()
2017-01-25 04:47:00 +01:00
return data.TOSHash
}
2016-08-18 19:40:35 +02:00
ConfigManager.prototype.getShapeShiftTxList = function () {
var data = this.getData()
var shapeShiftTxList = data.shapeShiftTxList ? data.shapeShiftTxList : []
shapeShiftTxList.forEach((tx) => {
2016-08-19 00:20:26 +02:00
if (tx.response.status !== 'complete') {
2016-08-18 19:40:35 +02:00
var requestListner = function (request) {
tx.response = JSON.parse(this.responseText)
2016-08-19 00:20:26 +02:00
if (tx.response.status === 'complete') {
tx.time = new Date().getTime()
2016-08-18 19:40:35 +02:00
}
}
var shapShiftReq = new XMLHttpRequest()
shapShiftReq.addEventListener('load', requestListner)
shapShiftReq.open('GET', `https://shapeshift.io/txStat/${tx.depositAddress}`, true)
shapShiftReq.send()
}
})
2016-08-19 00:20:26 +02:00
this.setData(data)
2016-08-18 19:40:35 +02:00
return shapeShiftTxList
}
ConfigManager.prototype.createShapeShiftTx = function (depositAddress, depositType) {
var data = this.getData()
var shapeShiftTx = {depositAddress, depositType, key: 'shapeshift', time: new Date().getTime(), response: {}}
2016-08-19 00:20:26 +02:00
if (!data.shapeShiftTxList) {
2016-08-18 19:40:35 +02:00
data.shapeShiftTxList = [shapeShiftTx]
} else {
data.shapeShiftTxList.push(shapeShiftTx)
}
this.setData(data)
}
2016-10-13 04:35:09 +02:00
ConfigManager.prototype.getGasMultiplier = function () {
var data = this.getData()
2017-01-25 04:47:00 +01:00
return data.gasMultiplier
2016-10-13 04:35:09 +02:00
}
ConfigManager.prototype.setGasMultiplier = function (gasMultiplier) {
var data = this.getData()
data.gasMultiplier = gasMultiplier
this.setData(data)
}
ConfigManager.prototype.setLostAccounts = function (lostAccounts) {
var data = this.getData()
data.lostAccounts = lostAccounts
this.setData(data)
}
ConfigManager.prototype.getLostAccounts = function () {
var data = this.getData()
2016-12-20 01:29:44 +01:00
return data.lostAccounts || []
}