When and where to add extra information when signing P2SH raw transaction? This post is quite long so in summary: I'm wondering where I can add extra information when signing a raw transaction (in this case a cleartext of a digest). I'm also wondering if Missing inputs (code -25) which I'm getting without having provided the digest and trying to sign anyway is because of that or because of something else.

I am trying to sign a hashed time-lock contract with bitcoind that looks like this:

OP_IF
    [HASHOP] &lt;digest&gt; OP_EQUALVERIFY OP_DUP OP_HASH160 &lt;seller pubkey hash&gt;
OP_ELSE
    &lt;num&gt; [TIMEOUTOP] OP_DROP OP_DUP OP_HASH160 &lt;buyer pubkey hash&gt;
OP_ENDIF
OP_EQUALVERIFY
OP_CHECKSIG

and like all P2SH this is how you are supposed to add it to the network and sign it:

Pubkey script: OP_HASH160 &lt;Hash160(redeemScript)&gt; OP_EQUAL
Signature script: &lt;sig&gt; [sig] [sig...] &lt;redeemScript&gt;.

So I have sent some bitcoins to the base58-encoded P2SH address and created a raw transaction to the same address as seller pubkey hash. But when I am about to sign it I just can't find where I can put the digest.

According to the signrawtransaction RPC call: http://chainquery.com/bitcoin-api/signrawtransaction I need to add 

2. "prevtxs"       (string, optional) An json array of previous dependent transaction outputs
     [               (json array of json objects, or 'null' if none provided)
       {
         "txid":"id",             (string, required) The transaction id
         "vout":n,                  (numeric, required) The output number
         "scriptPubKey": "hex",   (string, required) script key
         "redeemScript": "hex",   (string, required for P2SH or P2WSH) redeem script
         "amount": value            (numeric, required) The amount spent
       }
       ,...
    ]

which is where I'm guessing I can add the digest. Should it maybe be added to the redeemScript?

Then there is also:

3. "privkeys"     (string, optional) A json array of base58-encoded private keys for signing
    [                  (json array of strings, or 'null' if none provided)
      "privatekey"   (string) private key in base58-encoding
      ,...
    ]

but it seems weird to me that I should have to base58-encode the digest when it should probably be a hex string?

Just in case I don't miss anything important I will also list some of the steps in detail I've done up to this point (all done on regtest).

So I started by creating  a raw transaction to the P2SH address (since sendtoaddress gave me errors about the fee being too high or low):

createrawtransaction "[{\"txid\":\"5f3671ca7d5dea44dd3dd40fad076e76c57189702a4bb92bacb1747017136982\",\"vout\":0}]" "{\"2N5ZRwT42rHFjAtizZPa3Rp7Tn88PcoSzEB\":5, \"n1uGNwJ5nrrhTh4wQX5XKYpxDoZsp3w2fB\":40}"

After signing the raw transaction and decoding it to get the txid I then created another transaction to my own account:

createrawtransaction "[{\"txid\":\"f345938d21bc3fc0966a4f11a0e6b98831640b2cc6cf49bd143758cd95762a1c\",\"vout\":0}]" "{ \"n1uGNwJ5nrrhTh4wQX5XKYpxDoZsp3w2fB\":5}".

Finally I tried signing the transaction with nothing but the fields I knew from prevtxs:

signrawtransaction "02000000011c2a7695cd583714bd49cfc62c0b643188b9e6a0114f6a96c03fbc218d9345f30000000000ffffffff010065cd1d000000001976a914df9ac343e79ae35a727ff757b02e93ac5a4748dd88ac00000000" "[{\"txid\":\"f345938d21bc3fc0966a4f11a0e6b98831640b2cc6cf49bd143758cd95762a1c\",\"vout\":0, \"scriptPubKey\": \"76a9149a57624114b8c67ea007616a210ffe79e711bbd988ac\", \"redeemScript\":\"63a61486b8e067c700620260ad44f108e4add2ebf3c1d28876a91422a29435b1386d8fbc779a72612ccda7401d4aa2675cb27576a9147074ce8ff2f417d6b232d73a789ca09e7c0e8ef36888ac\", \"amount\":5}]".

When trying to sendrawtransaction on this I get error code Missing inputs (code -25). So my final question is: is Missing inputs related with not having provided digest in signrawtransaction or is it because I have missed to add something else?
Bitcoin Core's signrawtransaction RPC does not know about the specific type of script you're trying to sign for, and won't be usable.

You'll need to implement the signing logic yourself.