1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Move old keystore migration code to metamask controller

Allows keyring controller to be more generic, less opinionated, and who knows, maybe sooner publishable as its own thing.
This commit is contained in:
Dan Finlay 2016-12-21 11:01:04 -08:00
parent b3533f9bf7
commit 48f2ae2154
2 changed files with 50 additions and 46 deletions

View File

@ -9,7 +9,6 @@ const encryptor = require('browser-passworder')
const normalize = require('./lib/sig-util').normalize const normalize = require('./lib/sig-util').normalize
const messageManager = require('./lib/message-manager') const messageManager = require('./lib/message-manager')
const IdStoreMigrator = require('./lib/idStore-migrator')
const BN = ethUtil.BN const BN = ethUtil.BN
// Keyrings: // Keyrings:
@ -45,11 +44,6 @@ module.exports = class KeyringController extends EventEmitter {
this._unconfMsgCbs = {} this._unconfMsgCbs = {}
this.getNetwork = opts.getNetwork this.getNetwork = opts.getNetwork
// TEMPORARY UNTIL FULL DEPRECATION:
this.idStoreMigrator = new IdStoreMigrator({
configManager: this.configManager,
})
} }
// Set Store // Set Store
@ -221,10 +215,7 @@ module.exports = class KeyringController extends EventEmitter {
// Temporarily also migrates any old-style vaults first, as well. // Temporarily also migrates any old-style vaults first, as well.
// (Pre MetaMask 3.0.0) // (Pre MetaMask 3.0.0)
submitPassword (password) { submitPassword (password) {
return this.migrateOldVaultIfAny(password)
.then(() => {
return this.unlockKeyrings(password) return this.unlockKeyrings(password)
})
.then((keyrings) => { .then((keyrings) => {
this.keyrings = keyrings this.keyrings = keyrings
return this.fullUpdate() return this.fullUpdate()
@ -610,41 +601,6 @@ module.exports = class KeyringController extends EventEmitter {
// THESE METHODS ARE ONLY USED INTERNALLY TO THE KEYRING-CONTROLLER // THESE METHODS ARE ONLY USED INTERNALLY TO THE KEYRING-CONTROLLER
// AND SO MAY BE CHANGED MORE LIBERALLY THAN THE ABOVE METHODS. // AND SO MAY BE CHANGED MORE LIBERALLY THAN THE ABOVE METHODS.
// Migrate Old Vault If Any
// @string password
//
// returns Promise()
//
// Temporary step used when logging in.
// Checks if old style (pre-3.0.0) Metamask Vault exists.
// If so, persists that vault in the new vault format
// with the provided password, so the other unlock steps
// may be completed without interruption.
migrateOldVaultIfAny (password) {
const shouldMigrate = !!this.configManager.getWallet() && !this.configManager.getVault()
if (!shouldMigrate) {
return Promise.resolve()
}
return this.idStoreMigrator.migratedVaultForPassword(password)
.then((result) => {
this.password = password
if (result && shouldMigrate) {
const { serialized, lostAccounts } = result
this.configManager.setLostAccounts(lostAccounts)
return this.restoreKeyring(serialized)
.then(keyring => keyring.getAccounts())
.then((accounts) => {
this.configManager.setSelectedAccount(accounts[0])
return this.persistAllKeyrings()
})
} else {
return Promise.resolve()
}
})
}
// Create First Key Tree // Create First Key Tree
// returns @Promise // returns @Promise
// //

View File

@ -10,6 +10,7 @@ const ConfigManager = require('./lib/config-manager')
const extension = require('./lib/extension') const extension = require('./lib/extension')
const autoFaucet = require('./lib/auto-faucet') const autoFaucet = require('./lib/auto-faucet')
const nodeify = require('./lib/nodeify') const nodeify = require('./lib/nodeify')
const IdStoreMigrator = require('./lib/idStore-migrator')
module.exports = class MetamaskController { module.exports = class MetamaskController {
@ -44,6 +45,11 @@ module.exports = class MetamaskController {
this.checkTOSChange() this.checkTOSChange()
this.scheduleConversionInterval() this.scheduleConversionInterval()
// TEMPORARY UNTIL FULL DEPRECATION:
this.idStoreMigrator = new IdStoreMigrator({
configManager: this.configManager,
})
} }
getState () { getState () {
@ -61,6 +67,7 @@ module.exports = class MetamaskController {
getApi () { getApi () {
const keyringController = this.keyringController const keyringController = this.keyringController
const noticeController = this.noticeController const noticeController = this.noticeController
const submitPassword = keyringController.submitPassword.bind(keyringController)
return { return {
getState: (cb) => { cb(null, this.getState()) }, getState: (cb) => { cb(null, this.getState()) },
@ -81,7 +88,12 @@ module.exports = class MetamaskController {
placeSeedWords: nodeify(keyringController.placeSeedWords).bind(keyringController), placeSeedWords: nodeify(keyringController.placeSeedWords).bind(keyringController),
clearSeedWordCache: nodeify(keyringController.clearSeedWordCache).bind(keyringController), clearSeedWordCache: nodeify(keyringController.clearSeedWordCache).bind(keyringController),
setLocked: nodeify(keyringController.setLocked).bind(keyringController), setLocked: nodeify(keyringController.setLocked).bind(keyringController),
submitPassword: nodeify(keyringController.submitPassword).bind(keyringController), submitPassword: (password, cb) => {
this.migrateOldVaultIfAny()
.then(submitPassword)
.then((newState) => { cb(null, newState) })
.catch((reason) => { cb(reason) })
},
addNewKeyring: nodeify(keyringController.addNewKeyring).bind(keyringController), addNewKeyring: nodeify(keyringController.addNewKeyring).bind(keyringController),
addNewAccount: nodeify(keyringController.addNewAccount).bind(keyringController), addNewAccount: nodeify(keyringController.addNewAccount).bind(keyringController),
setSelectedAccount: nodeify(keyringController.setSelectedAccount).bind(keyringController), setSelectedAccount: nodeify(keyringController.setSelectedAccount).bind(keyringController),
@ -425,4 +437,40 @@ module.exports = class MetamaskController {
cb(null, this.getState()) cb(null, this.getState())
}) })
} }
// Migrate Old Vault If Any
// @string password
//
// returns Promise()
//
// Temporary step used when logging in.
// Checks if old style (pre-3.0.0) Metamask Vault exists.
// If so, persists that vault in the new vault format
// with the provided password, so the other unlock steps
// may be completed without interruption.
migrateOldVaultIfAny (password) {
const shouldMigrate = !!this.configManager.getWallet() && !this.configManager.getVault()
if (!shouldMigrate) {
return Promise.resolve(password)
}
return this.idStoreMigrator.migratedVaultForPassword(password)
.then((result) => {
if (result && shouldMigrate) {
const { serialized, lostAccounts } = result
this.configManager.setLostAccounts(lostAccounts)
return this.keyringController.restoreKeyring(serialized)
.then(keyring => keyring.getAccounts())
.then((accounts) => {
this.configManager.setSelectedAccount(accounts[0])
return this.keyringController.persistAllKeyrings()
.then(() => password)
})
} else {
return Promise.resolve(password)
}
})
}
} }