Executando verificação de segurança...
Respondendo a ":: Área de rascunho :: Esta é uma postagem sob..." dentro da publicação [Não disponível]
0
:: RASCUNHO PARA DESENVOLVIMENTO :: favor, não pontuar, pois assim que publicado o conteúdo será removido

Rascunho - O que são Merkle trees?

merkle_blk75000.py

Detalhes
# Uses      :  python 2
# Dependency:  hashlib
import hashlib

# Hash pairs of items recursively until a single value is obtained
def merkle(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(hash2(hashList[i], hashList[i+1]))
    if len(hashList) % 2 == 1: # odd, hash last item twice
        newHashList.append(hash2(hashList[-1], hashList[-1]))
    return merkle(newHashList)

def hash2(a, b):
    # Reverse inputs before and after hashing
    # due to big-endian / little-endian nonsense
    a1 = a.decode('hex')[::-1]
    b1 = b.decode('hex')[::-1]
    h = hashlib.sha256(hashlib.sha256(a1+b1).digest()).digest()
    return h[::-1].encode('hex')

# https://blockexplorer.com/rawblock/0000000000000000e067a478024addfecdc93628978aa52d91fabd4292982a50  (outdated)
# wget -qO- https://mempool.space/api/block/00000000000ace2adaabf1baf9dc0ec54434db11e9fd63c1819d8d77df40afda/txids
# expected Merkle Root
# wget -qO- https://mempool.space/api/block/75000
# ed385c2dbc69aa24965909c7d9d11bbd99faa085cb4ec17865d9b557ffb3a68a
#
txHashes = ["5277cf3790381c2cc2b071038d8c35b3b601207c92f8aec15978a5f01ecf8319",
"182c2ed191a35ea496ce84c42d8beee6f9d82b9f063de2e45a54692bb043696a",
"707e86e5e2356cb53a2edf0be391d56cfc998bcfa05a13a5772ef474c5eba105",
"711e15a9a819de4d1269d71de9744dedf9b6c32bba36bb0196f003f6507d4bb4",
"8c1e409484e30c205698647753cca07d826c34756773bd0432202487f28e2d54",
"abfaf8e7ad6241ca5161e517baade1275cf6333d0d118d221f894813bacb4f78"]

print merkle(txHashes)

Fontes

Carregando publicação patrocinada...