# Find specific transaction in merkel's tree

We know the merkel tree is branchs of transaction hash so that root of tree is hash that show in block header. If i want to find a specific transaction, how i can?

• What do you mean by "special transaction"? Can you be more specific?
– Ava Chow
Commented Nov 20, 2018 at 15:04
• Transactions that for example send to Alice address . Commented Nov 20, 2018 at 15:07
• Are you asking how to find a specific transaction? Commented Nov 20, 2018 at 15:11
• Yes. How To check if a transaction is included in the Merkle tree ? Commented Nov 20, 2018 at 15:17

In order to verify if a transaction is part of a merkle tree, you have to calculate the hash of the transaction id and it's sibling, and verify that the value is correct. For example,

## Merkle Verification

We’ll start with a simple 2 leaf example (see the First Bitcoin Transaction):

Lets assume that hashes `Ha` and `Hb` have already been calculated from the data `Ta` and `Tb` and that:

``````Ha = b1fea52486ce0c62bb442b530a3f0132b826c74e473d1f2c220bfa78111c5082
``````

First, we must reverse the bytes (they need to be little endian) because that is how Bitcoin calculates the hashes. You can use this python script, if you like:

``````#!/usr/bin/env python
line = raw_input("Input original hex string\n")
n = 2
orig_list = [line[i:i+n] for i in range(0, len(line), n)]
reversed_list = orig_list[::-1]
reversed = ''.join(reversed_list)
print reversed
``````

We get:

``````Ha = 82501c1178fa0b222c1f3d474ec726b832013f0a532b44bb620cce8624a5feb1
``````

Then, the node (and root in this case) `Hab` is calculated by concatenating the two and hashing with sha256 twice (i.e. hash256):

``````\$ printf "82501c1178fa0b222c1f3d474ec726b832013f0a532b44bb620cce8624a5feb1169e1e83e930853391bc6f35f605c6754cfead57cf8387639d3b4096c54f18f4" | xxd -r -p | openssl sha256
(stdin)= a24ee7fb7333f85c16560ed8850a1773d6977ce7a4936367eaf72f8fff33797e
\$ printf "a24ee7fb7333f85c16560ed8850a1773d6977ce7a4936367eaf72f8fff33797e" | xxd -r -p | openssl sha256
(stdin)= ff104ccb05421ab93e63f8c3ce5c2c2e9dbb37de2764b3a3175c8166562cac7d
``````

Now, reverse the bytes! (back to big endian):

``````7dac2c5666815c17a3b36427de37bb9d2e2c5ccec3f8633eb91a4205cb4c10ff
``````

https://medium.com/coinmonks/how-to-manually-verify-the-merkle-root-of-a-bitcoin-block-command-line-7881397d4db1

• I think he asked about how to find transaction knowing root hash not how to create the merkle tree. Commented Nov 18, 2019 at 17:39

To search for a transaction in a merkle root, one must know the TXID of the transaction being looked for. It's not possible to search for leaves (transaction IDs) that satisfy a complex pattern [without scanning each of the transactions]. After the TXID and the TXIDs of other transactions in that block are acquired, the tree is traversed by hashing (For details, please see Why is the full Merkle path needed to verify a transaction?) The transaction is proved to be a part of the block if the final hash is equal to the merkle root.

A common misconception is that a Merkle tree is a data structure which makes finding elements in it easier. It does not. While it does have a tree structure, it is not a search tree like a B-tree or Red-Black or Patricia tree.

As far as searching for data goes, it is conceptually just a list (the leaves of the tree) with a number of associated hash values. Searching in it is exactly as hard as searching in a list is. If the list is sorted, that can be done in O(log n) time, but if the list is not sorted, you need to do an O(n) exhaustive search.

So what is a Merkle tree then useful for?

Proofs. A Merkle tree lets one party prove that a particular element is in it, to anyone who knows the tree's root hash.

So in practice the answer to "How do I find something in a Merkle tree?" is "You don't. You let someone else search for you, and have them show you a proof of what they found.".

If you want to keep a proof that your transaction was included in a particular block, you need to create and save the proof yourself. To create the proof you need to get a copy of the block and using the transaction ids in the block you compute the path from your transaction to the merkel root. The path would be an array of hash outputs along with the side one which to hash it. If a block had 1000 transaction the path to the merkel root would only have about 10 hash outputs.

The user needs to save their own proof that the transaction was included in a block. You cannot query the network for that. You can query to see if the merkel root is correct.