From 5b1189c6797d60fcfa20dc0c99032f4f9357b09c Mon Sep 17 00:00:00 2001 From: Pavitra Agarwal Date: Mon, 29 Apr 2024 19:04:30 +0530 Subject: [PATCH] merkle root --- __pycache__/block.cpython-312.pyc | Bin 2179 -> 2016 bytes __pycache__/usefulfunctions.cpython-312.pyc | Bin 6710 -> 6710 bytes block.py | 8 ++-- main.py | 39 +++++++------------- output.txt | 2 +- 5 files changed, 18 insertions(+), 31 deletions(-) diff --git a/__pycache__/block.cpython-312.pyc b/__pycache__/block.cpython-312.pyc index 085d7d51362825bb2b718c7e2dadb026532dc697..101b708036f814c0e4c16371edbe2b423fb784fa 100644 GIT binary patch delta 144 zcmZn`e8A6pnwOW00SL}5)KBBx$Xmn2$S}E!sdzFc^FPMM$s8=^%ojP7HhZ&FGcwjq zKF+Gg%8-SeIh=t8G%$Q%o9xQ2rr5#qfM0k*$#rq{i$KJ$ ret|>%=O<1EA>(2ept62`tI5;Y%Nf}x^KoSGsxoSPVgM3F!aykiI=?0} delta 303 zcmaFB-z>;`nwOW00SJ!h=%-EH$Xmn2$TPW%sd%z4v&dvt=5LH$lm9TA^DH!35VXML zqL9W#4vo!bEY*xa@lC9Htc)p)DNK`}vZ`BR3I$mJ delta 20 acmdmHvdx71G%qg~0}y2F)7!|cEd>BPTLmNl diff --git a/block.py b/block.py index f597e3c..87e9aa7 100644 --- a/block.py +++ b/block.py @@ -14,7 +14,7 @@ def __init__(self, version, prev_block, merkle_root, def serialize(self): result = int_to_little_endian(self.version, 4) result += self.prev_block[::-1] - result += self.merkle_root[::-1] + result += self.merkle_root result += int_to_little_endian(self.timestamp, 4) result += self.bits[::-1] result += self.nonce[::-1] @@ -30,12 +30,10 @@ def hash(self): return h256[::-1] def validate_merkle_root(self): - '''Gets the merkle root of the tx_hashes and checks that it's - the same as the merkle root of this block. - ''' # reverse each item in self.tx_hashes + hashes = [h[::-1] for h in self.tx_hashes] # compute the Merkle Root and reverse - root = merkle_root(hashes)[::-1] + root = merkle_root(hashes) # return whether self.merkle_root is the same return root == self.merkle_root \ No newline at end of file diff --git a/main.py b/main.py index 15655be..3d77f22 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ from usefulfunctions import int_to_little_endian , hash256 , sha256 , merkle_root , read_varint , encode_varint from io import BytesIO import os +import hashlib from block import Block import random @@ -18,12 +19,8 @@ def nonce(): hex_string = ''.join(format(byte, '02x') for byte in random_bytes) return hex_string - def wxcommitment(hash,val) : return hash256(hash + val) - - - def scripttype(cmds) : x = bytes.fromhex(cmds) xx = encode_varint(len(x))+x @@ -115,6 +112,12 @@ def initializeTxn(txex , segwitness) : except Exception as e: continue +def hash2561(data): + return hashlib.sha256(hashlib.sha256(bytes.fromhex(data)).digest()).digest().hex() + + + + hashes = [bytes.fromhex(h)[::-1] for h in wxtid] wxc = wxcommitment(merkle_root(hashes),bytes.fromhex(wxtidcons)).hex() witnesscomitmentpubkeyscript = "6a24aa21a9ed"+wxc @@ -148,28 +151,14 @@ def initializeTxn(txex , segwitness) : CoinbaseTxnSerialize = ctx.serialize().hex() -# print(CoinbaseTxnSerialize) CoinbaseTxnId = ctx.id() # this is our coinbase transaction - -# print(totalfees) -# print(totalwu) -# print(txids) - txids.insert(0,CoinbaseTxnId) -# txids = [ -# "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87", -# "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4", -# "6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4", -# "e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d" -# ] -txides = [bytes.fromhex(h)[::-1] for h in txids] -# print(merkle_root(txidess).hex()) -# print(txides) -# now we can create the blockheader : +# calculation of the merkle root +txides = [bytes.fromhex(h)[::-1] for h in txids] block = Block ( version = 0x20000002 , prev_block= bytes.fromhex(base_block), @@ -177,14 +166,13 @@ def initializeTxn(txex , segwitness) : timestamp= int(ts), bits=bytes.fromhex('1f00ffff') , nonce= bytes.fromhex(nonce()) , #nonce()) , #nonce should be of the bytes - tx_hashes = txides + tx_hashes = [bytes.fromhex(h) for h in txids] ) -# for h in txides : -# print(h.hex()) -# print(block.validate_merkle_root()) -# print(block.merkle_root.hex()) +for h in txids : + print(h) +print(block.validate_merkle_root()) @@ -206,6 +194,7 @@ def initializeTxn(txex , segwitness) : # print(Coinbase_txn_serialize) # for tx in txids : # print(tx) + txcount = "FD"+int_to_little_endian(len(txids)+1,2).hex() script_directory = os.path.dirname(os.path.abspath(__file__)) output_file_path = os.path.join(script_directory, "output.txt") diff --git a/output.txt b/output.txt index e51f196..6b7cf56 100644 --- a/output.txt +++ b/output.txt @@ -1,4 +1,4 @@ -020000200000000000000000000000000000000000000000000000000000000000000000aa540c9d3d1c6f8812d00349518135f8fd675cbf348c139ea6a9571985e116e0ca342f66ffff001f115f4f78 +020000200000000000000000000000000000000000000000000000000000000000000000e016e1851957a9a69e138c34bf5c67fdf83581514903d012886f1c3d9d0c54aacfa12f66ffff001f09deb1c9 010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff6a47304402201f6e9296e322013f90f89433926509a9ff686ce91452679c82c921cf18257c95022027497b1354b4dce14b3e15d2372c69d6323ecfad9cbc0c8bb20d8d8d088fc22001210224c6633127ca04e9b678ae7d106a9828ba2aed9a402eefae69f52fbe7a065699ffffffff02f595814a000000001976a914edf10a7fac6b32e24daa5305c723f3de58db1bc888ac0000000000000000266a24aa21a9ed0743cafb2ad89219798f1b2651467d2f6256998e493800f2560784f5f1bcd4670120000000000000000000000000000000000000000000000000000000000000000000000000 FD0600 49a6af777488f947e2b6e2e0ab87f56f44cbfe8280c4dfd9c7c5d9c1b1291f10