diff --git a/bin/main b/bin/main index ca091fb..0d5b000 100755 Binary files a/bin/main and b/bin/main differ diff --git a/src/crypto.h b/src/crypto.h index e7f082d..127ea14 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -6,7 +6,7 @@ #include #include -int hex2int(char ch) +uint8_t hex2int(char ch) { if (ch >= '0' && ch <= '9') return ch - '0'; diff --git a/src/main.cpp b/src/main.cpp index 2715561..c8fe4b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,28 +10,28 @@ using namespace std; int main() { // vector 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 s; // for (auto &txn : transactions) { @@ -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; diff --git a/src/script.h b/src/script.h index 57d430b..4af6c70 100644 --- a/src/script.h +++ b/src/script.h @@ -43,9 +43,6 @@ bool p2pkh_verify(std::vector scriptPubKeyOps, std::vector using namespace std; +extern uint8_t hex2int(char ch); + #define IS_UINT32_T 0 #define IS_INT64_T 1 @@ -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 = ""; @@ -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());