* 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 signat
| 92 | * pass false, or omit the this argument (defaults to false), for scriptPubKeys. |
| 93 | */ |
| 94 | string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode, bool f64BitNums) |
| 95 | { |
| 96 | string str; |
| 97 | opcodetype opcode; |
| 98 | vector<uint8_t> vch; |
| 99 | CScript::const_iterator pc = script.begin(); |
| 100 | while (pc < script.end()) |
| 101 | { |
| 102 | if (!str.empty()) |
| 103 | { |
| 104 | str += " "; |
| 105 | } |
| 106 | if (!script.GetOp(pc, opcode, vch)) |
| 107 | { |
| 108 | str += "[error]"; |
| 109 | return str; |
| 110 | } |
| 111 | size_t const maxScriptNumSize = |
| 112 | f64BitNums ? CScriptNum::MAXIMUM_ELEMENT_SIZE_64_BIT : CScriptNum::MAXIMUM_ELEMENT_SIZE_32_BIT; |
| 113 | if (0 <= opcode && opcode <= OP_PUSHDATA4) |
| 114 | { |
| 115 | if (vch.size() <= maxScriptNumSize) |
| 116 | { |
| 117 | str += strprintf("%d", CScriptNum(vch, false, maxScriptNumSize).getint64()); |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a |
| 122 | // signature |
| 123 | if (fAttemptSighashDecode && !script.IsUnspendable()) |
| 124 | { |
| 125 | string strSigHashDecode; |
| 126 | // goal: only attempt to decode a defined sighash type from data that looks like a signature within |
| 127 | // a scriptSig. |
| 128 | // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to |
| 129 | // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous |
| 130 | // with the |
| 131 | // checks in CheckSignatureEncoding. |
| 132 | if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) |
| 133 | { |
| 134 | const unsigned char chSigHashType = vch.back(); |
| 135 | if (mapSigHashTypes.count(chSigHashType)) |
| 136 | { |
| 137 | strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]"; |
| 138 | vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode. |
| 139 | } |
| 140 | } |
| 141 | str += HexStr(vch) + strSigHashDecode; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | str += HexStr(vch); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | str += GetOpName(opcode); |