kevinhealy / split-it

0 stars 0 forks source link

Validate address inputs as ethereum addresses. #8

Open kevinhealy opened 6 years ago

kevinhealy commented 6 years ago

web3.isAddress(input) returns a true or false.

the input is a string value (that could potentially be an ethereum address)

Here is an example in pure javascript without react... It needs to be integrated in the react project when people are submitting addresses.

(This code is live at http://www.kevinmhealy.com/experiments/ethereum-tests/functions-2/ )

function isEthereumAddress() {

    var input = document.getElementById("isEthereumAddressInput").value
    var outputParagraph = document.getElementById("isEthereumAddressOutput")

    outputParagraph.innerHTML = web3.isAddress(input)

}
kevinhealy commented 6 years ago

If it is a valid ethereum address...

Then the next condition to check to validate it is:

Is it a contract or a user account?

Another function live on the page is called getAddressCode():

function getAddressCode() {

    var input = document.getElementById("getAddressCodeInput").value
    var outputParagraph = document.getElementById("getAddressCodeOutput")

    if (web3.isAddress(input)) {

        web3.eth.getCode(input, (err, result) => {
                      outputParagraph.innerHTML = err || JSON.stringify(result, null, 4)
        })

    } else {
        outputParagraph.innerHTML = "Invalid Address"
    }

}

If it returns 0x then it is a user account

If it returns something longer (0x....) than it is a contract account.

If it is a contract account it should be invalidated or if we are feeling generous it should just give them a warning telling them to make sure the contract account is programmed to be able to call withdraw... otherwise there will be no way for it to make withdrawals so the money would get permanently locked. aka lost.

I think for now we should just disallow it.