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

"       ,...\n"
            "     ]\n"
            "2. \"outputs\"               (object, required) a json object with outputs\n"
            "    {\n"
            "      \"address\": x.xxx,    (numeric or string, required) The key is the bitcoin address, the numeric value (can be string) is the " + CURRENCY_UNIT + " amount\n"
            "      \"data\": \"hex\"      (string, required) The key is \"data\", the value is hex encoded data\n"
            "      ,...\n"
            "    }\n"
            "3. locktime                  (numeric, optional, default=0) Raw locktime. Non-0 value also locktime-activates inputs\n"
            "4. replaceable               (boolean, optional, default=false) Marks this transaction as BIP125 replaceable.\n"
            "                             Allows this transaction to be replaced by a transaction with higher fees. If provided, it is an error if explicit sequence numbers are incompatible.\n"
            "\nResult:\n"
            "\"transaction\"              (string) hex string of the transaction\n"
            "\nExamples:\n"
            + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"{\\\"address\\\":0.01}\"")
            + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"{\\\"data\\\":\\\"00010203\\\"}\"")
            + HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"address\\\":0.01}\"")
            + HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"data\\\":\\\"00010203\\\"}\"")
        );
    RPCTypeCheck(request.params, {UniValue::VARR, UniValue::VOBJ, UniValue::VNUM, UniValue::VBOOL}, true);
    if (request.params[0].isNull() || request.params[1].isNull())
        throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, arguments 1 and 2 must be non-null");
    UniValue inputs = request.params[0].get_array();
    UniValue sendTo = request.params[1].get_obj();
    CMutableTransaction rawTx;
    if (!request.params[2].isNull()) {
        int64_t nLockTime = request.params[2].get_int64();
        if (nLockTime < 0 || nLockTime > std::numeric_limits<uint32_t>::max())
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
        rawTx.nLockTime = nLockTime;