Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocked people from sending to addresses listed in CityOfZion/phishing #887

Merged
merged 2 commits into from
Mar 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions app/components/Modals/SendModal/SendModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import AddRecipientDisplay from './AddRecipientDisplay'
import ConfirmDisplay from './ConfirmDisplay'
import withAddressCheck from './withAddressCheck'

import { validateTransactionBeforeSending, getTokenBalancesMap } from '../../../core/wallet'
import { validateTransactionBeforeSending, getTokenBalancesMap, isInBlacklist } from '../../../core/wallet'
import { ASSETS } from '../../../core/constants'
import { toBigNumber } from '../../../core/math'

Expand Down Expand Up @@ -110,19 +110,25 @@ export default class SendModal extends Component<Props, State> {
handleConfirmAddRecipient = (entry: SendEntryType) => {
const { showErrorNotification } = this.props
const { balances } = this.state
const error = validateTransactionBeforeSending(balances[entry.symbol], entry)

if (error) {
showErrorNotification({ message: error })
} else {
const newBalance = toBigNumber(balances[entry.symbol]).minus(entry.amount).toString()

this.setState({
entries: [...this.state.entries, entry],
balances: { ...balances, [entry.symbol]: newBalance },
display: DISPLAY_MODES.CONFIRM
})
}
isInBlacklist(entry.address).then(inBlacklist => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI using async/await syntax here can remove a layer of nesting and improve the readability. We've been favoring this syntax over raw promises throughout the codebase.

You would first need to change the method signature to include async, i.e.:

handleConfirmAddRecipient = async (entry: SendEntryType) => { ... }

Then the skeleton of this function simply becomes:

if (await isInBlacklist(entry.address)) {
  ...
} else {
  ...
}

Copy link
Contributor Author

@alexhulbert alexhulbert Mar 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, making that function async/await removes access to the "this" variable. I get Cannot read property 'showErrorNotification' of undefined or ReferenceError: _this11 is not defined. There's a weird workaround to this in withAddressCheck.jsx, but applying it breaks other methods within the class. There's another promise ".then" in the same file, presumably for the same reason.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, that should be fixable by using async function(...) { ... } instead of async (...) => { ... }. Regardless, I can probably merge this as-is and look into it afterwards.

if (inBlacklist) {
showErrorNotification({ message: 'You have attempted enter a phishing address.' })
} else {
const error = validateTransactionBeforeSending(balances[entry.symbol], entry)
if (error) {
showErrorNotification({ message: error })
} else {
const newBalance = toBigNumber(balances[entry.symbol]).minus(entry.amount).toString()

this.setState({
entries: [...this.state.entries, entry],
balances: { ...balances, [entry.symbol]: newBalance },
display: DISPLAY_MODES.CONFIRM
})
}
}
})
}

handleCancelAddRecipient = () => {
Expand Down
13 changes: 13 additions & 0 deletions app/core/wallet.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
// @flow
import { wallet } from 'neon-js'
import { map, extend } from 'lodash'
import fetch from 'node-fetch'

import { ASSETS } from './constants'
import { toBigNumber } from './math'

const MIN_PASSPHRASE_LEN = 4

let addressBlacklist: Array<string> | null = null
export const isInBlacklist = (address: string): Promise<boolean> => {
if (addressBlacklist !== null) return Promise.resolve(addressBlacklist.includes(address))
return fetch('https://raw.githubusercontent.com/CityOfZion/phishing/master/blockedAddresses.json')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could write this with axios and async/await syntax in a pretty direct manner:

import axios from 'axios'

export const isInBlacklist = async (address: string): Promise<boolean> => {
  if (addressBlacklist === null) {
    const { data } = await axios.get('https://raw.githubusercontent.com/CityOfZion/phishing/master/blockedAddresses.json')
    addressBlacklist = data
  }
  return addressBlacklist.includes(address)
}

.catch(() => false)
.then(res => res.json())
.then(blacklist => {
addressBlacklist = blacklist
return blacklist.includes(address)
})
}

export const validatePassphraseLength = (passphrase: string): boolean =>
passphrase.length >= MIN_PASSPHRASE_LEN

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"isomorphic-fetch": "2.2.1",
"lodash": "4.17.4",
"neon-js": "git+https://github.com/cityofzion/neon-js.git#3.3.3",
"node-fetch": "^2.1.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have axios in this repo -- we should try to avoid bringing in extra packages to prevent bloat.

"qrcode": "0.9.0",
"raf": "3.4.0",
"react": "16.1.1",
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6598,6 +6598,10 @@ node-fetch@^1.0.1:
encoding "^0.1.11"
is-stream "^1.0.1"

node-fetch@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.1.1.tgz#369ca70b82f50c86496104a6c776d274f4e4a2d4"

[email protected]:
version "0.7.1"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.1.tgz#9da611ea08982f4b94206b3beb4cc9665f20c300"
Expand Down