Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
justcode740 committed May 3, 2024
1 parent 1a47953 commit 376431a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

40 changes: 20 additions & 20 deletions src/p2pkh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ extern crate secp256k1;
extern crate sha2;

use hex::FromHex;
use ripemd160::Ripemd160;
use secp256k1::{Message, PublicKey, Secp256k1, Signature};
// use ripemd160::Ripemd160;
// use secp256k1::{Message, PublicKey, Secp256k1, Signature};
use sha2::{Digest, Sha256};
use std::error::Error;

Expand All @@ -16,7 +16,7 @@ use crate::tx::Transaction;
impl Transaction {
/// Validates all inputs in the transaction based on P2PKH rules.
pub fn validate_p2pkh_inputs(&self) -> Result<bool, Box<dyn Error>> {
let secp = Secp256k1::new();
// let secp = Secp256k1::new();

// Process each input
for input in &self.vin {
Expand Down Expand Up @@ -46,31 +46,31 @@ impl Transaction {
let signature_bytes = hex::decode(signature_hex)?;

// Verify public key hash
let pubkey_hash = Self::hash160(&pubkey_bytes);
if hex::encode(pubkey_hash) != expected_pubkey_hash {
return Ok(false); // Public key hash does not match
}
// let pubkey_hash = Self::hash160(&pubkey_bytes);
// if hex::encode(pubkey_hash) != expected_pubkey_hash {
// return Ok(false); // Public key hash does not match
// }

// Verify signature
let pubkey = PublicKey::from_slice(&pubkey_bytes)?;
let signature = Signature::from_der(&signature_bytes[..signature_bytes.len() - 1])?; // remove sighash type byte
let message =
Message::from_hashed_data::<Sha256>(&double_sha256(&self.encode_to_vec()?));
// let pubkey = PublicKey::from_slice(&pubkey_bytes)?;
// let signature = Signature::from_der(&signature_bytes[..signature_bytes.len() - 1])?; // remove sighash type byte
// let message =
// Message::from_hashed_data::<Sha256>(&double_sha256(&self.encode_to_vec()?));

if !secp.verify(&message, &signature, &pubkey).is_ok() {
return Ok(false); // Signature does not verify
}
// if !secp.verify(&message, &signature, &pubkey).is_ok() {
// return Ok(false); // Signature does not verify
// }
}
}
Ok(true)
}

/// Helper function to hash data using SHA256 followed by RIPEMD-160.
fn hash160(input: &[u8]) -> Vec<u8> {
let sha256_result = Sha256::digest(input);
let ripemd_result = Ripemd160::digest(&sha256_result);
ripemd_result.to_vec()
}
// /// Helper function to hash data using SHA256 followed by RIPEMD-160.
// fn hash160(input: &[u8]) -> Vec<u8> {
// let sha256_result = Sha256::digest(input);
// // let ripemd_result = Ripemd160::digest(&sha256_result);
// // ripemd_result.to_vec()
// }

/// Helper function to encode transaction to Vec<u8> for hashing.
fn encode_to_vec(&self) -> Result<Vec<u8>, Box<dyn Error>> {
Expand Down

0 comments on commit 376431a

Please sign in to comment.