Ethers.js: Verifying an address

Created on 9 Mar 2019  Â·  19Comments  Â·  Source: ethers-io/ethers.js

Is there any functionality for verifying an Ethereum address? My application is going to be parsing Ethereum addresses out of text that the users do not control directly, and it would be great to verify the addresses are real before sending ETH to them. Sorry if this is a noob question, perhaps there is a common way of doing this already. Any help would be appreciated, thanks!

Most helpful comment

There is an isAddress in v5. :)

All 19 comments

Something like: https://web3js.readthedocs.io/en/1.0/web3-utils.html#isaddress

But I'm not sure if that functionality actually ensure there is a private key associated with the address...is there any way to tell that a public key has truly been generated by someone?

I think you want ethers.utils.getAddress(address), which will also verify any checksum, and add it if not present. :)

There is no way to determine if an address has a public key. In fact, many addresses do not, such as contract addresses, and there is no way to tell the difference, which is an intentional choice; you shouldn’t be making decisions base on EOA vs contracts. :)

Awesome, thanks!

There seems to be a second parameter to getAddress not in the docs, a boolean. What is that for? And what happens if the address is not verifiable?

It looks like an error gets thrown if the addres is not verifiable. And the boolean parameter isn't in the source code it seems, but the examples here have it: https://docs.ethers.io/ethers.js/html/api-utils.html#addresses

@lastmjs In v3, there was a second boolean parameter to indicate IBAN checksum instead of mixed-case checksum, but in v4, those two bits of functionality were split out, so there should only be one parameter passed in. I will update that documentation.

You are correct! It will throw on invalid addresses, so you would need to write a simple wrapper if you only care about valid/invalid. Something like this:

function isAddress(address) {
    try {
        getAddress(address);
    } catch (e) { return false; }
    return true;
}

Awesome. I just wanted to say, great work on this library, it has been a joy so far. I was having quite a few issues with web3.js, so just a few hours ago I decided to make the switch and I've completely replaced everything with this library, and it's all working great (less ES Modules, but we'll get there). So thanks!

Thanks! ES modules are certainly something I want too... If you figure something out, let me know. :)

I think the point here was verifying external inputs and the like, to determine if an incoming function argument is an address or something else altogether. If getAddress already checks that, why not expose that bit as isAddress? It might as well check stuff like that the incoming thing is a string etc.

There is an isAddress in v5. :)

One more idea would be ethers.utils.addressEquals(a1, a2) that checks if addresses are equal, ignoring 0x and case. Obv I can simply write this myself, but...

I guess ethers.utils.getAddress(a1) === ethers.utils.getAddress(a2) would work -___-

Yeah, that one seems a bit more special case. Also, keep in mind that in ethers an address MUST start with 0x, bare addresses have resulted in a lot of lost funds. Ethers fails loud and fails early if things don’t quite seem right. :)

What's a "bare address", hex string that doesn't start with "0x"? Why does it cause lost funds, provided it's the correct length (that would be 40)?

Yes, bare as in without the 0x prefix.

There were early bugs in Geth and various libraries, that would automatically return the zero address for incorrect addresses. So, for example, cutting and pasting, which from some UIs, would include a space, became a string like “ 1234...”, notice the space at the beginning? That would just become the address “0x000000000...” internally.

Sometimes people would copy a big number in decimal, which is technical a valid hex string sans the 0x. There are lots of sources of things that look “address-like”.

Here is another read: https://www.reddit.com/r/ethereum/comments/6ettq5/statement_on_quadrigacx_ether_contract_error/?utm_source=share&utm_medium=ios_app

So basically, I made the decision early on that an address had to be absolutely correct, including a 0x prefix, otherwise the library will fail and fail loud. Getting an address right is important, so no “guessing”. I’m still happy with the decision, even if occasionally I need a few extra lines of code. :)

That said, ENS is the way most things should be moving these days, which is why ethers supports them as a first-class citizen. :)

I'm using [email protected] and the isAddress method doesn't exist anymore. Has it been intentionally, or accidentally, removed?

Yeah it's weird, I tried to reproduce this on CodeSandbox and the function works fine there. Perhaps I had a packaging misconfig and was using v4 by mistake. Sorry for the false warning.

ethers.utils.getAddress('0x3Bc8E82B5856B2f2BdC7F6693F79dB9648C0aaAa');

It will throw an error if address is invalid.

Was this page helpful?
0 / 5 - 0 ratings