Merge pull request #4467 from MetaMask/preferences-account-sync

bugfix - update preferences controller identities on keyring controller update
feature/default_network_editable
Dan Finlay 7 years ago committed by GitHub
commit 8cd692e37c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      app/scripts/controllers/preferences.js
  2. 6
      app/scripts/metamask-controller.js

@ -63,6 +63,13 @@ class PreferencesController {
this.store.updateState({ currentLocale: key }) this.store.updateState({ currentLocale: key })
} }
/**
* Updates identities to only include specified addresses. Removes identities
* not included in addresses array
*
* @param {string[]} addresses An array of hex addresses
*
*/
setAddresses (addresses) { setAddresses (addresses) {
const oldIdentities = this.store.getState().identities const oldIdentities = this.store.getState().identities
const identities = addresses.reduce((ids, address, index) => { const identities = addresses.reduce((ids, address, index) => {
@ -73,6 +80,24 @@ class PreferencesController {
this.store.updateState({ identities }) this.store.updateState({ identities })
} }
/**
* Adds addresses to the identities object without removing identities
*
* @param {string[]} addresses An array of hex addresses
*
*/
addAddresses (addresses) {
const identities = this.store.getState().identities
addresses.forEach((address) => {
// skip if already exists
if (identities[address]) return
// add missing identity
const identityCount = Object.keys(identities).length
identities[address] = { name: `Account ${identityCount + 1}`, address }
})
this.store.updateState({ identities })
}
/** /**
* Setter for the `selectedAddress` property * Setter for the `selectedAddress` property
* *

@ -139,6 +139,8 @@ module.exports = class MetamaskController extends EventEmitter {
const address = addresses[0] const address = addresses[0]
this.preferencesController.setSelectedAddress(address) this.preferencesController.setSelectedAddress(address)
} }
// ensure preferences + identities controller know about all addresses
this.preferencesController.addAddresses(addresses)
this.accountTracker.syncWithAddresses(addresses) this.accountTracker.syncWithAddresses(addresses)
}) })
@ -456,7 +458,11 @@ module.exports = class MetamaskController extends EventEmitter {
async createNewVaultAndRestore (password, seed) { async createNewVaultAndRestore (password, seed) {
const release = await this.createVaultMutex.acquire() const release = await this.createVaultMutex.acquire()
try { try {
// clear known identities
this.preferencesController.setAddresses([])
// create new vault
const vault = await this.keyringController.createNewVaultAndRestore(password, seed) const vault = await this.keyringController.createNewVaultAndRestore(password, seed)
// set new identities
const accounts = await this.keyringController.getAccounts() const accounts = await this.keyringController.getAccounts()
this.preferencesController.setAddresses(accounts) this.preferencesController.setAddresses(accounts)
this.selectFirstIdentity() this.selectFirstIdentity()

Loading…
Cancel
Save