signrawtransactionwithkey doesn’t work with non segwit p2sh scripts #32722

issue Christewart openend this issue on June 10, 2025
  1. Christewart commented at 8:09 pm on June 10, 2025: contributor

    Is there an existing issue for this?

    • I have searched the existing issues

    Current behaviour

    I’m attempting to reproduce the transaction specified by AJ Towns that pointed out BitVM bridges with CTV+CSFS for p2sh outputs are vulnerable to theft. You can read more about the topic here.

    The TLDR is i’m attempting to sign a p2sh(p2pk) script.

    I’ve attempted to create this transaction via the signrawtransactionwithkey RPC. I kept receiving an error saying

    { 'error': 'Unable to sign input, invalid stack size (possibly missing key)'}]}

    despite explicitly passing in the public key and redeem script in the prevTxs and keys rpc argument. You can view this here:

    https://github.com/Christewart/bitcoin/blob/ae6d4476b730d0909f88405f9a9c08383611a49f/test/functional/feature_bitvmctvcsfs_bridge.py#L48

    After wandering through the source code, it seems that there is a disconnect between the Solver and creation of the KeyStore inside of ParsePrevOuts.

    The KeyStore attempts to search for the KeyId of the p2sh output script.

    The ParsePrevOuts function only adds the KeyId of the redeemScript for p2sh outputs.

    I’ve modified the code on my feature branch to add both the keyid of the p2sh redeem script and the keyid of the output script to the keystore, and now I at least get a digital signature produced (rather than Unable to sign input, invalid stack size (possibly missing key)).

    https://github.com/Christewart/bitcoin/blob/ae6d4476b730d0909f88405f9a9c08383611a49f/src/rpc/rawtransaction_util.cpp#L251

    From a quick glance through rpc_signrawtransactionwithkey.py it seems that p2sh(p2wsh()) is tested, but AFAICT legacy p2sh is not tested.

    Expected behaviour

    rpc_signrawtransactionwithkey should be able to sign p2pk redeem scripts

    Steps to reproduce

    Run ./build/test/functional/feature_bitvmctvcsfs_bridge.py on this commit: https://github.com/Christewart/bitcoin/commit/ae6d4476b730d0909f88405f9a9c08383611a49f

    and remove this line of code to see the failure:

    https://github.com/Christewart/bitcoin/blob/2025-06-07-ctvcsfs-bitvm/src/rpc/rawtransaction_util.cpp#L251

    Relevant log output

    No response

    How did you obtain Bitcoin Core

    Compiled from source

    What version of Bitcoin Core are you using?

    https://github.com/Christewart/bitcoin/commit/ae6d4476b730d0909f88405f9a9c08383611a49f

    Operating system and version

    Mac

    Machine specifications

    No response

  2. achow101 commented at 10:00 pm on June 10, 2025: member

    Your test has a bug:

    0        script_b = script_to_p2sh_script(redeem_script)
    1        address = script_to_p2sh(script_b)
    

    address is computed incorrectly. script_to-p2sh is like script_to_p2sh_script in that it takes the redeem script. When you pass it script_b, what you’re really creating is a p2sh(p2sh()) which is invalid. I believe if you modify your test to try to broadcast the transaction, it would fail to validate.

  3. Christewart commented at 6:43 pm on June 11, 2025: contributor

    Your test has a bug:

    0        script_b = script_to_p2sh_script(redeem_script)
    1        address = script_to_p2sh(script_b)
    

    address is computed incorrectly. script_to-p2sh is like script_to_p2sh_script in that it takes the redeem script. When you pass it script_b, what you’re really creating is a p2sh(p2sh()) which is invalid. I believe if you modify your test to try to broadcast the transaction, it would fail to validate.

    This is correct. Thank you for pointing that out @achow101 . Much appreciated!

  4. Christewart closed this on Jun 11, 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-06-15 06:13 UTC

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