2016-10-29 11:29:25 +02:00
|
|
|
const IdentityStore = require('./idStore')
|
2016-12-08 23:22:02 +01:00
|
|
|
const HdKeyring = require('../keyrings/hd')
|
|
|
|
const sigUtil = require('./sig-util')
|
|
|
|
const normalize = sigUtil.normalize
|
2016-12-20 01:29:44 +01:00
|
|
|
const denodeify = require('denodeify')
|
2016-10-29 11:29:25 +02:00
|
|
|
|
|
|
|
module.exports = class IdentityStoreMigrator {
|
|
|
|
|
|
|
|
constructor ({ configManager }) {
|
|
|
|
this.configManager = configManager
|
2016-11-02 23:04:50 +01:00
|
|
|
const hasOldVault = this.hasOldVault()
|
|
|
|
if (!hasOldVault) {
|
|
|
|
this.idStore = new IdentityStore({ configManager })
|
|
|
|
}
|
2016-10-29 11:29:25 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 04:52:56 +01:00
|
|
|
migratedVaultForPassword (password) {
|
2016-11-02 23:04:50 +01:00
|
|
|
const hasOldVault = this.hasOldVault()
|
|
|
|
const configManager = this.configManager
|
|
|
|
|
|
|
|
if (!this.idStore) {
|
|
|
|
this.idStore = new IdentityStore({ configManager })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasOldVault) {
|
2016-10-29 11:29:25 +02:00
|
|
|
return Promise.resolve(null)
|
|
|
|
}
|
|
|
|
|
2016-12-20 01:29:44 +01:00
|
|
|
const idStore = this.idStore
|
|
|
|
const submitPassword = denodeify(idStore.submitPassword.bind(idStore))
|
|
|
|
|
|
|
|
return submitPassword(password)
|
|
|
|
.then(() => {
|
|
|
|
const serialized = this.serializeVault()
|
|
|
|
return this.checkForLostAccounts(serialized)
|
2016-10-29 11:29:25 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
serializeVault () {
|
2016-10-29 11:29:25 +02:00
|
|
|
const mnemonic = this.idStore._idmgmt.getSeed()
|
2016-11-18 20:37:07 +01:00
|
|
|
const numberOfAccounts = this.idStore._getAddresses().length
|
2016-10-29 11:29:25 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'HD Key Tree',
|
2016-11-18 20:37:07 +01:00
|
|
|
data: { mnemonic, numberOfAccounts },
|
2016-10-29 11:29:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 01:29:44 +01:00
|
|
|
checkForLostAccounts (serialized) {
|
2016-12-08 23:22:02 +01:00
|
|
|
const hd = new HdKeyring()
|
2016-12-09 21:24:25 +01:00
|
|
|
return hd.deserialize(serialized.data)
|
2016-12-08 23:22:02 +01:00
|
|
|
.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,
|
2016-12-22 02:21:10 +01:00
|
|
|
lostAccounts: lostAccounts.map((address) => {
|
|
|
|
return {
|
|
|
|
address,
|
|
|
|
privateKey: this.idStore.exportAccount(address),
|
|
|
|
}
|
|
|
|
}),
|
2016-12-08 23:22:02 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
hasOldVault () {
|
2016-10-29 11:29:25 +02:00
|
|
|
const wallet = this.configManager.getWallet()
|
|
|
|
return wallet
|
|
|
|
}
|
|
|
|
}
|