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

Merge pull request #1073 from MetaMask/kumavis-refactor4

Kumavis refactor4
This commit is contained in:
Frankie 2017-02-01 13:00:27 -08:00 committed by GitHub
commit cadea1e09e
5 changed files with 74 additions and 84 deletions

View File

@ -31,13 +31,17 @@ class KeyringController extends EventEmitter {
constructor (opts) { constructor (opts) {
super() super()
const initState = opts.initState || {} const initState = opts.initState || {}
this.keyringTypes = keyringTypes
this.store = new ObservableStore(initState) this.store = new ObservableStore(initState)
this.memStore = new ObservableStore({
keyringTypes: this.keyringTypes.map(krt => krt.type),
keyrings: [],
identities: {},
})
this.configManager = opts.configManager this.configManager = opts.configManager
this.ethStore = opts.ethStore this.ethStore = opts.ethStore
this.encryptor = encryptor this.encryptor = encryptor
this.keyringTypes = keyringTypes
this.keyrings = [] this.keyrings = []
this.identities = {} // Essentially a name hash
this._unconfMsgCbs = {} this._unconfMsgCbs = {}
@ -74,31 +78,23 @@ class KeyringController extends EventEmitter {
// in this class, but will need to be Promisified when we move our // in this class, but will need to be Promisified when we move our
// persistence to an async model. // persistence to an async model.
getState () { getState () {
return Promise.all(this.keyrings.map(this.displayForKeyring))
.then((displayKeyrings) => {
const state = this.store.getState()
// old wallet // old wallet
const wallet = this.configManager.getWallet() const memState = this.memStore.getState()
return { const result = {
// computed // computed
isInitialized: (!!wallet || !!state.vault),
isUnlocked: (!!this.password), isUnlocked: (!!this.password),
keyrings: displayKeyrings,
// hard coded
keyringTypes: this.keyringTypes.map(krt => krt.type),
// memStore // memStore
identities: this.identities, keyringTypes: memState.keyringTypes,
// configManager identities: memState.identities,
seedWords: this.configManager.getSeedWords(), keyrings: memState.keyrings,
isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(),
currentFiat: this.configManager.getCurrentFiat(),
conversionRate: this.configManager.getConversionRate(),
conversionDate: this.configManager.getConversionDate(),
// messageManager // messageManager
unconfMsgs: messageManager.unconfirmedMsgs(), unconfMsgs: messageManager.unconfirmedMsgs(),
messages: messageManager.getMsgList(), messages: messageManager.getMsgList(),
// configManager
seedWords: this.configManager.getSeedWords(),
isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(),
} }
}) return result
} }
// Create New Vault And Keychain // Create New Vault And Keychain
@ -164,6 +160,7 @@ class KeyringController extends EventEmitter {
setLocked () { setLocked () {
this.password = null this.password = null
this.keyrings = [] this.keyrings = []
this._updateMemStoreKeyrings()
return this.fullUpdate() return this.fullUpdate()
} }
@ -245,7 +242,9 @@ class KeyringController extends EventEmitter {
walletNicknames[hexAddress] = label walletNicknames[hexAddress] = label
this.store.updateState({ walletNicknames }) this.store.updateState({ walletNicknames })
// update state on memStore // update state on memStore
this.identities[hexAddress].name = label const identities = this.memStore.getState().identities
identities[hexAddress].name = label
this.memStore.updateState({ identities })
return Promise.resolve(label) return Promise.resolve(label)
} catch (err) { } catch (err) {
return Promise.reject(err) return Promise.reject(err)
@ -439,14 +438,16 @@ class KeyringController extends EventEmitter {
// Takes an address, and assigns it an incremented nickname, persisting it. // Takes an address, and assigns it an incremented nickname, persisting it.
createNickname (address) { createNickname (address) {
const hexAddress = normalizeAddress(address) const hexAddress = normalizeAddress(address)
const currentIdentityCount = Object.keys(this.identities).length + 1 const identities = this.memStore.getState().identities
const currentIdentityCount = Object.keys(identities).length + 1
const nicknames = this.store.getState().walletNicknames || {} const nicknames = this.store.getState().walletNicknames || {}
const existingNickname = nicknames[hexAddress] const existingNickname = nicknames[hexAddress]
const name = existingNickname || `Account ${currentIdentityCount}` const name = existingNickname || `Account ${currentIdentityCount}`
this.identities[hexAddress] = { identities[hexAddress] = {
address: hexAddress, address: hexAddress,
name, name,
} }
this.memStore.updateState({ identities })
return this.saveAccountLabel(hexAddress, name) return this.saveAccountLabel(hexAddress, name)
} }
@ -635,8 +636,19 @@ class KeyringController extends EventEmitter {
this.ethStore.removeAccount(address) this.ethStore.removeAccount(address)
}) })
// clear keyrings from memory
this.keyrings = [] this.keyrings = []
this.identities = {} this.memStore.updateState({
keyrings: [],
identities: {},
})
}
_updateMemStoreKeyrings() {
Promise.all(this.keyrings.map(this.displayForKeyring))
.then((keyrings) => {
this.memStore.updateState({ keyrings })
})
} }
} }

View File

