Skip to content

Commit

Permalink
refactor: Move ParseHexUV to bitcoin-tx.cpp and rename it
Browse files Browse the repository at this point in the history
  • Loading branch information
hebasto committed Jul 21, 2023
1 parent d23fda0 commit 6d0fcfe
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
13 changes: 11 additions & 2 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,14 @@ static CAmount AmountFromValue(const UniValue& value)
return amount;
}

static std::vector<unsigned char> ParseHexStr(const std::string& hex, const std::string& name)
{
if (!IsHex(hex)) {
throw std::runtime_error(name + " must be hexadecimal string (not '" + hex + "')");
}
return ParseHex(hex);
}

static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
{
int nHashType = SIGHASH_ALL;
Expand Down Expand Up @@ -620,7 +628,7 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
throw std::runtime_error("vout cannot be negative");

COutPoint out(txid, nOut);
std::vector<unsigned char> pkData(ParseHexUV(prevOut["scriptPubKey"], "scriptPubKey"));
std::vector<unsigned char> pkData(ParseHexStr(prevOut["scriptPubKey"].get_str(), "scriptPubKey"));
CScript scriptPubKey(pkData.begin(), pkData.end());

{
Expand All @@ -646,7 +654,8 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
if ((scriptPubKey.IsPayToScriptHash() || scriptPubKey.IsPayToWitnessScriptHash()) &&
prevOut.exists("redeemScript")) {
UniValue v = prevOut["redeemScript"];
std::vector<unsigned char> rsData(ParseHexUV(v, "redeemScript"));
const std::string& script_hex{v.isStr() ? v.getValStr() : ""};
std::vector<unsigned char> rsData(ParseHexStr(script_hex, "redeemScript"));
CScript redeemScript(rsData.begin(), rsData.end());
tempKeystore.AddCScript(redeemScript);
}
Expand Down
1 change: 0 additions & 1 deletion src/core_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
* @see ParseHashV for an RPC-oriented version of this
*/
bool ParseHashStr(const std::string& strHex, uint256& result);
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
int ParseSighashString(const UniValue& sighash);

// core_write.cpp
Expand Down
10 changes: 0 additions & 10 deletions src/core_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,6 @@ bool ParseHashStr(const std::string& strHex, uint256& result)
return true;
}

std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
{
std::string strHex;
if (v.isStr())
strHex = v.getValStr();
if (!IsHex(strHex))
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
return ParseHex(strHex);
}

int ParseSighashString(const UniValue& sighash)
{
int hash_type = SIGHASH_DEFAULT;
Expand Down
6 changes: 0 additions & 6 deletions src/test/fuzz/parse_univalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ FUZZ_TARGET(parse_univalue, .init = initialize_parse_univalue)
(void)ParseHexO(univalue, random_string);
} catch (const UniValue&) {
}
try {
(void)ParseHexUV(univalue, "A");
(void)ParseHexUV(univalue, random_string);
} catch (const UniValue&) {
} catch (const std::runtime_error&) {
}
try {
(void)ParseHexV(univalue, "A");
} catch (const UniValue&) {
Expand Down

0 comments on commit 6d0fcfe

Please sign in to comment.