Created
March 2, 2022 23:23
-
-
Save notcod/7bed6e67063bb1f2f46b09fcd6abb89b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import hashlib | |
| import binascii | |
| def hashIt(firstTxHash, secondTxHash): | |
| # Reverse inputs before and after hashing | |
| # due to big-endian | |
| unhex_reverse_first = binascii.unhexlify(firstTxHash)[::-1] | |
| unhex_reverse_second = binascii.unhexlify(secondTxHash)[::-1] | |
| concat_inputs = unhex_reverse_first+unhex_reverse_second | |
| first_hash_inputs = hashlib.sha256(concat_inputs).digest() | |
| final_hash_inputs = hashlib.sha256(first_hash_inputs).digest() | |
| # reverse final hash and hex result | |
| return binascii.hexlify(final_hash_inputs[::-1]) | |
| # Hash pairs of items recursively until a single value is obtained | |
| def merkleCalculator(hashList): | |
| if len(hashList) == 1: | |
| return hashList[0] | |
| newHashList = [] | |
| # Process pairs. For odd length, the last is skipped | |
| for i in range(0, len(hashList)-1, 2): | |
| newHashList.append(hashIt(hashList[i], hashList[i+1])) | |
| if len(hashList) % 2 == 1: # odd, hash last item twice | |
| newHashList.append(hashIt(hashList[-1], hashList[-1])) | |
| return merkleCalculator(newHashList) | |
| txHashes = [ | |
| '5baccea82a0c35772f2fe7019627f17fe464fcd3ef0189d70dc15fa7aecf426a', | |
| '9996f5ad442be27bdc8c05ba32c0837185a36626fd8bc1c9cd0a4a2576277ec2', | |
| '563ea83f9641d37a36f9294d172fdb4fb86c19b0e9cac45e0b27610331138775', | |
| '12f1af4a30af57c69d0d4c2db3435dad50f3a3b179d4a70a612c51dab61fac58', | |
| 'b643b753deec27b1d15b674b75bbc9751de96ba5996271d3806f65c38d5bef23', | |
| '971af80218684017722429be08548d1f30a2f1f220abc064380cbca5cabf7623', | |
| 'fe09b2906ad4c63ee8ea3c126f80260b2a87171d13b40047eeb53883c885ac90', | |
| 'b1ec9c44009147f3cee26caba45abec2610c74df9751fad14074119b5314da21', | |
| 'd9a574cca50973c09fb2df1c798e459711bf263e3d0815f7afc6fb2ea006e727', | |
| 'd523483bcf84974e0c9881907996531cf41b7af520740565f5b193319dc4007b', | |
| '22e7721cdb38772016ddc246fcb37c88cb07b710823467f7f3705443ccbbc78c', | |
| '7d8eda75a7c7fa2a4ca550a654d8d25b22a7c3dbc4cd5343e13363394c929dd3' | |
| ] | |
| CalculatedMerkleRoot = str(merkleCalculator(txHashes), 'utf-8') | |
| print(CalculatedMerkleRoot) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment