2016-10-29 11:29:25 +02:00
|
|
|
const IdentityStore = require('./idStore')
|
|
|
|
|
|
|
|
|
|
|
|
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-11 19:26:12 +01:00
|
|
|
oldSeedForPassword (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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.idStore.submitPassword(password, (err) => {
|
|
|
|
if (err) return reject(err)
|
|
|
|
try {
|
|
|
|
resolve(this.serializeVault())
|
|
|
|
} catch (e) {
|
|
|
|
reject(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-11-11 19:26:12 +01:00
|
|
|
hasOldVault () {
|
2016-10-29 11:29:25 +02:00
|
|
|
const wallet = this.configManager.getWallet()
|
|
|
|
return wallet
|
|
|
|
}
|
|
|
|
}
|