I’m working on a paper multisig system, improving it to use p2sh-segwit instead of legacy p2sh. I’m using my own Linux build of Bitcoin Core tag v0.16.0rc3 and using bitcoin-cli to build transactions.
I used addmultisigaddress to create a 2-of-4 p2sh-segwit address and funded it on testnet. I created a transaction to spend those coins using createrawtransaction and I’m attempting to sign it.
Actual behavior
If I provide no keys to signrawtransaction and let it use the wallet keys, everything works. But if I provide the keys explicitly, I get the error: “Unable to sign input, invalid stack size (possibly missing key)”.
Expected behavior
I expect signrawtransaction to be able to sign the transaction using the explicltly-provided keys. (For a paper wallet, the bitcoind wallet will not know the keys or the multisig address.)
This flow works as expected when I use type ’legacy’ for the addmultisigaddress. It fails when I use type ‘p2sh-segwit’.
My guess is that it assumes it’s legacy p2sh and therefore the signature fails. Shouldn’t bitcoind be able to figure out (based on the scriptPubKey and the redeemScript) that this is p2sh-segwit, and construct the appropriate signatures? Or is there a different/better way to use the CLI to accomplish this?
To recreate
This script shows the problem. The final signrawtransaction step should succeed. [output]
Ugly workaround
I could use addmultisigaddress to teach the wallet about the p2sh-segwit address, and importprivkey to teach it 2-of-4 keys, then use signrawtransaction without providing any keys. This seems to work [output] but is very ugly because at the time of signing, I only have 2 of 4 keys, so in order to run addmultisigaddress I have to decode the redeemScript and parse its “asm” field to find the 4 pubkeys.
Related
Somewhat related issue, never merged: issue #11693 and its associated PR #11708