From aad9c1d08e970ef7d8533298f21d783136412ce5 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 6 May 2024 11:13:06 +0530 Subject: [PATCH] SOB Assignment --- __pycache__/validator.cpython-310.pyc | Bin 20748 -> 20720 bytes output.txt | 6 +-- requirements.txt | 3 +- test.py | 66 ++++++++++++++------------ validator.py | 24 +++++----- 5 files changed, 52 insertions(+), 47 deletions(-) diff --git a/__pycache__/validator.cpython-310.pyc b/__pycache__/validator.cpython-310.pyc index c019e779a1f184648620694d02a54ef2614dc544..64bfd162afa5fd32e7775088e8ff2919656c54ea 100644 GIT binary patch delta 6358 zcmcIoX>eS}aelKm0D>eak^m@@0CA82L7b#O0ECG9APxc$NI`fa2`$l$#S*~7?(BjG zbUeE#CBA5{Jhps^HXuu~EL({c$dY`;N1Q0Jo#O`+SJsuJqDoSBB~?kKQt^+ZqIAzL z7Q0x*|3cLJ-n^Oa>G}GYndQe`5dXF=@|W}Ta}EBt)H1jDZ2lo}<=L7(8(fPyOSxvQ zFu*YLaK~-K%nummPTkt2aks_-je9iSr*W^weH!;`EF{e$dWz{eK+pa39MmI+G#(Bb z;0BKp-~}IH4%oT$%7Yz*`LL647wjf1fIWow!Cu0Bu%ECHiU^D00O9>`knj*3CMdSPm70m2gxEGIR{8=&Krz6V|{9!dj>!tcM1|Mra~DX~QXcJpj#wr=f+g71{{T zKs(`saF(zGItja=o3ICZ3HzX*Z~z9Cc!^D&gCY7Jh7rP1I8QhR^+ky&j707-5R0)+Dr zB)kR-gp065Xu&dJ2*QLCB7{*`A-rA$H|TW}ZV|4+@yhuIi3%qHqMiM8;>V&mE= z_!6wa`W?sG1M7y_Oua8;^`0h^Eo873zFchF7G^89!ix(cCgeA;1zTy3yIXUdHp~`l z7+%6wm*F(D)OX?K1!0r;pIHuJSi7+WzQSTjT*TPrRl;iNKp5$Lrx?fAvOHf6W^%UBneJO{&lXU1` zZ*youIn$vn+M!#z|CsdWuT#34nZbrL{n=vK>8Zg2nGWS-)Z92rE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Uv I&ffR`3!e-#V*mgE diff --git a/output.txt b/output.txt index 9a50756..e4b895b 100644 --- a/output.txt +++ b/output.txt @@ -1,6 +1,6 @@ -020000000000aeff0000000000000000000000000000000000000000000000000000000092d09dd75693f566bcb8d8f2e5d8d387eafb1d51e171612484f91c59aa4dc5f36637e28a1f00ffff2ea50000 -010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff0000000000025c91f313000000001976a914cb4f45b4ecfe54b25106a919237cf34ce193c1b988ac0000000000000000266a24aa21a9ed2a8c05a37101641b8c1b579dc23df9847f75954ce4e4329ff1d2fcffd70d995b0120000000000000000000000000000000000000000000000000000000000000000000000000 -e2d6e8562a693c738b622b6146a3fc30da8b776a6b33809ee01f0b8961b5af56 +020000000000aeff0000000000000000000000000000000000000000000000000000000043767bcb8b59d2f6a37372381087b3b767bb6d9fe25867f7b2d79ba94cc405056637e28a1f00ffffd9ff0000 +010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff00000000000245805201000000001976a914cb4f45b4ecfe54b25106a919237cf34ce193c1b988ac0000000000000000266a24aa21a9ed2a8c05a37101641b8c1b579dc23df9847f75954ce4e4329ff1d2fcffd70d995b0120000000000000000000000000000000000000000000000000000000000000000000000000 +7126789d60979ff28fe141bcab6b281f50741fecf77ed8aaa0cfa04e84393f9d 4ab3cc4296fee78153d60d2884323a84260157db0b83a72309272f109ad9dd32 99bf982da397947eb0999292e909c17c78d884e18d3e59ac03fd2aa7f0241c7e dcd522b3588c7adb0418454539e1a929fff936f211e5a20383fdcbc3ad8751b9 diff --git a/requirements.txt b/requirements.txt index 4942fa3..eaa4804 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ ecdsa base58 -bech32 \ No newline at end of file +bech32 +pycryptodome \ No newline at end of file diff --git a/test.py b/test.py index 6c10608..6701fe5 100644 --- a/test.py +++ b/test.py @@ -1141,42 +1141,46 @@ # else: # print('valid addr') -import hashlib - -def dsha256(message): - byte_value = bytes.fromhex(message) - first_hash = hashlib.sha256(byte_value).digest() - second_hash = hashlib.sha256(first_hash).digest() - return second_hash.hex() - -def merkle_root(tx_hashes): - if len(tx_hashes) == 0: - return None - elif len(tx_hashes) == 1: - return tx_hashes[0] - - new_tx_hashes = [] - for i in range(0, len(tx_hashes), 2): - if i < len(tx_hashes) - 1: - message = tx_hashes[i] + tx_hashes[i + 1] - new_tx_hashes.append(dsha256(message)) +# import hashlib + +# def dsha256(message): +# byte_value = bytes.fromhex(message) +# first_hash = hashlib.sha256(byte_value).digest() +# second_hash = hashlib.sha256(first_hash).digest() +# return second_hash.hex() + +# def merkle_root(tx_hashes): +# if len(tx_hashes) == 0: +# return None +# elif len(tx_hashes) == 1: +# return tx_hashes[0] + +# new_tx_hashes = [] +# for i in range(0, len(tx_hashes), 2): +# if i < len(tx_hashes) - 1: +# message = tx_hashes[i] + tx_hashes[i + 1] +# new_tx_hashes.append(dsha256(message)) - else: - message = tx_hashes[i] + tx_hashes[i] - new_tx_hashes.append(dsha256(message)) +# else: +# message = tx_hashes[i] + tx_hashes[i] +# new_tx_hashes.append(dsha256(message)) - print(new_tx_hashes) - return merkle_root(new_tx_hashes) +# print(new_tx_hashes) +# return merkle_root(new_tx_hashes) -l1 = [ - "0000000000000000000000000000000000000000000000000000000000000000", - "a28e549dc50610430bf7e224effd50db0662356780c934af0f1a9eb346d50087", - "87cbcb26ef9618f1363c0b0ae62c3ab6de1daf67fa6404c416a4d36059ab4bc5", - "85770dfeb29679fdb24e7ca87ca7d162962f6247269282f155f99e0061e31de5" -] +# l1 = [ +# "0000000000000000000000000000000000000000000000000000000000000000", +# "a28e549dc50610430bf7e224effd50db0662356780c934af0f1a9eb346d50087", +# "87cbcb26ef9618f1363c0b0ae62c3ab6de1daf67fa6404c416a4d36059ab4bc5", +# "85770dfeb29679fdb24e7ca87ca7d162962f6247269282f155f99e0061e31de5" +# ] -print(merkle_root(l1)) +# print(merkle_root(l1)) +from Crypto.Hash import RIPEMD160 +data_bytes = bytes.fromhex('a28e549dc50610430bf7e224effd50db0662356780c934af0f1a9eb346d50087') +h = RIPEMD160.new(data_bytes) +print(h.hexdigest()) diff --git a/validator.py b/validator.py index 3532625..5161940 100644 --- a/validator.py +++ b/validator.py @@ -5,6 +5,7 @@ from ecdsa.util import sigdecode_der import base58 import bech32 +from Crypto.Hash import RIPEMD160 cnt = 0 new_opcodes = set() @@ -124,9 +125,8 @@ def dsha256(message): def ripemd160(message): bytes_data = bytes.fromhex(message) - ripemd160_hash = hashlib.new('ripemd160', bytes_data).digest() - ripemd160_hex = ripemd160_hash.hex() - return ripemd160_hex + h = RIPEMD160.new(bytes_data) + return h.hexdigest() def sha256(hex_string): byte_string = bytes.fromhex(hex_string) @@ -1032,9 +1032,9 @@ def validate_script(script_list, stack, transaction, filename, ip): public_key = script_sig_list[len(script_sig_list) - 1] scriptpubkey_pkh = asm_list[3] - sha256_hash = hashlib.sha256(bytes.fromhex(public_key)).digest() - ripemd160_hash = hashlib.new('ripemd160', sha256_hash).digest() - scriptsig_pkh = ripemd160_hash.hex() + sha256_hash = hashlib.sha256(bytes.fromhex(public_key)).hexdigest() + ripemd160_hash = ripemd160(sha256_hash) + scriptsig_pkh = ripemd160_hash if scriptsig_pkh != scriptpubkey_pkh: invalid_txn.add(filename) @@ -1078,9 +1078,9 @@ def validate_script(script_list, stack, transaction, filename, ip): public_key = ip['witness'][1] scriptpubkey_pkh = asm_list[2] - sha256_hash = hashlib.sha256(bytes.fromhex(public_key)).digest() - ripemd160_hash = hashlib.new('ripemd160', sha256_hash).digest() - segwit_pkh = ripemd160_hash.hex() + sha256_hash = hashlib.sha256(bytes.fromhex(public_key)).hexdigest() + ripemd160_hash = ripemd160(sha256_hash) + segwit_pkh = ripemd160_hash if segwit_pkh != scriptpubkey_pkh: mismatch_wpkh_transaction.append(transaction) @@ -1117,9 +1117,9 @@ def validate_script(script_list, stack, transaction, filename, ip): # check whether serialized inner redeem script gives the correct OP_HASH160 scriptsig_asm_list = ip['scriptsig_asm'].split(' ') ser_redeem_script = scriptsig_asm_list[len(scriptsig_asm_list) - 1] - sha256_hash = hashlib.sha256(bytes.fromhex(ser_redeem_script)).digest() - ripemd160_hash = hashlib.new('ripemd160', sha256_hash).digest() - redeem_script_hash = ripemd160_hash.hex() + sha256_hash = hashlib.sha256(bytes.fromhex(ser_redeem_script)).hexdigest() + ripemd160_hash = ripemd160(sha256_hash) + redeem_script_hash = ripemd160_hash if redeem_script_hash != asm_list[2]: invalid_txn.add(filename)