diff options
author | Csaba Solya <csaba.solya@gmail.com> | 2018-03-03 07:32:57 +0800 |
---|---|---|
committer | Csaba Solya <csaba.solya@gmail.com> | 2018-03-03 07:32:57 +0800 |
commit | 92453f8715b78c0e6e2cdb9b2e1cfe48c0b013ad (patch) | |
tree | d430911e722a580ffa7d282c59a8a94554a2feb1 /app | |
parent | f22dfd4ae8031e3f7b4972a1cc8f119b99007717 (diff) | |
download | tangerine-wallet-browser-92453f8715b78c0e6e2cdb9b2e1cfe48c0b013ad.tar tangerine-wallet-browser-92453f8715b78c0e6e2cdb9b2e1cfe48c0b013ad.tar.gz tangerine-wallet-browser-92453f8715b78c0e6e2cdb9b2e1cfe48c0b013ad.tar.bz2 tangerine-wallet-browser-92453f8715b78c0e6e2cdb9b2e1cfe48c0b013ad.tar.lz tangerine-wallet-browser-92453f8715b78c0e6e2cdb9b2e1cfe48c0b013ad.tar.xz tangerine-wallet-browser-92453f8715b78c0e6e2cdb9b2e1cfe48c0b013ad.tar.zst tangerine-wallet-browser-92453f8715b78c0e6e2cdb9b2e1cfe48c0b013ad.zip |
seed phrase verifier
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/lib/seed-phrase-verifier.js | 43 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 20 |
2 files changed, 61 insertions, 2 deletions
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) + }) + }) }) } |