Skip to content

Commit

Permalink
Fix input index
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchocholaty committed Nov 27, 2024
1 parent 4822ae2 commit a6cbd91
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def execute(self) -> bool:

# Handle opcodes
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 Expand Up @@ -257,7 +258,7 @@ def op_checksig(self) -> int:
return 1

@staticmethod
def combine_scripts(*scripts: Union[bytes, 'Script'], json_transaction: dict, segwit: bool = False) -> 'Script':
def combine_scripts(*scripts: Union[bytes, 'Script'], json_transaction: dict, input_index: int = 0, segwit: bool = False) -> 'Script':
"""
Combine multiple scripts into a single script.
Accepts both bytes and Script objects.
Expand All @@ -270,4 +271,4 @@ def combine_scripts(*scripts: Union[bytes, 'Script'], json_transaction: dict, se
combined.extend(script)
else:
raise InvalidScriptException(f"Invalid script type: {type(script)}")
return Script(bytes(combined), json_transaction, segwit=segwit)
return Script(bytes(combined), json_transaction, input_index=input_index, segwit=segwit)
33 changes: 21 additions & 12 deletions src/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ def check_input_output_sum(self):

self.fee = input_sum - output_sum

# Output sum can't be greater than the input sum.
if input_sum < output_sum:
return False
#if input_sum <= output_sum:
# return False

return True
#return True
#return input_sum - output_sum >= 1000
return input_sum - output_sum

def calculate_weight(self):
base_size = len(serialize_transaction(self.json_transaction))
Expand All @@ -111,15 +112,20 @@ def valid_input(self, vin_idx, vin):

if scriptpubkey_type == "p2pkh":
return self.validate_p2pkh(vin_idx, vin)
# return False
elif scriptpubkey_type == "p2sh":
pass
# pass
return False
elif scriptpubkey_type == "v0_p2wsh":
pass
# pass
return False
elif scriptpubkey_type == "v1_p2tr":
pass
# pass
return False
elif scriptpubkey_type == "v0_p2wpkh":
self.has_witness = True
return self.validate_p2wpkh(vin_idx, vin)
#self.has_witness = True
#return self.validate_p2wpkh(vin_idx, vin)
return False


# Unknown script type.
Expand All @@ -142,13 +148,14 @@ def validate_p2pkh(self, vin_idx, vin):
scriptpubkey = decode_hex(prevout.get("scriptpubkey", ""))

# Combine and verify
script = Script.combine_scripts(scriptsig, scriptpubkey, json_transaction=self.json_transaction)
script = Script.combine_scripts(scriptsig, scriptpubkey, json_transaction=self.json_transaction, input_index=vin_idx)
is_valid = script.execute()

#print(is_valid)

return is_valid

"""
def validate_p2sh(self, vin_idx, vin):
#################
# Pubkey script #
Expand Down Expand Up @@ -191,7 +198,7 @@ def validate_p2sh(self, vin_idx, vin):
is_valid = script.execute()
return is_valid

"""
def validate_p2wpkh(self, vin_idx, vin):
"""
Validate a Pay-to-Witness-Public-Key-Hash (P2WPKH) transaction input
Expand Down Expand Up @@ -259,7 +266,9 @@ def validate_p2wpkh(self, vin_idx, vin):

# Execute the script and catch any errors during the process
try:
return script.execute()
result = script.execute()
return result
# return script.execute()
except Exception as e:
print(f"P2WPKH validation error: {str(e)}")
return False

0 comments on commit a6cbd91

Please sign in to comment.