Add warning to watchAsset API when editing a known token (#8049)

* Add warning when editing a known token with watchAsset API

* Add warning when watchAsset attempts to reuse token symbol

* Linted
feature/default_network_editable
Dan Finlay 5 years ago committed by GitHub
parent cfcccd266a
commit eb57763056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/_locales/en/messages.json
  2. 47
      ui/app/pages/confirm-add-suggested-token/confirm-add-suggested-token.component.js
  3. 3
      ui/app/pages/confirm-add-suggested-token/confirm-add-suggested-token.container.js

@ -743,6 +743,12 @@
"knownAddressRecipient": { "knownAddressRecipient": {
"message": "Known contract address." "message": "Known contract address."
}, },
"knownTokenWarning": {
"message": "This action will edit tokens that are already listed in your wallet, which can be used to phish you. Only approve if you are certain that you mean to change what these tokens represent."
},
"reusedTokenNameWarning": {
"message": "A token here reuses a symbol from another token you watch, this can be confusing or deceptive."
},
"invalidAddressRecipientNotEthNetwork": { "invalidAddressRecipientNotEthNetwork": {
"message": "Not ETH network, set to lowercase" "message": "Not ETH network, set to lowercase"
}, },

@ -15,6 +15,7 @@ export default class ConfirmAddSuggestedToken extends Component {
addToken: PropTypes.func, addToken: PropTypes.func,
pendingTokens: PropTypes.object, pendingTokens: PropTypes.object,
removeSuggestedTokens: PropTypes.func, removeSuggestedTokens: PropTypes.func,
tokens: PropTypes.array,
} }
componentDidMount () { componentDidMount () {
@ -32,9 +33,11 @@ export default class ConfirmAddSuggestedToken extends Component {
} }
render () { render () {
const { addToken, pendingTokens, removeSuggestedTokens, history } = this.props const { addToken, pendingTokens, tokens, removeSuggestedTokens, history } = this.props
const pendingTokenKey = Object.keys(pendingTokens)[0] const pendingTokenKey = Object.keys(pendingTokens)[0]
const pendingToken = pendingTokens[pendingTokenKey] const pendingToken = pendingTokens[pendingTokenKey]
const hasTokenDuplicates = this.checkTokenDuplicates(pendingTokens, tokens)
const reusesName = this.checkNameReuse(pendingTokens, tokens)
return ( return (
<div className="page-container"> <div className="page-container">
@ -45,6 +48,20 @@ export default class ConfirmAddSuggestedToken extends Component {
<div className="page-container__subtitle"> <div className="page-container__subtitle">
{ this.context.t('likeToAddTokens') } { this.context.t('likeToAddTokens') }
</div> </div>
{ hasTokenDuplicates ?
(
<div className="warning">
{ this.context.t('knownTokenWarning') }
</div>
) : null
}
{ reusesName ?
(
<div className="warning">
{ this.context.t('reusedTokenNameWarning') }
</div>
) : null
}
</div> </div>
<div className="page-container__content"> <div className="page-container__content">
<div className="confirm-add-token"> <div className="confirm-add-token">
@ -118,4 +135,32 @@ export default class ConfirmAddSuggestedToken extends Component {
</div> </div>
) )
} }
checkTokenDuplicates (pendingTokens, tokens) {
const pending = Object.keys(pendingTokens)
const existing = tokens.map(token => token.address)
const dupes = pending.filter((proposed) => {
return existing.includes(proposed)
})
return dupes.length > 0
}
/**
* Returns true if any pendingTokens both:
* - Share a symbol with an existing `tokens` member.
* - Does not share an address with that same `tokens` member.
* This should be flagged as possibly deceptive or confusing.
*/
checkNameReuse (pendingTokens, tokens) {
const duplicates = Object.keys(pendingTokens)
.map((addr) => pendingTokens[addr])
.filter((token) => {
const dupes = tokens.filter(old => old.symbol === token.symbol)
.filter(old => old.address !== token.address)
return dupes.length > 0
})
return duplicates.length > 0
}
} }

@ -5,11 +5,12 @@ import { withRouter } from 'react-router-dom'
import { addToken, removeSuggestedTokens } from '../../store/actions' import { addToken, removeSuggestedTokens } from '../../store/actions'
const mapStateToProps = ({ metamask }) => { const mapStateToProps = ({ metamask }) => {
const { pendingTokens, suggestedTokens } = metamask const { pendingTokens, suggestedTokens, tokens } = metamask
const params = { ...pendingTokens, ...suggestedTokens } const params = { ...pendingTokens, ...suggestedTokens }
return { return {
pendingTokens: params, pendingTokens: params,
tokens,
} }
} }

Loading…
Cancel
Save