I'm working on a [paper multisig system](https://glacierprotocol.org), 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](https://pastebin.com/2KLHkV9Y) shows the problem. The final `signrawtransaction` step should succeed. [[output]](https://pastebin.com/k5wwERLG)

# 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](https://pastebin.com/MMJhWCfP) seems to work [[output]](https://pastebin.com/M5gw7FkG) 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

{
    if (request.fHelp || request.params.size() < 1 || request.params.size() > 3)
        throw std::runtime_error(
            "getrawtransaction \"txid\" ( verbose \"blockhash\" )\n"
            "\nNOTE: By default this function only works for mempool transactions. If the -txindex option is\n"
            "enabled, it also works for blockchain transactions. If the block which contains the transaction\n"
            "is known, its hash can be provided even for nodes without -txindex. Note that if a blockhash is\n"
            "provided, only that block will be searched and if the transaction is in the mempool or other\n"
            "blocks, or if this node does not have the given block available, the transaction will not be found.\n"
            "DEPRECATED: for now, it also works for transactions with unspent outputs.\n"
            "\nReturn the raw transaction data.\n"
            "\nIf verbose is 'true', returns an Object with information about 'txid'.\n"
            "If verbose is 'false' or omitted, returns a string that is serialized, hex-encoded data for 'txid'.\n"
            "\nArguments:\n"
            "1. \"txid\"      (string, required) The transaction id\n"
            "2. verbose     (bool, optional, default=false) If false, return a string, otherwise return a json object\n"
            "3. \"blockhash\" (string, optional) The block in which to look for the transaction\n"
            "\nResult (if verbose is not set or set to false):\n"
            "\"data\"      (string) The serialized, hex-encoded data for 'txid'\n"
            "\nResult (if verbose is set to true):\n"
            "{\n"
            "  \"in_active_chain\": b, (bool) Whether specified block is in the active chain or not (only present with explicit \"blockhash\" argument)\n"
            "  \"hex\" : \"data\",       (string) The serialized, hex-encoded data for 'txid'\n"
            "  \"txid\" : \"id\",        (string) The transaction id (same as provided)\n"
            "  \"hash\" : \"id\",        (string) The transaction hash (differs from txid for witness transactions)\n"
            "  \"size\" : n,             (numeric) The serialized transaction size\n"
            "  \"vsize\" : n,            (numeric) The virtual transaction size (differs from size for witness transactions)\n"
            "  \"version\" : n,          (numeric) The version\n"