3

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.

5

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.

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.

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
}
  • Thanks, this doesn't look like anything I'm yet familiar with, are these commands I can execute or query via RPC? – BitcoinBLues Sep 5 '17 at 18:10
  • 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 Sep 5 '17 at 21:43
  • @DavidSchwartz Thanks, can you please provide a link to how to perform this sort of query via RPC? – BitcoinBLues Sep 6 '17 at 9:03
-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
  }
  • 2
    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 Sep 19 '17 at 12:42
  • 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 Sep 19 '17 at 13:41
  • I literally just gave you an example above. Why do you think it's not possible? – MeshCollider Sep 19 '17 at 21:32
  • i searched the blockchain.info for that transaction, no results were found – troy_achilies Sep 20 '17 at 9:28
  • Here: blockchain.info/tx/… – MeshCollider Sep 20 '17 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.