diff options
author | kumavis <kumavis@users.noreply.github.com> | 2017-01-17 07:02:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-17 07:02:24 +0800 |
commit | ee62a6a3913967a6840d28ca812b94bbfa7a5779 (patch) | |
tree | cac32de6cc520917db9621ce93f7f4ad9abd787e /app/scripts/lib/idStore-migrator.js | |
parent | e8e06542e47b41668f196b6db54290490003845b (diff) | |
parent | 88fabdd2de78d0dd4be86b02c94a64d9748667fa (diff) | |
download | tangerine-wallet-browser-ee62a6a3913967a6840d28ca812b94bbfa7a5779.tar.gz tangerine-wallet-browser-ee62a6a3913967a6840d28ca812b94bbfa7a5779.tar.zst tangerine-wallet-browser-ee62a6a3913967a6840d28ca812b94bbfa7a5779.zip |
Merge pull request #1005 from MetaMask/dev
Merge dev into master!
Diffstat (limited to 'app/scripts/lib/idStore-migrator.js')
-rw-r--r-- | app/scripts/lib/idStore-migrator.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/app/scripts/lib/idStore-migrator.js b/app/scripts/lib/idStore-migrator.js new file mode 100644 index 000000000..655aed0af --- /dev/null +++ b/app/scripts/lib/idStore-migrator.js @@ -0,0 +1,80 @@ +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 { + + constructor ({ configManager }) { + this.configManager = configManager + const hasOldVault = this.hasOldVault() + if (!hasOldVault) { + this.idStore = new IdentityStore({ configManager }) + } + } + + migratedVaultForPassword (password) { + const hasOldVault = this.hasOldVault() + const configManager = this.configManager + + if (!this.idStore) { + this.idStore = new IdentityStore({ configManager }) + } + + if (!hasOldVault) { + return Promise.resolve(null) + } + + const idStore = this.idStore + const submitPassword = denodeify(idStore.submitPassword.bind(idStore)) + + return submitPassword(password) + .then(() => { + const serialized = this.serializeVault() + return this.checkForLostAccounts(serialized) + }) + } + + serializeVault () { + const mnemonic = this.idStore._idmgmt.getSeed() + const numberOfAccounts = this.idStore._getAddresses().length + + return { + type: 'HD Key Tree', + data: { mnemonic, numberOfAccounts }, + } + } + + 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: lostAccounts.map((address) => { + return { + address, + privateKey: this.idStore.exportAccount(address), + } + }), + } + }) + } + + hasOldVault () { + const wallet = this.configManager.getWallet() + return wallet + } +} |