Skip to content

Commit

Permalink
support added for multiple inputs p2pkh
Browse files Browse the repository at this point in the history
  • Loading branch information
aaravm committed Apr 11, 2024
1 parent 50b0a71 commit 5d1e18b
Show file tree
Hide file tree
Showing 89 changed files with 67 additions and 47 deletions.
102 changes: 64 additions & 38 deletions foo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate secp256k1;
extern crate hex;

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

use std::fs::File;
Expand All @@ -16,7 +16,7 @@ fn hex_to_little_endian(hex_number: &str) -> String {
hex::encode(little_endian_bytes)
}

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

let version = format!("{:08x}", data["version"].as_u64().unwrap());
Expand All @@ -25,21 +25,38 @@ fn create_transaction(data: serde_json::Value) -> 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;

for input in data["vin"].as_array().unwrap() {
let prev_txid = input["txid"].as_str().unwrap();
raw_transaction += &hex_to_little_endian(prev_txid);
if ind == parameter {
println!("index is {}",ind);
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 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_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;
let script_pubkey = input["prevout"]["scriptpubkey"].as_str().unwrap();
raw_transaction += script_pubkey;

raw_transaction += "ffffffff";
}
else {
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);

raw_transaction += "ffffffff";
raw_transaction += "00";

raw_transaction += "ffffffff";
}
ind = ind + 1;
}

let output_count = format!("{:02x}", data["vout"].as_array().unwrap().len());
Expand Down Expand Up @@ -75,36 +92,45 @@ fn create_transaction(data: serde_json::Value) -> String {
}

fn main() {
let mut f = File::open("../mempool/0ac528562a1626863c0cb912eb725530c54e786e6485380c16633e4b9bce1720.json").unwrap();
let mut f = File::open("../mempool/2a11dfa8a9c3ee8950a4c2328306dc4b3643ecaa737bd85e019a236532d65e6a.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 hex= create_transaction(data.clone());
let hash_hex: &str = hex.as_str();
let input_count = data["vin"].as_array().unwrap().len();

for i in 0..input_count {
let hex= create_transaction(data.clone(),i as u64);
// If scriptSigtype!="p2pkh", continue
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 = &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();
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("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!"),
}
}

let secp = Secp256k1::new();

// Decode the signature, public key, and hash
let signature = data["vin"][0]["scriptsig_asm"].as_str().unwrap().split_whitespace().nth(1).unwrap();
let signature = &signature[..signature.len() - 2];
let signature_bytes = decode(signature).expect("Failed to decode signature hex");

let pub_key = data["vin"][0]["scriptsig_asm"].as_str().unwrap().split_whitespace().nth(3).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");

// 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!"),
}
}
2 changes: 1 addition & 1 deletion foo/target/.rustc_info.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"rustc_fingerprint":3873439692611108760,"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":{}}
{"rustc_fingerprint":13052377172341671592,"outputs":{"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":""},"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":""}},"successes":{}}

This file was deleted.

This file was deleted.

This file was deleted.

Binary file modified foo/target/debug/deps/foo-16b23fee036e51ee
Binary file not shown.
Binary file modified foo/target/debug/foo
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"scriptpubkey_asm": "OP_DUP OP_HASH160 OP_PUSHBYTES_20 50eddab2e7b2b4ad23f04bd8f401e7ca22d711ba OP_EQUALVERIFY OP_CHECKSIG",
"scriptpubkey_type": "p2pkh",
"scriptpubkey_address": "18Nuzke4KSAJK3mnFbZZ6Fayu6pN41RGzx",
"value": 13908
"value": 13908
},
"scriptsig": "47304402206e8aed94e46abe5cda427f0aee2f38faaa7041083bd77941d407969aba397860022035ef725fc3f10fbaaf734f1b040b49176f357dadab2bfed3790bb6ba07acff7501210345f308155ac73fb42e128e568395976fdd0d8e801172c1b0e0a0a5abf5b37877",
"scriptsig_asm": "OP_PUSHBYTES_71 304402206e8aed94e46abe5cda427f0aee2f38faaa7041083bd77941d407969aba397860022035ef725fc3f10fbaaf734f1b040b49176f357dadab2bfed3790bb6ba07acff7501 OP_PUSHBYTES_33 0345f308155ac73fb42e128e568395976fdd0d8e801172c1b0e0a0a5abf5b37877",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"scriptpubkey_asm": "OP_DUP OP_HASH160 OP_PUSHBYTES_20 9341816355ad6da1acbccea32ee8f751044b5d76 OP_EQUALVERIFY OP_CHECKSIG",
"scriptpubkey_type": "p2pkh",
"scriptpubkey_address": "1ERcobYYC7Wbz4ZrXDjWn9VxFZvuqoatYP",
"value": 21133801
"value": 21133801
},
"scriptsig": "4730440220577b99864bf8f60b8aae264fd39c0b942fda049f81e92cbc560a4ebde956e84b022072c52e5806ae28f20a28c9f1e2232e6a96267e609065fdd0576107ec4c0ada730121031e3ee2d9675e36214a4f3bcf1496b284fcc044bb028056201f238d83d1a16735",
"scriptsig_asm": "OP_PUSHBYTES_71 30440220577b99864bf8f60b8aae264fd39c0b942fda049f81e92cbc560a4ebde956e84b022072c52e5806ae28f20a28c9f1e2232e6a96267e609065fdd0576107ec4c0ada7301 OP_PUSHBYTES_33 031e3ee2d9675e36214a4f3bcf1496b284fcc044bb028056201f238d83d1a16735",
Expand Down

0 comments on commit 5d1e18b

Please sign in to comment.