aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/migrations/_multi-keyring.js
blob: 7a4578ea71bb4ecf20d15f197b58597a98d2487e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const version = 5

/*

This is an incomplete migration bc it requires post-decrypted data
which we dont have access to at the time of this writing.

*/

const ObservableStore = require('obs-store')
const ConfigManager = require('../../app/scripts/lib/config-manager')
const IdentityStoreMigrator = require('../../app/scripts/lib/idStore-migrator')
const KeyringController = require('eth-keyring-controller')

const password = 'obviously not correct'

module.exports = {
  version,

  migrate: function (versionedData) {
    versionedData.meta.version = version

    const store = new ObservableStore(versionedData.data)
    const configManager = new ConfigManager({ store })
    const idStoreMigrator = new IdentityStoreMigrator({ configManager })
    const keyringController = new KeyringController({
      configManager: configManager,
    })

    // attempt to migrate to multiVault
    return idStoreMigrator.migratedVaultForPassword(password)
    .then((result) => {
      // skip if nothing to migrate
      if (!result) return Promise.resolve(versionedData)
      delete versionedData.data.wallet
      // create new keyrings
      const privKeys = result.lostAccounts.map(acct => acct.privateKey)
      return Promise.all([
        keyringController.restoreKeyring(result.serialized),
        keyringController.restoreKeyring({ type: 'Simple Key Pair', data: privKeys }),
      ]).then(() => {
        return keyringController.persistAllKeyrings(password)
      }).then(() => {
        // copy result on to state object
        versionedData.data = store.get()
        return Promise.resolve(versionedData)
      })
    })
  },
}