Skip to content

Commit

Permalink
Removed the unused functions and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchocholaty committed Nov 8, 2024
1 parent df0439b commit 3e3d01f
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 90 deletions.
13 changes: 2 additions & 11 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,10 @@ def parse_arguments():

mempool = MemPool(args.mempool)

# TODO pokracovani

block_transactions = []

#block_transactions = [COINBASE_TRANSACTION] + mempool.valid_transactions

# Initialize an empty list for transactions
block_transactions = [COINBASE_TRANSACTION]

# Initialize total weight and total fees
total_weight = 0
total_fees = 0

# Set the maximum block weight
max_block_weight = 4000000

# Sort the transactions by the fee in descending order
Expand All @@ -54,7 +45,7 @@ def parse_arguments():
total_weight = total_weight + tx_weight
total_fees = total_fees + tx.fee

transaction_hashes = [calculate_txid(COINBASE_TRANSACTION, True)] + [calculate_txid(transaction.json_transaction, transaction.has_witness) for transaction in block_transactions[1:]]
transaction_hashes = [calculate_txid(COINBASE_TRANSACTION, True)] + [calculate_txid(transaction.json_transaction, transaction.has_witness) for transaction in block_transactions]
block_hash = block_mining(transaction_hashes).hex()

wtxids = ["0000000000000000000000000000000000000000000000000000000000000000"] + transaction_hashes[1:]
Expand Down
46 changes: 0 additions & 46 deletions src/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,51 +46,6 @@ def __init__(self, script: bytes, json_transaction: dict = None, input_index: in
def create_signature_hash(self, hash_type: int) -> bytes:
data_signed = serialize_transaction(self.transaction, self.input_index, int(hash_type), self.segwit)
return hashlib.sha256(data_signed).digest()

def serialize_transaction(self, tx: dict) -> bytes:
"""Serialize a transaction for signing/verification"""
result = bytearray()

# Version
result.extend(tx['version'].to_bytes(4, 'little'))

# Number of inputs
result.extend(len(tx['vin']).to_bytes(1, 'little'))

# Inputs
for inp in tx['vin']:
# Previous transaction hash (reverse byte order)
prev_tx = bytes.fromhex(inp['txid'])[::-1]
result.extend(prev_tx)

# Previous output index
result.extend(inp['vout'].to_bytes(4, 'little'))

# Script
script_sig = bytes.fromhex(inp['scriptsig']) if inp['scriptsig'] else b''
result.extend(len(script_sig).to_bytes(1, 'little'))
result.extend(script_sig)

# Sequence
result.extend(inp['sequence'].to_bytes(4, 'little'))

# Number of outputs
result.extend(len(tx['vout']).to_bytes(1, 'little'))

# Outputs
for out in tx['vout']:
# Amount in satoshis
result.extend(out['value'].to_bytes(8, 'little'))

# Script
script_pubkey = bytes.fromhex(out['scriptpubkey'])
result.extend(len(script_pubkey).to_bytes(1, 'little'))
result.extend(script_pubkey)

# Locktime
result.extend(tx['locktime'].to_bytes(4, 'little'))

return bytes(result)

def execute(self) -> bool:
"""Execute the script and return True if it executed successfully"""
Expand Down Expand Up @@ -317,4 +272,3 @@ def combine_scripts(*scripts: Union[bytes, 'Script'], json_transaction: dict, se
else:
raise InvalidScriptException(f"Invalid script type: {type(script)}")
return Script(bytes(combined), json_transaction, segwit=segwit)

5 changes: 2 additions & 3 deletions src/transaction.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import hashlib
import json

from ecdsa import VerifyingKey, SECP256k1, BadSignatureError
from Crypto.Hash import RIPEMD160

from src.script import Script, InvalidScriptException
from src.script import Script
from src.serialize import serialize_transaction
from src.utils import decode_hex, get_filename_without_extension, hash160
from src.verify import parse_der_signature_bytes, valid_transaction_syntax
from src.verify import valid_transaction_syntax


def calculate_txid(transaction_content, segwit=False):
Expand Down
30 changes: 0 additions & 30 deletions src/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,3 @@ def valid_transaction_syntax(json_transaction):
return False

return True


def parse_der_signature_bytes(der_signature):
# Parse the DER signature
if der_signature[0] != 0x30:
raise ValueError("Invalid DER signature format")

length = der_signature[1]
if length + 2 != len(der_signature):
raise ValueError("Invalid DER signature length")

if der_signature[2] != 0x02:
raise ValueError("Invalid DER signature format")

r_length = der_signature[3]
r = der_signature[4:4 + r_length]

if der_signature[4 + r_length] != 0x02:
raise ValueError("Invalid DER signature format")

s_length = der_signature[5 + r_length]
s = der_signature[6 + r_length:6 + r_length + s_length]

# Determine the hash type
if len(der_signature) > 6 + r_length + s_length:
hash_type = der_signature[-1]
else:
hash_type = 0x01 # Default to SIGHASH_ALL

return r, s, hash_type

0 comments on commit 3e3d01f

Please sign in to comment.