Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Add warning to watchAsset API when editing a known token (MetaMask#8049)
Browse files Browse the repository at this point in the history
* Add warning when editing a known token with watchAsset API

* Add warning when watchAsset attempts to reuse token symbol

* Linted
  • Loading branch information
danfinlay authored and yqrashawn committed Feb 17, 2020
1 parent 8991a2c commit c630659
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
6 changes: 6 additions & 0 deletions app/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,12 @@
"knownAddressRecipient": {
"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": {
"message": "Not CFX network, set to lowercase"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class ConfirmAddSuggestedToken extends Component {
addToken: PropTypes.func,
pendingTokens: PropTypes.object,
removeSuggestedTokens: PropTypes.func,
tokens: PropTypes.array,
}

componentDidMount () {
Expand All @@ -30,14 +31,11 @@ export default class ConfirmAddSuggestedToken extends Component {
}

render () {
const {
addToken,
pendingTokens,
removeSuggestedTokens,
history,
} = this.props
const { addToken, pendingTokens, tokens, removeSuggestedTokens, history } = this.props
const pendingTokenKey = Object.keys(pendingTokens)[0]
const pendingToken = pendingTokens[pendingTokenKey]
const hasTokenDuplicates = this.checkTokenDuplicates(pendingTokens, tokens)
const reusesName = this.checkNameReuse(pendingTokens, tokens)

return (
<div className="page-container">
Expand All @@ -48,6 +46,20 @@ export default class ConfirmAddSuggestedToken extends Component {
<div className="page-container__subtitle">
{this.context.t('likeToAddTokens')}
</div>
{ hasTokenDuplicates ?
(
<div className="warning">
{ this.context.t('knownTokenWarning') }
</div>
) : null
}
{ reusesName ?
(
<div className="warning">
{ this.context.t('reusedTokenNameWarning') }
</div>
) : null
}
</div>
<div className="page-container__content">
<div className="confirm-add-token">
Expand Down Expand Up @@ -117,4 +129,32 @@ export default class ConfirmAddSuggestedToken extends Component {
</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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { withRouter } from 'react-router-dom'
import { addToken, removeSuggestedTokens } from '../../store/actions'

const mapStateToProps = ({ metamask }) => {
const { pendingTokens, suggestedTokens } = metamask
const { pendingTokens, suggestedTokens, tokens } = metamask
const params = { ...pendingTokens, ...suggestedTokens }

return {
pendingTokens: params,
tokens,
}
}

Expand Down

0 comments on commit c630659

Please sign in to comment.