* 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
| 71 | * pass false, or omit the this argument (defaults to false), for scriptPubKeys. |
| 72 | */ |
| 73 | std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode) |
| 74 | { |
| 75 | std::string str; |
| 76 | opcodetype opcode; |
| 77 | std::vector<unsigned char> vch; |
| 78 | CScript::const_iterator pc = script.begin(); |
| 79 | while (pc < script.end()) { |
| 80 | if (!str.empty()) { |
| 81 | str += " "; |
| 82 | } |
| 83 | if (!script.GetOp(pc, opcode, vch)) { |
| 84 | str += "[error]"; |
| 85 | return str; |
| 86 | } |
| 87 | if (0 <= opcode && opcode <= OP_PUSHDATA4) { |
| 88 | if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) { |
| 89 | str += strprintf("%d", CScriptNum(vch, false).getint()); |
| 90 | } else { |
| 91 | // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature |
| 92 | if (fAttemptSighashDecode && !script.IsUnspendable()) { |
| 93 | std::string strSigHashDecode; |
| 94 | // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig. |
| 95 | // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to |
| 96 | // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the |
| 97 | // checks in CheckSignatureEncoding. |
| 98 | if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, NULL)) { |
| 99 | const unsigned char chSigHashType = vch.back(); |
| 100 | if (mapSigHashTypes.count(chSigHashType)) { |
| 101 | strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]"; |
| 102 | vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode. |
| 103 | } |
| 104 | } |
| 105 | str += HexStr(vch) + strSigHashDecode; |
| 106 | } else { |
| 107 | str += HexStr(vch); |
| 108 | } |
| 109 | } |
| 110 | } else { |
| 111 | str += GetOpName(opcode); |
| 112 | } |
| 113 | } |
| 114 | return str; |
| 115 | } |
| 116 | |
| 117 | std::string EncodeHexTx(const CTransaction& tx, const int serialFlags) |
| 118 | { |