* Create the assembly string representation of a CScript object. * @param[in] script CScript object to convert into the asm string representation. * @param[in] fAttemptSighashDecode Whether to attempt to decode sighash types on data within the script that matches the format * of a signature. Only pass true for scripts you believe could contain signature
| 121 | * pass false, or omit the this argument (defaults to false), for scriptPubKeys. |
| 122 | */ |
| 123 | std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode) |
| 124 | { |
| 125 | std::string str; |
| 126 | opcodetype opcode; |
| 127 | std::vector<unsigned char> vch; |
| 128 | CScript::const_iterator pc = script.begin(); |
| 129 | while (pc < script.end()) { |
| 130 | if (!str.empty()) { |
| 131 | str += " "; |
| 132 | } |
| 133 | if (!script.GetOp(pc, opcode, vch)) { |
| 134 | str += "[error]"; |
| 135 | return str; |
| 136 | } |
| 137 | if (0 <= opcode && opcode <= OP_PUSHDATA4) { |
| 138 | if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) { |
| 139 | str += strprintf("%d", CScriptNum(vch, false).getint()); |
| 140 | } else { |
| 141 | // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature |
| 142 | if (fAttemptSighashDecode && !script.IsUnspendable()) { |
| 143 | std::string strSigHashDecode; |
| 144 | // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig. |
| 145 | // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to |
| 146 | // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the |
| 147 | // checks in CheckSignatureEncoding. |
| 148 | if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) { |
| 149 | const unsigned char chSigHashType = vch.back(); |
| 150 | const auto it = mapSigHashTypes.find(chSigHashType); |
| 151 | if (it != mapSigHashTypes.end()) { |
| 152 | strSigHashDecode = "[" + it->second + "]"; |
| 153 | vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode. |
| 154 | } |
| 155 | } |
| 156 | str += HexStr(vch) + strSigHashDecode; |
| 157 | } else { |
| 158 | str += HexStr(vch); |
| 159 | } |
| 160 | } |
| 161 | } else { |
| 162 | str += GetOpName(opcode); |
| 163 | } |
| 164 | } |
| 165 | return str; |
| 166 | } |
| 167 | |
| 168 | std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags) |
| 169 | { |