Skip to content

Commit

Permalink
added p2wpkh
Browse files Browse the repository at this point in the history
  • Loading branch information
aaravm committed Apr 12, 2024
1 parent 5d1e18b commit b4bbe9a
Show file tree
Hide file tree
Showing 90 changed files with 177 additions and 13 deletions.
160 changes: 147 additions & 13 deletions foo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate hex;
use secp256k1::{Secp256k1, Message, PublicKey, Signature, Error};
use hex::{decode, FromHex};

use std::fs::File;
use std::{fs::File, os::linux::raw};
use std::io::Read;
use sha2::{Digest, Sha256};
use std::str;
Expand All @@ -16,7 +16,99 @@ fn hex_to_little_endian(hex_number: &str) -> String {
hex::encode(little_endian_bytes)
}

fn create_transaction(data: serde_json::Value, parameter: u64) -> String {
fn create_transaction_p2wpkh(data: serde_json::Value, parameter: usize) -> String {
let mut raw_transaction = String::new();

let version = format!("{:08x}", data["version"].as_u64().unwrap());
raw_transaction += &hex_to_little_endian(&version);

let mut hashPrevouts = String::new();

for input in data["vin"].as_array().unwrap() {
let prev_txid = input["txid"].as_str().unwrap();
hashPrevouts += &hex_to_little_endian(prev_txid);

let prev_index = format!("{:08x}", input["vout"].as_u64().unwrap());
hashPrevouts += &hex_to_little_endian(&prev_index);
}

// println!("hashPrevouts is: {}",hashPrevouts);
let data1 = Vec::from_hex(hashPrevouts).unwrap();
let data2 = Sha256::digest(&data1).to_vec();
let data3 = Sha256::digest(&data2).to_vec();
let data4 = hex::encode(data3);

raw_transaction += &data4;

let mut hashSequence = String::new();

for input in data["vin"].as_array().unwrap() {
let sequence_decimal = input["sequence"].as_u64().unwrap_or_default();
let sequence_hex = format!("{:08x}", sequence_decimal);
hashSequence += &hex_to_little_endian(&sequence_hex);
}

// println!("hashSequence is: {}",hashSequence);
let data1 = Vec::from_hex(hashSequence).unwrap();
let data2 = Sha256::digest(&data1).to_vec();
let data3 = Sha256::digest(&data2).to_vec();
let data4 = hex::encode(data3);

raw_transaction += &data4;

let prev_txid = data["vin"][parameter]["txid"].as_str().unwrap();
raw_transaction += &hex_to_little_endian(prev_txid);

let prev_index = format!("{:08x}", data["vin"][parameter]["vout"].as_u64().unwrap());
raw_transaction += &hex_to_little_endian(&prev_index);

raw_transaction += "1976a914";
raw_transaction += data["vin"][parameter]["prevout"]["scriptpubkey_asm"].as_str().unwrap().split_whitespace().nth(2).unwrap();
raw_transaction += "88ac";

let value = format!("{:016x}", (data["vin"][parameter]["prevout"]["value"].as_f64().unwrap()) as u64);
raw_transaction += &hex_to_little_endian(&value);

let sequence_decimal = data["vin"][parameter]["sequence"].as_u64().unwrap_or_default();
let sequence_hex = format!("{:08x}", sequence_decimal);
raw_transaction += &hex_to_little_endian(&sequence_hex);

let mut hashOutputs = String::new();

for output in data["vout"].as_array().unwrap() {
let value = format!("{:016x}", (output["value"].as_f64().unwrap()) as u64);
hashOutputs += &hex_to_little_endian(&value);

hashOutputs += "1976a914";
hashOutputs += output["scriptpubkey_asm"].as_str().unwrap().split_whitespace().nth(3).unwrap();
hashOutputs += "88ac";
}

let data1 = Vec::from_hex(hashOutputs).unwrap();
let data2 = Sha256::digest(&data1).to_vec();
let data3 = Sha256::digest(&data2).to_vec();
let data4 = hex::encode(data3);

raw_transaction += &data4;

let locktime = format!("{:08x}", data["locktime"].as_u64().unwrap());
raw_transaction += &hex_to_little_endian(&locktime);

let sighash_all = "01000000";
raw_transaction += sighash_all;

// println!("raw_transaction is: {}",raw_transaction);

let data1 = Vec::from_hex(raw_transaction).unwrap();
let data2 = Sha256::digest(&data1).to_vec();
let hash_hex1 = Sha256::digest(&data2).to_vec();

let hex = hex::encode(hash_hex1);
hex
}


fn create_transaction_p2pkh(data: serde_json::Value, parameter: usize) -> String {
let mut raw_transaction = String::new();

let version = format!("{:08x}", data["version"].as_u64().unwrap());
Expand All @@ -25,7 +117,7 @@ fn create_transaction(data: serde_json::Value, parameter: u64) -> String {
let input_count = format!("{:02x}", data["vin"].as_array().unwrap().len());
raw_transaction += &hex_to_little_endian(&input_count);

let mut ind: u64 = 0;
let mut ind: usize = 0;

for input in data["vin"].as_array().unwrap() {
if ind == parameter {
Expand All @@ -43,7 +135,10 @@ fn create_transaction(data: serde_json::Value, parameter: u64) -> String {
let script_pubkey = input["prevout"]["scriptpubkey"].as_str().unwrap();
raw_transaction += script_pubkey;

raw_transaction += "ffffffff";
let sequence_decimal = data["vin"][parameter]["sequence"].as_u64().unwrap_or_default();
let sequence_hex = format!("{:08x}", sequence_decimal);

raw_transaction += &hex_to_little_endian(&sequence_hex);
}
else {
let prev_txid = input["txid"].as_str().unwrap();
Expand All @@ -54,7 +149,10 @@ fn create_transaction(data: serde_json::Value, parameter: u64) -> String {

raw_transaction += "00";

raw_transaction += "ffffffff";
let sequence_decimal = data["vin"][parameter]["sequence"].as_u64().unwrap_or_default();
let sequence_hex = format!("{:08x}", sequence_decimal);

raw_transaction += &hex_to_little_endian(&sequence_hex);
}
ind = ind + 1;
}
Expand All @@ -80,7 +178,6 @@ fn create_transaction(data: serde_json::Value, parameter: u64) -> String {
let sighash_all = "01000000";
raw_transaction += sighash_all;


let data1 = Vec::from_hex(raw_transaction).unwrap();
let sha256_hash1 = Sha256::digest(&data1).to_vec();

Expand All @@ -92,32 +189,68 @@ fn create_transaction(data: serde_json::Value, parameter: u64) -> String {
}

fn main() {
let mut f = File::open("../mempool/2a11dfa8a9c3ee8950a4c2328306dc4b3643ecaa737bd85e019a236532d65e6a.json").unwrap();
let mut f = File::open("../mempool/00a5be9434f4d97613391cdce760293fd142786a00952ed4edfd66dd19c5c23a.json").unwrap();
let mut data = String::new();
f.read_to_string(&mut data).unwrap();
let data: serde_json::Value = serde_json::from_str(&data).unwrap();

let input_count = data["vin"].as_array().unwrap().len();
// let hex= create_transaction_p2wpkh(data.clone(),0);

let mut flag = false;

for i in 0..input_count {
let hex= create_transaction(data.clone(),i as u64);
// If scriptSigtype!="p2pkh", continue
let script_sigtype = data["vin"][i]["prevout"]["scriptpubkey_type"].as_str().unwrap();
// println!("scriptSigtype is: {}",scriptSigtype);


let hex = if script_sigtype == "p2pkh" {
create_transaction_p2pkh(data.clone(), i)
} else if script_sigtype == "v0_p2wpkh" {
create_transaction_p2wpkh(data.clone(), i)
} else {
flag = true;
break;
};
let hash_hex: &str = hex.as_str();

let secp = Secp256k1::new();

// Decode the signature, public key, and hash
let signature = data["vin"][i]["scriptsig_asm"].as_str().unwrap().split_whitespace().nth(1).unwrap();

let signature: String = if script_sigtype == "p2pkh" {
data["vin"][i]["scriptsig_asm"].as_str().unwrap().split_whitespace().nth(1).unwrap().to_string()
} else if script_sigtype == "v0_p2wpkh" {
data["vin"][i]["witness"][0].as_str().unwrap().to_string()
} else {
flag = true;
break;
};

// IF P2PKH SIGNATURE IS HERE
// let signature = data["vin"][i]["scriptsig_asm"].as_str().unwrap().split_whitespace().nth(1).unwrap();

// let signature = data["vin"][i]["witness"][0].as_str().unwrap();
let signature = &signature[..signature.len() - 2];
// println!("signature is: {}",signature);
// let signature = "3045022100bf3ec2ec7506a3c3e29f5ee4d39162ccdb063fb547f1749a1cc282b9b7a261c9022029cedd3aea84c612012856cd654a639a3112cfcdf3fa5b7c9815a29496f28001";
let signature_bytes = decode(signature).expect("Failed to decode signature hex");

let pub_key = data["vin"][i]["scriptsig_asm"].as_str().unwrap().split_whitespace().nth(3).unwrap();
// IF P2PKH PUBKEY IS HERE
let pub_key: String = if script_sigtype == "p2pkh" {
data["vin"][i]["scriptsig_asm"].as_str().unwrap().split_whitespace().nth(3).unwrap().to_string()
} else if script_sigtype == "v0_p2wpkh" {
data["vin"][i]["witness"][1].as_str().unwrap().to_string()
} else {
flag = true;
break;
};

// let pub_key = data["vin"][i]["scriptsig_asm"].as_str().unwrap().split_whitespace().nth(3).unwrap();

// let pub_key = data["vin"][i]["witness"][1].as_str().unwrap();
let pubkey_bytes = decode(pub_key).expect("Failed to decode pubkey hex");
let pubkey = PublicKey::from_slice(&pubkey_bytes).expect("Invalid public key");

// let hash_hex = "713f55b5ea939f8269a0757a86df761a7a0ddaca9e2f5d6cf761cf43fdf7e6f9";
let hash_bytes = decode(hash_hex).expect("Failed to decode hash hex");
let message = Message::from_slice(&hash_bytes).expect("Invalid message");

Expand All @@ -131,6 +264,7 @@ fn main() {
_ => println!("Failed to verify signature!"),
}
}



}
Loading

0 comments on commit b4bbe9a

Please sign in to comment.