mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-23 02:10:12 +01:00
Update address sync logic (#8224)
* update address sync logic * error on sync with no addresses
This commit is contained in:
parent
57035c6410
commit
0af02d5194
@ -302,7 +302,7 @@ class PreferencesController {
|
||||
this.store.updateState({ identities, accountTokens })
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Synchronizes identity entries with known accounts.
|
||||
* Removes any unknown identities, and returns the resulting selected address.
|
||||
*
|
||||
@ -310,6 +310,11 @@ class PreferencesController {
|
||||
* @returns {Promise<string>} - selectedAddress the selected address.
|
||||
*/
|
||||
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 newlyLost = {}
|
||||
|
@ -773,7 +773,6 @@ export default class MetamaskController extends EventEmitter {
|
||||
*/
|
||||
async submitPassword (password) {
|
||||
await this.keyringController.submitPassword(password)
|
||||
const accounts = await this.keyringController.getAccounts()
|
||||
|
||||
// verify keyrings
|
||||
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.preferencesController.syncAddresses(accounts)
|
||||
await this.txController.pendingTxTracker.updatePendingTxs()
|
||||
|
||||
try {
|
||||
@ -1773,7 +1771,7 @@ export default class MetamaskController extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
async _onKeyringControllerUpdate (state) {
|
||||
const { isUnlocked, keyrings } = state
|
||||
const { keyrings } = state
|
||||
const addresses = keyrings.reduce((acc, { accounts }) => acc.concat(accounts), [])
|
||||
|
||||
if (!addresses.length) {
|
||||
@ -1781,17 +1779,8 @@ export default class MetamaskController extends EventEmitter {
|
||||
}
|
||||
|
||||
// Ensure preferences + identities controller know about all addresses
|
||||
this.preferencesController.addAddresses(addresses)
|
||||
this.preferencesController.syncAddresses(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
|
||||
|
@ -912,11 +912,12 @@ describe('MetaMaskController', function () {
|
||||
})
|
||||
|
||||
describe('#_onKeyringControllerUpdate', 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()
|
||||
sandbox.replace(metamaskController, 'preferencesController', {
|
||||
addAddresses,
|
||||
syncAddresses,
|
||||
})
|
||||
sandbox.replace(metamaskController, 'accountTracker', {
|
||||
syncWithAddresses,
|
||||
@ -925,20 +926,16 @@ describe('MetaMaskController', function () {
|
||||
const oldState = metamaskController.getState()
|
||||
await metamaskController._onKeyringControllerUpdate({ keyrings: [] })
|
||||
|
||||
assert.ok(addAddresses.notCalled)
|
||||
assert.ok(syncAddresses.notCalled)
|
||||
assert.ok(syncWithAddresses.notCalled)
|
||||
assert.deepEqual(metamaskController.getState(), oldState)
|
||||
})
|
||||
|
||||
it('should update selected address if keyrings was locked', async function () {
|
||||
const addAddresses = sinon.fake()
|
||||
const getSelectedAddress = sinon.fake.returns('0x42')
|
||||
const setSelectedAddress = sinon.fake()
|
||||
it('should sync addresses if there are keyrings in state', async function () {
|
||||
const syncAddresses = sinon.fake()
|
||||
const syncWithAddresses = sinon.fake()
|
||||
sandbox.replace(metamaskController, 'preferencesController', {
|
||||
addAddresses,
|
||||
getSelectedAddress,
|
||||
setSelectedAddress,
|
||||
syncAddresses,
|
||||
})
|
||||
sandbox.replace(metamaskController, 'accountTracker', {
|
||||
syncWithAddresses,
|
||||
@ -946,23 +943,21 @@ describe('MetaMaskController', function () {
|
||||
|
||||
const oldState = metamaskController.getState()
|
||||
await metamaskController._onKeyringControllerUpdate({
|
||||
isUnlocked: false,
|
||||
keyrings: [{
|
||||
accounts: ['0x1', '0x2'],
|
||||
}],
|
||||
})
|
||||
|
||||
assert.deepEqual(addAddresses.args, [[['0x1', '0x2']]])
|
||||
assert.deepEqual(syncAddresses.args, [[['0x1', '0x2']]])
|
||||
assert.deepEqual(syncWithAddresses.args, [[['0x1', '0x2']]])
|
||||
assert.deepEqual(setSelectedAddress.args, [['0x1']])
|
||||
assert.deepEqual(metamaskController.getState(), oldState)
|
||||
})
|
||||
|
||||
it('should NOT update selected address if already unlocked', async function () {
|
||||
const addAddresses = sinon.fake()
|
||||
const syncAddresses = sinon.fake()
|
||||
const syncWithAddresses = sinon.fake()
|
||||
sandbox.replace(metamaskController, 'preferencesController', {
|
||||
addAddresses,
|
||||
syncAddresses,
|
||||
})
|
||||
sandbox.replace(metamaskController, 'accountTracker', {
|
||||
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(metamaskController.getState(), oldState)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user