diff options
Merge pull request #916 from MetaMask/i894-DetectBadAccounts
I894 detect bad accounts
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/keyring-controller.js | 11 | ||||
-rw-r--r-- | app/scripts/keyrings/hd.js | 8 | ||||
-rw-r--r-- | app/scripts/lib/config-manager.js | 11 | ||||
-rw-r--r-- | app/scripts/lib/idStore-migrator.js | 43 |
4 files changed, 57 insertions, 16 deletions
diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js index 58366c26f..2888e58a9 100644 --- a/app/scripts/keyring-controller.js +++ b/app/scripts/keyring-controller.js @@ -114,6 +114,7 @@ module.exports = class KeyringController extends EventEmitter { conversionDate: this.configManager.getConversionDate(), keyringTypes: this.keyringTypes.map(krt => krt.type), identities: this.identities, + lostAccounts: this.configManager.getLostAccounts(), } } @@ -622,11 +623,17 @@ module.exports = class KeyringController extends EventEmitter { // may be completed without interruption. migrateOldVaultIfAny (password) { const shouldMigrate = !!this.configManager.getWallet() && !this.configManager.getVault() + if (!shouldMigrate) { + return Promise.resolve() + } + return this.idStoreMigrator.migratedVaultForPassword(password) - .then((serialized) => { + .then((result) => { this.password = password - if (serialized && shouldMigrate) { + if (result && shouldMigrate) { + const { serialized, lostAccounts } = result + this.configManager.setLostAccounts(lostAccounts) return this.restoreKeyring(serialized) .then(keyring => keyring.getAccounts()) .then((accounts) => { diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index cfec56561..80b713b58 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -33,15 +33,15 @@ class HdKeyring extends EventEmitter { this.mnemonic = null this.root = null - if ('mnemonic' in opts) { + if (opts.mnemonic) { this._initFromMnemonic(opts.mnemonic) } - if ('numberOfAccounts' in opts) { - this.addAccounts(opts.numberOfAccounts) + if (opts.numberOfAccounts) { + return this.addAccounts(opts.numberOfAccounts) } - return Promise.resolve() + return Promise.resolve([]) } addAccounts (numberOfAccounts = 1) { diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index 59cc2b63c..d36ccf0db 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -432,3 +432,14 @@ ConfigManager.prototype.setGasMultiplier = function (gasMultiplier) { data.gasMultiplier = gasMultiplier this.setData(data) } + +ConfigManager.prototype.setLostAccounts = function (lostAccounts) { + var data = this.getData() + data.lostAccounts = lostAccounts + this.setData(data) +} + +ConfigManager.prototype.getLostAccounts = function () { + var data = this.getData() + return data.lostAccounts || [] +} diff --git a/app/scripts/lib/idStore-migrator.js b/app/scripts/lib/idStore-migrator.js index 40b08efee..2e9418376 100644 --- a/app/scripts/lib/idStore-migrator.js +++ b/app/scripts/lib/idStore-migrator.js @@ -1,5 +1,8 @@ const IdentityStore = require('./idStore') - +const HdKeyring = require('../keyrings/hd') +const sigUtil = require('./sig-util') +const normalize = sigUtil.normalize +const denodeify = require('denodeify') module.exports = class IdentityStoreMigrator { @@ -23,15 +26,13 @@ module.exports = class IdentityStoreMigrator { return Promise.resolve(null) } - return new Promise((resolve, reject) => { - this.idStore.submitPassword(password, (err) => { - if (err) return reject(err) - try { - resolve(this.serializeVault()) - } catch (e) { - reject(e) - } - }) + const idStore = this.idStore + const submitPassword = denodeify(idStore.submitPassword.bind(idStore)) + + return submitPassword(password) + .then(() => { + const serialized = this.serializeVault() + return this.checkForLostAccounts(serialized) }) } @@ -45,6 +46,28 @@ module.exports = class IdentityStoreMigrator { } } + checkForLostAccounts (serialized) { + const hd = new HdKeyring() + return hd.deserialize(serialized.data) + .then((hexAccounts) => { + const newAccounts = hexAccounts.map(normalize) + const oldAccounts = this.idStore._getAddresses().map(normalize) + const lostAccounts = oldAccounts.reduce((result, account) => { + if (newAccounts.includes(account)) { + return result + } else { + result.push(account) + return result + } + }, []) + + return { + serialized, + lostAccounts, + } + }) + } + hasOldVault () { const wallet = this.configManager.getWallet() return wallet |