<p>As outlined in the pseudocode in amaclin's answer, a block explorer website would simply check to see if they can find the hash/txid/address in their database, then they will know what type of input it is.</p>

<p>You can't directly perform this check via one bitcoin core RPC call, you would have to write a script of some sort to perform the 3 checks to figure out which type of input you were given. You can first check for a valid block hash using the <code>getblock</code> RPC, and if that failed you know its not a block hash. Next you would have to have <code>txindex</code> enabled on your node to allow yourself to query txids using the <code>gettransaction</code> / <code>getrawtransaction</code> RPCs (because by default the node only indexs transactions which are related to your wallet). And then to check if its a valid address, you can use the <code>validateaddress</code> RPC call. If all of those failed, you know the input is invalid.</p>

CC BY-SA 3.0
2017-09-19T09:02:44.317Z
2017-09-21T03:31:23.080Z
2017-09-21T03:31:23.080Z
meshcollider
<p>There is no way to determine from a hash alone if it represents an address, transaction ID or block ID. All three could be any valid SHA-256 number, and vice versa.</p>

<p>In order to determine Blockchain explorers sites likely do something like the following pseudocode, where <code>blockDB</code>, <code>transactionDB</code>, and <code>addressDB</code> represent all known block IDs, transaction IDs, and addresses respectively.</p>

<pre><code>if ( blockDB.contains ( input ) )
{
  // this is block hash
}
else if ( transactionDB.contains ( input ) )
{
  // this is transaction id
}
else if ( addressDB.contains ( input ) )
{
  // this is address
}
else
{
  // error! unknown input
}
</code></pre>

CC BY-SA 3.0
2017-09-05T18:10:26.287Z
Thanks, this doesn't look like anything I'm yet familiar with, are these commands I can execute or query via RPC?
BitcoinBLues
CC BY-SA 3.0
2017-09-05T21:43:00.270Z
It's pseudo-code. What he means by `if ( blockDB.contains ( input ) )` is to test if the database of valid blocks contains the input as the hash of one of the blocks in the database.
David Schwartz
CC BY-SA 3.0
2017-09-06T09:03:25.213Z
@DavidSchwartz Thanks, can you please provide a link to how to perform this sort of query via RPC?
BitcoinBLues
CC BY-SA 4.0
2017-09-05T18:03:52.810Z
2019-02-08T06:42:35.717Z
2019-02-08T06:42:35.717Z
amaclin
<p>The differentiation between blockhash, txid and address can be done on the basis of their length, both txid and blockhash are 64 characters long, so if string has 2 zero's as first two characters, it is bockhash and not txid. Here's the sample javascript code:</p>

<pre><code> if (user_input.length == 64) {

 for (var i = 0; i &lt; 2; i++) {                       

       if (user_input[i] == '0') {
            block_flag = true;
        }
        else {
             block_flag = false;
             break;
        }
  }
  if (block_flag == true) {
      // it's a blockhash
  }
  else{
     //it's a transaction
  }
  }
  else if (user_input.length == 38 || user_input.length === 34) {
     //it's an address
  }
</code></pre>

CC BY-SA 3.0
2017-09-19T12:42:13.410Z
This is wrong, many transactions begin with two 0's and are not block hashes. Two 0's in hex is one null byte, which happens roughly once every 256 transactions - pretty common. For example, here's one from the most recent block which begins with three 0's: 000c5e131451fb96d04eb0b971b7a2334f057a4d1f43a5bee652caf34e8759f0. The only way to reliably differentiate between block hash and transaction hash is to search for the hash in the blockchain.
meshcollider
CC BY-SA 3.0
2017-09-19T13:41:29.780Z
blockhash ofcourse contains more than two zeros, two zero's is minimum number of zereos, and there is not a single transaction which starts with two zeros, there can't be, show me if there is
troy_achilies
CC BY-SA 3.0
2017-09-19T21:32:04.507Z
I literally just gave you an example above. Why do you think it's not possible?
meshcollider
CC BY-SA 3.0
2017-09-20T09:28:46.850Z
i searched the blockchain.info for that transaction, no results were found
troy_achilies
CC BY-SA 3.0
2017-09-20T12:48:29.017Z
Here: https://blockchain.info/tx/000c5e131451fb96d04eb0b971b7a2334f057a4d1f43a5bee652caf34e8759f0
meshcollider
CC BY-SA 3.0
2017-09-19T12:20:36.040Z
2017-09-19T12:27:40.230Z
2017-09-19T12:27:40.230Z
troy_achilies
<p>how do sites like <a href="https://blockexplorer.com" rel="nofollow noreferrer">https://blockexplorer.com</a> determine if the input (see inputbox in the header of <a href="https://blockexplorer.com" rel="nofollow noreferrer">https://blockexplorer.com</a> ) is an address, transaction ID or block ID?</p>