@ -169,22 +169,29 @@ module.exports = class MetamaskController extends EventEmitter {
// //
getState () { getState () {
return this.keyringController.getState() const wallet = this.configManager.getWallet()
.then((keyringControllerState) => { const vault = this.keyringController.store.getState().vault
const isInitialized = (!!wallet || !!vault)
return extend( return extend(
{
isInitialized,
},
this.state, this.state,
this.ethStore.getState(), this.ethStore.getState(),
this.configManager.getConfig(),
this.txManager.getState(), this.txManager.getState(),
keyringControllerState, this.keyringController.getState(),
this.preferencesController.store.getState(), this.preferencesController.store.getState(),
this.noticeController.getState(), this.noticeController.getState(),
// config manager
this.configManager.getConfig(),
{ {
shapeShiftTxList: this.configManager.getShapeShiftTxList(), shapeShiftTxList: this.configManager.getShapeShiftTxList(),
lostAccounts: this.configManager.getLostAccounts(), lostAccounts: this.configManager.getLostAccounts(),
currentFiat: this.configManager.getCurrentFiat(),
conversionRate: this.configManager.getConversionRate(),
conversionDate: this.configManager.getConversionDate(),
} }
) )
})
} }
// //
@ -199,7 +206,7 @@ module.exports = class MetamaskController extends EventEmitter {
return { return {
// etc // etc
getState: nodeify(this.getState.bind(this)), getState: (cb) => cb(null, this.getState()),
setRpcTarget: this.setRpcTarget.bind(this), setRpcTarget: this.setRpcTarget.bind(this),
setProviderType: this.setProviderType.bind(this), setProviderType: this.setProviderType.bind(this),
useEtherscanProvider: this.useEtherscanProvider.bind(this), useEtherscanProvider: this.useEtherscanProvider.bind(this),
@ -296,10 +303,7 @@ module.exports = class MetamaskController extends EventEmitter {
} }
sendUpdate () { sendUpdate () {
this.getState() this.emit('update', this.getState())
.then((state) => {
this.emit('update', state)
})
} }
// //

View File

@ -16,7 +16,6 @@ const extend = require('xtend')
const render = require('react-dom').render const render = require('react-dom').render
const h = require('react-hyperscript') const h = require('react-hyperscript')
const pipe = require('mississippi').pipe const pipe = require('mississippi').pipe
const LocalStorageStore = require('obs-store/lib/localStorage')
const Root = require('./ui/app/root') const Root = require('./ui/app/root')
const configureStore = require('./ui/app/store') const configureStore = require('./ui/app/store')
const actions = require('./ui/app/actions') const actions = require('./ui/app/actions')
@ -27,7 +26,6 @@ const firstTimeState = require('./app/scripts/first-time-state')
const extension = require('./development/mockExtension') const extension = require('./development/mockExtension')
const noop = function () {} const noop = function () {}
const STORAGE_KEY = 'metamask-config'
// //
// Query String // Query String
@ -56,27 +54,15 @@ const injectCss = require('inject-css')
// MetaMask Controller // MetaMask Controller
// //
let dataStore = new LocalStorageStore({ storageKey: STORAGE_KEY })
// initial state for first time users
if (!dataStore.getState()) {
dataStore.putState(firstTimeState)
}
const controller = new MetamaskController({ const controller = new MetamaskController({
// User confirmation callbacks: // User confirmation callbacks:
showUnconfirmedMessage: noop, showUnconfirmedMessage: noop,
unlockAccountMessage: noop, unlockAccountMessage: noop,
showUnapprovedTx: noop, showUnapprovedTx: noop,
// initial state // initial state
initState: dataStore.getState(), initState: firstTimeState,
}) })
// setup state persistence
pipe(
controller.store,
dataStore
)
// //
// User Interface // User Interface
// //

View File

@ -80,18 +80,4 @@ describe('IdentityStore to KeyringController migration', function() {
}) })
}) })
describe('entering a password', function() {
it('should identify an old wallet as an initialized keyring', function(done) {
keyringController.configManager.setWallet('something')
keyringController.getState()
.then((state) => {
assert(state.isInitialized, 'old vault counted as initialized.')
assert(!state.lostAccounts, 'no lost accounts')
done()
})
.catch((err) => {
done(err)
})
})
})
}) })

View File

@ -104,7 +104,7 @@ describe('KeyringController', function() {
it('should add the address to the identities hash', function() { it('should add the address to the identities hash', function() {
const fakeAddress = '0x12345678' const fakeAddress = '0x12345678'
keyringController.createNickname(fakeAddress) keyringController.createNickname(fakeAddress)
const identities = keyringController.identities const identities = keyringController.memStore.getState().identities
const identity = identities[fakeAddress] const identity = identities[fakeAddress]
assert.equal(identity.address, fakeAddress) assert.equal(identity.address, fakeAddress)
}) })
@ -114,7 +114,9 @@ describe('KeyringController', function() {
it ('sets the nickname', function(done) { it ('sets the nickname', function(done) {
const account = addresses[0] const account = addresses[0]
var nick = 'Test nickname' var nick = 'Test nickname'
keyringController.identities[ethUtil.addHexPrefix(account)] = {} const identities = keyringController.memStore.getState().identities
identities[ethUtil.addHexPrefix(account)] = {}
keyringController.memStore.updateState({ identities })
keyringController.saveAccountLabel(account, nick) keyringController.saveAccountLabel(account, nick)
.then((label) => { .then((label) => {
try { try {