* 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
| 353 | * pass false, or omit the this argument (defaults to false), for scriptPubKeys. |
| 354 | */ |
| 355 | std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode) |
| 356 | { |
| 357 | std::string str; |
| 358 | opcodetype opcode; |
| 359 | std::vector<unsigned char> vch; |
| 360 | CScript::const_iterator pc = script.begin(); |
| 361 | while (pc < script.end()) { |
| 362 | if (!str.empty()) { |
| 363 | str += " "; |
| 364 | } |
| 365 | if (!script.GetOp(pc, opcode, vch)) { |
| 366 | str += "[error]"; |
| 367 | return str; |
| 368 | } |
| 369 | if (0 <= opcode && opcode <= OP_PUSHDATA4) { |
| 370 | if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) { |
| 371 | str += strprintf("%d", CScriptNum(vch, false).getint()); |
| 372 | } else { |
| 373 | // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature |
| 374 | if (fAttemptSighashDecode && !script.IsUnspendable()) { |
| 375 | std::string strSigHashDecode; |
| 376 | // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig. |
| 377 | // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to |
| 378 | // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the |
| 379 | // checks in CheckSignatureEncoding. |
| 380 | if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) { |
| 381 | const unsigned char chSigHashType = vch.back(); |
| 382 | const auto it = mapSigHashTypes.find(chSigHashType); |
| 383 | if (it != mapSigHashTypes.end()) { |
| 384 | strSigHashDecode = "[" + it->second + "]"; |
| 385 | vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode. |
| 386 | } |
| 387 | } |
| 388 | str += HexStr(vch) + strSigHashDecode; |
| 389 | } else { |
| 390 | str += HexStr(vch); |
| 391 | } |
| 392 | } |
| 393 | } else { |
| 394 | str += GetOpName(opcode); |
| 395 | } |
| 396 | } |
| 397 | return str; |
| 398 | } |
| 399 | |
| 400 | std::string EncodeHexTx(const CTransaction& tx) |
| 401 | { |