Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: jl2012 on October 10, 2012, 02:58:35 AM



Title: Order of inputs/outputs in transaction and order of transactions in block
Post by: jl2012 on October 10, 2012, 02:58:35 AM
Assuming we have an unsigned standard transaction with 2 inputs (A, followed by B) and 2 outputs (C, followed by D). After the input A is signed, is it possible to alter the order of inputs or outputs without voiding the A signature? (Obviously we cannot alter the order of inputs / outputs AFTER the transaction is recorded in the blockchain since that will change the transaction hash and will be regarded as double-spend. My question is about altering the order before the transaction is broadcast, or when it is partially signed)

Similarly, when a correct nonce is found for a block, will changing the order of transactions within the block voiding it? (I guess it will since it should change the block hash. Just want to confirm)

Thanks!


Title: Re: Order of inputs/outputs in transaction and order of transactions in block
Post by: kjj on October 10, 2012, 12:56:31 PM
Depends on the signature flag in the transaction.  If no flag is specified, the signature is calculated based on the entire transaction. *

That means that for a standard transaction, a change in the order of either the inputs or the outputs will break the signatures.

See the hashtype values here (https://en.bitcoin.it/wiki/OP_CHECKSIG) for more information.

And yes, changing the order of the transactions in the block will change the merkle root, which will change the block header and invalidate the hash.

* Technical detail:  the signature fields in the inputs are deleted for the purposes of calculating the signatures.


Title: Re: Order of inputs/outputs in transaction and order of transactions in block
Post by: Mike Hearn on October 10, 2012, 06:02:07 PM
If you want the input signature to be independent of other inputs (so you can re-order or add or delete other inputs) you can sign with SIGHASH_ANYONECANPAY.