Skip to content

Commit

Permalink
Adding method to hash the Tx structure
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielgusn committed Apr 24, 2024
1 parent 043ca43 commit cd49954
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
20 changes: 18 additions & 2 deletions src/transactions/tx.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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::<String>();

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;
Expand Down

0 comments on commit cd49954

Please sign in to comment.