Skip to content

rpc: print P2WSH and P2SH redem Script in (get/decode)rawtransaction#31252

Open
polespinasa wants to merge 2 commits intobitcoin:masterfrom
polespinasa:p2wsh_redeem
Open

rpc: print P2WSH and P2SH redem Script in (get/decode)rawtransaction#31252
polespinasa wants to merge 2 commits intobitcoin:masterfrom
polespinasa:p2wsh_redeem

Conversation

@polespinasa
Copy link
Member

@polespinasa polespinasa commented Nov 7, 2024

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.

$ bitcoin-cli getrawtransaction 44f5ab666cb9dccef69120fe5b51757a647c1f96882931a9faa0b2b5781ddfd2 2 000000000000000000018fa64b5e32335f7d3ac14b9e5553db20f2909c902c34
{
  "in_active_chain": true,
  "txid": "44f5ab666cb9dccef69120fe5b51757a647c1f96882931a9faa0b2b5781ddfd2",
  "hash": "53836d2a4fb1e14356eac69fe1dad8be5dfd9a1b8101bc8aeb04ea2a320aa398",
  "version": 1,
  "size": 382,
  "vsize": 192,
  "weight": 766,
  "locktime": 0,
  "vin": [
    {
      "txid": "d15236206fbe06d6f38505656a2006a9a5a1ddf189a2647750091fd218d59581",
      "vout": 2,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "",
        "304402204d91eb67d798d071278ea0570973f3488d60c3ec98a611856f6a813b2e35df3202200bb36403ebd2dcc96b236efaf07db4f1da26b1031fc854548634ff44a340dc6601",
        "3044022046abaa75082c2d1cdef57f8ef59a0046729220eabe2c2a229d1d592b89ccf1b0022028954703f13d836b1b77acd1eb72639a7a918d756fdd3ddef1d42a28567403ed01",
        "522102f69779f4bb466a8c0aa7d6bf2a2dde34e44bae29d5186ee0c3bed4d903d57fa7210333c337637453164d5fd0dc31787a614a74d2acca5c08710937b9b831bd41b11f2103be7c86e40ad380b33f9abd653c6d04b95b818abd5653b63402678fcc45a017e753ae"
      ],
      "witnessScript": {
        "asm": "2 02f69779f4bb466a8c0aa7d6bf2a2dde34e44bae29d5186ee0c3bed4d903d57fa7 0333c337637453164d5fd0dc31787a614a74d2acca5c08710937b9b831bd41b11f 03be7c86e40ad380b33f9abd653c6d04b95b818abd5653b63402678fcc45a017e7 3 OP_CHECKMULTISIG",
        "desc": "multi(2,02f69779f4bb466a8c0aa7d6bf2a2dde34e44bae29d5186ee0c3bed4d903d57fa7,0333c337637453164d5fd0dc31787a614a74d2acca5c08710937b9b831bd41b11f,03be7c86e40ad380b33f9abd653c6d04b95b818abd5653b63402678fcc45a017e7)#sll5t8hh",
        "type": "multisig"
      },
      "prevout": {
        ... bla bla bla prev output info ...
        }
      },
      "sequence": 4294967293
    }
  ],
  "vout": [ ... bla bla bla outputs ...  ],
  "fee": 0.00000594,
  "hex": "010000000001018195d518d21f09507764a289f1dda1a5a906206a650585f3d606be6f203652d10200000000fdffffff025b730500000000001976a914381d46badae879d1c6037a03da52074e3914812c88ac38088000000000002200205ab691555ede8432ed7765585edb1c9b285532daf639e3e5ccd4843ff3b04cdf040047304402204d91eb67d798d071278ea0570973f3488d60c3ec98a611856f6a813b2e35df3202200bb36403ebd2dcc96b236efaf07db4f1da26b1031fc854548634ff44a340dc6601473044022046abaa75082c2d1cdef57f8ef59a0046729220eabe2c2a229d1d592b89ccf1b0022028954703f13d836b1b77acd1eb72639a7a918d756fdd3ddef1d42a28567403ed0169522102f69779f4bb466a8c0aa7d6bf2a2dde34e44bae29d5186ee0c3bed4d903d57fa7210333c337637453164d5fd0dc31787a614a74d2acca5c08710937b9b831bd41b11f2103be7c86e40ad380b33f9abd653c6d04b95b818abd5653b63402678fcc45a017e753ae00000000",
  "blockhash": "000000000000000000018fa64b5e32335f7d3ac14b9e5553db20f2909c902c34",
  "confirmations": 1,
  "time": 1746727602,
  "blocktime": 1746727602
}

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 7, 2024

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.

