<p>Bitcoin Core's <code>signrawtransaction</code> RPC does not know about the specific type of script you're trying to sign for, and won't be usable.</p>

<p>You'll need to implement the signing logic yourself.</p>

CC BY-SA 3.0
2018-03-25T10:07:52.480Z
Ok, that's too bad... A couple of questions: is there any other client that supports this kind of transaction more easily? If I were to try implement this, how much work would you expect it to entail?
Jesper
CC BY-SA 3.0
2018-03-25T17:52:26.593Z
It isn't generally possible, I believe, or at least very hard. You can't just look at a script and know how you can satisfy it.
Pieter Wuille
CC BY-SA 3.0
2018-03-25T20:44:50.043Z
Ok, so then a lot of the P2SH functionality hasn't actually been implemented in its entirety? Just seems weird because it is mentioned in a lot of places, like here: https://en.bitcoin.it/wiki/Hashed_Timelock_Contracts but they also talk about setting up payment channels which I guess must be something outside of the network like the _Lightning Network_ then?
Jesper
CC BY-SA 3.0
2018-03-25T20:59:17.610Z
@Jestin "implemented in its entirety" is meaningless; scripts aren't a goal on themselves but a means to accomplish certain higher level goals. If your application needs an unusual script then yes you'll need to implement a signer for that script.
Pieter Wuille
CC BY-SA 3.0
2018-03-25T21:18:31.160Z
I think you need to distinguish P2SH (as a blockchain validation rule) and P2SH implementations in wallets. Since P2SH can be used to send to *arbitrary scripts* it is impossible to have wallet software that can sign for every possible script. That's no different from wondering since we have CPUs that can run arbitrary software, why haven't we solved cancer yet using simulations (as CPUs can run arbitrary simulations!). Someone still needs to implement it.
Pieter Wuille
CC BY-SA 3.0
2018-03-24T20:39:43.683Z
2018-03-24T20:39:43.683Z
Pieter Wuille
<p><strong>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 <em>digest</em>). I'm also wondering if <code>Missing inputs (code -25)</code> which I'm getting without having provided the digest and trying to sign anyway is because of that or because of something else.</strong></p>

<p>I am trying to sign a <em>hashed time-lock contract</em> with bitcoind that looks like this:</p>

<p><code>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</code></p>

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

<p><code>Pubkey script: OP_HASH160 &lt;Hash160(redeemScript)&gt; OP_EQUAL
Signature script: &lt;sig&gt; [sig] [sig...] &lt;redeemScript&gt;</code>.</p>

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

<p>According to the signrawtransaction RPC call: <a href="http://chainquery.com/bitcoin-api/signrawtransaction" rel="nofollow noreferrer">http://chainquery.com/bitcoin-api/signrawtransaction</a> I need to add </p>

<p><code>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
       }
       ,...
    ]</code></p>

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

<p>Then there is also:</p>

<p><code>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
      ,...
    ]</code></p>

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

<p>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).</p>

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

<p><code>createrawtransaction "[{\"txid\":\"5f3671ca7d5dea44dd3dd40fad076e76c57189702a4bb92bacb1747017136982\",\"vout\":0}]" "{\"2N5ZRwT42rHFjAtizZPa3Rp7Tn88PcoSzEB\":5, \"n1uGNwJ5nrrhTh4wQX5XKYpxDoZsp3w2fB\":40}"
</code>
After signing the raw transaction and decoding it to get the <code>txid</code> I then created another transaction to my own account:</p>

<p><code>createrawtransaction "[{\"txid\":\"f345938d21bc3fc0966a4f11a0e6b98831640b2cc6cf49bd143758cd95762a1c\",\"vout\":0}]" "{ \"n1uGNwJ5nrrhTh4wQX5XKYpxDoZsp3w2fB\":5}"</code>.</p>

<p>Finally I tried signing the transaction with nothing but the fields I knew from <code>prevtxs</code>:</p>

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

<p>When trying to <code>sendrawtransaction</code> on this I get error code <code>Missing inputs (code -25)</code>. So my final question is: is <code>Missing inputs</code> related with not having provided <code>digest</code> in <code>signrawtransaction</code> or is it because I have missed to add something else?</p>

<bitcoind><p2sh><createrawtransaction><signrawtransaction>
When and where to add extra information when signing P2SH raw transaction?
Jesper
# When and where to add extra information when signing P2SH raw transaction?

(Asked by: username_0 on 2018-03-24)

**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] <digest> OP_EQUALVERIFY OP_DUP OP_HASH160 <seller pubkey hash>
OP_ELSE
<num> [TIMEOUTOP] OP_DROP OP_DUP OP_HASH160 <buyer pubkey hash>
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 <Hash160(redeemScript)> OP_EQUAL
Signature script: <sig> [sig] [sig...] <redeemScript>`.

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?

**Tags:** bitcoind, p2sh, createrawtransaction, signrawtransaction


---

## 1 answers

### Answer by username_1 on 2018-03-24. Score: 3

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.

> **Comments:**
> **username_0**: Ok, that's too bad... A couple of questions: is there any other client that supports this kind of transaction more easily? If I were to try implement this, how much work would you expect it to entail?  
> **username_1**: It isn't generally possible, I believe, or at least very hard. You can't just look at a script and know how you can satisfy it.  
> **username_0**: Ok, so then a lot of the P2SH functionality hasn't actually been implemented in its entirety? Just seems weird because it is mentioned in a lot of places, like here: https://en.bitcoin.it/wiki/Hashed_Timelock_Contracts but they also talk about setting up payment channels which I guess must be something outside of the network like the _Lightning Network_ then?  
> **username_1**: @Jestin "implemented in its entirety" is meaningless; scripts aren't a goal on themselves but a means to accomplish certain higher level goals. If your application needs an unusual script then yes you'll need to implement a signer for that script.  
> **username_1**: I think you need to distinguish P2SH (as a blockchain validation rule) and P2SH implementations in wallets. Since P2SH can be used to send to *arbitrary scripts* it is impossible to have wallet software that can sign for every possible script. That's no different from wondering since we have CPUs that can run arbitrary software, why haven't we solved cancer yet using simulations (as CPUs can run arbitrary simulations!). Someone still needs to implement it.  

---

