Skip to content

Commit

Permalink
SOB Assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
m-g-rathod committed May 6, 2024
1 parent 7f466b9 commit aad9c1d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 47 deletions.
Binary file modified __pycache__/validator.cpython-310.pyc
Binary file not shown.
6 changes: 3 additions & 3 deletions output.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
020000000000aeff0000000000000000000000000000000000000000000000000000000092d09dd75693f566bcb8d8f2e5d8d387eafb1d51e171612484f91c59aa4dc5f36637e28a1f00ffff2ea50000
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff0000000000025c91f313000000001976a914cb4f45b4ecfe54b25106a919237cf34ce193c1b988ac0000000000000000266a24aa21a9ed2a8c05a37101641b8c1b579dc23df9847f75954ce4e4329ff1d2fcffd70d995b0120000000000000000000000000000000000000000000000000000000000000000000000000
e2d6e8562a693c738b622b6146a3fc30da8b776a6b33809ee01f0b8961b5af56
020000000000aeff0000000000000000000000000000000000000000000000000000000043767bcb8b59d2f6a37372381087b3b767bb6d9fe25867f7b2d79ba94cc405056637e28a1f00ffffd9ff0000
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff00000000000245805201000000001976a914cb4f45b4ecfe54b25106a919237cf34ce193c1b988ac0000000000000000266a24aa21a9ed2a8c05a37101641b8c1b579dc23df9847f75954ce4e4329ff1d2fcffd70d995b0120000000000000000000000000000000000000000000000000000000000000000000000000
7126789d60979ff28fe141bcab6b281f50741fecf77ed8aaa0cfa04e84393f9d
4ab3cc4296fee78153d60d2884323a84260157db0b83a72309272f109ad9dd32
99bf982da397947eb0999292e909c17c78d884e18d3e59ac03fd2aa7f0241c7e
dcd522b3588c7adb0418454539e1a929fff936f211e5a20383fdcbc3ad8751b9
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ecdsa
base58
bech32
bech32
pycryptodome
66 changes: 35 additions & 31 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())



Expand Down
24 changes: 12 additions & 12 deletions validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ecdsa.util import sigdecode_der
import base58
import bech32
from Crypto.Hash import RIPEMD160

cnt = 0
new_opcodes = set()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit aad9c1d

Please sign in to comment.