Skip to content

Commit

Permalink
Add p2sh
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchocholaty committed Nov 6, 2024
1 parent 8c80537 commit 05790b3
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 282 deletions.
61 changes: 32 additions & 29 deletions src/coinbase_transaction.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
COINBASE_TRANSACTION = {
"version": 2,
"locktime": 0xffffffff,
"vin": [
{
"txid": "0000000000000000000000000000000000000000000000000000000000000000",
"vout": 0xffffffff,
"sequence": 0xffffffff,
"is_coinbase": True,
"scriptsig": "160014fd91039e25b0827748473fce351afd8ead4ecdce",
"scriptsig_asm": "OP_PUSHBYTES_22 0014fd91039e25b0827748473fce351afd8ead4ecdce",
"witness": [
"0000000000000000000000000000000000000000000000000000000000000000",
BLOCKREWARD = 1250000000

def create_coinbase(total_fee):
return {
"version": 2,
"locktime": 0xffffffff,
"vin": [
{
"txid": "0000000000000000000000000000000000000000000000000000000000000000",
"vout": 0xffffffff,
"sequence": 0xffffffff,
"is_coinbase": True,
"scriptsig": "160014fd91039e25b0827748473fce351afd8ead4ecdce",
"scriptsig_asm": "OP_PUSHBYTES_22 0014fd91039e25b0827748473fce351afd8ead4ecdce",
"witness": [
"0000000000000000000000000000000000000000000000000000000000000000"
]
}
],
"vout": [
{
"scriptpubkey": "0014ad4cc1cc859c57477bf90d0f944360d90a3998bf",
"scriptpubkey_asm": "OP_0 OP_PUSHBYTES_20 ad4cc1cc859c57477bf90d0f944360d90a3998bf",
"scriptpubkey_type": "v0_p2wpkh",
"scriptpubkey_address": "bc1q44xvrny9n3t5w7lep58egsmqmy9rnx9lt6u0tc",
"value": BLOCKREWARD + total_fee
},
{
"scriptpubkey": "",
"scriptpubkey_type": "op_return",
"value": 0
}
]
}
],
"vout": [
{
"scriptpubkey": "0014ad4cc1cc859c57477bf90d0f944360d90a3998bf",
"scriptpubkey_asm": "OP_0 OP_PUSHBYTES_20 ad4cc1cc859c57477bf90d0f944360d90a3998bf",
"scriptpubkey_type": "v0_p2wpkh",
"scriptpubkey_address": "bc1q44xvrny9n3t5w7lep58egsmqmy9rnx9lt6u0tc",
"value": 100000
},
{
"scriptpubkey": "",
"scriptpubkey_type": "op_return",
"value": 0
}
]
}
6 changes: 4 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
if project_root_directory not in sys.path:
sys.path.append(project_root_directory)

from src.coinbase_transaction import COINBASE_TRANSACTION
from src.coinbase_transaction import create_coinbase
from src.mempool import MemPool
from src.mining import calculate_witness_commitment, block_mining
from src.serialize import serialize_transaction
from src.transaction import calculate_txid
from src.utils import total_fee

def parse_arguments():
parser = argparse.ArgumentParser(description='Simulation of the mining process of a block')
Expand All @@ -30,7 +31,8 @@ def parse_arguments():

# TODO pokracovani

block_transactions = [COINBASE_TRANSACTION] + mempool.valid_transactions
COINBASE_TRANSACTION = create_coinbase(total_fee(mempool.valid_transactions))
block_transactions = [COINBASE_TRANSACTION] + mempool.valid_transactions_json

transaction_hashes = [calculate_txid(COINBASE_TRANSACTION)] + [calculate_txid(json_transaction) for json_transaction in block_transactions[1:]]
block_hash = block_mining(transaction_hashes).hex()
Expand Down
3 changes: 2 additions & 1 deletion src/mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ def __init__(self, root_dir):
self.root_dir = root_dir
self.transaction_files = [os.path.join(self.root_dir, file) for file in os.listdir(self.root_dir) if file.endswith('.json')]
self.transactions = [Transaction(file) for file in self.transaction_files]
self.valid_transactions = [transaction.json_transaction for transaction in self.transactions if transaction.is_valid()]
self.valid_transactions = [transaction for transaction in self.transactions if transaction.is_valid()]
self.valid_transactions_json = [transaction.json_transaction for transaction in self.valid_transactions]
80 changes: 1 addition & 79 deletions src/op_codes.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,7 @@
# The following code is taken from: https://github.com/SummerOfBitcoin/code-challenge-2024-Nesopie/blob/840aeb58dd68cba3fba33f96d71ea0790c1840a8/src/features/script/op_codes.ts

OP_CODES = {
""" 'OP_0': b'\x00',
'OP_PUSHBYTES_1': b'\x01',
'OP_PUSHBYTES_2': b'\x02',
'OP_PUSHBYTES_3': b'\x03',
'OP_PUSHBYTES_4': b'\x04',
'OP_PUSHBYTES_5': b'\x05',
'OP_PUSHBYTES_6': b'\x06',
'OP_PUSHBYTES_7': b'\x07',
'OP_PUSHBYTES_8': b'\x08',
'OP_PUSHBYTES_9': b'\x09',
'OP_PUSHBYTES_10': b'\x0a',
'OP_PUSHBYTES_11': b'\x0b',
'OP_PUSHBYTES_12': b'\x0c',
'OP_PUSHBYTES_13': b'\x0d',
'OP_PUSHBYTES_14': b'\x0e',
'OP_PUSHBYTES_15': b'\x0f',
'OP_PUSHBYTES_16': b'\x10',
'OP_PUSHBYTES_17': b'\x11',
'OP_PUSHBYTES_18': b'\x12',
'OP_PUSHBYTES_19': b'\x13',
'OP_PUSHBYTES_20': b'\x14',
'OP_PUSHBYTES_21': b'\x15',
'OP_PUSHBYTES_22': b'\x16',
'OP_PUSHBYTES_23': b'\x17',
'OP_PUSHBYTES_24': b'\x18',
'OP_PUSHBYTES_25': b'\x19',
'OP_PUSHBYTES_26': b'\x1a',
'OP_PUSHBYTES_27': b'\x1b',
'OP_PUSHBYTES_28': b'\x1c',
'OP_PUSHBYTES_29': b'\x1d',
'OP_PUSHBYTES_30': b'\x1e',
'OP_PUSHBYTES_31': b'\x1f',
'OP_PUSHBYTES_32': b'\x20',
'OP_PUSHBYTES_33': b'\x21',
'OP_PUSHBYTES_34': b'\x22',
'OP_PUSHBYTES_35': b'\x23',
'OP_PUSHBYTES_36': b'\x24',
'OP_PUSHBYTES_37': b'\x25',
'OP_PUSHBYTES_38': b'\x26',
'OP_PUSHBYTES_39': b'\x27',
'OP_PUSHBYTES_40': b'\x28',
'OP_PUSHBYTES_41': b'\x29',
'OP_PUSHBYTES_42': b'\x2a',
'OP_PUSHBYTES_43': b'\x2b',
'OP_PUSHBYTES_44': b'\x2c',
'OP_PUSHBYTES_45': b'\x2d',
'OP_PUSHBYTES_46': b'\x2e',
'OP_PUSHBYTES_47': b'\x2f',
'OP_PUSHBYTES_48': b'\x30',
'OP_PUSHBYTES_49': b'\x31',
'OP_PUSHBYTES_50': b'\x32',
'OP_PUSHBYTES_51': b'\x33',
'OP_PUSHBYTES_52': b'\x34',
'OP_PUSHBYTES_53': b'\x35',
'OP_PUSHBYTES_54': b'\x36',
'OP_PUSHBYTES_55': b'\x37',
'OP_PUSHBYTES_56': b'\x38',
'OP_PUSHBYTES_57': b'\x39',
'OP_PUSHBYTES_58': b'\x3a',
'OP_PUSHBYTES_59': b'\x3b',
'OP_PUSHBYTES_60': b'\x3c',
'OP_PUSHBYTES_61': b'\x3d',
'OP_PUSHBYTES_62': b'\x3e',
'OP_PUSHBYTES_63': b'\x3f',
'OP_PUSHBYTES_64': b'\x40',
'OP_PUSHBYTES_65': b'\x41',
'OP_PUSHBYTES_66': b'\x42',
'OP_PUSHBYTES_67': b'\x43',
'OP_PUSHBYTES_68': b'\x44',
'OP_PUSHBYTES_69': b'\x45',
'OP_PUSHBYTES_70': b'\x46',
'OP_PUSHBYTES_71': b'\x47',
'OP_PUSHBYTES_72': b'\x48',
'OP_PUSHBYTES_73': b'\x49',
'OP_PUSHBYTES_74': b'\x4a',
'OP_PUSHBYTES_75': b'\x4b', """
'OP_PUSHDATA1': b'\x4c',
'OP_PUSHDATA2': b'\x4d',
'OP_PUSHDATA4': b'\x4e',
'OP_0': b'\x00',
'OP_1NEGATE': b'\x4f',
'OP_RESERVED': b'\x50',
'OP_1': b'\x51',
Expand Down
26 changes: 21 additions & 5 deletions src/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,33 @@ def execute(self) -> bool:
op = self.script[i:i+1]

# Handle data push operations
if op not in OP_CODES.values():
length = int.from_bytes(op, 'little')
if length > 75: # Use OP_PUSHDATA operations for larger chunks
raise InvalidScriptException(f"Invalid push operation length: {length}")
if op not in OP_CODES.values():
length = int.from_bytes(op, 'little')
if length > 75: # Use OP_PUSHDATA operations for larger chunks
if length == 76: #'OP_PUSHDATA1'
length = self.script[i+1]
data = self.script[i+2:i+2+length]
self.stack.push(data)
return length + 2
elif length == 77: # 'OP_PUSHDATA2'
length = int.from_bytes(self.script[i+1:i+3], 'little')
data = self.script[i+3:i+3+length]
self.stack.push(data)
return length + 3
elif length == 78: # 'OP_PUSHDATA4'
length = int.from_bytes(self.script[i+1:i+5], 'little')
data = self.script[i+5:i+5+length]
self.stack.push(data)
return length + 5
else:
raise InvalidScriptException(f"Invalid push operation length: {length}")
data = self.script[i+1:i+1+length]
self.stack.push(data)
i += length + 1
continue

# Handle opcodes
op_name = list(OP_CODES.keys())[list(OP_CODES.values()).index(op)]
op_name = list(OP_CODES.keys())[list(OP_CODES.values()).index(op)]
i += self._execute_opcode(op_name)

# Script executed successfully if stack is not empty and top value is true
Expand Down
Loading

0 comments on commit 05790b3

Please sign in to comment.