I'm creating a bitcoin network client and I'm wondering if it's possible to decode the block height from a blockhash whenever I receive a new block inventory vector.
2 Answers
No.
The block hash is a hash of the block header... but even if you could reverse the hash function (which is impossible, or Bitcoin would be broken), you wouldn't know the height, as the height is not stored in the header.
The header does contain a hash of the transaction Merkle root... but even if you could reverse the hash functions in the transaction Merkle tree, you wouldn't know the height, as the height is not stored in the transaction Merkle tree.
The Merkle tree does contain transaction ids... and if you could reverse the hash function used for computing the transaction ids, you would could learn the height, as since BIP34, the block height is stored in the input of the first transaction of a block (the coinbase transaction).
So, if you want to know a block's height for sure, you need to request at least its coinbase transaction, the Merkle path from the coinbase to the root, and the full block header. If they all match, you can be pretty sure.
No. The whole point of a hash is that it's a one way function. You can't "decode" any of the header data from the block hash.
Moreover, the block height isn't even contained in the block header. You're supposed to calculate the block height by looking at the previous block hash in the header, which hopefully refers to a block whose height you already know; if not, get that block and recurse. (For sufficiently recent blocks, the block height is contained in the scripts of the coinbase transaction, but that's not part of the header.)