Skip to content

Commit

Permalink
merkle
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavitra Agarwal authored and Pavitra Agarwal committed Apr 29, 2024
1 parent 588b3d3 commit e94041d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
39 changes: 35 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,52 @@ def initializeTxn(txex , segwitness) :
# print(totalwu)
# print(txids)

import hashlib

def hash2561(data):
"""Compute double SHA-256 hash of the input data (hex string)."""
first_hash = hashlib.sha256(bytes.fromhex(data)).digest()
second_hash = hashlib.sha256(first_hash).digest()
return second_hash.hex()
def merkle_root(txids):
if len(txids) == 0:
return None

# Reverse the transaction IDs and convert to hexadecimal strings
level = [txid[::-1].hex() for txid in txids]

while len(level) > 1:
next_level = []

# Process pairs of hashes
for i in range(0, len(level), 2):
if i + 1 == len(level):
# In case of an odd number of elements, duplicate the last one
pair_hash = hash2561(level[i] + level[i])
else:
pair_hash = hash2561(level[i] + level[i + 1])

next_level.append(pair_hash)

# Update the current level with the next level of hashes
level = next_level

return bytes.fromhex(level[0])

# import hashlib
txides = [h[::-1] for h in txids]
txides = [bytes.fromhex(tx) for tx in txides]

# txides = [bytes.fromhex(tx) for tx in txides]

# print(txides)
# now we can create the blockheader :
block = Block (
version = 0x20000002,
prev_block= bytes.fromhex(base_block),
merkle_root= merkle_root(txides)[::-1],
merkle_root= merkle_root([bytes.fromhex(h) for h in txids]),
timestamp= int(ts),
bits=bytes.fromhex('1f00ffff'),
nonce= bytes.fromhex(nonce()), #nonce()) , #nonce should be of the bytes
tx_hashes = txides
tx_hashes = txids
)

cout = 0
Expand Down
2 changes: 1 addition & 1 deletion output.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
020000200000000000000000000000000000000000000000000000000000000000000000ccd2a044998cfe850f823d68c3f6594347ac7be1f97dd5ba104f225fe6fd79a3caef2f66ffff001ff3c4e53f
020000200000000000000000000000000000000000000000000000000000000000000000aa540c9d3d1c6f8812d00349518135f8fd675cbf348c139ea6a9571985e116e0bbf32f66ffff001f5d72f996
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff6a47304402201f6e9296e322013f90f89433926509a9ff686ce91452679c82c921cf18257c95022027497b1354b4dce14b3e15d2372c69d6323ecfad9cbc0c8bb20d8d8d088fc22001210224c6633127ca04e9b678ae7d106a9828ba2aed9a402eefae69f52fbe7a065699ffffffff02f595814a000000001976a914edf10a7fac6b32e24daa5305c723f3de58db1bc888ac0000000000000000266a24aa21a9ed0743cafb2ad89219798f1b2651467d2f6256998e493800f2560784f5f1bcd4670120000000000000000000000000000000000000000000000000000000000000000000000000
FD0600
49a6af777488f947e2b6e2e0ab87f56f44cbfe8280c4dfd9c7c5d9c1b1291f10
Expand Down

0 comments on commit e94041d

Please sign in to comment.