From 7fbf83b32eb5489bdaad078fc98b4a5eae6d0b1c Mon Sep 17 00:00:00 2001 From: Pavitra Agarwal Date: Tue, 30 Apr 2024 08:10:52 +0530 Subject: [PATCH] message --- main.py | 66 ++++++++++++++++++++++++++++-------------------------- output.txt | 2 +- test.py | 1 + 3 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 test.py diff --git a/main.py b/main.py index 6a54f74..c616f99 100644 --- a/main.py +++ b/main.py @@ -36,6 +36,35 @@ def witnessbyte(wits) : ans.append(bytes.fromhex(wit)) return ans + +def little_endian_to_big_endian_txid(hex_str): + # Split the input string into pairs of characters + byte_pairs = [hex_str[i:i+2] for i in range(0, len(hex_str), 2)] + + # Reverse the order of byte pairs to switch endianness + reversed_byte_pairs = byte_pairs[::-1] + + # Reconstruct the string from reversed byte pairs + big_endian_hex_str = ''.join(reversed_byte_pairs) + + return bytes.fromhex(big_endian_hex_str) + +def big_endian_to_little_endian_txid(hex_str): + # Split the input string into pairs of characters + byte_pairs = [hex_str[i:i+2] for i in range(0, len(hex_str), 2)] + + # Reverse the order of byte pairs to switch endianness + reversed_byte_pairs = byte_pairs[::-1] + + # Reconstruct the string from reversed byte pairs + little_endian_hex_str = ''.join(reversed_byte_pairs) + + return bytes.fromhex(little_endian_hex_str) + + + + + def initializeTxn(txex , segwitness) : version = txex['version'] locktime = txex['locktime'] @@ -155,48 +184,21 @@ 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 +txinlittle = [little_endian_to_big_endian_txid(tx) for tx in txids] +merklebigedian = big_endian_to_little_endian_txid(merkle_root(txinlittle).hex()) - return bytes.fromhex(level[0]) +# print(little_endian_to_big_endian_txid(txids[0]).hex()) +# print(big_endian_to_little_endian_txid(little_endian_to_big_endian_txid(txids[0]).hex()).hex()) -# import hashlib -# 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([bytes.fromhex(h) for h in txids]), + merkle_root= merklebigedian , timestamp= int(ts), bits=bytes.fromhex('1f00ffff'), nonce= bytes.fromhex(nonce()), #nonce()) , #nonce should be of the bytes diff --git a/output.txt b/output.txt index 3fe8000..44c9482 100644 --- a/output.txt +++ b/output.txt @@ -1,4 +1,4 @@ -020000200000000000000000000000000000000000000000000000000000000000000000e016e1851957a9a69e138c34bf5c67fdf83581514903d012886f1c3d9d0c54aa39f52f66ffff001fb9a0c0c8 +020000200000000000000000000000000000000000000000000000000000000000000000aa540c9d3d1c6f8812d00349518135f8fd675cbf348c139ea6a9571985e116e0185a3066ffff001f58e129c9 010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff6a47304402201f6e9296e322013f90f89433926509a9ff686ce91452679c82c921cf18257c95022027497b1354b4dce14b3e15d2372c69d6323ecfad9cbc0c8bb20d8d8d088fc22001210224c6633127ca04e9b678ae7d106a9828ba2aed9a402eefae69f52fbe7a065699ffffffff02f595814a000000001976a914edf10a7fac6b32e24daa5305c723f3de58db1bc888ac0000000000000000266a24aa21a9ed0743cafb2ad89219798f1b2651467d2f6256998e493800f2560784f5f1bcd4670120000000000000000000000000000000000000000000000000000000000000000000000000 FD0600 49a6af777488f947e2b6e2e0ab87f56f44cbfe8280c4dfd9c7c5d9c1b1291f10 diff --git a/test.py b/test.py new file mode 100644 index 0000000..0d5a09e --- /dev/null +++ b/test.py @@ -0,0 +1 @@ +from usefulfunctions import merkle_root