Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: djuser on April 07, 2014, 10:05:05 PM



Title: Using SIGHASH_ANYONECANPAY
Post by: djuser on April 07, 2014, 10:05:05 PM
Hi,

I'm rather new to Bitcoin Protocol Development and would like to thank you in advance of any help you can offer me.

Essentially, I would like make use of SIGHASH_ANYONECANPAY hash type.

Currently, after following Ken Shirriff's blog entry on http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html (http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html), I was able to get a simple transaction working which is send from one input to multiple outputs. In addition, after doing some additional research and reading, I was able to extend the knowledge and get multiple inputs and multiple outputs working.

Right now, I'm at a loss at how to implement SIGHASH_ANYONECANPAY hash type and was wondering if someone can offer me where to look in terms of sample transaction script or code. I have taken a look at the contracts wiki but still unsure about the implementation steps.

My attempts at SIGHASH_ANYONECANPAY (assuming two inputs and one output) has been the following:

- Signing the first input with SIGHASH_ALL==> ("01") and second input with SIGHASH_ALL ("01") | SIGHASH_ANYONECANPAY ("80") ==> ("81")
- Here's the breakdown of my current signed transaction that's not being accepted by the TESTNET3:
I have highlighted in RED the SIGHASH_ALL==> ("01") and SIGHASH_ALL ("01") | SIGHASH_ANYONECANPAY ("80") ==> ("81")
https://s3.amazonaws.com/research.btc/btcmisc/TxBreakdown.png

Once again, any help would be greatly appreciated!

Thanks!


Title: Re: Using SIGHASH_ANYONECANPAY
Post by: kjj on April 08, 2014, 05:38:06 PM
When doing SIGHASH_ALL, each input script other than the current one is set to 0x00.  With SIGHASH_ANYONECANPAY, the other inputs are removed completely.

Are you creating the transaction with both inputs first, then calculating both signatures?  Or adding the ANYONECANPAY second input later, after the ALL has been signed?


Title: Re: Using SIGHASH_ANYONECANPAY
Post by: djuser on April 08, 2014, 07:35:05 PM
Hi kjj,

Thanks for replying.

Given the two inputs and one output, I'm doing the following:

Yes, with SIGHASH_ALL, I'm currently using 0x00 to replace the second input script (not the current one). Also, with SIGHASH_ALL | SIGHASH_ANYONECANPAY, I removed all of the first input completely.

Now, when creating the transaction, first, I'm calculating the signature based on the first input using SIGHASH_ALL (which is the raw transaction that includes the first input script and the second input script set to 0x00).

Then I'm adding the second signature using SIGHASH_ANYONECANPAY after the SIGHASH_ALL has been signed (which is the raw transaction that only includes the second input script using the ANYONECANPAY).

Thanks again

When doing SIGHASH_ALL, each input script other than the current one is set to 0x00.  With SIGHASH_ANYONECANPAY, the other inputs are removed completely.

Are you creating the transaction with both inputs first, then calculating both signatures?  Or adding the ANYONECANPAY second input later, after the ALL has been signed?


Title: Re: Using SIGHASH_ANYONECANPAY
Post by: djuser on April 10, 2014, 02:50:47 PM
I still have not figured out what might be wrong. Does anyone have any suggestions on how to debug or what I might be doing wrong?

Thanks in advanced!


Title: Re: Using SIGHASH_ANYONECANPAY
Post by: djuser on April 12, 2014, 05:39:46 AM
Found the problem, it was because I was adding the wrong hash type code to the end of the trailing raw transaction prior to double hashing, signing, etc. I will post my detailed steps when I have some extra time and hopefully it will benefit others experiencing the same problem.

Cheers!