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

magically hex TDT ID if given in decimal #328

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

brandoncurtis
Copy link
Contributor

TDT IDs are given in decimal in many block explorers: https://ropsten.etherscan.io/token/0x20ea5b307ee28eefc15a3e6d945c5e8da5a8add9

If a decimal TDT ID is provided, automatically convert from decimal to a hex address.

@mhluongo
Copy link
Member

mhluongo commented Aug 1, 2020

Ooh good idea, thanks @brandoncurtis!


const isValid = isValidAddr || isValidAddrDecimal
const addr = isValidAddrDecimal ? addressHexed : address
const hasError = ! isValid && ! isValidAddrDecimal
Copy link
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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants