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

migration 5 - move keyring controller state to substate

This commit is contained in:
kumavis 2017-01-28 19:19:03 -08:00
parent e9cdbf4f89
commit 4dd6ba9c1b
9 changed files with 122 additions and 112 deletions

View File

@ -6,7 +6,7 @@ const ObservableStore = require('obs-store')
const filter = require('promise-filter') const filter = require('promise-filter')
const encryptor = require('browser-passworder') const encryptor = require('browser-passworder')
const createId = require('./lib/random-id') const createId = require('./lib/random-id')
const normalize = require('./lib/sig-util').normalize const normalizeAddress = require('./lib/sig-util').normalize
const messageManager = require('./lib/message-manager') const messageManager = require('./lib/message-manager')
function noop () {} function noop () {}
@ -74,28 +74,31 @@ class KeyringController extends EventEmitter {
// in this class, but will need to be Promisified when we move our // in this class, but will need to be Promisified when we move our
// persistence to an async model. // persistence to an async model.
getState () { getState () {
const configManager = this.configManager return Promise.all(this.keyrings.map(this.displayForKeyring))
const address = configManager.getSelectedAccount()
const wallet = configManager.getWallet() // old style vault
const vault = configManager.getVault() // new style vault
const keyrings = this.keyrings
return Promise.all(keyrings.map(this.displayForKeyring))
.then((displayKeyrings) => { .then((displayKeyrings) => {
const state = this.store.getState()
// old wallet
const wallet = this.configManager.getWallet()
return { return {
// computed
isInitialized: (!!wallet || !!state.vault),
isUnlocked: (!!this.password),
keyrings: displayKeyrings,
// hard coded
keyringTypes: this.keyringTypes.map(krt => krt.type),
// memStore
identities: this.identities,
// diskStore
selectedAccount: state.selectedAccount,
// configManager
seedWords: this.configManager.getSeedWords(), seedWords: this.configManager.getSeedWords(),
isInitialized: (!!wallet || !!vault),
isUnlocked: Boolean(this.password),
isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(),
unconfMsgs: messageManager.unconfirmedMsgs(),
messages: messageManager.getMsgList(),
selectedAccount: address,
currentFiat: this.configManager.getCurrentFiat(), currentFiat: this.configManager.getCurrentFiat(),
conversionRate: this.configManager.getConversionRate(), conversionRate: this.configManager.getConversionRate(),
conversionDate: this.configManager.getConversionDate(), conversionDate: this.configManager.getConversionDate(),
keyringTypes: this.keyringTypes.map(krt => krt.type), // messageManager
identities: this.identities, unconfMsgs: messageManager.unconfirmedMsgs(),
keyrings: displayKeyrings, messages: messageManager.getMsgList(),
} }
}) })
} }
@ -148,8 +151,8 @@ class KeyringController extends EventEmitter {
.then((accounts) => { .then((accounts) => {
const firstAccount = accounts[0] const firstAccount = accounts[0]
if (!firstAccount) throw new Error('KeyringController - First Account not found.') if (!firstAccount) throw new Error('KeyringController - First Account not found.')
const hexAccount = normalize(firstAccount) const hexAccount = normalizeAddress(firstAccount)
this.configManager.setSelectedAccount(hexAccount) this.setSelectedAccount(hexAccount)
return this.setupAccounts(accounts) return this.setupAccounts(accounts)
}) })
.then(this.persistAllKeyrings.bind(this, password)) .then(this.persistAllKeyrings.bind(this, password))
@ -235,9 +238,9 @@ class KeyringController extends EventEmitter {
// //
// Sets the state's `selectedAccount` value // Sets the state's `selectedAccount` value
// to the specified address. // to the specified address.
setSelectedAccount (address) { setSelectedAccount (account) {
var addr = normalize(address) var address = normalizeAddress(account)
this.configManager.setSelectedAccount(addr) this.store.updateState({ selectedAccount: address })
return this.fullUpdate() return this.fullUpdate()
} }
@ -249,11 +252,19 @@ class KeyringController extends EventEmitter {
// //
// Persists a nickname equal to `label` for the specified account. // Persists a nickname equal to `label` for the specified account.
saveAccountLabel (account, label) { saveAccountLabel (account, label) {
const address = normalize(account) try {
const configManager = this.configManager const hexAddress = normalizeAddress(account)
configManager.setNicknameForWallet(address, label) // update state on diskStore
this.identities[address].name = label const state = this.store.getState()
return Promise.resolve(label) const walletNicknames = state.walletNicknames || {}
walletNicknames[hexAddress] = label
this.store.updateState({ walletNicknames })
// update state on memStore
this.identities[hexAddress].name = label
return Promise.resolve(label)
} catch (err) {
return Promise.reject(err)
}
} }
// Export Account // Export Account
@ -269,7 +280,7 @@ class KeyringController extends EventEmitter {
try { try {
return this.getKeyringForAccount(address) return this.getKeyringForAccount(address)
.then((keyring) => { .then((keyring) => {
return keyring.exportAccount(normalize(address)) return keyring.exportAccount(normalizeAddress(address))
}) })
} catch (e) { } catch (e) {
return Promise.reject(e) return Promise.reject(e)
@ -283,7 +294,7 @@ class KeyringController extends EventEmitter {
// TX Manager to update the state after signing // TX Manager to update the state after signing
signTransaction (ethTx, _fromAddress) { signTransaction (ethTx, _fromAddress) {
const fromAddress = normalize(_fromAddress) const fromAddress = normalizeAddress(_fromAddress)
return this.getKeyringForAccount(fromAddress) return this.getKeyringForAccount(fromAddress)
.then((keyring) => { .then((keyring) => {
return keyring.signTransaction(fromAddress, ethTx) return keyring.signTransaction(fromAddress, ethTx)
@ -356,7 +367,7 @@ class KeyringController extends EventEmitter {
delete msgParams.metamaskId delete msgParams.metamaskId
const approvalCb = this._unconfMsgCbs[msgId] || noop const approvalCb = this._unconfMsgCbs[msgId] || noop
const address = normalize(msgParams.from) const address = normalizeAddress(msgParams.from)
return this.getKeyringForAccount(address) return this.getKeyringForAccount(address)
.then((keyring) => { .then((keyring) => {
return keyring.signMessage(address, msgParams.data) return keyring.signMessage(address, msgParams.data)
@ -394,8 +405,8 @@ class KeyringController extends EventEmitter {
.then((accounts) => { .then((accounts) => {
const firstAccount = accounts[0] const firstAccount = accounts[0]
if (!firstAccount) throw new Error('KeyringController - No account found on keychain.') if (!firstAccount) throw new Error('KeyringController - No account found on keychain.')
const hexAccount = normalize(firstAccount) const hexAccount = normalizeAddress(firstAccount)
this.configManager.setSelectedAccount(hexAccount) this.setSelectedAccount(hexAccount)
this.emit('newAccount', hexAccount) this.emit('newAccount', hexAccount)
return this.setupAccounts(accounts) return this.setupAccounts(accounts)
}) })
@ -431,7 +442,7 @@ class KeyringController extends EventEmitter {
if (!account) { if (!account) {
throw new Error('Problem loading account.') throw new Error('Problem loading account.')
} }
const address = normalize(account) const address = normalizeAddress(account)
this.ethStore.addAccount(address) this.ethStore.addAccount(address)
return this.createNickname(address) return this.createNickname(address)
} }
@ -443,10 +454,11 @@ class KeyringController extends EventEmitter {
// //
// Takes an address, and assigns it an incremented nickname, persisting it. // Takes an address, and assigns it an incremented nickname, persisting it.
createNickname (address) { createNickname (address) {
const hexAddress = normalize(address) const hexAddress = normalizeAddress(address)
var i = Object.keys(this.identities).length const currentIdentityCount = Object.keys(this.identities).length + 1
const oldNickname = this.configManager.nicknameForWallet(address) const nicknames = this.store.getState().walletNicknames || {}
const name = oldNickname || `Account ${++i}` const existingNickname = nicknames[hexAddress]
const name = existingNickname || `Account ${currentIdentityCount}`
this.identities[hexAddress] = { this.identities[hexAddress] = {
address: hexAddress, address: hexAddress,
name, name,
@ -481,7 +493,7 @@ class KeyringController extends EventEmitter {
return this.encryptor.encrypt(this.password, serializedKeyrings) return this.encryptor.encrypt(this.password, serializedKeyrings)
}) })
.then((encryptedString) => { .then((encryptedString) => {
this.configManager.setVault(encryptedString) this.store.updateState({ vault: encryptedString })
return true return true
}) })
} }
@ -494,7 +506,7 @@ class KeyringController extends EventEmitter {
// Attempts to unlock the persisted encrypted storage, // Attempts to unlock the persisted encrypted storage,
// initializing the persisted keyrings to RAM. // initializing the persisted keyrings to RAM.
unlockKeyrings (password) { unlockKeyrings (password) {
const encryptedVault = this.configManager.getVault() const encryptedVault = this.store.getState().vault
if (!encryptedVault) { if (!encryptedVault) {
throw new Error('Cannot unlock without a previous vault.') throw new Error('Cannot unlock without a previous vault.')
} }
@ -574,7 +586,7 @@ class KeyringController extends EventEmitter {
// Returns the currently initialized keyring that manages // Returns the currently initialized keyring that manages
// the specified `address` if one exists. // the specified `address` if one exists.
getKeyringForAccount (address) { getKeyringForAccount (address) {
const hexed = normalize(address) const hexed = normalizeAddress(address)
return Promise.all(this.keyrings.map((keyring) => { return Promise.all(this.keyrings.map((keyring) => {
return Promise.all([ return Promise.all([
@ -583,7 +595,7 @@ class KeyringController extends EventEmitter {
]) ])
})) }))
.then(filter((candidate) => { .then(filter((candidate) => {
const accounts = candidate[1].map(normalize) const accounts = candidate[1].map(normalizeAddress)
return accounts.includes(hexed) return accounts.includes(hexed)
})) }))
.then((winners) => { .then((winners) => {
@ -641,35 +653,9 @@ class KeyringController extends EventEmitter {
this.keyrings = [] this.keyrings = []
this.identities = {} this.identities = {}
this.configManager.setSelectedAccount() this.setSelectedAccount()
} }
} }
//
// Static Class Methods
//
KeyringController.selectFromState = (state) => {
const config = state.config
return {
vault: state.vault,
selectedAccount: config.selectedAccount,
walletNicknames: state.walletNicknames,
}
}
KeyringController.flattenToOldState = (state) => {
const data = {
vault: state.vault,
walletNicknames: state.walletNicknames,
config: {
selectedAccount: state.selectedAccount,
},
}
return data
}
module.exports = KeyringController module.exports = KeyringController

View File

@ -0,0 +1,41 @@
const version = 5
/*
This migration moves state from the flat state trie into KeyringController substate
*/
const extend = require('xtend')
module.exports = {
version,
migrate: function (versionedData) {
versionedData.meta.version = version
try {
const state = versionedData.data
const newState = selectSubstateForKeyringController(state)
versionedData.data = newState
} catch (err) {
console.warn('MetaMask Migration #5' + err.stack)
}
return Promise.resolve(versionedData)
},
}
function selectSubstateForKeyringController (state) {
const config = state.config
const newState = extend(state, {
KeyringController: {
vault: state.vault,
selectedAccount: config.selectedAccount,
walletNicknames: state.walletNicknames,
},
})
delete newState.vault
delete newState.walletNicknames
delete newState.config.selectedAccount
return newState
}

View File

@ -15,4 +15,5 @@ module.exports = [
require('./002'), require('./002'),
require('./003'), require('./003'),
require('./004'), require('./004'),
require('./005'),
] ]

View File

@ -70,7 +70,7 @@
"mississippi": "^1.2.0", "mississippi": "^1.2.0",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"multiplex": "^6.7.0", "multiplex": "^6.7.0",
"obs-store": "^2.2.3", "obs-store": "^2.3.0",
"once": "^1.3.3", "once": "^1.3.3",
"ping-pong-stream": "^1.0.0", "ping-pong-stream": "^1.0.0",
"pojo-migrator": "^2.1.0", "pojo-migrator": "^2.1.0",

View File

@ -89,6 +89,9 @@ describe('IdentityStore to KeyringController migration', function() {
assert(!state.lostAccounts, 'no lost accounts') assert(!state.lostAccounts, 'no lost accounts')
done() done()
}) })
.catch((err) => {
done(err)
})
}) })
}) })
}) })

View File

@ -1,6 +1,6 @@
var assert = require('assert') const assert = require('assert')
var KeyringController = require('../../app/scripts/keyring-controller') const KeyringController = require('../../app/scripts/keyring-controller')
var configManagerGen = require('../lib/mock-config-manager') const configManagerGen = require('../lib/mock-config-manager')
const ethUtil = require('ethereumjs-util') const ethUtil = require('ethereumjs-util')
const BN = ethUtil.BN const BN = ethUtil.BN
const async = require('async') const async = require('async')
@ -55,17 +55,16 @@ describe('KeyringController', function() {
this.timeout(10000) this.timeout(10000)
it('should set a vault on the configManager', function(done) { it('should set a vault on the configManager', function(done) {
keyringController.configManager.setVault(null) keyringController.store.updateState({ vault: null })
assert(!keyringController.configManager.getVault(), 'no previous vault') assert(!keyringController.store.getState().vault, 'no previous vault')
keyringController.createNewVaultAndKeychain(password) keyringController.createNewVaultAndKeychain(password)
.then(() => { .then(() => {
const vault = keyringController.configManager.getVault() const vault = keyringController.store.getState().vault
assert(vault, 'vault created') assert(vault, 'vault created')
done() done()
}) })
.catch((reason) => { .catch((reason) => {
assert.ifError(reason) done(reason)
done()
}) })
}) })
}) })
@ -96,8 +95,7 @@ describe('KeyringController', function() {
done() done()
}) })
.catch((reason) => { .catch((reason) => {
assert.ifError(reason) done(reason)
done()
}) })
}) })
}) })
@ -109,9 +107,6 @@ describe('KeyringController', function() {
const identities = keyringController.identities const identities = keyringController.identities
const identity = identities[fakeAddress] const identity = identities[fakeAddress]
assert.equal(identity.address, fakeAddress) assert.equal(identity.address, fakeAddress)
const nick = keyringController.configManager.nicknameForWallet(fakeAddress)
assert.equal(typeof nick, 'string')
}) })
}) })
@ -122,34 +117,17 @@ describe('KeyringController', function() {
keyringController.identities[ethUtil.addHexPrefix(account)] = {} keyringController.identities[ethUtil.addHexPrefix(account)] = {}
keyringController.saveAccountLabel(account, nick) keyringController.saveAccountLabel(account, nick)
.then((label) => { .then((label) => {
assert.equal(label, nick) try {
const persisted = keyringController.configManager.nicknameForWallet(account) assert.equal(label, nick)
assert.equal(persisted, nick) const persisted = keyringController.store.getState().walletNicknames[account]
done() assert.equal(persisted, nick)
done()
} catch (err) {
done()
}
}) })
.catch((reason) => { .catch((reason) => {
assert.ifError(reason) done(reason)
done()
})
})
this.timeout(10000)
it('retrieves the persisted nickname', function(done) {
const account = addresses[0]
var nick = 'Test nickname'
keyringController.configManager.setNicknameForWallet(account, nick)
keyringController.createNewVaultAndRestore(password, seedWords)
.then((state) => {
const identity = keyringController.identities['0x' + account]
assert.equal(identity.name, nick)
assert(accounts)
done()
})
.catch((reason) => {
assert.ifError(reason)
done()
}) })
}) })
}) })

