rpc: print P2WSH and P2SH redem Script in getrawtransaction #31252

pull polespinasa wants to merge 1 commits into bitcoin:master from polespinasa:p2wsh_redeem changing 4 files +67 −1
  1. polespinasa commented at 8:39 pm on November 7, 2024: contributor

    This PR is motivated by #27391. And inspired by a previous PR #8849 that proposed something similar.

    This is an example with a real mainnet transaction (3-5 multisig) and using decoderawtransaction (also works for getrawtransaction 'txid' 2).

    Verbosity must be set to 2.

     0$ bitcoin-cli getrawtransaction 44f5ab666cb9dccef69120fe5b51757a647c1f96882931a9faa0b2b5781ddfd2 2 000000000000000000018fa64b5e32335f7d3ac14b9e5553db20f2909c902c34
     1{
     2  "in_active_chain": true,
     3  "txid": "44f5ab666cb9dccef69120fe5b51757a647c1f96882931a9faa0b2b5781ddfd2",
     4  "hash": "53836d2a4fb1e14356eac69fe1dad8be5dfd9a1b8101bc8aeb04ea2a320aa398",
     5  "version": 1,
     6  "size": 382,
     7  "vsize": 192,
     8  "weight": 766,
     9  "locktime": 0,
    10  "vin": [
    11    {
    12      "txid": "d15236206fbe06d6f38505656a2006a9a5a1ddf189a2647750091fd218d59581",
    13      "vout": 2,
    14      "scriptSig": {
    15        "asm": "",
    16        "hex": ""
    17      },
    18      "txinwitness": [
    19        "",
    20        "304402204d91eb67d798d071278ea0570973f3488d60c3ec98a611856f6a813b2e35df3202200bb36403ebd2dcc96b236efaf07db4f1da26b1031fc854548634ff44a340dc6601",
    21        "3044022046abaa75082c2d1cdef57f8ef59a0046729220eabe2c2a229d1d592b89ccf1b0022028954703f13d836b1b77acd1eb72639a7a918d756fdd3ddef1d42a28567403ed01",
    22        "522102f69779f4bb466a8c0aa7d6bf2a2dde34e44bae29d5186ee0c3bed4d903d57fa7210333c337637453164d5fd0dc31787a614a74d2acca5c08710937b9b831bd41b11f2103be7c86e40ad380b33f9abd653c6d04b95b818abd5653b63402678fcc45a017e753ae"
    23      ],
    24      "witnessScript": {
    25        "asm": "2 02f69779f4bb466a8c0aa7d6bf2a2dde34e44bae29d5186ee0c3bed4d903d57fa7 0333c337637453164d5fd0dc31787a614a74d2acca5c08710937b9b831bd41b11f 03be7c86e40ad380b33f9abd653c6d04b95b818abd5653b63402678fcc45a017e7 3 OP_CHECKMULTISIG",
    26        "desc": "multi(2,02f69779f4bb466a8c0aa7d6bf2a2dde34e44bae29d5186ee0c3bed4d903d57fa7,0333c337637453164d5fd0dc31787a614a74d2acca5c08710937b9b831bd41b11f,03be7c86e40ad380b33f9abd653c6d04b95b818abd5653b63402678fcc45a017e7)#sll5t8hh",
    27        "type": "multisig"
    28      },
    29      "prevout": {
    30        ... bla bla bla prev output info ...
    31        }
    32      },
    33      "sequence": 4294967293
    34    }
    35  ],
    36  "vout": [ ... bla bla bla outputs ...  ],
    37  "fee": 0.00000594,
    38  "hex": "010000000001018195d518d21f09507764a289f1dda1a5a906206a650585f3d606be6f203652d10200000000fdffffff025b730500000000001976a914381d46badae879d1c6037a03da52074e3914812c88ac38088000000000002200205ab691555ede8432ed7765585edb1c9b285532daf639e3e5ccd4843ff3b04cdf040047304402204d91eb67d798d071278ea0570973f3488d60c3ec98a611856f6a813b2e35df3202200bb36403ebd2dcc96b236efaf07db4f1da26b1031fc854548634ff44a340dc6601473044022046abaa75082c2d1cdef57f8ef59a0046729220eabe2c2a229d1d592b89ccf1b0022028954703f13d836b1b77acd1eb72639a7a918d756fdd3ddef1d42a28567403ed0169522102f69779f4bb466a8c0aa7d6bf2a2dde34e44bae29d5186ee0c3bed4d903d57fa7210333c337637453164d5fd0dc31787a614a74d2acca5c08710937b9b831bd41b11f2103be7c86e40ad380b33f9abd653c6d04b95b818abd5653b63402678fcc45a017e753ae00000000",
    39  "blockhash": "000000000000000000018fa64b5e32335f7d3ac14b9e5553db20f2909c902c34",
    40  "confirmations": 1,
    41  "time": 1746727602,
    42  "blocktime": 1746727602
    43}
    
  2. DrahtBot commented at 8:39 pm on November 7, 2024: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31252.

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #32517 (rpc: add “ischange: true” to decoded tx outputs in wallet gettransaction response by pinheadmz)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  3. DrahtBot added the label RPC/REST/ZMQ on Nov 7, 2024
  4. polespinasa renamed this:
    rpc: print P2WSH redeemScript in getrawtransaction
    rpc: print P2WSH witScript in getrawtransaction
    on Nov 7, 2024
  5. polespinasa marked this as a draft on Nov 7, 2024
  6. DrahtBot added the label CI failed on Nov 7, 2024
  7. DrahtBot removed the label CI failed on Nov 8, 2024
  8. polespinasa marked this as ready for review on Nov 8, 2024
  9. in src/core_write.cpp:215 in 66ea8941de outdated
    206@@ -207,6 +207,14 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
    207                 txinwitness.push_back(HexStr(item));
    208             }
    209             in.pushKV("txinwitness", std::move(txinwitness));
    210+            CPubKey pubkey(tx.vin[i].scriptWitness.stack.back());
    211+            if (!pubkey.IsFullyValid()){
    212+                CScript witnessScript(tx.vin[i].scriptWitness.stack.back().begin(), tx.vin[i].scriptWitness.stack.back().end());
    213+                UniValue witScript(UniValue::VOBJ);
    214+                ScriptToUniv(witnessScript, witScript, false, true);
    215+                in.pushKV("witScript", std::move(witScript));
    


    kevkevinpal commented at 3:10 am on November 13, 2024:

    I think it might make more sense to name it “witnessScript” instead of “witScript”, It might be confusing to some if it is abbreviated

    0                in.pushKV("witnessScript", std::move(witScript));
    

    polespinasa commented at 11:36 am on November 13, 2024:
    I agree, it needs to be changed in one more place apart from this line but yes I will apply it. Thanks!
  10. kevkevinpal commented at 3:12 am on November 13, 2024: contributor
    can we squash 66ea8941de3dfd498319c6390655a165f6a5a92b and 84fb299de72ca9e08fe5029380a779d57d302347 to a single commit
  11. polespinasa force-pushed on Nov 13, 2024
  12. polespinasa requested review from kevkevinpal on Nov 13, 2024
  13. in src/core_write.cpp:211 in 4e128d4f9b outdated
    206@@ -207,6 +207,14 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
    207                 txinwitness.push_back(HexStr(item));
    208             }
    209             in.pushKV("txinwitness", std::move(txinwitness));
    210+            CPubKey pubkey(tx.vin[i].scriptWitness.stack.back());
    211+            if (!pubkey.IsFullyValid()){
    


    luke-jr commented at 11:51 pm on November 20, 2024:
    This isn’t an indicator of p2wsh… Maybe use d1f0ff0e1fed2c4124d0d81f18daf7b76a770dda or similar?

    polespinasa commented at 6:29 pm on November 22, 2024:
    code rewritten based on this comment
  14. polespinasa force-pushed on Nov 22, 2024
  15. polespinasa force-pushed on Nov 22, 2024
  16. polespinasa force-pushed on Nov 22, 2024
  17. polespinasa requested review from luke-jr on Nov 26, 2024
  18. naiyoma commented at 12:36 pm on January 11, 2025: contributor
    I’ve tested https://github.com/bitcoin/bitcoin/pull/31252/commits/43dbc696b9e9bb5472b01708d33e022042b5ce3b on regtest, and both (P2SH redeemScript and P2WSH witnessScript )are only being decompiled when calling getblock … 2. I think you should consider editing the PR description to indicate exactly what is working so far.
  19. polespinasa renamed this:
    rpc: print P2WSH witScript in getrawtransaction
    rpc: print P2WSH and P2SH redem Script in getrawtransaction
    on Jan 27, 2025
  20. polespinasa commented at 9:42 am on January 27, 2025: contributor

    I’ve tested 43dbc69 on regtest, and both (P2SH redeemScript and P2WSH witnessScript )are only being decompiled when calling getblock … 2. I think you should consider editing the PR description to indicate exactly what is working so far.

    Thanks for the review and testing! Yes, the description was outdated to the first implementation of it. Updated 🫡

  21. in src/core_write.cpp:217 in 43dbc696b9 outdated
    215             const Coin& prev_coin = txundo->vprevout[i];
    216             const CTxOut& prev_txout = prev_coin.out;
    217 
    218             amt_total_in += prev_txout.nValue;
    219 
    220+            std::pair<CScript, int> script = GetScriptForTransactionInput(prev_txout.scriptPubKey, txin);
    


    naiyoma commented at 8:50 pm on February 11, 2025:
    the value of have_undo is always going to be 0 when calling getrawtransaction hence the reason why this only works for getblock..2

    naiyoma commented at 8:59 am on February 12, 2025:
    This is not a suggestion to replace your current approach, but rather an alternative way. I have an implementation here ->https://github.com/naiyoma/bitcoin/pull/1/commits/ff82e551404e55c75b1950e01e4f4626f5ad452e that shows how to decompile outside of if(have_undo) . While it needs more testing, it successfully works for both getrawtransaction and getblock..2. You might find some ideas there for handlinggetrawtransaction.

    polespinasa commented at 4:24 pm on May 8, 2025:

    Sorry for the delay, got this PR a bit abandoned, re-taking it now :).

    Thanks for the suggestion! The problem is that I don’t see the way to detect the type without the previous transaction. Your patch is similar to my first approach. If you test with a P2SH-P2WPKH, the decoding is done wrong.

  22. in src/core_write.cpp:223 in 43dbc696b9 outdated
    219 
    220+            std::pair<CScript, int> script = GetScriptForTransactionInput(prev_txout.scriptPubKey, txin);
    221+            UniValue redeemScript(UniValue::VOBJ);
    222+            ScriptToUniv(script.first, redeemScript, false, true);
    223+            if (script.second != -2) {
    224+                in.pushKV("redeemScript", std::move(redeemScript));
    


    naiyoma commented at 8:12 am on February 12, 2025:
    this outputs redeemScript for both P2SH and P2WSH, but it should be P2SH redeemScript and P2WSH witnessScript

    polespinasa commented at 4:21 pm on May 8, 2025:
    done
  23. DrahtBot commented at 4:53 pm on March 21, 2025: contributor

    🚧 At least one of the CI tasks failed. Debug: https://github.com/bitcoin/bitcoin/runs/33403307702

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

  24. DrahtBot added the label CI failed on Mar 21, 2025
  25. DrahtBot commented at 2:18 pm on April 1, 2025: contributor
    Are you still working on this?
  26. DrahtBot marked this as a draft on Apr 11, 2025
  27. polespinasa force-pushed on May 8, 2025
  28. polespinasa marked this as ready for review on May 8, 2025
  29. polespinasa force-pushed on May 8, 2025
  30. polespinasa force-pushed on May 8, 2025
  31. polespinasa force-pushed on May 8, 2025
  32. polespinasa force-pushed on May 8, 2025
  33. polespinasa force-pushed on May 8, 2025
  34. Decode P2WSH and P2SH if data from prev tx is available on decodingawtransaction functions 58ac220e85
  35. polespinasa force-pushed on May 8, 2025
  36. polespinasa commented at 5:29 pm on May 8, 2025: contributor
    Rebased on top of master 58ac220e857da32401a7291f77b4b8a864e80ebb @Sjors maybe you would like to review this
  37. polespinasa requested review from naiyoma on May 8, 2025
  38. DrahtBot removed the label CI failed on May 8, 2025
  39. DrahtBot added the label CI failed on May 17, 2025
  40. DrahtBot removed the label CI failed on May 18, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-05-25 18:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me