-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added solution of P2WPKH and P2PKH Transactions
- Loading branch information
Showing
6 changed files
with
485 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,53 @@ | ||
from .python_files import structural_check | ||
import os | ||
import json | ||
|
||
# Run the structural check on the transactions in the mempool | ||
structural_check.check_structure_transactions("./mempool") | ||
|
||
def count_transaction_types(folder_path): | ||
p2pkh_count = 0 | ||
p2wpkh_count = 0 | ||
p2tr_count = 0 | ||
|
||
# Iterate over all files in the folder | ||
for filename in os.listdir(folder_path): | ||
if filename.endswith(".json"): | ||
file_path = os.path.join(folder_path, filename) | ||
|
||
# Read the JSON file | ||
with open(file_path, "r") as file: | ||
data = json.load(file) | ||
|
||
is_p2pkh = False | ||
is_p2wpkh = False | ||
is_p2tr = False | ||
|
||
# Check the transaction type for each input (vin) | ||
for input_data in data["vin"]: | ||
if "prevout" in input_data: | ||
if input_data["prevout"]["scriptpubkey_type"] == "p2pkh": | ||
is_p2pkh = True | ||
elif input_data["prevout"]["scriptpubkey_type"] == "v0_p2wpkh": | ||
is_p2wpkh = True | ||
elif input_data["prevout"]["scriptpubkey_type"] == "v1_p2tr": | ||
is_p2tr = True | ||
|
||
# Increment the count based on the transaction type | ||
if is_p2pkh: | ||
p2pkh_count += 1 | ||
elif is_p2wpkh: | ||
p2wpkh_count += 1 | ||
elif is_p2tr: | ||
p2tr_count += 1 | ||
|
||
return p2pkh_count, p2wpkh_count, p2tr_count | ||
|
||
|
||
# Specify the folder path containing the JSON transaction files | ||
folder_path = "mempool_valid" | ||
|
||
# Count the number of p2pkh, p2wpkh & p2tr_count transactions | ||
p2pkh_count, p2wpkh_count, p2tr_count = count_transaction_types(folder_path) | ||
|
||
# Print the results | ||
print(f"Number of P2PKH transactions: {p2pkh_count}") | ||
print(f"Number of P2WPKH transactions: {p2wpkh_count}") | ||
print(f"Number of P2TR transactions: {p2tr_count}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import json | ||
import hashlib | ||
from ecdsa import SigningKey, VerifyingKey, SECP256k1 | ||
|
||
|
||
def validate_transaction(tx_json): | ||
# Parse the transaction JSON | ||
tx = json.loads(tx_json) | ||
|
||
# Iterate over the inputs | ||
for vin in tx["vin"]: | ||
# Get the scriptPubKey and scriptSig | ||
scriptpubkey = vin["prevout"]["scriptpubkey"] | ||
scriptsig = vin["scriptsig"] | ||
|
||
# Extract the public key hash from the scriptPubKey | ||
pubkey_hash = scriptpubkey[6:-4] | ||
|
||
# Extract the signature and public key from the scriptSig | ||
sig_len = int(scriptsig[2:4], 16) | ||
sig_start = 4 | ||
sig_end = sig_start + sig_len * 2 | ||
signature = scriptsig[sig_start:sig_end] | ||
pubkey_start = sig_end + 2 | ||
pubkey = scriptsig[pubkey_start:] | ||
|
||
# Verify the public key hash matches the hash of the public key | ||
hash_object = hashlib.sha256(bytes.fromhex(pubkey)) | ||
pubkey_hash_new = hashlib.new("ripemd160", hash_object.digest()).hexdigest() | ||
if pubkey_hash_new != pubkey_hash: | ||
print(f"Public key hash mismatch for input {vin['txid']}:{vin['vout']}") | ||
return False | ||
|
||
# Verify the signature | ||
try: | ||
# Decode the public key hash | ||
decoded_hash = bytes.fromhex(pubkey_hash) | ||
|
||
# Decode the public key | ||
public_key = VerifyingKey.from_string( | ||
bytes.fromhex(pubkey), curve=SECP256k1 | ||
) | ||
|
||
# Verify the signature | ||
public_key.verify_digest( | ||
bytes.fromhex(signature), | ||
decoded_hash, | ||
sigdecode=VerifyingKey.from_string( | ||
bytes.fromhex(pubkey), curve=SECP256k1, hashfunc=hashlib.sha256 | ||
).verify_digest, | ||
) | ||
except Exception as e: | ||
print( | ||
f"Signature verification failed for input {vin['txid']}:{vin['vout']}" | ||
) | ||
print(f"Error: {str(e)}") | ||
return False | ||
|
||
print("All inputs are valid!") | ||
return True |
Oops, something went wrong.