<p>I want to write something similar myself on my local machine, where I run a full node and have access to bitcoind RPC. I need to know how to direct the input given one box can be 3 things.</p>

<transactions><blockchain><bitcoind><address><block>
Determining if input is a block ID, transaction ID, or address
BitcoinBLues
# Determining if input is a block ID, transaction ID, or address

(Asked by: username_0 on 2017-09-05)

how do sites like <https://blockexplorer.com> determine if the input (see inputbox in the header of <https://blockexplorer.com> ) is an address, transaction ID or block ID?

I want to write something similar myself on my local machine, where I run a full node and have access to bitcoind RPC. I need to know how to direct the input given one box can be 3 things.

**Tags:** transactions, blockchain, bitcoind, address, block


---

## 3 answers

### Answer by username_1 on 2017-09-19. Score: 5

As outlined in the pseudocode in username_2's answer, a block explorer website would simply check to see if they can find the hash/txid/address in their database, then they will know what type of input it is.

You can't directly perform this check via one bitcoin core RPC call, you would have to write a script of some sort to perform the 3 checks to figure out which type of input you were given. You can first check for a valid block hash using the `getblock` RPC, and if that failed you know its not a block hash. Next you would have to have `txindex` enabled on your node to allow yourself to query txids using the `gettransaction` / `getrawtransaction` RPCs (because by default the node only indexs transactions which are related to your wallet). And then to check if its a valid address, you can use the `validateaddress` RPC call. If all of those failed, you know the input is invalid.


---

### Answer by username_2 on 2017-09-05. Score: 2

There is no way to determine from a hash alone if it represents an address, transaction ID or block ID. All three could be any valid SHA-256 number, and vice versa.

In order to determine Blockchain explorers sites likely do something like the following pseudocode, where `blockDB`, `transactionDB`, and `addressDB` represent all known block IDs, transaction IDs, and addresses respectively.

```
if ( blockDB.contains ( input ) )
{
  // this is block hash
}
else if ( transactionDB.contains ( input ) )
{
  // this is transaction id
}
else if ( addressDB.contains ( input ) )
{
  // this is address
}
else
{
  // error! unknown input
}

```

> **Comments:**
> **username_0**: Thanks, this doesn't look like anything I'm yet familiar with, are these commands I can execute or query via RPC?  
> **username_3**: It's pseudo-code. What he means by `if ( blockDB.contains ( input ) )` is to test if the database of valid blocks contains the input as the hash of one of the blocks in the database.  
> **username_0**: @username_3 Thanks, can you please provide a link to how to perform this sort of query via RPC?  

---

### Answer by username_4 on 2017-09-19. Score: -2

The differentiation between blockhash, txid and address can be done on the basis of their length, both txid and blockhash are 64 characters long, so if string has 2 zero's as first two characters, it is bockhash and not txid. Here's the sample javascript code:

```
 if (user_input.length == 64) {

 for (var i = 0; i < 2; i++) {                       

       if (user_input[i] == '0') {
            block_flag = true;
        }
        else {
             block_flag = false;
             break;
        }
  }
  if (block_flag == true) {
      // it's a blockhash
  }
  else{
     //it's a transaction
  }
  }
  else if (user_input.length == 38 || user_input.length === 34) {
     //it's an address
  }

```

> **Comments:**
> **username_1**: This is wrong, many transactions begin with two 0's and are not block hashes. Two 0's in hex is one null byte, which happens roughly once every 256 transactions - pretty common. For example, here's one from the most recent block which begins with three 0's: 000c5e131451fb96d04eb0b971b7a2334f057a4d1f43a5bee652caf34e8759f0. The only way to reliably differentiate between block hash and transaction hash is to search for the hash in the blockchain.  
> **username_4**: blockhash ofcourse contains more than two zeros, two zero's is minimum number of zereos, and there is not a single transaction which starts with two zeros, there can't be, show me if there is  
> **username_1**: I literally just gave you an example above. Why do you think it's not possible?  
> **username_4**: i searched the blockchain.info for that transaction, no results were found  
> **username_1**: Here: https://blockchain.info/tx/000c5e131451fb96d04eb0b971b7a2334f057a4d1f43a5bee652caf34e8759f0  

---

