Could you please tell me what exactly I'm doing wrong with getting the Merkle root? Here is my Python code, but I am not getting the result I need.
Target: "merkleroot": "19f3769778eb2019476630aafb376634cdf3f114e617a7e0a300e822265022bc"
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from hashlib import sha256
import requests, json
DEFAULT_RPC_REGTEST_PORT = "18443"
DEFAULT_RPC_TEST_PORT = "18332"
DEFAULT_RPC_MAIN_PORT = "8332"
rpcAddress = "127.0.0.1" # localhost
rpcPort = DEFAULT_RPC_MAIN_PORT
rpcUser = "rpcuser"
rpcPassword = "rpcpassword"
rpcUrl = "http://" + rpcAddress + ":" + rpcPort
headers = {'content-type': 'application/json'}
def double_sha256(data):
return bytes.fromhex(sha256(bytes.fromhex(sha256(bytes.fromhex(data)).hexdigest())).hexdigest())
def request_rpc(method, params):
payload = json.dumps(
{
"jsonrpc": "2.0",
"id": "1",
"method": method,
"params": params
}
)
return requests.request("POST", rpcUrl, data=payload, headers=headers,
auth=(rpcUser, rpcPassword))
if __name__ == "__main__":
res = request_rpc(method="getblock", params=["00000000000000000000bd684e36789d16da2cf5b08b47deb72d1448a73eae5c"])
tx = res.json()["result"]["tx"]
print(tx)
hashes = []
for i in range(len(tx)):
res = request_rpc(method="getrawtransaction",
params=[tx[i], 1, '00000000000000000000bd684e36789d16da2cf5b08b47deb72d1448a73eae5c'])
hashes.append(res.json()["result"]["hash"])
print(len(hashes))
# print(hashes)
hashes = [bytes.fromhex(x) for x in hashes]
while len(hashes) > 1:
if len(hashes) % 2 == 1:
hashes.append(hashes[-1])
parent_level = []
for x in range(0, len(hashes), 2):
hash = sha256(hashes[x] + hashes[x + 1]).digest()
parent_level.append(hash)
hashes = parent_level
print(hashes[0].hex())
I also did a double hash, and also tried to get the root by hashing TXID, but that also didn't help me get the result I wanted.