Go-ethereum: how to check an eth address is valid ?

Created on 26 Oct 2016  路  11Comments  路  Source: ethereum/go-ethereum

i haven't found the api to check an eth-address is valid ~

have the func like bitcoin-core of "bool ValidateAddress(address)" ?

Most helpful comment

Use this ^0x[a-fA-F0-9]{40}$

please use the CRC to check the eth address: https://gist.github.com/adyliu/6c5ff4d41aa0177da55f4b8b1703f54a

thx @adyliu

All 11 comments

Ethereum addresses are plain 20 bytes, anything can be in them. Since it's hard to reason about them, programs represent them as 40 character long hex strings. So every 0x<40 hex character> is a valid Ethereum address.

In the Geth 1.5 codebase if you create a common.Address form a hex string I'd imagine it would validate it's length.

ok ~ i see

hello karalabe ~

i sent some eth to an address of "0x4961aC1d43B6249bc998D611F33d42B54E31712E"

why it return me the error msg :

error":{"code":-32602,"message":"Invalid address length, expected 40 got 41 bytes"}}

i can check this address on chain:
https://etherchain.org/account/0x4961aC1d43B6249bc998D611F33d42B54E31712E%20

The link you sent contains a space at the end (encoded %20). Perhaps you also inserted a space at the end of the address?

oh ~~ i know ~~thanks again .

i would have a try ~!

Use this ^0x[a-fA-F0-9]{40}$

please use the CRC to check the eth address: https://gist.github.com/adyliu/6c5ff4d41aa0177da55f4b8b1703f54a

thx @adyliu

@xuzhiping7 you can validate an CRC for your address
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md

hey you guys, do you have golang eip55 implement to check address? in the eip55 readme page, only list python js swift implement, no golang implement been found.

Then I search in go ethereum codebase only find below code which is to generate an eip55 compliant hex address but doesn't provide the method to verify the address.

// Hex returns an EIP55-compliant hex string representation of the address.
func (a Address) Hex() string {
    unchecksummed := hex.EncodeToString(a[:])
    sha := sha3.NewKeccak256()
    sha.Write([]byte(unchecksummed))
    hash := sha.Sum(nil)

    result := []byte(unchecksummed)
    for i := 0; i < len(result); i++ {
        hashByte := hash[i/2]
        if i%2 == 0 {
            hashByte = hashByte >> 4
        } else {
            hashByte &= 0xf
        }
        if result[i] > '9' && hashByte > 7 {
            result[i] -= 32
        }
    }
    return "0x" + string(result)
}

there's another code snippet in types.go which I think is as same as ^0x[a-fA-F0-9]{40}$

// IsHexAddress verifies whether a string can represent a valid hex-encoded
// Ethereum address or not.
func IsHexAddress(s string) bool {
    if hasHexPrefix(s) {
        s = s[2:]
    }
    return len(s) == 2*AddressLength && isHex(s)
}

@qyvlik @adyliu

Was this page helpful?
0 / 5 - 0 ratings

Related issues

freshonline picture freshonline  路  3Comments

phpsamsb picture phpsamsb  路  3Comments

vogelito picture vogelito  路  3Comments

aakilfernandes picture aakilfernandes  路  3Comments

RoodyChan picture RoodyChan  路  3Comments