5
votes

How to detect empty address that has initial value of 0x0000000000000000000000000000000000000000 in web3.js?

What I'm doing now is:

if (address !== '0x0000000000000000000000000000000000000000') {
   ...
}

Is there any simpler way to filter out empty addresses or a helper method in web3 that can create this value(like address(0) in Solidity)? It's quite bothering to count(or type) exact number of all that 0s.

2
Maybe web3.toBigNumber(address).isZero()?user94559
@smarx Really nice to know that web3 depends on BigNumber library. Why couldn't I think this way.. brilliant.viz
You could post it as an answer and I can mark it accepted. Seems like your suggestion is the best I can think of so far.viz

2 Answers

2
votes

Aside from @smarx's way: web3.toBigNumber(address).isZero()

You can use a simple regex, which is shorter:

const emptyAddress = /^0x0+$/.test(address);
0
votes

You could also use OpenZeppelin's Test Helpers.

They have a constant called ZERO_ADDRESS which equals to zero address you mentioned.

More Info Here

Usage

First, you need to install:

npm install @openzeppelin/test-helpers

In JS file:

const constants = require('@openzeppelin/test-helpers');

console.log(constants.ZERO_ADDRESS);