View File

@ -41,6 +41,7 @@ function AccountDetailScreen () {
AccountDetailScreen.prototype.render = function () { AccountDetailScreen.prototype.render = function () {
var props = this.props var props = this.props
var selected = props.address || Object.keys(props.accounts)[0] var selected = props.address || Object.keys(props.accounts)[0]
var checksumAddress = selected && ethUtil.toChecksumAddress(selected)
var identity = props.identities[selected] var identity = props.identities[selected]
var account = props.accounts[selected] var account = props.accounts[selected]
const { network } = props const { network } = props
@ -116,7 +117,7 @@ AccountDetailScreen.prototype.render = function () {
marginBottom: '15px', marginBottom: '15px',
color: '#AEAEAE', color: '#AEAEAE',
}, },
}, ethUtil.toChecksumAddress(selected)), }, checksumAddress),
// copy and export // copy and export
@ -129,7 +130,7 @@ AccountDetailScreen.prototype.render = function () {
h(AccountInfoLink, { selected, network }), h(AccountInfoLink, { selected, network }),
h(CopyButton, { h(CopyButton, {
value: ethUtil.toChecksumAddress(selected), value: checksumAddress,
}), }),
h(Tooltip, { h(Tooltip, {

View File

@ -17,6 +17,7 @@ function AccountListItem () {
AccountListItem.prototype.render = function () { AccountListItem.prototype.render = function () {
const { identity, selectedAccount, accounts, onShowDetail } = this.props const { identity, selectedAccount, accounts, onShowDetail } = this.props
const checksumAddress = identity && identity.address && ethUtil.toChecksumAddress(identity.address)
const isSelected = selectedAccount === identity.address const isSelected = selectedAccount === identity.address
const account = accounts[identity.address] const account = accounts[identity.address]
const selectedClass = isSelected ? '.selected' : '' const selectedClass = isSelected ? '.selected' : ''
@ -48,7 +49,7 @@ AccountListItem.prototype.render = function () {
overflow: 'hidden', overflow: 'hidden',
textOverflow: 'ellipsis', textOverflow: 'ellipsis',
}, },
}, ethUtil.toChecksumAddress(identity.address)), }, checksumAddress),
h(EthBalance, { h(EthBalance, {
value: account && account.balance, value: account && account.balance,
style: { style: {
@ -65,7 +66,7 @@ AccountListItem.prototype.render = function () {
}, },
}, [ }, [
h(CopyButton, { h(CopyButton, {
value: ethUtil.toChecksumAddress(identity.address), value: checksumAddress,
}), }),
]), ]),
]) ])

View File

@ -152,7 +152,6 @@ InitializeMenuScreen.prototype.createNewVaultAndKeychain = function () {
var password = passwordBox.value var password = passwordBox.value
var passwordConfirmBox = document.getElementById('password-box-confirm') var passwordConfirmBox = document.getElementById('password-box-confirm')
var passwordConfirm = passwordConfirmBox.value var passwordConfirm = passwordConfirmBox.value
// var entropy = document.getElementById('entropy-text-entry').value
if (password.length < 8) { if (password.length < 8) {
this.warning = 'password not long enough' this.warning = 'password not long enough'