Move setAccountLabel into PreferencesController

feature/default_network_editable
Whymarrh Whitby 7 years ago
parent 708422432c
commit 924cc1fcf7
  1. 20
      app/scripts/controllers/preferences.js
  2. 2
      app/scripts/metamask-controller.js
  3. 2
      old-ui/app/account-detail.js
  4. 35
      test/unit/actions/save_account_label_test.js
  5. 34
      test/unit/actions/set_account_label_test.js
  6. 51
      test/unit/preferences-controller-test.js
  7. 2
      ui/app/accounts/new-account/index.js
  8. 12
      ui/app/actions.js
  9. 6
      ui/app/components/modals/account-details-modal.js
  10. 8
      ui/app/components/modals/edit-account-name-modal.js
  11. 2
      ui/app/components/modals/new-account-modal.js
  12. 2
      ui/app/components/pages/create-account/index.js
  13. 2
      ui/app/components/pages/create-account/new-account.js
  14. 2
      ui/app/reducers/metamask.js

@ -27,6 +27,7 @@ class PreferencesController {
useBlockie: false, useBlockie: false,
featureFlags: {}, featureFlags: {},
currentLocale: opts.initLangCode, currentLocale: opts.initLangCode,
identities: {},
}, opts.initState) }, opts.initState)
this.store = new ObservableStore(initState) this.store = new ObservableStore(initState)
} }
@ -155,6 +156,21 @@ class PreferencesController {
return this.store.getState().tokens return this.store.getState().tokens
} }
/**
* Sets a custom label for an account
* @param {string} account the account to set a label for
* @param {string} label the custom label for the account
* @return {Promise<string>}
*/
setAccountLabel (account, label) {
const address = normalizeAddress(account)
const {identities} = this.store.getState()
identities[address] = identities[address] || {}
identities[address].name = label
this.store.updateState({ identities })
return Promise.resolve(label)
}
/** /**
* Gets an updated rpc list from this.addToFrequentRpcList() and sets the `frequentRpcList` to this update list. * Gets an updated rpc list from this.addToFrequentRpcList() and sets the `frequentRpcList` to this update list.
* *
@ -189,8 +205,8 @@ class PreferencesController {
* The returned list will have a max length of 2. If the _url currently exists it the list, it will be moved to the * The returned list will have a max length of 2. If the _url currently exists it the list, it will be moved to the
* end of the list. The current list is modified and returned as a promise. * end of the list. The current list is modified and returned as a promise.
* *
* @param {string} _url The rpc url to add to the frequentRpcList. * @param {string} _url The rpc url to add to the frequentRpcList.
* @returns {Promise<array>} The updated frequentRpcList. * @returns {Promise<array>} The updated frequentRpcList.
* *
*/ */
addToFrequentRpcList (_url) { addToFrequentRpcList (_url) {

@ -363,6 +363,7 @@ module.exports = class MetamaskController extends EventEmitter {
addToken: nodeify(preferencesController.addToken, preferencesController), addToken: nodeify(preferencesController.addToken, preferencesController),
removeToken: nodeify(preferencesController.removeToken, preferencesController), removeToken: nodeify(preferencesController.removeToken, preferencesController),
setCurrentAccountTab: nodeify(preferencesController.setCurrentAccountTab, preferencesController), setCurrentAccountTab: nodeify(preferencesController.setCurrentAccountTab, preferencesController),
setAccountLabel: nodeify(preferencesController.setAccountLabel, preferencesController),
setFeatureFlag: nodeify(preferencesController.setFeatureFlag, preferencesController), setFeatureFlag: nodeify(preferencesController.setFeatureFlag, preferencesController),
// AddressController // AddressController
@ -373,7 +374,6 @@ module.exports = class MetamaskController extends EventEmitter {
createNewVaultAndKeychain: nodeify(this.createNewVaultAndKeychain, this), createNewVaultAndKeychain: nodeify(this.createNewVaultAndKeychain, this),
createNewVaultAndRestore: nodeify(this.createNewVaultAndRestore, this), createNewVaultAndRestore: nodeify(this.createNewVaultAndRestore, this),
addNewKeyring: nodeify(keyringController.addNewKeyring, keyringController), addNewKeyring: nodeify(keyringController.addNewKeyring, keyringController),
saveAccountLabel: nodeify(keyringController.saveAccountLabel, keyringController),
exportAccount: nodeify(keyringController.exportAccount, keyringController), exportAccount: nodeify(keyringController.exportAccount, keyringController),
// txController // txController

@ -91,7 +91,7 @@ AccountDetailScreen.prototype.render = function () {
isEditingLabel: false, isEditingLabel: false,
}, },
saveText: (text) => { saveText: (text) => {
props.dispatch(actions.saveAccountLabel(selected, text)) props.dispatch(actions.setAccountLabel(selected, text))
}, },
}, [ }, [

@ -1,35 +0,0 @@
// var jsdom = require('mocha-jsdom')
var assert = require('assert')
var freeze = require('deep-freeze-strict')
var path = require('path')
var actions = require(path.join(__dirname, '..', '..', '..', 'ui', 'app', 'actions.js'))
var reducers = require(path.join(__dirname, '..', '..', '..', 'ui', 'app', 'reducers.js'))
describe('SAVE_ACCOUNT_LABEL', function () {
it('updates the state.metamask.identities[:i].name property of the state to the action.value.label', function () {
var initialState = {
metamask: {
identities: {
foo: {
name: 'bar',
},
},
},
}
freeze(initialState)
const action = {
type: actions.SAVE_ACCOUNT_LABEL,
value: {
account: 'foo',
label: 'baz',
},
}
freeze(action)
var resultingState = reducers(initialState, action)
assert.equal(resultingState.metamask.identities.foo.name, action.value.label)
})
})

@ -0,0 +1,34 @@
const assert = require('assert')
const freeze = require('deep-freeze-strict')
const path = require('path')
const actions = require(path.join(__dirname, '..', '..', '..', 'ui', 'app', 'actions.js'))
const reducers = require(path.join(__dirname, '..', '..', '..', 'ui', 'app', 'reducers.js'))
describe('SET_ACCOUNT_LABEL', function () {
it('updates the state.metamask.identities[:i].name property of the state to the action.value.label', function () {
const initialState = {
metamask: {
identities: {
foo: {
name: 'bar',
},
},
},
}
freeze(initialState)
const action = {
type: actions.SET_ACCOUNT_LABEL,
value: {
account: 'foo',
label: 'baz',
},
}
freeze(action)
const resultingState = reducers(initialState, action)
assert.equal(resultingState.metamask.identities.foo.name, action.value.label)
})
})

@ -4,16 +4,26 @@ const PreferencesController = require('../../app/scripts/controllers/preferences
describe('preferences controller', function () { describe('preferences controller', function () {
let preferencesController let preferencesController
before(() => { beforeEach(() => {
preferencesController = new PreferencesController() preferencesController = new PreferencesController()
}) })
describe('getTokens', function () {
it('should return an empty list initially', async function () {
await preferencesController.setSelectedAddress('0x7e57e2')
const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 0, 'empty list of tokens')
})
})
describe('addToken', function () { describe('addToken', function () {
it('should add that token to its state', async function () { it('should add that token to its state', async function () {
const address = '0xabcdef1234567' const address = '0xabcdef1234567'
const symbol = 'ABBR' const symbol = 'ABBR'
const decimals = 5 const decimals = 5
await preferencesController.setSelectedAddress('0x7e57e2')
await preferencesController.addToken(address, symbol, decimals) await preferencesController.addToken(address, symbol, decimals)
const tokens = preferencesController.getTokens() const tokens = preferencesController.getTokens()
@ -30,6 +40,7 @@ describe('preferences controller', function () {
const symbol = 'ABBR' const symbol = 'ABBR'
const decimals = 5 const decimals = 5
await preferencesController.setSelectedAddress('0x7e57e2')
await preferencesController.addToken(address, symbol, decimals) await preferencesController.addToken(address, symbol, decimals)
const newDecimals = 6 const newDecimals = 6
@ -43,6 +54,44 @@ describe('preferences controller', function () {
assert.equal(added.symbol, symbol, 'set symbol correctly') assert.equal(added.symbol, symbol, 'set symbol correctly')
assert.equal(added.decimals, newDecimals, 'updated decimals correctly') assert.equal(added.decimals, newDecimals, 'updated decimals correctly')
}) })
it('should allow adding tokens to two separate addresses', async function () {
const address = '0xabcdef1234567'
const symbol = 'ABBR'
const decimals = 5
await preferencesController.setSelectedAddress('0x7e57e2')
await preferencesController.addToken(address, symbol, decimals)
assert.equal(preferencesController.getTokens().length, 1, 'one token added for 1st address')
await preferencesController.setSelectedAddress('0xda22le')
await preferencesController.addToken(address, symbol, decimals)
assert.equal(preferencesController.getTokens().length, 1, 'one token added for 2nd address')
})
})
describe('removeToken', function () {
it('should remove the only token from its state', async function () {
await preferencesController.setSelectedAddress('0x7e57e2')
await preferencesController.addToken('0xa', 'A', 5)
await preferencesController.removeToken('0xa')
const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 0, 'one token removed')
})
it('should remove a token from its state', async function () {
await preferencesController.setSelectedAddress('0x7e57e2')
await preferencesController.addToken('0xa', 'A', 4)
await preferencesController.addToken('0xb', 'B', 5)
await preferencesController.removeToken('0xa')
const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 1, 'one token removed')
const [token1] = tokens
assert.deepEqual(token1, {address: '0xb', symbol: 'B', decimals: 5})
})
}) })
}) })

@ -24,7 +24,7 @@ function mapDispatchToProps (dispatch) {
dispatch(actions.showModal({ name: 'EXPORT_PRIVATE_KEY' })) dispatch(actions.showModal({ name: 'EXPORT_PRIVATE_KEY' }))
}, },
hideModal: () => dispatch(actions.hideModal()), hideModal: () => dispatch(actions.hideModal()),
saveAccountLabel: (address, label) => dispatch(actions.saveAccountLabel(address, label)), setAccountLabel: (address, label) => dispatch(actions.setAccountLabel(address, label)),
} }
} }

