aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorkumavis <kumavis@users.noreply.github.com>2018-06-05 07:04:07 +0800
committerGitHub <noreply@github.com>2018-06-05 07:04:07 +0800
commitddce3d148ef9323c209a7f963057297c6181f779 (patch)
tree937246d88e3dac3598567a1ae6cba81600c5def9 /app
parent5d58f0e9a02872cff298b8b9604c395688fd6c6c (diff)
parent41f292437dd6c7144e36efa8609a419c7dd88da7 (diff)
downloadtangerine-wallet-browser-ddce3d148ef9323c209a7f963057297c6181f779.tar
tangerine-wallet-browser-ddce3d148ef9323c209a7f963057297c6181f779.tar.gz
tangerine-wallet-browser-ddce3d148ef9323c209a7f963057297c6181f779.tar.bz2
tangerine-wallet-browser-ddce3d148ef9323c209a7f963057297c6181f779.tar.lz
tangerine-wallet-browser-ddce3d148ef9323c209a7f963057297c6181f779.tar.xz
tangerine-wallet-browser-ddce3d148ef9323c209a7f963057297c6181f779.tar.zst
tangerine-wallet-browser-ddce3d148ef9323c209a7f963057297c6181f779.zip
Merge pull request #4486 from MetaMask/i4475-ClearUnknownIdentities
Clear unknown identities
Diffstat (limited to 'app')
-rw-r--r--app/scripts/controllers/preferences.js60
-rw-r--r--app/scripts/lib/bug-notifier.js22
-rw-r--r--app/scripts/metamask-controller.js19
3 files changed, 100 insertions, 1 deletions
diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js
index 760868ddf..2fe009f9a 100644
--- a/app/scripts/controllers/preferences.js
+++ b/app/scripts/controllers/preferences.js
@@ -1,6 +1,9 @@
const ObservableStore = require('obs-store')
const normalizeAddress = require('eth-sig-util').normalize
const extend = require('xtend')
+const notifier = require('../lib/bug-notifier')
+const log = require('loglevel')
+const { version } = require('../../manifest.json')
class PreferencesController {
@@ -28,7 +31,12 @@ class PreferencesController {
featureFlags: {},
currentLocale: opts.initLangCode,
identities: {},
+ lostIdentities: {},
}, opts.initState)
+
+ this.getFirstTimeInfo = opts.getFirstTimeInfo || null
+ this.notifier = opts.notifier || notifier
+
this.store = new ObservableStore(initState)
}
// PUBLIC METHODS
@@ -98,6 +106,58 @@ class PreferencesController {
this.store.updateState({ identities })
}
+ /*
+ * Synchronizes identity entries with known accounts.
+ * Removes any unknown identities, and returns the resulting selected address.
+ *
+ * @param {Array<string>} addresses known to the vault.
+ * @returns {Promise<string>} selectedAddress the selected address.
+ */
+ syncAddresses (addresses) {
+ let { identities, lostIdentities } = this.store.getState()
+
+ let newlyLost = {}
+ Object.keys(identities).forEach((identity) => {
+ if (!addresses.includes(identity)) {
+ newlyLost[identity] = identities[identity]
+ delete identities[identity]
+ }
+ })
+
+ // Identities are no longer present.
+ if (Object.keys(newlyLost).length > 0) {
+
+ // Notify our servers:
+ const uri = 'https://diagnostics.metamask.io/v1/orphanedAccounts'
+ const firstTimeInfo = this.getFirstTimeInfo ? this.getFirstTimeInfo() : {}
+ this.notifier.notify(uri, {
+ accounts: Object.keys(newlyLost),
+ metadata: {
+ version,
+ firstTimeInfo,
+ },
+ })
+ .catch(log.error)
+
+ for (let key in newlyLost) {
+ lostIdentities[key] = newlyLost[key]
+ }
+ }
+
+ this.store.updateState({ identities, lostIdentities })
+ this.addAddresses(addresses)
+
+ // If the selected account is no longer valid,
+ // select an arbitrary other account:
+ let selected = this.getSelectedAddress()
+ if (!addresses.includes(selected)) {
+ selected = addresses[0]
+ this.setSelectedAddress(selected)
+ }
+
+ return selected
+ }
+
/**
* Setter for the `selectedAddress` property
*
diff --git a/app/scripts/lib/bug-notifier.js b/app/scripts/lib/bug-notifier.js
new file mode 100644
index 000000000..4d305b894
--- /dev/null
+++ b/app/scripts/lib/bug-notifier.js
@@ -0,0 +1,22 @@
+class BugNotifier {
+ notify (uri, message) {
+ return postData(uri, message)
+ }
+}
+
+function postData(uri, data) {
+ return fetch(uri, {
+ body: JSON.stringify(data), // must match 'Content-Type' header
+ credentials: 'same-origin', // include, same-origin, *omit
+ headers: {
+ 'content-type': 'application/json',
+ },
+ method: 'POST', // *GET, POST, PUT, DELETE, etc.
+ mode: 'cors', // no-cors, cors, *same-origin
+ })
+}
+
+const notifier = new BugNotifier()
+
+module.exports = notifier
+
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index 96f976568..c753fc06f 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -85,6 +85,7 @@ module.exports = class MetamaskController extends EventEmitter {
this.preferencesController = new PreferencesController({
initState: initState.PreferencesController,
initLangCode: opts.initLangCode,
+ getFirstTimeInfo: () => initState.firstTimeInfo,
})
// currency controller
@@ -356,7 +357,7 @@ module.exports = class MetamaskController extends EventEmitter {
importAccountWithStrategy: nodeify(this.importAccountWithStrategy, this),
// vault management
- submitPassword: nodeify(keyringController.submitPassword, keyringController),
+ submitPassword: nodeify(this.submitPassword, this),
// network management
setProviderType: nodeify(networkController.setProviderType, networkController),
@@ -474,6 +475,22 @@ module.exports = class MetamaskController extends EventEmitter {
}
}
+ /*
+ * Submits the user's password and attempts to unlock the vault.
+ * Also synchronizes the preferencesController, to ensure its schema
+ * is up to date with known accounts once the vault is decrypted.
+ *
+ * @param {string} password - The user's password
+ * @returns {Promise<object>} - The keyringController update.
+ */
+ async submitPassword (password) {
+ await this.keyringController.submitPassword(password)
+ const accounts = await this.keyringController.getAccounts()
+
+ await this.preferencesController.syncAddresses(accounts)
+ return this.keyringController.fullUpdate()
+ }
+
/**
* @type Identity
* @property {string} name - The account nickname.