Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.
Open
Changes from all commits
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
11 changes: 8 additions & 3 deletions src/components/redemption/Start.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ class Start extends Component {

handleDepositAddressChange = (evt) => {
const address = evt.target.value
const addressHexed = web3.utils.toHex(address)

const isValid = web3.utils.isAddress(address)
const hasError = ! isValid
const isValidAddr = web3.utils.isAddress(address)
const isValidAddrDecimal = web3.utils.isAddress(addressHexed)

const isValid = isValidAddr || isValidAddrDecimal
const addr = isValidAddrDecimal ? addressHexed : address
const hasError = ! isValid && ! isValidAddrDecimal
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Feels like we could rework this somewhat like:

let address = evt.target.value
let isValid = web3.utils.isAddress(address)

try {
  // Handle non-hex values that are BN-convertible.
  address = '0x' + web3.utils.toBN(evt.target.value).toString(16)
  isValid = web3.utils.isAddress(address)
} catch (_) {}

this.setState({
  depositAddress: address,
  depositAddressIsValid: isValid,
  depositAddressHasError: ! isValid,
})

This feels a little safer; for a (very contrived) example that would pass the PR's current test, the user could enter The dog is a wee fox (or any 20-character string) and toHex would happily convert it to hex and isAddress mark it as valid.


this.setState({
depositAddress: address,
depositAddress: addr,
depositAddressIsValid: isValid,
depositAddressHasError: hasError
})
Expand Down