aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/metamask-controller.js
diff options
context:
space:
mode:
authorWhymarrh Whitby <whymarrh.whitby@gmail.com>2018-08-16 23:29:39 +0800
committerWhymarrh Whitby <whymarrh.whitby@gmail.com>2018-08-23 02:08:34 +0800
commit003d445a98164dac0c0529dfd69f5e3987d7d05c (patch)
treea4f2e0f706c168855ea9f6b57ce389b3c1626492 /app/scripts/metamask-controller.js
parent1e8e8bdfc87903249320b97d569c64c55a524899 (diff)
downloadtangerine-wallet-browser-003d445a98164dac0c0529dfd69f5e3987d7d05c.tar
tangerine-wallet-browser-003d445a98164dac0c0529dfd69f5e3987d7d05c.tar.gz
tangerine-wallet-browser-003d445a98164dac0c0529dfd69f5e3987d7d05c.tar.bz2
tangerine-wallet-browser-003d445a98164dac0c0529dfd69f5e3987d7d05c.tar.lz
tangerine-wallet-browser-003d445a98164dac0c0529dfd69f5e3987d7d05c.tar.xz
tangerine-wallet-browser-003d445a98164dac0c0529dfd69f5e3987d7d05c.tar.zst
tangerine-wallet-browser-003d445a98164dac0c0529dfd69f5e3987d7d05c.zip
Update unlock logic to not overwrite existing selected address
Diffstat (limited to 'app/scripts/metamask-controller.js')
-rw-r--r--app/scripts/metamask-controller.js42
1 files changed, 29 insertions, 13 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index 29838ad2d..4ee88186a 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -137,19 +137,7 @@ module.exports = class MetamaskController extends EventEmitter {
encryptor: opts.encryptor || undefined,
})
- // If only one account exists, make sure it is selected.
- this.keyringController.memStore.subscribe((state) => {
- const addresses = state.keyrings.reduce((res, keyring) => {
- return res.concat(keyring.accounts)
- }, [])
- if (addresses.length === 1) {
- const address = addresses[0]
- this.preferencesController.setSelectedAddress(address)
- }
- // ensure preferences + identities controller know about all addresses
- this.preferencesController.addAddresses(addresses)
- this.accountTracker.syncWithAddresses(addresses)
- })
+ this.keyringController.memStore.subscribe((s) => this._onKeyringControllerUpdate(s))
// detect tokens controller
this.detectTokensController = new DetectTokensController({
@@ -1279,6 +1267,34 @@ module.exports = class MetamaskController extends EventEmitter {
}
/**
+ * Handle a KeyringController update
+ * @param {object} state the KC state
+ * @return {Promise<void>}
+ * @private
+ */
+ async _onKeyringControllerUpdate (state) {
+ const {isUnlocked, keyrings} = state
+ const addresses = keyrings.reduce((acc, {accounts}) => acc.concat(accounts), [])
+
+ if (!addresses.length) {
+ return
+ }
+
+ // Ensure preferences + identities controller know about all addresses
+ this.preferencesController.addAddresses(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)
+ }
+ }
+ }
+
+ /**
* A method for emitting the full MetaMask state to all registered listeners.
* @private
*/