Type Reviewers
Concept ACK murchandamus

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34764 (rpc: replace ELISION references with explicit result fields by satsfy)
  • #34683 (RFC: Support a format description of our JSON-RPC interface by willcl-ark)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • deduce -> deduct [The comment "# deduce fee (must be higher than the min relay fee)" should use "deduct" to indicate subtracting the fee; "deduce" means infer and changes the meaning.]

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • ScriptToUniv(script.first, dScript, false, true) in src/core_io.cpp

2026-01-20

@polespinasa polespinasa changed the title rpc: print P2WSH redeemScript in getrawtransaction rpc: print P2WSH witScript in getrawtransaction Nov 7, 2024
@polespinasa polespinasa marked this pull request as draft November 7, 2024 20:52
@DrahtBot DrahtBot removed the CI failed label Nov 8, 2024
@polespinasa polespinasa marked this pull request as ready for review November 8, 2024 19:18
@kevkevinpal
Copy link
Contributor

can we squash 66ea8941de3dfd498319c6390655a165f6a5a92b and 84fb299de72ca9e08fe5029380a779d57d302347
to a single commit

Copy link
Contributor

@naiyoma naiyoma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@polespinasa polespinasa changed the title rpc: print P2WSH witScript in getrawtransaction rpc: print P2WSH and P2SH redem Script in getrawtransaction Jan 27, 2025
@polespinasa
Copy link
Member Author

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 🫡

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the value of have_undo is always going to be 0 when calling getrawtransaction hence the reason why this only works for getblock..2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a suggestion to replace your current approach, but rather an alternative way. I have an implementation here ->naiyoma@ff82e55 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.

Copy link
Member Author

@polespinasa polespinasa May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@DrahtBot
Copy link
Contributor

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

Hints

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.

@DrahtBot
Copy link
Contributor

DrahtBot commented Apr 1, 2025

Are you still working on this?

@DrahtBot DrahtBot marked this pull request as draft April 11, 2025 11:52
@polespinasa polespinasa marked this pull request as ready for review May 8, 2025 16:21
@polespinasa polespinasa force-pushed the p2wsh_redeem branch 5 times, most recently from 3791c4d to 58ac220 Compare May 8, 2025 17:24
@polespinasa
Copy link
Member Author

polespinasa commented May 8, 2025

Rebased on top of master 58ac220e857da32401a7291f77b4b8a864e80ebb

@Sjors maybe you would like to review this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a non-P2SH transaction, returning -1 will result in a redeemScript field being added.
I tested this using a P2PKH transaction, and this was the output: "redeemScript": { "asm": "304402...7981", "type": "nonstandard" }"

Copy link
Member Author

@polespinasa polespinasa Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!
You're right, solved in 18c513f8babf65ea73ea9c785fba90446ef947db.
Now non-P2SH and non-segwit inputs return -2 and don't show any script as it's out the scope of the PR.

@polespinasa polespinasa force-pushed the p2wsh_redeem branch 3 times, most recently from 18c513f to 2318af9 Compare September 16, 2025 20:26
@murchandamus
Copy link
Member

Concept ACK

@polespinasa
Copy link
Member Author

Rebased to resolve conflicts with #32517, only moved DecodeTxDoc from rawtransaction.cpp to rawtransaction_util.cpp

@polespinasa polespinasa changed the title rpc: print P2WSH and P2SH redem Script in getrawtransaction rpc: print P2WSH and P2SH redem Script in (get/decode)rawtransaction Jan 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants