From 63d3ddbf5cbc5f0982d16cac8f948c5e71b9dc58 Mon Sep 17 00:00:00 2001 From: Ian Slane Date: Wed, 27 Mar 2024 22:14:38 -0600 Subject: [PATCH] Have most of the logic worked out for the validate_tx function. verify_script function is next --- mine-your-first-block/src/main.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/mine-your-first-block/src/main.rs b/mine-your-first-block/src/main.rs index a7052cc..3af4bff 100644 --- a/mine-your-first-block/src/main.rs +++ b/mine-your-first-block/src/main.rs @@ -83,7 +83,31 @@ fn deserialize_tx(filename: &str) -> Result bool { - // Code to validate tx based + + // If the input value in sats is less than the output value then the transaction is already + // invalid. the sum of the input must be greater than the sum of the output the difference is + // the transaction fee. need to filter out higher tx fees later to include in block + let input_value: u64 = transaction.vin.iter().map(|vin| vin.prevout.value).sum(); + let output_value: u64 = transaction.vout.iter().map(|vout| vout.value).sum(); + if input_value < output_value { + return false; + } + + // Verify if scriptpubkey_asm returns true + // will this work as for outputs in transaction outputs? verify if this is correct + for vout in &transaction.vout { + if !verify_script(&vout.scriptpubkey_asm) { + return false; + } + } + // if all verifications pass the transaction is validated and returns true or OK + true +} + +// TODO make a function that verifies if a script returns OK +fn verify_script(script: &str) -> bool { + // Verify the script based off grokking bitcoin chapter 5 + // look over OP_CODES or operators true }