Merge pull request #5236 from whymarrh/excise-config-manager
Delete ConfigManager class and replace its valid usagesfeature/default_network_editable
commit
1d95dc72b1
@ -1,254 +0,0 @@ |
||||
const ethUtil = require('ethereumjs-util') |
||||
const normalize = require('eth-sig-util').normalize |
||||
const { |
||||
MAINNET_RPC_URL, |
||||
ROPSTEN_RPC_URL, |
||||
KOVAN_RPC_URL, |
||||
RINKEBY_RPC_URL, |
||||
} = require('../controllers/network/enums') |
||||
|
||||
/* 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 |
||||
} |
||||
|
||||
ConfigManager.prototype.setConfig = function (config) { |
||||
var data = this.getData() |
||||
data.config = config |
||||
this.setData(data) |
||||
this._emitUpdates(config) |
||||
} |
||||
|
||||
ConfigManager.prototype.getConfig = function () { |
||||
var data = this.getData() |
||||
return data.config |
||||
} |
||||
|
||||
ConfigManager.prototype.setData = function (data) { |
||||
this.store.putState(data) |
||||
} |
||||
|
||||
ConfigManager.prototype.getData = function () { |
||||
return this.store.getState() |
||||
} |
||||
|
||||
ConfigManager.prototype.setPasswordForgotten = function (passwordForgottenState) { |
||||
const data = this.getData() |
||||
data.forgottenPassword = passwordForgottenState |
||||
this.setData(data) |
||||
} |
||||
|
||||
ConfigManager.prototype.getPasswordForgotten = function (passwordForgottenState) { |
||||
const data = this.getData() |
||||
return data.forgottenPassword |
||||
} |
||||
|
||||
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) |
||||
} |
||||
|
||||
ConfigManager.prototype.getSelectedAccount = function () { |
||||
var config = this.getConfig() |
||||
return config.selectedAccount |
||||
} |
||||
|
||||
ConfigManager.prototype.setSelectedAccount = function (address) { |
||||
var config = this.getConfig() |
||||
config.selectedAccount = ethUtil.addHexPrefix(address) |
||||
this.setConfig(config) |
||||
} |
||||
|
||||
ConfigManager.prototype.getWallet = function () { |
||||
return this.getData().wallet |
||||
} |
||||
|
||||
// Takes a boolean
|
||||
ConfigManager.prototype.setShowSeedWords = function (should) { |
||||
var data = this.getData() |
||||
data.showSeedWords = should |
||||
this.setData(data) |
||||
} |
||||
|
||||
|
||||
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 |
||||
} |
||||
ConfigManager.prototype.setRpcTarget = function (rpcUrl) { |
||||
var config = this.getConfig() |
||||
config.provider = { |
||||
type: 'rpc', |
||||
rpcTarget: rpcUrl, |
||||
} |
||||
this.setConfig(config) |
||||
} |
||||
|
||||
ConfigManager.prototype.setProviderType = function (type) { |
||||
var config = this.getConfig() |
||||
config.provider = { |
||||
type: type, |
||||
} |
||||
this.setConfig(config) |
||||
} |
||||
|
||||
ConfigManager.prototype.useEtherscanProvider = function () { |
||||
var config = this.getConfig() |
||||
config.provider = { |
||||
type: 'etherscan', |
||||
} |
||||
this.setConfig(config) |
||||
} |
||||
|
||||
ConfigManager.prototype.getProvider = function () { |
||||
var config = this.getConfig() |
||||
return config.provider |
||||
} |
||||
|
||||
ConfigManager.prototype.getCurrentRpcAddress = function () { |
||||
var provider = this.getProvider() |
||||
if (!provider) return null |
||||
switch (provider.type) { |
||||
|
||||
case 'mainnet': |
||||
return MAINNET_RPC_URL |
||||
|
||||
case 'ropsten': |
||||
return ROPSTEN_RPC_URL |
||||
|
||||
case 'kovan': |
||||
return KOVAN_RPC_URL |
||||
|
||||
case 'rinkeby': |
||||
return RINKEBY_RPC_URL |
||||
|
||||
default: |
||||
return provider && provider.rpcTarget ? provider.rpcTarget : RINKEBY_RPC_URL |
||||
} |
||||
} |
||||
|
||||
//
|
||||
// Tx
|
||||
//
|
||||
|
||||
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
|
||||
|
||||
ConfigManager.prototype.getWalletNicknames = function () { |
||||
var data = this.getData() |
||||
const nicknames = ('walletNicknames' in data) ? data.walletNicknames : {} |
||||
return nicknames |
||||
} |
||||
|
||||
ConfigManager.prototype.nicknameForWallet = function (account) { |
||||
const address = normalize(account) |
||||
const nicknames = this.getWalletNicknames() |
||||
return nicknames[address] |
||||
} |
||||
|
||||
ConfigManager.prototype.setNicknameForWallet = function (account, nickname) { |
||||
const address = normalize(account) |
||||
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) |
||||
} |
||||
|
||||
ConfigManager.prototype.subscribe = function (fn) { |
||||
this._subs.push(fn) |
||||
var unsubscribe = this.unsubscribe.bind(this, fn) |
||||
return unsubscribe |
||||
} |
||||
|
||||
ConfigManager.prototype.unsubscribe = function (fn) { |
||||
var index = this._subs.indexOf(fn) |
||||
if (index !== -1) this._subs.splice(index, 1) |
||||
} |
||||
|
||||
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 || [] |
||||
} |
@ -1,50 +0,0 @@ |
||||
const version = 5 |
||||
|
||||
/* |
||||
|
||||
This is an incomplete migration bc it requires post-decrypted data |
||||
which we dont have access to at the time of this writing. |
||||
|
||||
*/ |
||||
|
||||
const ObservableStore = require('obs-store') |
||||
const ConfigManager = require('../../app/scripts/lib/config-manager') |
||||
const IdentityStoreMigrator = require('../../app/scripts/lib/idStore-migrator') |
||||
const KeyringController = require('eth-keyring-controller') |
||||
|
||||
const password = 'obviously not correct' |
||||
|
||||
module.exports = { |
||||
version, |
||||
|
||||
migrate: function (versionedData) { |
||||
versionedData.meta.version = version |
||||
|
||||
const store = new ObservableStore(versionedData.data) |
||||
const configManager = new ConfigManager({ store }) |
||||
const idStoreMigrator = new IdentityStoreMigrator({ configManager }) |
||||
const keyringController = new KeyringController({ |
||||
configManager: configManager, |
||||
}) |
||||
|
||||
// attempt to migrate to multiVault
|
||||
return idStoreMigrator.migratedVaultForPassword(password) |
||||
.then((result) => { |
||||
// skip if nothing to migrate
|
||||
if (!result) return Promise.resolve(versionedData) |
||||
delete versionedData.data.wallet |
||||
// create new keyrings
|
||||
const privKeys = result.lostAccounts.map(acct => acct.privateKey) |
||||
return Promise.all([ |
||||
keyringController.restoreKeyring(result.serialized), |
||||
keyringController.restoreKeyring({ type: 'Simple Key Pair', data: privKeys }), |
||||
]).then(() => { |
||||
return keyringController.persistAllKeyrings(password) |
||||
}).then(() => { |
||||
// copy result on to state object
|
||||
versionedData.data = store.get() |
||||
return Promise.resolve(versionedData) |
||||
}) |
||||
}) |
||||
}, |
||||
} |
@ -1,9 +0,0 @@ |
||||
const ObservableStore = require('obs-store') |
||||
const clone = require('clone') |
||||
const ConfigManager = require('../../app/scripts/lib/config-manager') |
||||
const firstTimeState = require('../../app/scripts/first-time-state') |
||||
|
||||
module.exports = function () { |
||||
const store = new ObservableStore(clone(firstTimeState)) |
||||
return new ConfigManager({ store }) |
||||
} |
@ -1,112 +0,0 @@ |
||||
const assert = require('assert') |
||||
const configManagerGen = require('../lib/mock-config-manager') |
||||
|
||||
describe('config-manager', function () { |
||||
var configManager |
||||
|
||||
beforeEach(function () { |
||||
configManager = configManagerGen() |
||||
}) |
||||
|
||||
describe('#setConfig', function () { |
||||
it('should set the config key', function () { |
||||
var testConfig = { |
||||
provider: { |
||||
type: 'rpc', |
||||
rpcTarget: 'foobar', |
||||
}, |
||||
} |
||||
configManager.setConfig(testConfig) |
||||
var result = configManager.getData() |
||||
|
||||
assert.equal(result.config.provider.type, testConfig.provider.type) |
||||
assert.equal(result.config.provider.rpcTarget, testConfig.provider.rpcTarget) |
||||
}) |
||||
|
||||
it('setting wallet should not overwrite config', function () { |
||||
var testConfig = { |
||||
provider: { |
||||
type: 'rpc', |
||||
rpcTarget: 'foobar', |
||||
}, |
||||
} |
||||
configManager.setConfig(testConfig) |
||||
|
||||
var testWallet = { |
||||
name: 'this is my fake wallet', |
||||
} |
||||
configManager.setWallet(testWallet) |
||||
|
||||
var result = configManager.getData() |
||||
assert.equal(result.wallet.name, testWallet.name, 'wallet name is set') |
||||
assert.equal(result.config.provider.rpcTarget, testConfig.provider.rpcTarget) |
||||
|
||||
testConfig.provider.type = 'something else!' |
||||
configManager.setConfig(testConfig) |
||||
|
||||
result = configManager.getData() |
||||
assert.equal(result.wallet.name, testWallet.name, 'wallet name is set') |
||||
assert.equal(result.config.provider.rpcTarget, testConfig.provider.rpcTarget) |
||||
assert.equal(result.config.provider.type, testConfig.provider.type) |
||||
}) |
||||
}) |
||||
|
||||
describe('wallet nicknames', function () { |
||||
it('should return null when no nicknames are saved', function () { |
||||
var nick = configManager.nicknameForWallet('0x0') |
||||
assert.equal(nick, null, 'no nickname returned') |
||||
}) |
||||
|
||||
it('should persist nicknames', function () { |
||||
var account = '0x0' |
||||
var nick1 = 'foo' |
||||
var nick2 = 'bar' |
||||
configManager.setNicknameForWallet(account, nick1) |
||||
|
||||
var result1 = configManager.nicknameForWallet(account) |
||||
assert.equal(result1, nick1) |
||||
|
||||
configManager.setNicknameForWallet(account, nick2) |
||||
var result2 = configManager.nicknameForWallet(account) |
||||
assert.equal(result2, nick2) |
||||
}) |
||||
}) |
||||
|
||||
describe('rpc manipulations', function () { |
||||
it('changing rpc should return a different rpc', function () { |
||||
var firstRpc = 'first' |
||||
var secondRpc = 'second' |
||||
|
||||
configManager.setRpcTarget(firstRpc) |
||||
var firstResult = configManager.getCurrentRpcAddress() |
||||
assert.equal(firstResult, firstRpc) |
||||
|
||||
configManager.setRpcTarget(secondRpc) |
||||
var secondResult = configManager.getCurrentRpcAddress() |
||||
assert.equal(secondResult, secondRpc) |
||||
}) |
||||
}) |
||||
|
||||
describe('transactions', function () { |
||||
beforeEach(function () { |
||||
configManager.setTxList([]) |
||||
}) |
||||
|
||||
describe('#getTxList', function () { |
||||
it('when new should return empty array', function () { |
||||
var result = configManager.getTxList() |
||||
assert.ok(Array.isArray(result)) |
||||
assert.equal(result.length, 0) |
||||
}) |
||||
}) |
||||
|
||||
describe('#setTxList', function () { |
||||
it('saves the submitted data to the tx list', function () { |
||||
var target = [{ foo: 'bar' }] |
||||
configManager.setTxList(target) |
||||
var result = configManager.getTxList() |
||||
assert.equal(result[0].foo, 'bar') |
||||
}) |
||||
}) |
||||
}) |
||||
}) |
Loading…
Reference in new issue