From 92453f8715b78c0e6e2cdb9b2e1cfe48c0b013ad Mon Sep 17 00:00:00 2001 From: Csaba Solya Date: Sat, 3 Mar 2018 00:32:57 +0100 Subject: [PATCH 1/7] seed phrase verifier --- app/scripts/lib/seed-phrase-verifier.js | 43 ++++++++ app/scripts/metamask-controller.js | 20 +++- test/unit/seed-phrase-verifier-test.js | 136 ++++++++++++++++++++++++ 3 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 app/scripts/lib/seed-phrase-verifier.js create mode 100644 test/unit/seed-phrase-verifier-test.js diff --git a/app/scripts/lib/seed-phrase-verifier.js b/app/scripts/lib/seed-phrase-verifier.js new file mode 100644 index 000000000..9bea2910e --- /dev/null +++ b/app/scripts/lib/seed-phrase-verifier.js @@ -0,0 +1,43 @@ +const KeyringController = require('eth-keyring-controller') + +const seedPhraseVerifier = { + + verifyAccounts(createdAccounts, seedWords) { + + return new Promise((resolve, reject) => { + + if (!createdAccounts || createdAccounts.length < 1) { + return reject(new Error('No created accounts defined.')) + } + + let keyringController = new KeyringController({}) + let Keyring = keyringController.getKeyringClassForType('HD Key Tree') + let opts = { + mnemonic: seedWords, + numberOfAccounts: createdAccounts.length, + } + + let keyring = new Keyring(opts) + keyring.getAccounts() + .then((restoredAccounts) => { + + log.debug('Created accounts: ' + JSON.stringify(createdAccounts)) + log.debug('Restored accounts: ' + JSON.stringify(restoredAccounts)) + + if (restoredAccounts.length != createdAccounts.length) { + // this should not happen... + return reject(new Error("Wrong number of accounts")) + } + + for (let i = 0; i < restoredAccounts.length; i++) { + if (restoredAccounts[i] !== createdAccounts[i]) { + return reject(new Error('Not identical accounts! Original: ' + createdAccounts[i] + ', Restored: ' + restoredAccounts[i])) + } + } + return resolve() + }) + }) + } +} + +module.exports = seedPhraseVerifier \ No newline at end of file diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index ad4e71792..89bcbd51b 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -37,6 +37,7 @@ const version = require('../manifest.json').version const BN = require('ethereumjs-util').BN const GWEI_BN = new BN('1000000000') const percentile = require('percentile') +const seedPhraseVerifier = require('./lib/seed-phrase-verifier') module.exports = class MetamaskController extends EventEmitter { @@ -592,8 +593,23 @@ module.exports = class MetamaskController extends EventEmitter { primaryKeyring.serialize() .then((serialized) => { const seedWords = serialized.mnemonic - this.configManager.setSeedWords(seedWords) - cb(null, seedWords) + + primaryKeyring.getAccounts() + .then((accounts) => { + if (accounts.length < 1) { + return cb(new Error('MetamaskController - No accounts found')) + } + + seedPhraseVerifier.verifyAccounts(accounts, seedWords) + .then(() => { + this.configManager.setSeedWords(seedWords) + cb(null, seedWords) + }) + .catch((err) => { + log.error(err) + cb(err) + }) + }) }) } diff --git a/test/unit/seed-phrase-verifier-test.js b/test/unit/seed-phrase-verifier-test.js new file mode 100644 index 000000000..a7a463dd3 --- /dev/null +++ b/test/unit/seed-phrase-verifier-test.js @@ -0,0 +1,136 @@ +const assert = require('assert') +const clone = require('clone') +const KeyringController = require('eth-keyring-controller') +const firstTimeState = require('../../app/scripts/first-time-state') +const seedPhraseVerifier = require('../../app/scripts/lib/seed-phrase-verifier') +const mockEncryptor = require('../lib/mock-encryptor') + +describe('SeedPhraseVerifier', function () { + + describe('verifyAccounts', function () { + + var password = 'passw0rd1' + let hdKeyTree = 'HD Key Tree' + + it('should be able to verify created account with seed words', async function () { + + let keyringController = new KeyringController({ + initState: clone(firstTimeState), + encryptor: mockEncryptor, + }) + assert(keyringController) + + let vault = await keyringController.createNewVaultAndKeychain(password) + let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] + + let createdAccounts = await primaryKeyring.getAccounts() + assert.equal(createdAccounts.length, 1) + + let serialized = await primaryKeyring.serialize() + let seedWords = serialized.mnemonic + assert.notEqual(seedWords.length, 0) + + let result = await seedPhraseVerifier.verifyAccounts(createdAccounts, seedWords) + }) + + it('should return error with good but different seed words', async function () { + + let keyringController = new KeyringController({ + initState: clone(firstTimeState), + encryptor: mockEncryptor, + }) + assert(keyringController) + + let vault = await keyringController.createNewVaultAndKeychain(password) + let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] + + let createdAccounts = await primaryKeyring.getAccounts() + assert.equal(createdAccounts.length, 1) + + let serialized = await primaryKeyring.serialize() + let seedWords = 'debris dizzy just program just float decrease vacant alarm reduce speak stadium' + + try { + let result = await seedPhraseVerifier.verifyAccounts(createdAccounts, seedWords) + assert.fail("Should reject") + } catch (err) { + assert.ok(err.message.indexOf('Not identical accounts!') >= 0, 'Wrong error message') + } + }) + + it('should return error with undefined existing accounts', async function () { + + let keyringController = new KeyringController({ + initState: clone(firstTimeState), + encryptor: mockEncryptor, + }) + assert(keyringController) + + let vault = await keyringController.createNewVaultAndKeychain(password) + let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] + + let createdAccounts = await primaryKeyring.getAccounts() + assert.equal(createdAccounts.length, 1) + + let serialized = await primaryKeyring.serialize() + let seedWords = 'debris dizzy just program just float decrease vacant alarm reduce speak stadium' + + try { + let result = await seedPhraseVerifier.verifyAccounts(undefined, seedWords) + assert.fail("Should reject") + } catch (err) { + assert.equal(err.message, 'No created accounts defined.') + } + }) + + it('should return error with empty accounts array', async function () { + + let keyringController = new KeyringController({ + initState: clone(firstTimeState), + encryptor: mockEncryptor, + }) + assert(keyringController) + + let vault = await keyringController.createNewVaultAndKeychain(password) + let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] + + let createdAccounts = await primaryKeyring.getAccounts() + assert.equal(createdAccounts.length, 1) + + let serialized = await primaryKeyring.serialize() + let seedWords = 'debris dizzy just program just float decrease vacant alarm reduce speak stadium' + + try { + let result = await seedPhraseVerifier.verifyAccounts([], seedWords) + assert.fail("Should reject") + } catch (err) { + assert.equal(err.message, 'No created accounts defined.') + } + }) + + it('should be able to verify more than one created account with seed words', async function () { + + let keyringController = new KeyringController({ + initState: clone(firstTimeState), + encryptor: mockEncryptor, + }) + assert(keyringController) + + let vault = await keyringController.createNewVaultAndKeychain(password) + + let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] + + const keyState = await keyringController.addNewAccount(primaryKeyring) + const keyState2 = await keyringController.addNewAccount(primaryKeyring) + + let createdAccounts = await primaryKeyring.getAccounts() + assert.equal(createdAccounts.length, 3) + + let serialized = await primaryKeyring.serialize() + let seedWords = serialized.mnemonic + assert.notEqual(seedWords.length, 0) + + let result = await seedPhraseVerifier.verifyAccounts(createdAccounts, seedWords) + }) + }) +}) From 4bd7f1a37abcd09dc8816fc5b28ad41bc86b1aea Mon Sep 17 00:00:00 2001 From: Csaba Solya Date: Sat, 3 Mar 2018 00:40:40 +0100 Subject: [PATCH 2/7] fix lint issues --- app/scripts/lib/seed-phrase-verifier.js | 18 +++++++++--------- app/scripts/metamask-controller.js | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/scripts/lib/seed-phrase-verifier.js b/app/scripts/lib/seed-phrase-verifier.js index 9bea2910e..97a433fd8 100644 --- a/app/scripts/lib/seed-phrase-verifier.js +++ b/app/scripts/lib/seed-phrase-verifier.js @@ -2,7 +2,7 @@ const KeyringController = require('eth-keyring-controller') const seedPhraseVerifier = { - verifyAccounts(createdAccounts, seedWords) { + verifyAccounts (createdAccounts, seedWords) { return new Promise((resolve, reject) => { @@ -10,23 +10,23 @@ const seedPhraseVerifier = { return reject(new Error('No created accounts defined.')) } - let keyringController = new KeyringController({}) - let Keyring = keyringController.getKeyringClassForType('HD Key Tree') - let opts = { + const keyringController = new KeyringController({}) + const Keyring = keyringController.getKeyringClassForType('HD Key Tree') + const opts = { mnemonic: seedWords, numberOfAccounts: createdAccounts.length, } - let keyring = new Keyring(opts) + const keyring = new Keyring(opts) keyring.getAccounts() .then((restoredAccounts) => { log.debug('Created accounts: ' + JSON.stringify(createdAccounts)) log.debug('Restored accounts: ' + JSON.stringify(restoredAccounts)) - if (restoredAccounts.length != createdAccounts.length) { + if (restoredAccounts.length !== createdAccounts.length) { // this should not happen... - return reject(new Error("Wrong number of accounts")) + return reject(new Error('Wrong number of accounts')) } for (let i = 0; i < restoredAccounts.length; i++) { @@ -37,7 +37,7 @@ const seedPhraseVerifier = { return resolve() }) }) - } + }, } -module.exports = seedPhraseVerifier \ No newline at end of file +module.exports = seedPhraseVerifier diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 89bcbd51b..b9231aa3d 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -599,9 +599,9 @@ module.exports = class MetamaskController extends EventEmitter { if (accounts.length < 1) { return cb(new Error('MetamaskController - No accounts found')) } - + seedPhraseVerifier.verifyAccounts(accounts, seedWords) - .then(() => { + .then(() => { this.configManager.setSeedWords(seedWords) cb(null, seedWords) }) From 3e05b693dbf55ea7ecb791e8f31b7599a6b89ffd Mon Sep 17 00:00:00 2001 From: Csaba Solya Date: Sat, 3 Mar 2018 14:11:02 +0100 Subject: [PATCH 3/7] verify addresses regardless case --- app/scripts/lib/seed-phrase-verifier.js | 2 +- test/unit/seed-phrase-verifier-test.js | 44 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/scripts/lib/seed-phrase-verifier.js b/app/scripts/lib/seed-phrase-verifier.js index 97a433fd8..1f35c2c67 100644 --- a/app/scripts/lib/seed-phrase-verifier.js +++ b/app/scripts/lib/seed-phrase-verifier.js @@ -30,7 +30,7 @@ const seedPhraseVerifier = { } for (let i = 0; i < restoredAccounts.length; i++) { - if (restoredAccounts[i] !== createdAccounts[i]) { + if (restoredAccounts[i].toLowerCase() !== createdAccounts[i].toLowerCase()) { return reject(new Error('Not identical accounts! Original: ' + createdAccounts[i] + ', Restored: ' + restoredAccounts[i])) } } diff --git a/test/unit/seed-phrase-verifier-test.js b/test/unit/seed-phrase-verifier-test.js index a7a463dd3..3e9acfa82 100644 --- a/test/unit/seed-phrase-verifier-test.js +++ b/test/unit/seed-phrase-verifier-test.js @@ -33,6 +33,50 @@ describe('SeedPhraseVerifier', function () { let result = await seedPhraseVerifier.verifyAccounts(createdAccounts, seedWords) }) + it('should be able to verify created account (upper case) with seed words', async function () { + + let keyringController = new KeyringController({ + initState: clone(firstTimeState), + encryptor: mockEncryptor, + }) + assert(keyringController) + + let vault = await keyringController.createNewVaultAndKeychain(password) + let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] + + let createdAccounts = await primaryKeyring.getAccounts() + assert.equal(createdAccounts.length, 1) + let upperCaseAccounts = [createdAccounts[0].toUpperCase()] + + let serialized = await primaryKeyring.serialize() + let seedWords = serialized.mnemonic + assert.notEqual(seedWords.length, 0) + + let result = await seedPhraseVerifier.verifyAccounts(upperCaseAccounts, seedWords) + }) + + it('should be able to verify created account (lower case) with seed words', async function () { + + let keyringController = new KeyringController({ + initState: clone(firstTimeState), + encryptor: mockEncryptor, + }) + assert(keyringController) + + let vault = await keyringController.createNewVaultAndKeychain(password) + let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] + + let createdAccounts = await primaryKeyring.getAccounts() + assert.equal(createdAccounts.length, 1) + let lowerCaseAccounts = [createdAccounts[0].toLowerCase()] + + let serialized = await primaryKeyring.serialize() + let seedWords = serialized.mnemonic + assert.notEqual(seedWords.length, 0) + + let result = await seedPhraseVerifier.verifyAccounts(lowerCaseAccounts, seedWords) + }) + it('should return error with good but different seed words', async function () { let keyringController = new KeyringController({ From 2b86d65d0c3266e8ddfe814abe1d1755fbf23fda Mon Sep 17 00:00:00 2001 From: Csaba Solya Date: Sat, 3 Mar 2018 22:08:10 +0100 Subject: [PATCH 4/7] verify seedwords on log in --- app/scripts/metamask-controller.js | 19 ++++++++-- test/unit/seed-phrase-verifier-test.js | 48 +++++--------------------- ui/app/actions.js | 7 ++++ 3 files changed, 31 insertions(+), 43 deletions(-) diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index b9231aa3d..df9adc248 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -345,6 +345,7 @@ module.exports = class MetamaskController extends EventEmitter { // primary HD keyring management addNewAccount: nodeify(this.addNewAccount, this), placeSeedWords: this.placeSeedWords.bind(this), + verifySeedPhrase: this.verifySeedPhrase.bind(this), clearSeedWordCache: this.clearSeedWordCache.bind(this), resetAccount: this.resetAccount.bind(this), importAccountWithStrategy: this.importAccountWithStrategy.bind(this), @@ -588,6 +589,19 @@ module.exports = class MetamaskController extends EventEmitter { // Used when creating a first vault, to allow confirmation. // Also used when revealing the seed words in the confirmation view. placeSeedWords (cb) { + + this.verifySeedPhrase((err, seedWords) => { + + if (err) { + return cb(err) + } + this.configManager.setSeedWords(seedWords) + return cb(null, seedWords) + }) + } + + verifySeedPhrase (cb) { + const primaryKeyring = this.keyringController.getKeyringsByType('HD Key Tree')[0] if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found')) primaryKeyring.serialize() @@ -602,12 +616,11 @@ module.exports = class MetamaskController extends EventEmitter { seedPhraseVerifier.verifyAccounts(accounts, seedWords) .then(() => { - this.configManager.setSeedWords(seedWords) - cb(null, seedWords) + return cb(null, seedWords) }) .catch((err) => { log.error(err) - cb(err) + return cb(err) }) }) }) diff --git a/test/unit/seed-phrase-verifier-test.js b/test/unit/seed-phrase-verifier-test.js index 3e9acfa82..654fb5994 100644 --- a/test/unit/seed-phrase-verifier-test.js +++ b/test/unit/seed-phrase-verifier-test.js @@ -9,16 +9,20 @@ describe('SeedPhraseVerifier', function () { describe('verifyAccounts', function () { - var password = 'passw0rd1' + let password = 'passw0rd1' let hdKeyTree = 'HD Key Tree' - it('should be able to verify created account with seed words', async function () { - - let keyringController = new KeyringController({ + let keyringController + beforeEach(function () { + keyringController = new KeyringController({ initState: clone(firstTimeState), encryptor: mockEncryptor, }) + assert(keyringController) + }) + + it('should be able to verify created account with seed words', async function () { let vault = await keyringController.createNewVaultAndKeychain(password) let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] @@ -35,12 +39,6 @@ describe('SeedPhraseVerifier', function () { it('should be able to verify created account (upper case) with seed words', async function () { - let keyringController = new KeyringController({ - initState: clone(firstTimeState), - encryptor: mockEncryptor, - }) - assert(keyringController) - let vault = await keyringController.createNewVaultAndKeychain(password) let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] @@ -57,12 +55,6 @@ describe('SeedPhraseVerifier', function () { it('should be able to verify created account (lower case) with seed words', async function () { - let keyringController = new KeyringController({ - initState: clone(firstTimeState), - encryptor: mockEncryptor, - }) - assert(keyringController) - let vault = await keyringController.createNewVaultAndKeychain(password) let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] @@ -79,12 +71,6 @@ describe('SeedPhraseVerifier', function () { it('should return error with good but different seed words', async function () { - let keyringController = new KeyringController({ - initState: clone(firstTimeState), - encryptor: mockEncryptor, - }) - assert(keyringController) - let vault = await keyringController.createNewVaultAndKeychain(password) let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] @@ -104,12 +90,6 @@ describe('SeedPhraseVerifier', function () { it('should return error with undefined existing accounts', async function () { - let keyringController = new KeyringController({ - initState: clone(firstTimeState), - encryptor: mockEncryptor, - }) - assert(keyringController) - let vault = await keyringController.createNewVaultAndKeychain(password) let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] @@ -129,12 +109,6 @@ describe('SeedPhraseVerifier', function () { it('should return error with empty accounts array', async function () { - let keyringController = new KeyringController({ - initState: clone(firstTimeState), - encryptor: mockEncryptor, - }) - assert(keyringController) - let vault = await keyringController.createNewVaultAndKeychain(password) let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] @@ -154,12 +128,6 @@ describe('SeedPhraseVerifier', function () { it('should be able to verify more than one created account with seed words', async function () { - let keyringController = new KeyringController({ - initState: clone(firstTimeState), - encryptor: mockEncryptor, - }) - assert(keyringController) - let vault = await keyringController.createNewVaultAndKeychain(password) let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] diff --git a/ui/app/actions.js b/ui/app/actions.js index 64d5b67e0..9606841ae 100644 --- a/ui/app/actions.js +++ b/ui/app/actions.js @@ -296,6 +296,13 @@ function tryUnlockMetamask (password) { dispatch(actions.unlockSucceeded()) dispatch(actions.transitionForward()) forceUpdateMetamaskState(dispatch) + + background.verifySeedPhrase((err) => { + if (err) { + dispatch(actions.displayWarning(err.message)) + } + }) + } }) } From f7d4a1080df6d1c8ea5f68f88b01caea065b5e92 Mon Sep 17 00:00:00 2001 From: Csaba Solya Date: Sun, 4 Mar 2018 08:47:46 +0100 Subject: [PATCH 5/7] add documentation --- app/scripts/lib/seed-phrase-verifier.js | 5 +++++ app/scripts/metamask-controller.js | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/app/scripts/lib/seed-phrase-verifier.js b/app/scripts/lib/seed-phrase-verifier.js index 1f35c2c67..9cea22029 100644 --- a/app/scripts/lib/seed-phrase-verifier.js +++ b/app/scripts/lib/seed-phrase-verifier.js @@ -2,6 +2,11 @@ const KeyringController = require('eth-keyring-controller') const seedPhraseVerifier = { + // Verifies if the seed words can restore the accounts. + // + // The seed words can recreate the primary keyring and the accounts belonging to it. + // The created accounts in the primary keyring are always the same. + // The keyring always creates the accounts in the same sequence. verifyAccounts (createdAccounts, seedWords) { return new Promise((resolve, reject) => { diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index df9adc248..f523e3919 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -600,6 +600,10 @@ module.exports = class MetamaskController extends EventEmitter { }) } + // Verifies the current vault's seed words if they can restore the + // accounts belonging to the current vault. + // + // Called when the first account is created and on unlocking the vault. verifySeedPhrase (cb) { const primaryKeyring = this.keyringController.getKeyringsByType('HD Key Tree')[0] From 8fde208f0b1da39ccd63b5f256902786e73e9368 Mon Sep 17 00:00:00 2001 From: Csaba Solya Date: Sun, 4 Mar 2018 08:57:55 +0100 Subject: [PATCH 6/7] move more test code to beforeEach --- test/unit/seed-phrase-verifier-test.js | 31 +++++++------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/test/unit/seed-phrase-verifier-test.js b/test/unit/seed-phrase-verifier-test.js index 654fb5994..4e314806b 100644 --- a/test/unit/seed-phrase-verifier-test.js +++ b/test/unit/seed-phrase-verifier-test.js @@ -13,20 +13,23 @@ describe('SeedPhraseVerifier', function () { let hdKeyTree = 'HD Key Tree' let keyringController - beforeEach(function () { + let vault + let primaryKeyring + + beforeEach(async function () { keyringController = new KeyringController({ initState: clone(firstTimeState), encryptor: mockEncryptor, }) assert(keyringController) + + vault = await keyringController.createNewVaultAndKeychain(password) + primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] }) it('should be able to verify created account with seed words', async function () { - let vault = await keyringController.createNewVaultAndKeychain(password) - let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] - let createdAccounts = await primaryKeyring.getAccounts() assert.equal(createdAccounts.length, 1) @@ -39,11 +42,9 @@ describe('SeedPhraseVerifier', function () { it('should be able to verify created account (upper case) with seed words', async function () { - let vault = await keyringController.createNewVaultAndKeychain(password) - let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] - let createdAccounts = await primaryKeyring.getAccounts() assert.equal(createdAccounts.length, 1) + let upperCaseAccounts = [createdAccounts[0].toUpperCase()] let serialized = await primaryKeyring.serialize() @@ -55,9 +56,6 @@ describe('SeedPhraseVerifier', function () { it('should be able to verify created account (lower case) with seed words', async function () { - let vault = await keyringController.createNewVaultAndKeychain(password) - let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] - let createdAccounts = await primaryKeyring.getAccounts() assert.equal(createdAccounts.length, 1) let lowerCaseAccounts = [createdAccounts[0].toLowerCase()] @@ -71,9 +69,6 @@ describe('SeedPhraseVerifier', function () { it('should return error with good but different seed words', async function () { - let vault = await keyringController.createNewVaultAndKeychain(password) - let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] - let createdAccounts = await primaryKeyring.getAccounts() assert.equal(createdAccounts.length, 1) @@ -90,9 +85,6 @@ describe('SeedPhraseVerifier', function () { it('should return error with undefined existing accounts', async function () { - let vault = await keyringController.createNewVaultAndKeychain(password) - let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] - let createdAccounts = await primaryKeyring.getAccounts() assert.equal(createdAccounts.length, 1) @@ -109,9 +101,6 @@ describe('SeedPhraseVerifier', function () { it('should return error with empty accounts array', async function () { - let vault = await keyringController.createNewVaultAndKeychain(password) - let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] - let createdAccounts = await primaryKeyring.getAccounts() assert.equal(createdAccounts.length, 1) @@ -128,10 +117,6 @@ describe('SeedPhraseVerifier', function () { it('should be able to verify more than one created account with seed words', async function () { - let vault = await keyringController.createNewVaultAndKeychain(password) - - let primaryKeyring = keyringController.getKeyringsByType(hdKeyTree)[0] - const keyState = await keyringController.addNewAccount(primaryKeyring) const keyState2 = await keyringController.addNewAccount(primaryKeyring) From 59007a6c36055f9197ad83ccb1741fa186b85f53 Mon Sep 17 00:00:00 2001 From: Csaba Solya Date: Tue, 6 Mar 2018 15:56:27 +0100 Subject: [PATCH 7/7] modify verifySeedPhrase to async and call it from addNewAccount also --- app/scripts/metamask-controller.js | 62 +++++++++++++++--------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index f523e3919..0a5c1d36f 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -345,7 +345,7 @@ module.exports = class MetamaskController extends EventEmitter { // primary HD keyring management addNewAccount: nodeify(this.addNewAccount, this), placeSeedWords: this.placeSeedWords.bind(this), - verifySeedPhrase: this.verifySeedPhrase.bind(this), + verifySeedPhrase: nodeify(this.verifySeedPhrase, this), clearSeedWordCache: this.clearSeedWordCache.bind(this), resetAccount: this.resetAccount.bind(this), importAccountWithStrategy: this.importAccountWithStrategy.bind(this), @@ -567,14 +567,18 @@ module.exports = class MetamaskController extends EventEmitter { // Opinionated Keyring Management // - async addNewAccount (cb) { + async addNewAccount () { const primaryKeyring = this.keyringController.getKeyringsByType('HD Key Tree')[0] - if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found')) + if (!primaryKeyring) { + throw new Error('MetamaskController - No HD Key Tree found') + } const keyringController = this.keyringController const oldAccounts = await keyringController.getAccounts() const keyState = await keyringController.addNewAccount(primaryKeyring) const newAccounts = await keyringController.getAccounts() + await this.verifySeedPhrase() + newAccounts.forEach((address) => { if (!oldAccounts.includes(address)) { this.preferencesController.setSelectedAddress(address) @@ -590,44 +594,42 @@ module.exports = class MetamaskController extends EventEmitter { // Also used when revealing the seed words in the confirmation view. placeSeedWords (cb) { - this.verifySeedPhrase((err, seedWords) => { - - if (err) { + this.verifySeedPhrase() + .then((seedWords) => { + this.configManager.setSeedWords(seedWords) + return cb(null, seedWords) + }) + .catch((err) => { return cb(err) - } - this.configManager.setSeedWords(seedWords) - return cb(null, seedWords) - }) + }) } // Verifies the current vault's seed words if they can restore the // accounts belonging to the current vault. // // Called when the first account is created and on unlocking the vault. - verifySeedPhrase (cb) { + async verifySeedPhrase () { const primaryKeyring = this.keyringController.getKeyringsByType('HD Key Tree')[0] - if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found')) - primaryKeyring.serialize() - .then((serialized) => { - const seedWords = serialized.mnemonic + if (!primaryKeyring) { + throw new Error('MetamaskController - No HD Key Tree found') + } - primaryKeyring.getAccounts() - .then((accounts) => { - if (accounts.length < 1) { - return cb(new Error('MetamaskController - No accounts found')) - } + const serialized = await primaryKeyring.serialize() + const seedWords = serialized.mnemonic - seedPhraseVerifier.verifyAccounts(accounts, seedWords) - .then(() => { - return cb(null, seedWords) - }) - .catch((err) => { - log.error(err) - return cb(err) - }) - }) - }) + const accounts = await primaryKeyring.getAccounts() + if (accounts.length < 1) { + throw new Error('MetamaskController - No accounts found') + } + + try { + await seedPhraseVerifier.verifyAccounts(accounts, seedWords) + return seedWords + } catch (err) { + log.error(err.message) + throw err + } } // ClearSeedWordCache