@ -124,8 +124,8 @@ var actions = {
SHOW_PRIVATE_KEY: 'SHOW_PRIVATE_KEY', SHOW_PRIVATE_KEY: 'SHOW_PRIVATE_KEY',
showPrivateKey: showPrivateKey, showPrivateKey: showPrivateKey,
exportAccountComplete, exportAccountComplete,
SAVE_ACCOUNT_LABEL: 'SAVE_ACCOUNT_LABEL', SET_ACCOUNT_LABEL: 'SET_ACCOUNT_LABEL',
saveAccountLabel: saveAccountLabel, setAccountLabel,
// tx conf screen // tx conf screen
COMPLETED_TX: 'COMPLETED_TX', COMPLETED_TX: 'COMPLETED_TX',
TRANSACTION_ERROR: 'TRANSACTION_ERROR', TRANSACTION_ERROR: 'TRANSACTION_ERROR',
@ -1598,13 +1598,13 @@ function showPrivateKey (key) {
} }
} }
function saveAccountLabel (account, label) { function setAccountLabel (account, label) {
return (dispatch) => { return (dispatch) => {
dispatch(actions.showLoadingIndication()) dispatch(actions.showLoadingIndication())
log.debug(`background.saveAccountLabel`) log.debug(`background.setAccountLabel`)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
background.saveAccountLabel(account, label, (err) => { background.setAccountLabel(account, label, (err) => {
dispatch(actions.hideLoadingIndication()) dispatch(actions.hideLoadingIndication())
if (err) { if (err) {
@ -1613,7 +1613,7 @@ function saveAccountLabel (account, label) {
} }
dispatch({ dispatch({
type: actions.SAVE_ACCOUNT_LABEL, type: actions.SET_ACCOUNT_LABEL,
value: { account, label }, value: { account, label },
}) })

@ -25,7 +25,7 @@ function mapDispatchToProps (dispatch) {
dispatch(actions.showModal({ name: 'EXPORT_PRIVATE_KEY' })) dispatch(actions.showModal({ name: 'EXPORT_PRIVATE_KEY' }))
}, },
hideModal: () => dispatch(actions.hideModal()), hideModal: () => dispatch(actions.hideModal()),
saveAccountLabel: (address, label) => dispatch(actions.saveAccountLabel(address, label)), setAccountLabel: (address, label) => dispatch(actions.setAccountLabel(address, label)),
} }
} }
@ -49,7 +49,7 @@ AccountDetailsModal.prototype.render = function () {
selectedIdentity, selectedIdentity,
network, network,
showExportPrivateKeyModal, showExportPrivateKeyModal,
saveAccountLabel, setAccountLabel,
} = this.props } = this.props
const { name, address } = selectedIdentity const { name, address } = selectedIdentity
@ -57,7 +57,7 @@ AccountDetailsModal.prototype.render = function () {
h(EditableLabel, { h(EditableLabel, {
className: 'account-modal__name', className: 'account-modal__name',
defaultValue: name, defaultValue: name,
onSubmit: label => saveAccountLabel(address, label), onSubmit: label => setAccountLabel(address, label),
}), }),
h(QrView, { h(QrView, {

@ -18,8 +18,8 @@ function mapDispatchToProps (dispatch) {
hideModal: () => { hideModal: () => {
dispatch(actions.hideModal()) dispatch(actions.hideModal())
}, },
saveAccountLabel: (account, label) => { setAccountLabel: (account, label) => {
dispatch(actions.saveAccountLabel(account, label)) dispatch(actions.setAccountLabel(account, label))
}, },
} }
} }
@ -41,7 +41,7 @@ module.exports = connect(mapStateToProps, mapDispatchToProps)(EditAccountNameMod
EditAccountNameModal.prototype.render = function () { EditAccountNameModal.prototype.render = function () {
const { hideModal, saveAccountLabel, identity } = this.props const { hideModal, setAccountLabel, identity } = this.props
return h('div', {}, [ return h('div', {}, [
h('div.flex-column.edit-account-name-modal-content', { h('div.flex-column.edit-account-name-modal-content', {
@ -69,7 +69,7 @@ EditAccountNameModal.prototype.render = function () {
h('button.btn-clear.edit-account-name-modal-save-button.allcaps', { h('button.btn-clear.edit-account-name-modal-save-button.allcaps', {
onClick: () => { onClick: () => {
if (this.state.inputText.length !== 0) { if (this.state.inputText.length !== 0) {
saveAccountLabel(identity.address, this.state.inputText) setAccountLabel(identity.address, this.state.inputText)
hideModal() hideModal()
} }
}, },

@ -95,7 +95,7 @@ const mapDispatchToProps = dispatch => {
dispatch(actions.addNewAccount()) dispatch(actions.addNewAccount())
.then((newAccountAddress) => { .then((newAccountAddress) => {
if (newAccountName) { if (newAccountName) {
dispatch(actions.saveAccountLabel(newAccountAddress, newAccountName)) dispatch(actions.setAccountLabel(newAccountAddress, newAccountName))
} }
dispatch(actions.hideModal()) dispatch(actions.hideModal())
}) })

@ -75,7 +75,7 @@ const mapDispatchToProps = dispatch => ({
dispatch(actions.showModal({ name: 'EXPORT_PRIVATE_KEY' })) dispatch(actions.showModal({ name: 'EXPORT_PRIVATE_KEY' }))
}, },
hideModal: () => dispatch(actions.hideModal()), hideModal: () => dispatch(actions.hideModal()),
saveAccountLabel: (address, label) => dispatch(actions.saveAccountLabel(address, label)), setAccountLabel: (address, label) => dispatch(actions.setAccountLabel(address, label)),
}) })
module.exports = connect(mapStateToProps, mapDispatchToProps)(CreateAccountPage) module.exports = connect(mapStateToProps, mapDispatchToProps)(CreateAccountPage)

@ -87,7 +87,7 @@ const mapDispatchToProps = dispatch => {
return dispatch(actions.addNewAccount()) return dispatch(actions.addNewAccount())
.then(newAccountAddress => { .then(newAccountAddress => {
if (newAccountName) { if (newAccountName) {
dispatch(actions.saveAccountLabel(newAccountAddress, newAccountName)) dispatch(actions.setAccountLabel(newAccountAddress, newAccountName))
} }
}) })
}, },

@ -163,7 +163,7 @@ function reduceMetamask (state, action) {
selectedTokenAddress: action.value, selectedTokenAddress: action.value,
}) })
case actions.SAVE_ACCOUNT_LABEL: case actions.SET_ACCOUNT_LABEL:
const account = action.value.account const account = action.value.account
const name = action.value.label const name = action.value.label
const id = {} const id = {}

Loading…
Cancel
Save