Update address sync logic (#8224)

* update address sync logic

* error on sync with no addresses
feature/default_network_editable
Erik Marks 5 years ago committed by GitHub
parent 57035c6410
commit 0af02d5194
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/scripts/controllers/preferences.js
  2. 15
      app/scripts/metamask-controller.js
  3. 27
      test/unit/app/controllers/metamask-controller-test.js

@ -302,7 +302,7 @@ class PreferencesController {
this.store.updateState({ identities, accountTokens }) this.store.updateState({ identities, accountTokens })
} }
/* /**
* Synchronizes identity entries with known accounts. * Synchronizes identity entries with known accounts.
* Removes any unknown identities, and returns the resulting selected address. * Removes any unknown identities, and returns the resulting selected address.
* *
@ -310,6 +310,11 @@ class PreferencesController {
* @returns {Promise<string>} - selectedAddress the selected address. * @returns {Promise<string>} - selectedAddress the selected address.
*/ */
syncAddresses (addresses) { syncAddresses (addresses) {
if (!Array.isArray(addresses) || addresses.length === 0) {
throw new Error('Expected non-empty array of addresses.')
}
const { identities, lostIdentities } = this.store.getState() const { identities, lostIdentities } = this.store.getState()
const newlyLost = {} const newlyLost = {}

@ -773,7 +773,6 @@ export default class MetamaskController extends EventEmitter {
*/ */
async submitPassword (password) { async submitPassword (password) {
await this.keyringController.submitPassword(password) await this.keyringController.submitPassword(password)
const accounts = await this.keyringController.getAccounts()
// verify keyrings // verify keyrings
const nonSimpleKeyrings = this.keyringController.keyrings.filter((keyring) => keyring.type !== 'Simple Key Pair') const nonSimpleKeyrings = this.keyringController.keyrings.filter((keyring) => keyring.type !== 'Simple Key Pair')
@ -781,7 +780,6 @@ export default class MetamaskController extends EventEmitter {
await this.diagnostics.reportMultipleKeyrings(nonSimpleKeyrings) await this.diagnostics.reportMultipleKeyrings(nonSimpleKeyrings)
} }
await this.preferencesController.syncAddresses(accounts)
await this.txController.pendingTxTracker.updatePendingTxs() await this.txController.pendingTxTracker.updatePendingTxs()
try { try {
@ -1773,7 +1771,7 @@ export default class MetamaskController extends EventEmitter {
* @private * @private
*/ */
async _onKeyringControllerUpdate (state) { async _onKeyringControllerUpdate (state) {
const { isUnlocked, keyrings } = state const { keyrings } = state
const addresses = keyrings.reduce((acc, { accounts }) => acc.concat(accounts), []) const addresses = keyrings.reduce((acc, { accounts }) => acc.concat(accounts), [])
if (!addresses.length) { if (!addresses.length) {
@ -1781,17 +1779,8 @@ export default class MetamaskController extends EventEmitter {
} }
// Ensure preferences + identities controller know about all addresses // Ensure preferences + identities controller know about all addresses
this.preferencesController.addAddresses(addresses) this.preferencesController.syncAddresses(addresses)
this.accountTracker.syncWithAddresses(addresses) this.accountTracker.syncWithAddresses(addresses)
const wasLocked = !isUnlocked
if (wasLocked) {
const oldSelectedAddress = this.preferencesController.getSelectedAddress()
if (!addresses.includes(oldSelectedAddress)) {
const address = addresses[0]
await this.preferencesController.setSelectedAddress(address)
}
}
} }
// misc // misc

@ -912,11 +912,12 @@ describe('MetaMaskController', function () {
}) })
describe('#_onKeyringControllerUpdate', function () { describe('#_onKeyringControllerUpdate', function () {
it('should do nothing if there are no keyrings in state', async function () { it('should do nothing if there are no keyrings in state', async function () {
const addAddresses = sinon.fake() const syncAddresses = sinon.fake()
const syncWithAddresses = sinon.fake() const syncWithAddresses = sinon.fake()
sandbox.replace(metamaskController, 'preferencesController', { sandbox.replace(metamaskController, 'preferencesController', {
addAddresses, syncAddresses,
}) })
sandbox.replace(metamaskController, 'accountTracker', { sandbox.replace(metamaskController, 'accountTracker', {
syncWithAddresses, syncWithAddresses,
@ -925,20 +926,16 @@ describe('MetaMaskController', function () {
const oldState = metamaskController.getState() const oldState = metamaskController.getState()
await metamaskController._onKeyringControllerUpdate({ keyrings: [] }) await metamaskController._onKeyringControllerUpdate({ keyrings: [] })
assert.ok(addAddresses.notCalled) assert.ok(syncAddresses.notCalled)
assert.ok(syncWithAddresses.notCalled) assert.ok(syncWithAddresses.notCalled)
assert.deepEqual(metamaskController.getState(), oldState) assert.deepEqual(metamaskController.getState(), oldState)
}) })
it('should update selected address if keyrings was locked', async function () { it('should sync addresses if there are keyrings in state', async function () {
const addAddresses = sinon.fake() const syncAddresses = sinon.fake()
const getSelectedAddress = sinon.fake.returns('0x42')
const setSelectedAddress = sinon.fake()
const syncWithAddresses = sinon.fake() const syncWithAddresses = sinon.fake()
sandbox.replace(metamaskController, 'preferencesController', { sandbox.replace(metamaskController, 'preferencesController', {
addAddresses, syncAddresses,
getSelectedAddress,
setSelectedAddress,
}) })
sandbox.replace(metamaskController, 'accountTracker', { sandbox.replace(metamaskController, 'accountTracker', {
syncWithAddresses, syncWithAddresses,
@ -946,23 +943,21 @@ describe('MetaMaskController', function () {
const oldState = metamaskController.getState() const oldState = metamaskController.getState()
await metamaskController._onKeyringControllerUpdate({ await metamaskController._onKeyringControllerUpdate({
isUnlocked: false,
keyrings: [{ keyrings: [{
accounts: ['0x1', '0x2'], accounts: ['0x1', '0x2'],
}], }],
}) })
assert.deepEqual(addAddresses.args, [[['0x1', '0x2']]]) assert.deepEqual(syncAddresses.args, [[['0x1', '0x2']]])
assert.deepEqual(syncWithAddresses.args, [[['0x1', '0x2']]]) assert.deepEqual(syncWithAddresses.args, [[['0x1', '0x2']]])
assert.deepEqual(setSelectedAddress.args, [['0x1']])
assert.deepEqual(metamaskController.getState(), oldState) assert.deepEqual(metamaskController.getState(), oldState)
}) })
it('should NOT update selected address if already unlocked', async function () { it('should NOT update selected address if already unlocked', async function () {
const addAddresses = sinon.fake() const syncAddresses = sinon.fake()
const syncWithAddresses = sinon.fake() const syncWithAddresses = sinon.fake()
sandbox.replace(metamaskController, 'preferencesController', { sandbox.replace(metamaskController, 'preferencesController', {
addAddresses, syncAddresses,
}) })
sandbox.replace(metamaskController, 'accountTracker', { sandbox.replace(metamaskController, 'accountTracker', {
syncWithAddresses, syncWithAddresses,
@ -976,7 +971,7 @@ describe('MetaMaskController', function () {
}], }],
}) })
assert.deepEqual(addAddresses.args, [[['0x1', '0x2']]]) assert.deepEqual(syncAddresses.args, [[['0x1', '0x2']]])
assert.deepEqual(syncWithAddresses.args, [[['0x1', '0x2']]]) assert.deepEqual(syncWithAddresses.args, [[['0x1', '0x2']]])
assert.deepEqual(metamaskController.getState(), oldState) assert.deepEqual(metamaskController.getState(), oldState)
}) })

Loading…
Cancel
Save