diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index fc40c8b6d..ee148c285 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -464,7 +464,7 @@ module.exports = class MetamaskController extends EventEmitter { whitelistPhishingDomain: this.whitelistPhishingDomain.bind(this), // AddressController - setAddressBook: this.addressBookController.set.bind(this.addressBookController), + setAddressBook: nodeify(this.addressBookController.set, this.addressBookController), removeFromAddressBook: this.addressBookController.delete.bind(this.addressBookController), // AppStateController diff --git a/ui/app/pages/send/send-content/add-recipient/ens-input.component.js b/ui/app/pages/send/send-content/add-recipient/ens-input.component.js index 498d72605..483d5d344 100644 --- a/ui/app/pages/send/send-content/add-recipient/ens-input.component.js +++ b/ui/app/pages/send/send-content/add-recipient/ens-input.component.js @@ -30,10 +30,10 @@ export default class EnsInput extends Component { updateEnsResolution: PropTypes.func, scanQrCode: PropTypes.func, updateEnsResolutionError: PropTypes.func, - addressBook: PropTypes.array, onPaste: PropTypes.func, onReset: PropTypes.func, onValidAddressTyped: PropTypes.func, + contact: PropTypes.object, } state = { @@ -181,8 +181,7 @@ export default class EnsInput extends Component { renderSelected () { const { t } = this.context - const { className, selectedAddress, selectedName, addressBook } = this.props - const contact = addressBook.filter(item => item.address === selectedAddress)[0] || {} + const { className, selectedAddress, selectedName, contact = {} } = this.props const name = contact.name || selectedName diff --git a/ui/app/pages/send/send-content/add-recipient/ens-input.container.js b/ui/app/pages/send/send-content/add-recipient/ens-input.container.js index d74f44832..c37980c49 100644 --- a/ui/app/pages/send/send-content/add-recipient/ens-input.container.js +++ b/ui/app/pages/send/send-content/add-recipient/ens-input.container.js @@ -5,16 +5,19 @@ import { getSendToNickname, } from '../../send.selectors' import { - getAddressBook, + getAddressBookEntry, } from '../../../../selectors/selectors' const connect = require('react-redux').connect export default connect( - state => ({ - network: getCurrentNetwork(state), - selectedAddress: getSendTo(state), - selectedName: getSendToNickname(state), - addressBook: getAddressBook(state), - }) + state => { + const selectedAddress = getSendTo(state) + return { + network: getCurrentNetwork(state), + selectedAddress, + selectedName: getSendToNickname(state), + contact: getAddressBookEntry(state, selectedAddress), + } + } )(EnsInput) diff --git a/ui/app/pages/send/send-content/send-content.component.js b/ui/app/pages/send/send-content/send-content.component.js index aff675e7a..55e0e30e2 100644 --- a/ui/app/pages/send/send-content/send-content.component.js +++ b/ui/app/pages/send/send-content/send-content.component.js @@ -18,9 +18,10 @@ export default class SendContent extends Component { scanQrCode: PropTypes.func, showAddToAddressBookModal: PropTypes.func, showHexData: PropTypes.bool, - to: PropTypes.string, ownedAccounts: PropTypes.array, addressBook: PropTypes.array, + contact: PropTypes.object, + isOwnedAccount: PropTypes.bool, } updateGas = (updateData) => this.props.updateGas(updateData) @@ -47,9 +48,7 @@ export default class SendContent extends Component { maybeRenderAddContact () { const { t } = this.context - const { to, addressBook = [], ownedAccounts = [], showAddToAddressBookModal } = this.props - const isOwnedAccount = !!ownedAccounts.find(({ address }) => address.toLowerCase() === to.toLowerCase()) - const contact = addressBook.find(({ address }) => address === to) || {} + const { isOwnedAccount, showAddToAddressBookModal, contact = {} } = this.props if (isOwnedAccount || contact.name) { return diff --git a/ui/app/pages/send/send-content/send-content.container.js b/ui/app/pages/send/send-content/send-content.container.js index a0732fc20..a122aca1a 100644 --- a/ui/app/pages/send/send-content/send-content.container.js +++ b/ui/app/pages/send/send-content/send-content.container.js @@ -5,15 +5,17 @@ import { getSendTo, } from '../send.selectors' import { - getAddressBook, + getAddressBookEntry, } from '../../../selectors/selectors' import actions from '../../../store/actions' function mapStateToProps (state) { + const ownedAccounts = accountsWithSendEtherInfoSelector(state) + const to = getSendTo(state) return { - to: getSendTo(state), - addressBook: getAddressBook(state), - ownedAccounts: accountsWithSendEtherInfoSelector(state), + isOwnedAccount: !!ownedAccounts.find(({ address }) => address.toLowerCase() === to.toLowerCase()), + contact: getAddressBookEntry(state, to), + to, } } diff --git a/ui/app/pages/send/send-content/tests/send-content-component.test.js b/ui/app/pages/send/send-content/tests/send-content-component.test.js index 451d2ea53..479db9c18 100644 --- a/ui/app/pages/send/send-content/tests/send-content-component.test.js +++ b/ui/app/pages/send/send-content/tests/send-content-component.test.js @@ -52,11 +52,10 @@ describe('SendContent Component', function () { assert.equal(PageContainerContentChild.childAt(4).exists(), false) }) - it('should not render the Dialog if addressBook contains "to" address', () => { + it('should not render the Dialog if contact has a name', () => { wrapper.setProps({ showHexData: false, - to: '0x80F061544cC398520615B5d3e7A3BedD70cd4510', - addressBook: [{ address: '0x80F061544cC398520615B5d3e7A3BedD70cd4510', name: 'dinodan' }], + contact: { name: 'testName' }, }) const PageContainerContentChild = wrapper.find(PageContainerContent).children() assert(PageContainerContentChild.childAt(0).is(SendAssetRow), 'row[1] should be SendAssetRow') @@ -65,12 +64,10 @@ describe('SendContent Component', function () { assert.equal(PageContainerContentChild.childAt(3).exists(), false) }) - it('should not render the Dialog if ownedAccounts contains "to" address', () => { + it('should not render the Dialog if it is an ownedAccount', () => { wrapper.setProps({ showHexData: false, - to: '0x80F061544cC398520615B5d3e7A3BedD70cd4510', - addressBook: [], - ownedAccounts: [{ address: '0x80F061544cC398520615B5d3e7A3BedD70cd4510', name: 'dinodan' }], + isOwnedAccount: true, }) const PageContainerContentChild = wrapper.find(PageContainerContent).children() assert(PageContainerContentChild.childAt(0).is(SendAssetRow), 'row[1] should be SendAssetRow') diff --git a/ui/app/selectors/selectors.js b/ui/app/selectors/selectors.js index 0cf382d2c..751532ef5 100644 --- a/ui/app/selectors/selectors.js +++ b/ui/app/selectors/selectors.js @@ -11,6 +11,7 @@ const { } = require('../helpers/utils/conversion-util') import { addressSlicer, + checksumAddress, } from '../helpers/utils/util' const selectors = { @@ -217,7 +218,7 @@ function getAddressBook (state) { function getAddressBookEntry (state, address) { const addressBook = getAddressBook(state) - const entry = addressBook.find(contact => contact.address.toLowerCase() === address.toLowerCase()) + const entry = addressBook.find(contact => contact.address === checksumAddress(address)) return entry } diff --git a/ui/app/store/actions.js b/ui/app/store/actions.js index adb5fe450..c4fea1fd8 100644 --- a/ui/app/store/actions.js +++ b/ui/app/store/actions.js @@ -1956,10 +1956,17 @@ function addToAddressBook (recipient, nickname = '', memo = '') { return (dispatch, getState) => { const chainId = getState().metamask.network - const set = background.setAddressBook(checksumAddress(recipient), nickname, chainId, memo) - if (!set) { - return dispatch(displayWarning('Address book failed to update')) - } + background.setAddressBook(checksumAddress(recipient), nickname, chainId, memo, (err, set) => { + if (err) { + log.error(err) + dispatch(displayWarning('Address book failed to update')) + throw err + } + if (!set) { + return dispatch(displayWarning('Address book failed to update')) + } + }) + } }