Skip to content

Commit

Permalink
chore: remove unnecessary line and make code cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
15IITian committed Apr 27, 2024
1 parent 7d61e08 commit 4c9726c
Show file tree
Hide file tree
Showing 30 changed files with 887 additions and 13,012 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cSpell.words": [
"prevout",
"RIPEMD",
"secp",
"Transacttions",
"witnessscript"
Expand Down
16 changes: 5 additions & 11 deletions Block.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from io import BytesIO
from unittest import TestCase
import Transacttions
# from Transacttions import *
from Helper import *

GENESIS_BLOCK = bytes.fromhex('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c')
Expand All @@ -28,7 +27,7 @@ def __init__(self, version, prev_block, merkle_root,


def serialize_blockheader(self):
'''Returns the 80 byte block header'''
'''Returns the 80 byte block header in hex'''
# version - 4 bytes, little endian
result = int_to_little_endian(self.version, 4)
# prev_block - 32 bytes, little endian
Expand Down Expand Up @@ -108,7 +107,6 @@ def create_block(cls, tx_files):
#---------------------------------------------------------------------------------------------

merkle_root_hash= merkle_root(tx_hashes_list)
# print("merkle_root_hash: {}".format(merkle_root_hash.hex()))
bits= "1f00ffff"
bits_in_bytes=bytes.fromhex(bits)
target ="0000ffff00000000000000000000000000000000000000000000000000000000"
Expand All @@ -117,28 +115,23 @@ def create_block(cls, tx_files):

# get the data related to tx mined in our block
(tx_count,tx_included,fees_collected,weight_stored)=Block.get_mined_tx_data(tx_files)
# print("tx_count:{} , fees_collected :{} , weight_stored : {}".format(tx_count,fees_collected,weight_stored))

# create an instance of a block
block= Block(version,prev_block_in_bytes,merkle_root_hash,timestamp,bits_in_bytes,nonce,tx_hashes_list)
# get the serliased block header
serliased_block_header= block.serialize_blockheader()

# print("block header:{}\n".format(serliased_block_header))

# print("block_hash: {}".format(block.hash()))
#-------------------------------------------------------------------------------------------

# get valid block header/ pow


valid_block= block.get_valid_nonce(target)
# print(valid_block.serialize_blockheader())
#______________________________________BLOCK_HEADER_________________________________________

# get the serliased coinbase transaction
serliased_coinbase_tx= Transacttions.Tx.get_serliased_coinbase_tx(fees_collected,tx_files)
# print("coinbase transaction: {}".format(serliased_coinbase_tx))

#______________________________________________________________________________________________

Expand All @@ -155,10 +148,11 @@ def create_block(cls, tx_files):

# now print the required block details in the output.txt

# block header

print("{} {} {} {} {} {} ".format(valid_block.version,(valid_block.prev_block).hex(),(valid_block.merkle_root).hex(),valid_block.timestamp,(valid_block.bits).hex(),valid_block.nonce))
# print the serliased block header
print(valid_block.serialize_blockheader())
# print("{} {} {} {} {} {} ".format(valid_block.version,(valid_block.prev_block).hex(),(valid_block.merkle_root).hex(),valid_block.timestamp,(valid_block.bits).hex(),valid_block.nonce))

print("block hash: {}".format(valid_block.hash()))
# serliased coinbase transaction
print(serliased_coinbase_tx)

Expand Down
6 changes: 3 additions & 3 deletions Helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import json,hashlib,os


import Crypto
from Crypto.Hash import RIPEMD160


Expand Down Expand Up @@ -177,8 +177,8 @@ def HASH160(s):
return ripemd160.hexdigest()


a= 5
print(HASH160(a.to_bytes(4,"big")))
# a= 5
# print(HASH160(a.to_bytes(4,"big")))



Expand Down
21 changes: 0 additions & 21 deletions Opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
if module_path not in sys.path:
sys.path.append(module_path)

# from Work.Helper import *
from Helper import *
from logging import getLogger
from ecc import *
Expand Down Expand Up @@ -606,23 +605,3 @@ def OP_IF(stack, items):
items[:0] = true_items
return True




sec_pubkey= "02c371793f2e19d1652408efef67704a2e9953a43a9dd54360d56fc93277a5667d"
der_signature= "3044022008f4f37e2d8f74e18c1b8fde2374d5f28402fb8ab7fd1cc5b786aa40851a70cb02201f40afd1627798ee8529095ca4b205498032315240ac322c9d8ff0f205a93a5801"

# publickey= secp256k1.PublicKey(None,True)
# ecdsa= secp256k1.ECDSA()
# print(len(der_signature))
# print(int.from_bytes(bytes.fromhex("20"),"big"))
# point= publickey.deserialize(bytes.fromhex(sec_pubkey))
# sig= ecdsa.ecdsa_deserialize(bytes.fromhex(der_signature))

# print( S256Point.parse(bytes.fromhex(sec_pubkey)))
# print(Signature.parse(bytes.fromhex(der_signature[:-2])))
# print(publickey.ecdsa_verify(z,sig))
# print(b)

# print(publickey.public_key)
# print("b {}".format(b))
3 changes: 2 additions & 1 deletion Output.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
803823616 000000000000000000030ed9d2b8643c4c1d8519f34fbecaf150136e20ee183f 95a3d096687c9f8dc0aa5af129d24a06f2d2b30a30fb9b05f045591cdfb00ed4 1714064564 1f00ffff 98146
0060e92f3f18ee206e1350f1cabe4ff319851d4c3c64b8d2d90e03000000000000000000d40eb0df1c5945f0059bfb300ab3d2f2064ad229f15aaac08d9f7c6896d0a395b48c2a66ffff001f627f0100
block hash: 4de570732a1ad7f2277e60e60d1968a9c9bdbc7cd4cecacac6c8e9fc7dc20000
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff11030cd3bf0c004f4345414e2e58595a002effffffff02e412bf13000000001976a9142c30a6aaac6d96687291475d7d52f4b469f665a688ac0000000000000000266a24aa21a9ed07528e9068d79f4fa737ce2b8cdea09f1b9c3c7a588948ff29e9d29d9a9cb2ef0120000000000000000000000000000000000000000000000000000000000000000000000000
dcd522b3588c7adb0418454539e1a929fff936f211e5a20383fdcbc3ad8751b9
423c61f1ec58b9400a373e1052c26debdd5d55eb0057900c3dcf41f54762486c
Expand Down
62 changes: 1 addition & 61 deletions Script.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from Opcodes import *
from Helper import *

# "OP_DUP OP_HASH160 OP_PUSHBYTES_20 6085312a9c500ff9cc35b571b0a1e5efb7fb9f16 OP_EQUALVERIFY OP_CHECKSIG",
# "scriptpubkey":["OP_HASH160", "OP_PUSHBYTES_20","<20_BYTE_HASH>", "OP_EQUAL"],
# "script_sig":[]

script_types= {

"p2pkh":{
Expand Down Expand Up @@ -53,7 +51,6 @@
"witness":[]
}

# "op_return":"OP_RETURN"
}
class Script:
def __init__(self,cmds=None):
Expand Down Expand Up @@ -273,23 +270,6 @@ def evaluate(cls,cmds,z,witness):
return False
return True



















# function to check whether a given scriptpubkey matches with the type mentioned in scripttype
@classmethod
Expand Down Expand Up @@ -376,45 +356,5 @@ def is_p2wsh_script_pubkey(script_pubkey_asm):
def p2pkh_script(h160):
'''Takes a hash160 and returns the p2pkh ScriptPubKey in serialised hex format'''
return ("76a914"+ h160+"88ac")





b= "OP_HASH160 OP_PUSHBYTES_20 dfe791507cb5a44c9a527982f5a69ade6c5421c9 OP_EQUAL"
a="OP_0 OP_PUSHBYTES_72 3045022100e1804c80eb6aecbb423d1a1223bedc3de5300ce32825a5fe979643727ebb56b00220351385c13fc79e7f8405e433bdd18c8630a446db06b693362f288a7615f7075701 OP_PUSHBYTES_72 3045022100f4dda558ecfae6d2acb68a07f51d8314e6819afebc79fba2674f79320e60d3d002204e497fa8d610514e652939f07876589fa1f2ea36d4ae2570079251a46d4d2f6001 OP_PUSHBYTES_71 522102fa3e97f867f6dd61c8f3870174a62212c568bbfa1d55fd57b8d355343d35a65c2103031d20b72222b1f6fe8217783c0adf898358afc24dee408e518e4af0dc899e7052ae"
# c= Script.add(a,b)
# c= c[::-1]
# print(c)
# print(c.pop(0))
# print(c.pop())
# print(c[:])

d= "001429862d65368280039b5454830c4bb728e3e87412"
e= "76a9141ef7874d338d24ecf6577e6eadeeee6cd579c67188ac"
pkh="29862d65368280039b5454830c4bb728e3e87412"
script= p2pkh_script(pkh)
# print(script)

# print(Script.parse(script))

# print(d)
# print(type(d))
# print(d[1])
# print(d.split(" "))
# print(d)
# b= b.split(" ")
# d.extend(b)
# print(d)

target= 100
# print(target.to_bytes(32, 'big'))

# {'bare-multisig',
# 'op_return',
# 'p2pkh',
# 'p2sh',
# 'v0_p2wpkh',
# 'v0_p2wsh',
# 'v1_p2tr'}

35 changes: 6 additions & 29 deletions Transacttions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# import Script
# from Script import *
from Helper import *
from Script import *
from Block import *
Expand All @@ -19,9 +17,6 @@

# this list contains all tx files in mempool folder
all_files= list_all_tx()





class Tx:
Expand Down Expand Up @@ -84,10 +79,6 @@ def fee_for_each_tx(cls ,tx_file):
return fee


### fn to check whether a tx is using segwit or legacy
# @classmethod
# def tx_is_segwit(file):

### serialising tx ->
@classmethod
def serialize(cls,file):
Expand Down Expand Up @@ -177,15 +168,6 @@ def get_tx_hash(cls,file):
return tx_hash











@classmethod
def sig_hash_legacy(cls,file,input_index,redeem_script=None):
file_copy =file.copy()
Expand Down Expand Up @@ -234,10 +216,6 @@ def sig_hash_legacy(cls,file,input_index,redeem_script=None):

return int.from_bytes(h256,"big") # doubt







# Finding Sig_hash for segwit transaction ->
Expand Down Expand Up @@ -347,7 +325,7 @@ def verify_input(cls,file,input_index):

# check whether the scrip_pubkey is p2sh
if is_p2sh_script_pubkey(script_pubkey):
# OP_HASH160 OP_PUSHBYTES_20 524c9cae5ea3fa7639754d46540a7f67bb5c2642 OP_EQUAL
# OP_HASH160 OP_PUSHBYTES_20 524c9cae5ea3fa7639754d46540a7f67bb5c2642 OP_EQUAL
# then the last cmd would be Redeem Script of 20 bytes
cmd= script_pubkey[-2] # this will be serialised redeem script

Expand Down Expand Up @@ -411,7 +389,7 @@ def get_tx_weights(cls,file) -> int :
# get the serialised tx
serialise_tx= Tx.serialize(file)
if not file["is_segwit"]:
return len(serialise_tx) * 4 # Tx.serialize give in bytes
return len(serialise_tx) * 4 # Tx.serialize give in bytes

marker_size= 2

Expand Down Expand Up @@ -555,13 +533,13 @@ def get_unlocked_tx(cls,tx_files):
if unlock_flag:
tx_unlocked.append(file)

return (tx_unlocked,tx_locked,zeroes)
return (tx_unlocked,tx_locked,zeroes)




class Transaction_Input:


def __init__(self,prev_txid,vout,script_sig="",sequence=0xffffffff,witness=None):
self.prev_txid= prev_txid
self.vout= vout
Expand All @@ -571,14 +549,15 @@ def __init__(self,prev_txid,vout,script_sig="",sequence=0xffffffff,witness=None)
self.script_sig= script_sig
self.sequence= sequence

self.witness= witness # doing this for now
self.witness= witness

def __repr__(self):
return '{}:{}'.format(
self.prev_tx.hex(),
self.prev_index,
)


# function to serialize a transaction input
# Returns the byte serialization of the transaction input

Expand Down Expand Up @@ -632,8 +611,6 @@ def serialize(cls, tx_out):

def categorize_sequence(sequence):
result= {"rbf":False,"relative_locktime":False}



if sequence >= MIN_NO_RBF or sequence == SEQUENCE_MAX:
return result
Expand Down
Loading

0 comments on commit 4c9726c

Please sign in to comment.