18 #include <boost/algorithm/string/classification.hpp> 19 #include <boost/algorithm/string/replace.hpp> 20 #include <boost/algorithm/string/split.hpp> 28 static std::map<std::string, opcodetype> mapOpNames;
30 if (mapOpNames.empty())
32 for (
unsigned int op = 0; op <= MAX_OPCODE; op++)
39 if (strcmp(
name,
"OP_UNKNOWN") == 0)
41 std::string strName(
name);
42 mapOpNames[strName] =
static_cast<opcodetype>(op);
44 boost::algorithm::replace_first(strName,
"OP_",
"");
45 mapOpNames[strName] =
static_cast<opcodetype>(op);
49 std::vector<std::string> words;
50 boost::algorithm::split(words, s, boost::algorithm::is_any_of(
" \t\n"), boost::algorithm::token_compress_on);
52 for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
58 else if (std::all_of(w->begin(), w->end(),
::IsDigit) ||
59 (w->front() ==
'-' && w->size() > 1 && std::all_of(w->begin()+1, w->end(),
::IsDigit)))
65 else if (w->substr(0,2) ==
"0x" && w->size() > 2 &&
IsHex(std::string(w->begin()+2, w->end())))
68 std::vector<unsigned char> raw =
ParseHex(std::string(w->begin()+2, w->end()));
69 result.
insert(result.
end(), raw.begin(), raw.end());
71 else if (w->size() >= 2 && w->front() ==
'\'' && w->back() ==
'\'')
75 std::vector<unsigned char> value(w->begin()+1, w->end()-1);
78 else if (mapOpNames.count(*w))
81 result << mapOpNames[*w];
85 throw std::runtime_error(
"script parse error");
97 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
98 if (!tx.
vin[i].scriptSig.HasValidOps() || tx.
vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
104 for (
unsigned int i = 0; i < tx.
vout.size(); i++) {
105 if (!tx.
vout[i].scriptPubKey.HasValidOps() || tx.
vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
115 if (!
IsHex(hex_tx)) {
119 std::vector<unsigned char> txData(
ParseHex(hex_tx));
121 if (try_no_witness) {
125 if (ssData.
eof() && (!try_witness || CheckTxScriptsSanity(tx))) {
128 }
catch (
const std::exception&) {
137 if (ssData.
empty()) {
140 }
catch (
const std::exception&) {
150 if (!
IsHex(hex_header))
return false;
152 const std::vector<unsigned char> header_data{
ParseHex(hex_header)};
155 ser_header >> header;
156 }
catch (
const std::exception&) {
164 if (!
IsHex(strHexBlk))
167 std::vector<unsigned char> blockData(
ParseHex(strHexBlk));
172 catch (
const std::exception&) {
181 std::vector<unsigned char> tx_data =
DecodeBase64(base64_tx.c_str());
185 if (!ss_data.empty()) {
186 error =
"extra data after PSBT";
189 }
catch (
const std::exception& e) {
198 if ((strHex.size() != 64) || !
IsHex(strHex))
211 throw std::runtime_error(strName +
" must be hexadecimal string (not '" + strHex +
"')");
219 static std::map<std::string, int> map_sighash_values = {
227 std::string strHashType = sighash.
get_str();
228 const auto& it = map_sighash_values.find(strHashType);
229 if (it != map_sighash_values.end()) {
230 hash_type = it->second;
232 throw std::runtime_error(strHashType +
" is not a valid sighash parameter.");
bool DecodeHexBlockHeader(CBlockHeader &header, const std::string &hex_header)
int ParseSighashString(const UniValue &sighash)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
std::vector< unsigned char > ParseHexUV(const UniValue &v, const std::string &strName)
iterator insert(iterator pos, const T &value)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
const std::string & get_str() const
A version of CTransaction with the PSBT format.
Double ended buffer combining vector and stream-like interfaces.
const std::string & getValStr() const
opcodetype
Script opcodes.
bool IsHex(const std::string &str)
bool DecodeHexTx(CMutableTransaction &tx, const std::string &hex_tx, bool try_no_witness, bool try_witness)
std::vector< CTxOut > vout
const char * GetOpName(opcodetype opcode)
bool DecodePSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
CScript ParseScript(const std::string &s)
Serialized script, used inside transaction inputs and outputs.
int64_t atoi64(const char *psz)
bool error(const char *fmt, const Args &... args)
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
A mutable version of CTransaction.
The basic transaction that is broadcasted on the network and contained in blocks. ...
void SetHex(const char *psz)
bool DecodeHexBlk(CBlock &block, const std::string &strHexBlk)
std::vector< unsigned char > ParseHex(const char *psz)