diff --git a/Cargo.lock b/Cargo.lock index a862d91f..61576dcd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,15 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bumpalo" version = "3.16.0" @@ -61,6 +70,45 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.2.14" @@ -137,6 +185,7 @@ dependencies = [ "ring", "serde", "serde_json", + "sha2", ] [[package]] @@ -224,6 +273,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "spin" version = "0.9.8" @@ -241,6 +301,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -253,6 +319,12 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index d48d5c7e..445172fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ chrono = "0.4.37" hex-literal = "0.4.1" ring = "0.17.8" serde_json = "1.0.115" +sha2 = "0.10.8" diff --git a/src/main.rs b/src/main.rs index 1910044e..9acfaa60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![allow(dead_code, unused)] mod block; +mod transactions; use block::block_header::BlockHeader; use chrono::Utc; use mining_challenge::{read_mempool, read_tx_from_file}; diff --git a/src/transactions/tx.rs b/src/transactions/tx.rs index 16a09a66..694584a9 100644 --- a/src/transactions/tx.rs +++ b/src/transactions/tx.rs @@ -1,8 +1,9 @@ #![allow(dead_code, unused)] -// use serde::{Serialize, Deserialize} -// use serde_json; // use ring::digest; +use core::hash; + +use sha2::{Digest, Sha256, Sha256VarCore}; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] @@ -31,6 +32,21 @@ impl Tx{ } } + pub fn get_tx_hash(&self) -> String{ + let serialized_tx = format!("{:?}", self); + let mut hasher = Sha256::new(); + + hasher.update(serialized_tx.as_bytes()); + + let result = hasher.finalize(); + + let hash_string = result.iter().map(|byte| format!("{:02x}", byte)).collect::(); + + println!("{}", serialized_tx); + + return hash_string; + } + pub fn get_tx_size_in_bits(&self) -> u64 { // initial value for tx_size because every transaction will have at least 64 bits let mut tx_size: u64 = 32 + 32;