diff --git a/mine-your-first-block/src/main.rs b/mine-your-first-block/src/main.rs index 8080368..8fd41a6 100644 --- a/mine-your-first-block/src/main.rs +++ b/mine-your-first-block/src/main.rs @@ -1,6 +1,8 @@ -use serde; +use serde::Deserialize; use serde_json; use sha2; +use std::fs::File; +use std::io::Read; // Transaction struct that may be overcomplicated right now. We will see #[derive(Debug, Deserialize)] @@ -41,21 +43,50 @@ struct Vout { value: u64, } -/// +// Coinbase Transaction struct +// I'm reading that the txid of the coinbase tx is all zeros. +// The coinbase txid for input (VIN) is always "0000000000000000000000000000000000000000000000000000000000000000" +// The vout for Vin struct is "ffffffff" +// then lock the output to my own public key using a P2PK locking script (which is the typical +// locking script used for coinbase transactions early on in the blockchain). + +// struct CoinbaseTx { +// version: String, +// locktime: String, +// vin: Vec, +// vout: Vec, +// +// } /// // TODO Make a deserialize_tx function with serde and serde_json -fn deserialize_tx() -> Result { - // code to deserialize a tx +// First just working with one tx so I can learn +fn deserialize_tx(filename: &str) -> Result> { + // Open the file of a tx + let mut file = File::open(filename)?; + + let mut json_string = String::new(); + file.read_to_string(& mut json_string)?; + + let tx: Transaction = serde_json::from_str(&json_string)?; + + Ok(tx) } // TODO Make a validate_tx function that returns true if tx is valid and throws out invalid tx -fn validate_tx(transaction: Transaction) -> bool { - // Code to validate tx based -} +// fn validate_tx(transaction: Transaction) -> bool { +// // Code to validate tx based +// true +// } fn main() { - println!("Hello, world!"); + // Path to one transaction + let path = "../mempool/0a3c3139b32f021a35ac9a7bef4d59d4abba9ee0160910ac94b4bcefb294f196.json"; + + match deserialize_tx(path) { + Ok(tx) => println!("Deserialized Transaction is \n {:#?}", tx), + Err(e) => eprintln!("Error!!! {}", e), + } } // Start processing transactions, validating them,