From 7b9749e30c4f8228fe62c1ad81515117cf7504bc Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 9 Dec 2016 12:24:25 -0800 Subject: Got bad account detection working and added to state --- app/scripts/keyring-controller.js | 13 +++++++------ app/scripts/keyrings/hd.js | 4 ---- app/scripts/lib/idStore-migrator.js | 22 +--------------------- test/integration/lib/keyring-controller-test.js | 8 +++++--- 4 files changed, 13 insertions(+), 34 deletions(-) diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js index d0ce16cbb..6a087c918 100644 --- a/app/scripts/keyring-controller.js +++ b/app/scripts/keyring-controller.js @@ -623,16 +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((result) => { - console.log('migrator called back with') - console.dir(result) - const { serialized, lostAccounts } = result - console.dir({ serialized, lostAccounts }) - this.configManager.setLostAccounts(lostAccounts) 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 55c008601..097d995a7 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -38,7 +38,6 @@ class HdKeyring extends EventEmitter { } if ('numberOfAccounts' in opts) { - console.log('number of accounts detected, adding accounts.') return this.addAccounts(opts.numberOfAccounts) } @@ -49,7 +48,6 @@ class HdKeyring extends EventEmitter { if (!this.root) { this._initFromMnemonic(bip39.generateMnemonic()) } - console.log('attempting to add %s accounts', numberOfAccounts) const oldLen = this.wallets.length const newWallets = [] @@ -59,9 +57,7 @@ class HdKeyring extends EventEmitter { newWallets.push(wallet) this.wallets.push(wallet) } - console.log('hd has %s wallets', this.wallets.length) const hexWallets = newWallets.map(w => w.getAddress().toString('hex')) - console.log('hd calling back w promise of hex wallets ' + hexWallets) return Promise.resolve(hexWallets) } diff --git a/app/scripts/lib/idStore-migrator.js b/app/scripts/lib/idStore-migrator.js index c13015b96..14bd0d8b8 100644 --- a/app/scripts/lib/idStore-migrator.js +++ b/app/scripts/lib/idStore-migrator.js @@ -14,31 +14,21 @@ module.exports = class IdentityStoreMigrator { } migratedVaultForPassword (password) { - console.log('migrating vault for password') const hasOldVault = this.hasOldVault() const configManager = this.configManager if (!this.idStore) { - console.log('initializing id store') this.idStore = new IdentityStore({ configManager }) - console.log('initialized') } if (!hasOldVault) { - console.log('no old vault recognized') return Promise.resolve(null) } - console.log('returning new promise') return new Promise((resolve, reject) => { - console.log('submitting password to idStore') this.idStore.submitPassword(password, (err) => { - console.log('returned ' + err) if (err) return reject(err) - console.log('serializing vault') const serialized = this.serializeVault() - console.log('migrated and serialized into') - console.dir(serialized) this.checkForErrors(serialized) .then(resolve) .catch(reject) @@ -57,16 +47,9 @@ module.exports = class IdentityStoreMigrator { } checkForErrors (serialized) { - console.log('checking for errors, first making hd wallet') const hd = new HdKeyring() - return hd.deserialize(serialized) - .then(() => { - console.log('deserialized, now getting accounts') - console.dir(arguments) - return hd.getAccounts() - }) + return hd.deserialize(serialized.data) .then((hexAccounts) => { - console.log('hd returned accounts', hexAccounts) const newAccounts = hexAccounts.map(normalize) const oldAccounts = this.idStore._getAddresses().map(normalize) const lostAccounts = oldAccounts.reduce((result, account) => { @@ -78,9 +61,6 @@ module.exports = class IdentityStoreMigrator { } }, []) - console.log('migrator has') - console.dir({ newAccounts, oldAccounts, lostAccounts, hexAccounts }) - return { serialized, lostAccounts, diff --git a/test/integration/lib/keyring-controller-test.js b/test/integration/lib/keyring-controller-test.js index e37b5df50..666795a6d 100644 --- a/test/integration/lib/keyring-controller-test.js +++ b/test/integration/lib/keyring-controller-test.js @@ -8,6 +8,8 @@ var STORAGE_KEY = 'metamask-config' var PASSWORD = '12345678' var FIRST_ADDRESS = '0x4dd5d356c5A016A220bCD69e82e5AF680a430d00'.toLowerCase() +var BAD_STYLE_FIRST_ADDRESS = '0xac39b311dceb2a4b2f5d8461c1cdaf756f4f7ae9' + QUnit.module('Old Style Vaults', { beforeEach: function () { @@ -87,7 +89,7 @@ QUnit.module('Old Style Vaults with bad HD seed', { }) QUnit.test('keyringController:isInitialized', function (assert) { - assert.ok(this.keyringController.getState().isInitialized) + assert.ok(this.keyringController.getState().isInitialized, 'vault is initialized') }) QUnit.test('keyringController:submitPassword', function (assert) { @@ -95,9 +97,9 @@ QUnit.test('keyringController:submitPassword', function (assert) { this.keyringController.submitPassword(PASSWORD) .then((state) => { - assert.ok(state.identities[FIRST_ADDRESS]) + assert.ok(state.identities[BAD_STYLE_FIRST_ADDRESS]) assert.equal(state.lostAccounts.length, 1, 'one lost account') - assert.equal(state.lostAccounts[0], 'e15D894BeCB0354c501AE69429B05143679F39e0'.toLowerCase()) + assert.equal(state.lostAccounts[0], '0xe15D894BeCB0354c501AE69429B05143679F39e0'.toLowerCase()) assert.deepEqual(this.configManager.getLostAccounts(), state.lostAccounts, 'persisted') done() }) -- cgit