Skip to content

Commit

Permalink
added p2pkh verification
Browse files Browse the repository at this point in the history
  • Loading branch information
aaravm committed Apr 11, 2024
1 parent 0d6bd89 commit ac55073
Show file tree
Hide file tree
Showing 646 changed files with 37,181 additions and 3 deletions.
204 changes: 204 additions & 0 deletions foo/Cargo.lock

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

14 changes: 14 additions & 0 deletions foo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "foo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
secp256k1 = "0.18"
hex = "0.4"
serde_json = "1.0"
sha2 = "0.10"


111 changes: 111 additions & 0 deletions foo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
extern crate secp256k1;
extern crate hex;

use secp256k1::{Secp256k1, Message, PublicKey, Signature, All, Error};
use hex::{decode, FromHex};

use std::fs::File;
use std::io::Read;
use sha2::{Digest, Sha256};
use std::str;

fn hex_to_little_endian(hex_number: &str) -> String {
let hex_bytes = hex::decode(hex_number).unwrap();
let mut little_endian_bytes = hex_bytes.clone();
little_endian_bytes.reverse();
hex::encode(little_endian_bytes)
}

fn main() {
let mut f = File::open("../mempool/0ac528562a1626863c0cb912eb725530c54e786e6485380c16633e4b9bce1720.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 mut raw_transaction = String::new();

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

let input_count = format!("{:02x}", data["vin"].as_array().unwrap().len());
raw_transaction += &hex_to_little_endian(&input_count);

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

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

let script_pubkey_length = input["prevout"]["scriptpubkey"].as_str().unwrap().len() / 2;
let script_pubkey_length_hex = format!("{:02x}", script_pubkey_length);
raw_transaction += &hex_to_little_endian(&script_pubkey_length_hex);

let script_pubkey = input["prevout"]["scriptpubkey"].as_str().unwrap();
raw_transaction += script_pubkey;

raw_transaction += "ffffffff";
}

let output_count = format!("{:02x}", data["vout"].as_array().unwrap().len());
raw_transaction += &hex_to_little_endian(&output_count);

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

let script_length = output["scriptpubkey"].as_str().unwrap().len() / 2;
let script_length_hex = format!("{:02x}", script_length);
raw_transaction += &hex_to_little_endian(&script_length_hex);

let script_pubkey = output["scriptpubkey"].as_str().unwrap();
raw_transaction += script_pubkey;
}

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

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


let signature = data["vin"][0]["scriptsig_asm"].as_str().unwrap().split_whitespace().nth(1).unwrap();
let signature = &signature[..signature.len() - 2];

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

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

let data2 = sha256_hash1;
let hash_hex1 = Sha256::digest(&data2).to_vec();

let hex = hex::encode(hash_hex1);
let hash_hex: &str = hex.as_str();

// println!("sha256_hash: {}", (hash_hex));

let secp = Secp256k1::new();

// Decode the signature, public key, and hash
// let signature = "30440220200b9a61529151f9f264a04e9aa17bb6e1d53fb345747c44885b1e185a82c17502200e41059f8ab4d3b3709dcb91b050c344b06c5086f05598d62bc06a8b746db429";
let signature_bytes = decode(signature).expect("Failed to decode signature hex");

// let pub_key = "025f0ba0cdc8aa97ec1fffd01fac34d3a7f700baf07658048263a2c925825e8d33";
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");

// Create a signature object
let signature = Signature::from_der(&signature_bytes).expect("Fuck! Invalid signature");

// Verify the signature
match secp.verify(&message, &signature, &pubkey) {
Ok(_) => println!("Signature is valid!"),
Err(Error::IncorrectSignature) => println!("Signature is invalid!"),
_ => println!("Failed to verify signature!"),
}
}
1 change: 1 addition & 0 deletions foo/target/.rustc_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc_fingerprint":13052377172341671592,"outputs":{"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/aarav/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.75.0 (82e1608df 2023-12-21)\nbinary: rustc\ncommit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112\ncommit-date: 2023-12-21\nhost: x86_64-unknown-linux-gnu\nrelease: 1.75.0\nLLVM version: 17.0.6\n","stderr":""}},"successes":{}}
3 changes: 3 additions & 0 deletions foo/target/CACHEDIR.TAG
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by cargo.
# For information about cache directory tags see https://bford.info/cachedir/
Empty file added foo/target/debug/.cargo-lock
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d1b4fefb2b685ce6
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":2497478894085024098,"features":"[]","target":2661632913477203689,"profile":12206360443249279867,"path":9451348585837955133,"deps":[[9665562089965330559,"generic_array",false,458546676318917479]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/block-buffer-7e9270fb9b7ff635/dep-lib-block-buffer"}}],"rustflags":[],"metadata":5573904726092117450,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bda0eb1713b26991
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":2497478894085024098,"features":"[]","target":2661632913477203689,"profile":10243973527296709326,"path":9451348585837955133,"deps":[[9665562089965330559,"generic_array",false,15805434118010697427]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/block-buffer-d41a6494e362082a/dep-lib-block-buffer"}}],"rustflags":[],"metadata":5573904726092117450,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
1 change: 1 addition & 0 deletions foo/target/debug/.fingerprint/cc-84c7bad5892ee297/lib-cc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e5546d7a2d382253
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":2497478894085024098,"features":"[]","target":15023190189141807623,"profile":13232757476167777671,"path":15290129505144383770,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cc-84c7bad5892ee297/dep-lib-cc"}}],"rustflags":[],"metadata":5862599371499774553,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1577a5f37b165e1e
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":2497478894085024098,"features":"[]","target":10623512480563079566,"profile":10243973527296709326,"path":8005513008532774518,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cfg-if-1b06147f67f93fdc/dep-lib-cfg-if"}}],"rustflags":[],"metadata":8462187951337715540,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c558675480403888
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":2497478894085024098,"features":"[]","target":10623512480563079566,"profile":12206360443249279867,"path":8005513008532774518,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cfg-if-61dc7a6799e72b18/dep-lib-cfg-if"}}],"rustflags":[],"metadata":8462187951337715540,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fbfad6d00aea6f5e
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":2497478894085024098,"features":"[]","target":12245745790804801655,"profile":12206360443249279867,"path":135489290781565813,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cpufeatures-b8d9ed5bb45874e9/dep-lib-cpufeatures"}}],"rustflags":[],"metadata":6650989611501850964,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b25c6b21fb7d7fe5
Loading

0 comments on commit ac55073

Please sign in to comment.