A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/app/scripts/lib/config-manager.js

250 lines
5.6 KiB

const MetamaskConfig = require('../config.js')
const ethUtil = require('ethereumjs-util')
const normalize = require('eth-sig-util').normalize
9 years ago
const TESTNET_RPC = MetamaskConfig.network.testnet
const MAINNET_RPC = MetamaskConfig.network.mainnet
const MORDEN_RPC = MetamaskConfig.network.morden
const KOVAN_RPC = MetamaskConfig.network.kovan
const RINKEBY_RPC = MetamaskConfig.network.rinkeby
/* 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 = []
this.store = opts.store
}
8 years ago
ConfigManager.prototype.setConfig = function (config) {
var data = this.getData()
data.config = config
this.setData(data)
this._emitUpdates(config)
}
8 years ago
ConfigManager.prototype.getConfig = function () {
var data = this.getData()
return data.config
}
8 years ago
ConfigManager.prototype.setRpcTarget = function (rpcUrl) {
var config = this.getConfig()
config.provider = {
type: 'rpc',
rpcTarget: rpcUrl,
}
this.setConfig(config)
}
8 years ago
ConfigManager.prototype.setProviderType = function (type) {
var config = this.getConfig()
config.provider = {
type: type,
}
this.setConfig(config)
}
8 years ago
ConfigManager.prototype.useEtherscanProvider = function () {
var config = this.getConfig()
config.provider = {
type: 'etherscan',
}
this.setConfig(config)
}
8 years ago
ConfigManager.prototype.getProvider = function () {
var config = this.getConfig()
return config.provider
}
8 years ago
ConfigManager.prototype.setData = function (data) {
this.store.putState(data)
}
8 years ago
ConfigManager.prototype.getData = function () {
return this.store.getState()
}
8 years ago
ConfigManager.prototype.setWallet = function (wallet) {
var data = this.getData()
data.wallet = wallet
this.setData(data)
}
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
}
ConfigManager.prototype.getKeychains = function () {
return this.getData().keychains || []
}
ConfigManager.prototype.setKeychains = function (keychains) {
var data = this.getData()
data.keychains = keychains
this.setData(data)
}
8 years ago
ConfigManager.prototype.getSelectedAccount = function () {
var config = this.getConfig()
return config.selectedAccount
}
8 years ago
ConfigManager.prototype.setSelectedAccount = function (address) {
var config = this.getConfig()
config.selectedAccount = ethUtil.addHexPrefix(address)
this.setConfig(config)
}
8 years ago
ConfigManager.prototype.getWallet = function () {
return this.getData().wallet
}
// Takes a boolean
8 years ago
ConfigManager.prototype.setShowSeedWords = function (should) {
var data = this.getData()
data.showSeedWords = should
this.setData(data)
}
8 years ago
ConfigManager.prototype.getShouldShowSeedWords = function () {
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()
return data.seedWords
}
8 years ago
ConfigManager.prototype.getCurrentRpcAddress = function () {
var provider = this.getProvider()
if (!provider) return null
8 years ago
switch (provider.type) {
8 years ago
case 'mainnet':
return MAINNET_RPC
8 years ago
case 'testnet':
return TESTNET_RPC
case 'morden':
return MORDEN_RPC
case 'kovan':
return KOVAN_RPC
8 years ago
case 'rinkeby':
return RINKEBY_RPC
8 years ago
default:
return provider && provider.rpcTarget ? provider.rpcTarget : TESTNET_RPC
}
}
//
// Tx
//
8 years ago
ConfigManager.prototype.getTxList = function () {
var data = this.getData()
if (data.transactions !== undefined) {
return data.transactions
} else {
return []
}
}
ConfigManager.prototype.setTxList = function (txList) {
var data = this.getData()
data.transactions = txList
this.setData(data)
}
// wallet nickname methods
8 years ago
ConfigManager.prototype.getWalletNicknames = function () {
var data = this.getData()
8 years ago
const nicknames = ('walletNicknames' in data) ? data.walletNicknames : {}
return nicknames
}
8 years ago
ConfigManager.prototype.nicknameForWallet = function (account) {
const address = normalize(account)
8 years ago
const nicknames = this.getWalletNicknames()
return nicknames[address]
}
8 years ago
ConfigManager.prototype.setNicknameForWallet = function (account, nickname) {
const address = normalize(account)
8 years ago
const nicknames = this.getWalletNicknames()
nicknames[address] = nickname
var data = this.getData()
data.walletNicknames = nicknames
this.setData(data)
}
// observable
ConfigManager.prototype.getSalt = function () {
var data = this.getData()
return data.salt
}
ConfigManager.prototype.setSalt = function (salt) {
var data = this.getData()
data.salt = salt
this.setData(data)
}
8 years ago
ConfigManager.prototype.subscribe = function (fn) {
this._subs.push(fn)
var unsubscribe = this.unsubscribe.bind(this, fn)
return unsubscribe
}
8 years ago
ConfigManager.prototype.unsubscribe = function (fn) {
var index = this._subs.indexOf(fn)
if (index !== -1) this._subs.splice(index, 1)
}
8 years ago
ConfigManager.prototype._emitUpdates = function (state) {
this._subs.forEach(function (handler) {
handler(state)
})
}
ConfigManager.prototype.setLostAccounts = function (lostAccounts) {
var data = this.getData()
data.lostAccounts = lostAccounts
this.setData(data)
}
ConfigManager.prototype.getLostAccounts = function () {
var data = this.getData()
return data.lostAccounts || []
}