1

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?

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.

  • 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 Mar 25 '18 at 10:07
  • 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 Mar 25 '18 at 17:52
  • 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: 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 Mar 25 '18 at 20:44
  • @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 Mar 25 '18 at 20:59
  • 2
    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 Mar 25 '18 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.