mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Merge pull request #5066 from whymarrh/fix-key-export
Don't re-render the export modal when the selected identity changes
This commit is contained in:
commit
171f6711d9
@ -137,19 +137,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
encryptor: opts.encryptor || undefined,
|
encryptor: opts.encryptor || undefined,
|
||||||
})
|
})
|
||||||
|
|
||||||
// If only one account exists, make sure it is selected.
|
this.keyringController.memStore.subscribe((s) => this._onKeyringControllerUpdate(s))
|
||||||
this.keyringController.memStore.subscribe((state) => {
|
|
||||||
const addresses = state.keyrings.reduce((res, keyring) => {
|
|
||||||
return res.concat(keyring.accounts)
|
|
||||||
}, [])
|
|
||||||
if (addresses.length === 1) {
|
|
||||||
const address = addresses[0]
|
|
||||||
this.preferencesController.setSelectedAddress(address)
|
|
||||||
}
|
|
||||||
// ensure preferences + identities controller know about all addresses
|
|
||||||
this.preferencesController.addAddresses(addresses)
|
|
||||||
this.accountTracker.syncWithAddresses(addresses)
|
|
||||||
})
|
|
||||||
|
|
||||||
// detect tokens controller
|
// detect tokens controller
|
||||||
this.detectTokensController = new DetectTokensController({
|
this.detectTokensController = new DetectTokensController({
|
||||||
@ -1278,6 +1266,34 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle a KeyringController update
|
||||||
|
* @param {object} state the KC state
|
||||||
|
* @return {Promise<void>}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
async _onKeyringControllerUpdate (state) {
|
||||||
|
const {isUnlocked, keyrings} = state
|
||||||
|
const addresses = keyrings.reduce((acc, {accounts}) => acc.concat(accounts), [])
|
||||||
|
|
||||||
|
if (!addresses.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure preferences + identities controller know about all addresses
|
||||||
|
this.preferencesController.addAddresses(addresses)
|
||||||
|
this.accountTracker.syncWithAddresses(addresses)
|
||||||
|
|
||||||
|
const wasLocked = !isUnlocked
|
||||||
|
if (wasLocked) {
|
||||||
|
const oldSelectedAddress = this.preferencesController.getSelectedAddress()
|
||||||
|
if (!addresses.includes(oldSelectedAddress)) {
|
||||||
|
const address = addresses[0]
|
||||||
|
await this.preferencesController.setSelectedAddress(address)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A method for emitting the full MetaMask state to all registered listeners.
|
* A method for emitting the full MetaMask state to all registered listeners.
|
||||||
* @private
|
* @private
|
||||||
|
@ -814,6 +814,77 @@ describe('MetaMaskController', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#_onKeyringControllerUpdate', function () {
|
||||||
|
it('should do nothing if there are no keyrings in state', async function () {
|
||||||
|
const addAddresses = sinon.fake()
|
||||||
|
const syncWithAddresses = sinon.fake()
|
||||||
|
sandbox.replace(metamaskController, 'preferencesController', {
|
||||||
|
addAddresses,
|
||||||
|
})
|
||||||
|
sandbox.replace(metamaskController, 'accountTracker', {
|
||||||
|
syncWithAddresses,
|
||||||
|
})
|
||||||
|
|
||||||
|
const oldState = metamaskController.getState()
|
||||||
|
await metamaskController._onKeyringControllerUpdate({keyrings: []})
|
||||||
|
|
||||||
|
assert.ok(addAddresses.notCalled)
|
||||||
|
assert.ok(syncWithAddresses.notCalled)
|
||||||
|
assert.deepEqual(metamaskController.getState(), oldState)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should update selected address if keyrings was locked', async function () {
|
||||||
|
const addAddresses = sinon.fake()
|
||||||
|
const getSelectedAddress = sinon.fake.returns('0x42')
|
||||||
|
const setSelectedAddress = sinon.fake()
|
||||||
|
const syncWithAddresses = sinon.fake()
|
||||||
|
sandbox.replace(metamaskController, 'preferencesController', {
|
||||||
|
addAddresses,
|
||||||
|
getSelectedAddress,
|
||||||
|
setSelectedAddress,
|
||||||
|
})
|
||||||
|
sandbox.replace(metamaskController, 'accountTracker', {
|
||||||
|
syncWithAddresses,
|
||||||
|
})
|
||||||
|
|
||||||
|
const oldState = metamaskController.getState()
|
||||||
|
await metamaskController._onKeyringControllerUpdate({
|
||||||
|
isUnlocked: false,
|
||||||
|
keyrings: [{
|
||||||
|
accounts: ['0x1', '0x2'],
|
||||||
|
}],
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.deepEqual(addAddresses.args, [[['0x1', '0x2']]])
|
||||||
|
assert.deepEqual(syncWithAddresses.args, [[['0x1', '0x2']]])
|
||||||
|
assert.deepEqual(setSelectedAddress.args, [['0x1']])
|
||||||
|
assert.deepEqual(metamaskController.getState(), oldState)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should NOT update selected address if already unlocked', async function () {
|
||||||
|
const addAddresses = sinon.fake()
|
||||||
|
const syncWithAddresses = sinon.fake()
|
||||||
|
sandbox.replace(metamaskController, 'preferencesController', {
|
||||||
|
addAddresses,
|
||||||
|
})
|
||||||
|
sandbox.replace(metamaskController, 'accountTracker', {
|
||||||
|
syncWithAddresses,
|
||||||
|
})
|
||||||
|
|
||||||
|
const oldState = metamaskController.getState()
|
||||||
|
await metamaskController._onKeyringControllerUpdate({
|
||||||
|
isUnlocked: true,
|
||||||
|
keyrings: [{
|
||||||
|
accounts: ['0x1', '0x2'],
|
||||||
|
}],
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.deepEqual(addAddresses.args, [[['0x1', '0x2']]])
|
||||||
|
assert.deepEqual(syncWithAddresses.args, [[['0x1', '0x2']]])
|
||||||
|
assert.deepEqual(metamaskController.getState(), oldState)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
function deferredPromise () {
|
function deferredPromise () {
|
||||||
|
@ -7,9 +7,9 @@ const actions = require('../../actions')
|
|||||||
const { getSelectedIdentity } = require('../../selectors')
|
const { getSelectedIdentity } = require('../../selectors')
|
||||||
const Identicon = require('../identicon')
|
const Identicon = require('../identicon')
|
||||||
|
|
||||||
function mapStateToProps (state) {
|
function mapStateToProps (state, ownProps) {
|
||||||
return {
|
return {
|
||||||
selectedIdentity: getSelectedIdentity(state),
|
selectedIdentity: ownProps.selectedIdentity || getSelectedIdentity(state),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,14 +11,22 @@ const ReadOnlyInput = require('../readonly-input')
|
|||||||
const copyToClipboard = require('copy-to-clipboard')
|
const copyToClipboard = require('copy-to-clipboard')
|
||||||
const { checksumAddress } = require('../../util')
|
const { checksumAddress } = require('../../util')
|
||||||
|
|
||||||
function mapStateToProps (state) {
|
function mapStateToPropsFactory () {
|
||||||
|
let selectedIdentity = null
|
||||||
|
return function mapStateToProps (state) {
|
||||||
|
// We should **not** change the identity displayed here even if it changes from underneath us.
|
||||||
|
// If we do, we will be showing the user one private key and a **different** address and name.
|
||||||
|
// Note that the selected identity **will** change from underneath us when we unlock the keyring
|
||||||
|
// which is the expected behavior that we are side-stepping.
|
||||||
|
selectedIdentity = selectedIdentity || getSelectedIdentity(state)
|
||||||
return {
|
return {
|
||||||
warning: state.appState.warning,
|
warning: state.appState.warning,
|
||||||
privateKey: state.appState.accountDetail.privateKey,
|
privateKey: state.appState.accountDetail.privateKey,
|
||||||
network: state.metamask.network,
|
network: state.metamask.network,
|
||||||
selectedIdentity: getSelectedIdentity(state),
|
selectedIdentity,
|
||||||
previousModalState: state.appState.modal.previousModalState.name,
|
previousModalState: state.appState.modal.previousModalState.name,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function mapDispatchToProps (dispatch) {
|
function mapDispatchToProps (dispatch) {
|
||||||
@ -43,7 +51,7 @@ ExportPrivateKeyModal.contextTypes = {
|
|||||||
t: PropTypes.func,
|
t: PropTypes.func,
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = connect(mapStateToProps, mapDispatchToProps)(ExportPrivateKeyModal)
|
module.exports = connect(mapStateToPropsFactory, mapDispatchToProps)(ExportPrivateKeyModal)
|
||||||
|
|
||||||
|
|
||||||
ExportPrivateKeyModal.prototype.exportAccountAndGetPrivateKey = function (password, address) {
|
ExportPrivateKeyModal.prototype.exportAccountAndGetPrivateKey = function (password, address) {
|
||||||
@ -113,6 +121,7 @@ ExportPrivateKeyModal.prototype.render = function () {
|
|||||||
const { privateKey } = this.state
|
const { privateKey } = this.state
|
||||||
|
|
||||||
return h(AccountModalContainer, {
|
return h(AccountModalContainer, {
|
||||||
|
selectedIdentity,
|
||||||
showBackButton: previousModalState === 'ACCOUNT_DETAILS',
|
showBackButton: previousModalState === 'ACCOUNT_DETAILS',
|
||||||
backButtonAction: () => showAccountDetailModal(),
|
backButtonAction: () => showAccountDetailModal(),
|
||||||
}, [
|
}, [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user