Skip to content

Commit

Permalink
Rectified p2pkh verification
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushj05 committed Mar 25, 2024
1 parent 8203b30 commit 35574c3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
Binary file modified bin/main
Binary file not shown.
2 changes: 1 addition & 1 deletion src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <openssl/ripemd.h>
#include <openssl/sha.h>

int hex2int(char ch)
uint8_t hex2int(char ch)
{
if (ch >= '0' && ch <= '9')
return ch - '0';
Expand Down
60 changes: 30 additions & 30 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ using namespace std;
int main() {
// vector<Json::Value> transactions; // Vector to store all transactions

// Iterate through all files in the mempool directory and read the transactions
for (auto &entry : filesystem::directory_iterator("mempool")) {
ifstream txn_file(entry.path(), ifstream::binary);

std::ostringstream tmp;
tmp << txn_file.rdbuf();
std::string txn_str = tmp.str();

Json::Value txn;
Json::Reader reader;
reader.parse(txn_str.c_str(), txn);

bool check = true;
for (auto &inp : txn["vin"]) {
if (inp["prevout"]["scriptpubkey_type"] != "p2pkh"){
check = false;
break;
}
}
if (check) cout << verify_txn(txn, txn_str) << '\n' << endl;
// transactions.push_back(txn);
}
// // Iterate through all files in the mempool directory and read the transactions
// for (auto &entry : filesystem::directory_iterator("mempool")) {
// ifstream txn_file(entry.path(), ifstream::binary);

// std::ostringstream tmp;
// tmp << txn_file.rdbuf();
// std::string txn_str = tmp.str();

// Json::Value txn;
// Json::Reader reader;
// reader.parse(txn_str.c_str(), txn);

// bool check = true;
// for (auto &inp : txn["vin"]) {
// if (inp["prevout"]["scriptpubkey_type"] != "p2pkh"){
// check = false;
// break;
// }
// }
// if (check) cout << verify_txn(txn, txn_str) << '\n' << endl;
// // transactions.push_back(txn);
// }

// set<string> s;
// for (auto &txn : transactions) {
Expand All @@ -55,17 +55,17 @@ int main() {



// ifstream txn_file("mempool/ff907975dc0cfa299e908e5fba6df56c764866d9a9c22828824c28b8e4511320.json");
ifstream txn_file("mempool/ff907975dc0cfa299e908e5fba6df56c764866d9a9c22828824c28b8e4511320.json");

// std::ostringstream tmp;
// tmp << txn_file.rdbuf();
// std::string txn_str = tmp.str();
std::ostringstream tmp;
tmp << txn_file.rdbuf();
std::string txn_str = tmp.str();

// Json::Value txn;
// Json::Reader reader;
// reader.parse(txn_str.c_str(), txn);
Json::Value txn;
Json::Reader reader;
reader.parse(txn_str.c_str(), txn);

// cout << verify_txn(txn, txn_str) << endl;
cout << verify_txn(txn, txn_str) << endl;



Expand Down
3 changes: 0 additions & 3 deletions src/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ bool p2pkh_verify(std::vector<std::string> scriptPubKeyOps, std::vector<std::str
rpmd160((char*) sha, (char*) rpmd);

if ((char*)rpmd == pkh) {
// Remove scriptsig and scriptsig_asm from txn
cout << "Entered" << endl;

// Clear scriptsig and scriptsig_asm from transaction
// int idx = txn_str.find("\"scriptsig\"");
// txn_str = txn_str.substr(0, idx + 14) + txn_str.substr(idx + 228);
Expand Down
23 changes: 15 additions & 8 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <jsoncpp/json/json.h>
using namespace std;

extern uint8_t hex2int(char ch);

#define IS_UINT32_T 0
#define IS_INT64_T 1

Expand All @@ -30,6 +32,13 @@ string bstr2hexstr(string byte_string){
return hex_string;
}

string hexstr2bstr (string hex_string) {
string byte_string = "";
for (int i = 0; i < hex_string.length(); i+=2)
byte_string.push_back((hex2int(hex_string[i]) << 4) + hex2int(hex_string[i+1]));
return byte_string;
}

// Convert integer to hex string (binary format)
string int2hex (int64_t num, int type = IS_UINT32_T) {
string hex = "";
Expand Down Expand Up @@ -95,26 +104,24 @@ string sertialize_txn (Json::Value txn, bool isCleared = false) {

for (auto &inp : txn["vin"]) {
// outpoint
ser_txn += inp["txid"].asString();
ser_txn += hexstr2bstr(inp["txid"].asString());
ser_txn += int2hex(inp["vout"].asUInt());

if (isCleared)
ser_txn += int2compact(0);
else {
ser_txn += int2compact(inp["scriptSig"].asString().size());
ser_txn += inp["scriptSig"].asString();
ser_txn += int2compact(inp["scriptsig"].asString().length()/2);
ser_txn += hexstr2bstr(inp["scriptsig"].asString());
}
// ser_txn += int2hex(inp["sequence"].asUInt()); // DOUBTFUL
for (int i = 0; i < 4; i++)
ser_txn.push_back(255);
ser_txn += int2hex(inp["sequence"].asUInt());
}

ser_txn += int2compact(txn["vout"].size());

for (auto &out : txn["vout"]) {
ser_txn += int2hex(out["value"].asInt64(), IS_INT64_T);
ser_txn += int2compact(out["scriptPubKey"].asString().size());
ser_txn += out["scriptPubKey"].asString();
ser_txn += int2compact(out["scriptpubkey"].asString().length()/2);
ser_txn += hexstr2bstr(out["scriptpubkey"].asString());
}

ser_txn += int2hex(txn["locktime"].asUInt());
Expand Down

0 comments on commit 35574c3

Please sign in to comment.