Skip to content

Commit

Permalink
Completed p2sh-p2wpkh verification
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushj05 committed Apr 4, 2024
1 parent 4aaae7b commit 09cc37b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Binary file modified bin/main
Binary file not shown.
6 changes: 3 additions & 3 deletions src/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,21 +442,21 @@ int verify_txn(Json::Value &txn) {
valid = p2pkh_verify(scriptPubKeyOps, scriptSigOps, txn);
}
else if (scriptPubKeyType == "p2sh") {
serialized_txn = serialize_segwit_1(txn) + serialize_segwit_inp(inp) + serialize_segwit_2(txn);
serialized_txn = serialize_segwit_1(txn) + serialize_segwit_inp(inp, "p2sh") + serialize_segwit_2(txn);
std::vector<std::string> scriptSigOps = getOps(inp["scriptsig_asm"].asString());
std::vector<std::string> redeemScriptOps = getOps(inp["inner_redeemscript_asm"].asString());
valid = p2sh_verify(scriptPubKeyOps, scriptSigOps, redeemScriptOps, txn, inp);
}
// DONE
else if (scriptPubKeyType == "v0_p2wpkh") {
serialized_txn = serialize_segwit_1(txn) + serialize_segwit_inp(inp) + serialize_segwit_2(txn);
serialized_txn = serialize_segwit_1(txn) + serialize_segwit_inp(inp, "p2wpkh") + serialize_segwit_2(txn);
std::vector<std::string> witness;
for (auto &wtns : inp["witness"])
witness.push_back(wtns.asString());
valid = p2wpkh_verify(scriptPubKeyOps, witness, txn);
}
else if (scriptPubKeyType == "v0_p2wsh") {
serialized_txn = serialize_segwit_1(txn) + serialize_segwit_inp(inp) + serialize_segwit_2(txn);
serialized_txn = serialize_segwit_1(txn) + serialize_segwit_inp(inp, "p2wsh") + serialize_segwit_2(txn);
std::vector<std::string> witness;
for (auto &wtns : inp["witness"])
witness.push_back(wtns.asString());
Expand Down
13 changes: 11 additions & 2 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ using namespace std;
#define IS_UINT32_T 0
#define IS_INT64_T 1

extern string identifyScriptType(std::vector<std::string> &ops);
extern std::vector<std::string> getOps(std::string asmScript);

typedef basic_string<unsigned char> ustring;

uint8_t hex2int(char ch)
Expand Down Expand Up @@ -184,7 +187,7 @@ string serialize_segwit_1 (Json::Value txn) {
}


string serialize_segwit_inp (Json::Value inp) {
string serialize_segwit_inp (Json::Value inp, string scriptType) {
string ser_inp = "";

ser_inp = hexstr2bstr(inp["txid"].asString());
Expand All @@ -193,7 +196,13 @@ string serialize_segwit_inp (Json::Value inp) {

// Need to find the PKH from prevout's scriptpubkey
string scriptcode = "1976a914";
scriptcode += inp["prevout"]["scriptpubkey"].asString().substr(4);
if (scriptType == "p2sh") {
vector<string> ops = getOps(inp["inner_redeemscript_asm"].asString());
if (identifyScriptType(ops) == "p2wpkh")
scriptcode += ops[2];
}
else if (scriptType == "p2wpkh")
scriptcode += inp["prevout"]["scriptpubkey"].asString().substr(4);
scriptcode += "88ac";
ser_inp += hexstr2bstr(scriptcode);

Expand Down

0 comments on commit 09cc37b

Please sign in to comment.