diff --git a/src/main.py b/src/main.py index fa6ed5e..d4fda4b 100644 --- a/src/main.py +++ b/src/main.py @@ -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 @@ -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:] diff --git a/src/script.py b/src/script.py index f3790b8..c0725b4 100644 --- a/src/script.py +++ b/src/script.py @@ -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""" @@ -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) - \ No newline at end of file diff --git a/src/transaction.py b/src/transaction.py index e432cde..42dd820 100644 --- a/src/transaction.py +++ b/src/transaction.py @@ -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): diff --git a/src/verify.py b/src/verify.py index 916149b..ad89e54 100644 --- a/src/verify.py +++ b/src/verify.py @@ -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