migrations - wip - 005 multivault migration

feature/default_network_editable
kumavis 8 years ago
parent b33c51c0a6
commit 80514d73b5
  1. 8
      app/scripts/background.js
  2. 19
      app/scripts/lib/migrator/index.js
  3. 12
      app/scripts/migrations/002.js
  4. 10
      app/scripts/migrations/003.js
  5. 14
      app/scripts/migrations/004.js
  6. 44
      app/scripts/migrations/005.js
  7. 6
      app/scripts/migrations/index.js

@ -3,7 +3,7 @@ const Dnode = require('dnode')
const eos = require('end-of-stream') const eos = require('end-of-stream')
const asyncQ = require('async-q') const asyncQ = require('async-q')
const Migrator = require('./lib/migrator/') const Migrator = require('./lib/migrator/')
const migrations = require('./lib/migrations') const migrations = require('./lib/migrations/')
const LocalStorageStore = require('./lib/observable/local-storage') const LocalStorageStore = require('./lib/observable/local-storage')
const PortStream = require('./lib/port-stream.js') const PortStream = require('./lib/port-stream.js')
const notification = require('./lib/notifications.js') const notification = require('./lib/notifications.js')
@ -35,10 +35,7 @@ asyncQ.waterfall([
function loadStateFromPersistence() { function loadStateFromPersistence() {
// migrations // migrations
let migrator = new Migrator({ migrations }) let migrator = new Migrator({ migrations })
let initialState = { let initialState = migrator.generateInitialState(firstTimeState)
meta: { version: migrator.defaultVersion },
data: firstTimeState,
}
return asyncQ.waterfall([ return asyncQ.waterfall([
// read from disk // read from disk
() => Promise.resolve(diskStore.get() || initialState), () => Promise.resolve(diskStore.get() || initialState),
@ -68,6 +65,7 @@ function setupController (initState) {
// initial state // initial state
initState, initState,
}) })
global.metamaskController = controller
// setup state persistence // setup state persistence
controller.store.subscribe((newState) => diskStore) controller.store.subscribe((newState) => diskStore)

@ -7,22 +7,31 @@ class Migrator {
this.migrations = migrations.sort((a, b) => a.version - b.version) this.migrations = migrations.sort((a, b) => a.version - b.version)
let lastMigration = this.migrations.slice(-1)[0] let lastMigration = this.migrations.slice(-1)[0]
// use specified defaultVersion or highest migration version // use specified defaultVersion or highest migration version
this.defaultVersion = opts.defaultVersion || lastMigration && lastMigration.version || 0 this.defaultVersion = opts.defaultVersion || (lastMigration && lastMigration.version) || 0
} }
// run all pending migrations on meta in place // run all pending migrations on meta in place
migrateData (meta = { version: this.defaultVersion }) { migrateData (versionedData = this.generateInitialState()) {
let remaining = this.migrations.filter(migrationIsPending) let remaining = this.migrations.filter(migrationIsPending)
return ( return (
asyncQ.eachSeries(remaining, (migration) => migration.migrate(meta)) asyncQ.eachSeries(remaining, (migration) => migration.migrate(versionedData))
.then(() => meta) .then(() => versionedData)
) )
// migration is "pending" if hit has a higher // migration is "pending" if hit has a higher
// version number than currentVersion // version number than currentVersion
function migrationIsPending(migration) { function migrationIsPending(migration) {
return migration.version > meta.version return migration.version > versionedData.meta.version
}
}
generateInitialState (initState) {
return {
meta: {
version: this.defaultVersion,
},
data: initState,
} }
} }

@ -3,14 +3,14 @@ const version = 2
module.exports = { module.exports = {
version, version,
migrate: function (meta) { migrate: function (versionedData) {
meta.version = version versionedData.meta.version = version
try { try {
if (meta.data.config.provider.type === 'etherscan') { if (versionedData.data.config.provider.type === 'etherscan') {
meta.data.config.provider.type = 'rpc' versionedData.data.config.provider.type = 'rpc'
meta.data.config.provider.rpcTarget = 'https://rpc.metamask.io/' versionedData.data.config.provider.rpcTarget = 'https://rpc.metamask.io/'
} }
} catch (e) {} } catch (e) {}
return Promise.resolve(meta) return Promise.resolve(versionedData)
}, },
} }

@ -5,13 +5,13 @@ const newTestRpc = 'https://testrpc.metamask.io/'
module.exports = { module.exports = {
version, version,
migrate: function (meta) { migrate: function (versionedData) {
meta.version = version versionedData.meta.version = version
try { try {
if (meta.data.config.provider.rpcTarget === oldTestRpc) { if (versionedData.data.config.provider.rpcTarget === oldTestRpc) {
meta.data.config.provider.rpcTarget = newTestRpc versionedData.data.config.provider.rpcTarget = newTestRpc
} }
} catch (e) {} } catch (e) {}
return Promise.resolve(meta) return Promise.resolve(versionedData)
}, },
} }

@ -3,23 +3,23 @@ const version = 4
module.exports = { module.exports = {
version, version,
migrate: function (meta) { migrate: function (versionedData) {
meta.version = version versionedData.meta.version = version
try { try {
if (meta.data.config.provider.type !== 'rpc') return Promise.resolve(meta) if (versionedData.data.config.provider.type !== 'rpc') return Promise.resolve(versionedData)
switch (meta.data.config.provider.rpcTarget) { switch (versionedData.data.config.provider.rpcTarget) {
case 'https://testrpc.metamask.io/': case 'https://testrpc.metamask.io/':
meta.data.config.provider = { versionedData.data.config.provider = {
type: 'testnet', type: 'testnet',
} }
break break
case 'https://rpc.metamask.io/': case 'https://rpc.metamask.io/':
meta.data.config.provider = { versionedData.data.config.provider = {
type: 'mainnet', type: 'mainnet',
} }
break break
} }
} catch (_) {} } catch (_) {}
return Promise.resolve(meta) return Promise.resolve(versionedData)
}, },
} }

@ -0,0 +1,44 @@
const version = 5
const ObservableStore = require('../../app/scripts/lib/observable/')
const ConfigManager = require('../../app/scripts/lib/config-manager')
const IdentityStoreMigrator = require('../../app/scripts/lib/idStore-migrator')
const KeyringController = require('../../app/scripts/lib/keyring-controller')
const password = 'obviously not correct'
module.exports = {
version,
migrate: function (versionedData) {
versionedData.meta.version = version
let store = new ObservableStore(versionedData.data)
let configManager = new ConfigManager({ store })
let idStoreMigrator = new IdentityStoreMigrator({ configManager })
let 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)
})
})
},
}

@ -12,7 +12,7 @@
// config data format, and returns the new one. // config data format, and returns the new one.
module.exports = [ module.exports = [
require('../migrations/002'), require('./002'),
require('../migrations/003'), require('./003'),
require('../migrations/004'), require('./004'),
] ]
Loading…
Cancel
Save