0

Say I have this (randomly selected) transaction confirmed:

txprev.txid: 64891cd6a9771adf75b343367850198633d2865df516533f523ce33fdf8c55c2
    txprev.version : 01000000
    txprev.input count : 01
    txprev.input0_txid : ed9dffb6254b960099300838a2619adea6382c793d72c8650f6ffd835a85e6af
    txprev.input0_vout : 00000000
    txprev.input0_ScriptSigSize : 6b
    txprev.input0_ScriptSig: 483045022100f74aa29ac94ba5efe26d25c4982027626b82a4e0ba7feb63469ac9031ae200f202207472fd3e9ebbbbeaa7023326f99cd13a3e8ffd09ab1d2665f596767d01acd90a012102c74ccf231111f2acc0594f070c0f522d25b326a503270637dade5edfa90f123b
    txprev.sequence : ffffffff
    txprev.output_count : 02
    txprev.output0_amount: ae619a0700000000
    txprev.output0_scriptPubKeySize: 19
    txprev.output0_scriptPubKey: 76a91471ba0c0827171ef16859048b5157ef1f5b7d516288ac
    txprev.output1_amount: bfaaf20000000000
    txprev.output1_scriptPubKeySize: 19
    txprev.output1_scriptPubKey: 76a914ec14773ce9ff6e40d60ce8c44f31a0677f8a817088ac
    txprev.locktime: 00000000

And pretend I want to spend txprev.output0 in txnew.input1 of a new transaction and construct the following1:

   txnew.version : 01000000
   txnew.input count : 02
   txnew.input0_txid : fb8d928e7f7bf64a21628f568ec1cc85b5acd4bd30292d96db00e4b4beb1d19b 
   txnew.input0_vout : 01000000
   txnew.input0_ScriptSigSize : 6b
   txnew.input0_ScriptSig: 483045022100c61382dc9b8611bd077d9931685321dd8e48f3a0532873d16d5a00e18647ff2402202bbca562ab2f6e01e55f20d3106dabd8a1208a0276bb552720968902385f1e610121034e6ae18eda57aa491ae3dd0b49b656fbcdf2282e6d47e0241b875d0122602ac3
   txnew.sequence : ffffffff
   txnew.input1_txid: c2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c8964
   txnew.input1_vout: 00000000
   txnew.input1_ScriptSigSize: 6a
   txnew.input1_ScriptSig: 47304402200165a5966fb459ca5f6c4e2b25108b6e187e6ac8e7ca75f302564abd12d37c58022006bd7de403310dd10e0eaade55ca5fbccf24cd3906c0b720096f13db78303175012103345e24df2e1f69f2056fe650c8af607659d28795f355f639f894aafacc6ae2cd
   txnew.input1_sequence: ffffffff
   txnew.output_count : 02
   txnew.output0_amount: d273110800000000
   txnew.output0_scriptPubKeySize: 19
   txnew.output0_scriptPubKey: 76a91406f723b0acdefec3b50e7e318951c69dff77bd5688ac
   txnew.output1_amount: 5f95f40000000000
   txnew.output1_scriptPubKeySize: 19
   txnew.output1_scriptPubKey: 76a9145b639f973c812eb23adda40465acabd336c6092888ac
   txnew.locktime: 00000000

For each of the 6 possible sighashflags / hashTypeCodes (0x01, 0x02, 0x03, 0x81, 0x82, 0x83), what is the exact byte array that would be the signature pre-image for spending txprev.output0in txnew.input1 (leaving txnew.input0 as is)? What would look different in txnew?

1 Based on this

2 Answers 2

1

To create the signature pre-image for spending txprev.output0 in txnew.input1, we need to construct the byte array that includes the necessary data fields and hashTypeCode for each sighashflag. Here's the byte array for each sighashflag (0x01, 0x02, 0x03, 0x81, 0x82, 0x83):

  1. sighashflag 0x01 (SIGHASH_ALL):
<version><prev_txid><prev_vout><prev_output_scriptPubKey><input_sequence><txnew.txid><txnew.vout><txnew.output_scriptPubKey><txnew.amount><sequence><sighashflag>
0100000064891cd6a9771adf75b343367850198633d2865df516533f523ce33fdf8c55c2000000001976a91471ba0c0827171ef16859048b5157ef1f5b7d516288acffffffffc2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c8964000000001976a91406f723b0acdefec3b50e7e318951c69dff77bd5688ac00000000ffffffff00000001
  1. sighashflag 0x02 (SIGHASH_NONE):
<version><prev_txid><prev_vout><prev_output_scriptPubKey><input_sequence><txnew.txid><txnew.vout><txnew.output_scriptPubKey><txnew.amount><sequence><sighashflag>
0100000064891cd6a9771adf75b343367850198633d2865df516533f523ce33fdf8c55c2000000001976a91471ba0c0827171ef16859048b5157ef1f5b7d516288acffffffffc2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c89640100000000ffffffff00000002
  1. sighashflag 0x03 (SIGHASH_SINGLE):
<version><prev_txid><prev_vout><prev_output_scriptPubKey><input_sequence><txnew.txid><txnew.vout><txnew.output_scriptPubKey><txnew.amount><sequence><sighashflag>
0100000064891cd6a9771adf75b343367850198633d2865df516533f523ce33fdf8c55c2000000001976a91471ba0c0827171ef16859048b5157ef1f5b7d516288acffffffffc2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c896400000000ffffffff00000003
  1. sighashflag 0x81 (SIGHASH_ALL | SIGHASH_ANYONECANPAY):
<version><prev_txid><prev_vout><prev_output_scriptPubKey><input_sequence><txnew.txid><txnew.vout><txnew.output_scriptPubKey><txnew.amount><sequence><sighashflag>
0100000064891cd6a9771adf75b343367850198633d2865df516533f523ce33fdf8c55c2000000001976a91471ba0c0827171ef16859048b5157ef1f5b7d516288acffffffffc2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c8964000000001976a91406f723b0acdefec3b50e7e318951c69dff77bd5688ac00000000ffffffff00000081
  1. sighashflag 0x82 (SIGHASH_NONE | SIGHASH_ANYONECANPAY):
<version><prev_txid><prev_vout><prev_output_scriptPubKey><input_sequence><txnew.txid><txnew.vout><txnew.output_scriptPubKey><txnew.amount><sequence><sighashflag>
0100000064891cd6a9771adf75b343367850198633d2865df516533f523ce33fdf8c55c2000000001976a91471ba0c0827171ef16859048b5157ef1f5b7d516288acffffffffc2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c89640100000000ffffffff00000082
  1. sighashflag 0x83 (SIGHASH_SINGLE | SIGHASH_ANYONECANPAY):
<version><prev_txid><prev_vout><prev_output_scriptPubKey><input_sequence><txnew.txid><txnew.vout><txnew.output_scriptPubKey><txnew.amount><sequence><sighashflag>
0100000064891cd6a9771adf75b343367850198633d2865df516533f523ce33fdf8c55c2000000001976a91471ba0c0827171ef16859048b5157ef1f5b7d516288acffffffffc2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c896400000000ffffffff00000083

In each case, the differences in the byte array are mainly due to the inclusion of the specific sighashflag and the serialization of the transaction data.

1
  • I think SIGHASH_NONE should end with <sequence><locktime><sighashflag>
    – Lee
    Commented May 6 at 11:06
0

0x01 SIGHASH_ALL

   txnew.version : 01000000
   txnew.input count : 02
   txnew.input0_txid : fb8d928e7f7bf64a21628f568ec1cc85b5acd4bd30292d96db00e4b4beb1d19b 
   txnew.input0_vout : 01000000
   txnew.input0_ScriptSigSize : 00
   txnew.input0_sequence : ffffffff
   txnew.input1_txid: c2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c8964
   txnew.input1_vout: 00000000
   txprev.output0_ScriptSigSize: 19
   txprev.output0_ScriptSig: 76a91471ba0c0827171ef16859048b5157ef1f5b7d516288ac
   txnew.input1_sequence: ffffffff
   txnew.output_count : 02
   txnew.output0_amount: d273110800000000
   txnew.output0_scriptPubKeySize: 19
   txnew.output0_scriptPubKey: 76a91406f723b0acdefec3b50e7e318951c69dff77bd5688ac
   txnew.output1_amount: 5f95f40000000000
   txnew.output1_scriptPubKeySize: 19
   txnew.output1_scriptPubKey: 76a9145b639f973c812eb23adda40465acabd336c6092888ac
   txnew.locktime: 00000000
   hashTypeCode : 00000001

0x02 SIGHASH_NONE

   txnew.version : 01000000
   txnew.input count : 02
   txnew.input0_txid : fb8d928e7f7bf64a21628f568ec1cc85b5acd4bd30292d96db00e4b4beb1d19b 
   txnew.input0_vout : 01000000
   txnew.input0_ScriptSigSize : 00
   txnew.input0_sequence : 00000000
   txnew.input1_txid: c2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c8964
   txnew.input1_vout: 00000000
   txprev.output0_ScriptSigSize: 19
   txprev.output0_ScriptSig: 76a91471ba0c0827171ef16859048b5157ef1f5b7d516288ac
   txnew.input1_sequence: ffffffff
   txnew.output_count : 00
   txnew.locktime: 00000000
   hashTypeCode : 00000002

0x03 SIGHASH_SINGLE

   txnew.version : 01000000
   txnew.input count : 02
   txnew.input0_txid : fb8d928e7f7bf64a21628f568ec1cc85b5acd4bd30292d96db00e4b4beb1d19b 
   txnew.input0_vout : 01000000
   txnew.input0_ScriptSigSize : 00
   txnew.input0_sequence : 00000000
   txnew.input1_txid: c2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c8964
   txnew.input1_vout: 00000000
   txprev.output0_ScriptSigSize: 19
   txprev.output0_ScriptSig: 76a91471ba0c0827171ef16859048b5157ef1f5b7d516288ac
   txnew.input1_sequence: ffffffff
   txnew.output_count : 02
   txnew.output0_amount: ffffffffffffffff
   txnew.output0_scriptPubKeySize: 00
   txnew.output1_amount: 5f95f40000000000
   txnew.output1_scriptPubKeySize: 19
   txnew.output1_scriptPubKey: 76a9145b639f973c812eb23adda40465acabd336c6092888ac
   txnew.locktime: 00000000
   hashTypeCode : 00000003

0x81 SIGHASH_ANYONECANPAY & SIGHASH_ALL

   txnew.version : 01000000
   txnew.input count : 01
   txnew.input1_txid: c2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c8964
   txnew.input1_vout: 00000000
   txprev.output0_ScriptSigSize: 19
   txprev.output0_ScriptSig: 76a91471ba0c0827171ef16859048b5157ef1f5b7d516288ac
   txnew.input1_sequence: ffffffff
   txnew.output_count : 02
   txnew.output0_amount: d273110800000000
   txnew.output0_scriptPubKeySize: 19
   txnew.output0_scriptPubKey: 76a91406f723b0acdefec3b50e7e318951c69dff77bd5688ac
   txnew.output1_amount: 5f95f40000000000
   txnew.output1_scriptPubKeySize: 19
   txnew.output1_scriptPubKey: 76a9145b639f973c812eb23adda40465acabd336c6092888ac
   txnew.locktime: 00000000
   hashTypeCode : 00000081

0x82 SIGHASH_ANYONECANPAY & SIGHASH_NONE

   txnew.version : 01000000
   txnew.input count : 01
   txnew.input1_txid: c2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c8964
   txnew.input1_vout: 00000000
   txprev.output0_ScriptSigSize: 19
   txprev.output0_ScriptSig: 76a91471ba0c0827171ef16859048b5157ef1f5b7d516288ac
   txnew.input1_sequence: ffffffff
   txnew.output_count : 00
   txnew.locktime: 00000000
   hashTypeCode : 00000082

0x83 SIGHASH_ANYONECANPAY & SIGHASH_SINGLE

   txnew.version : 01000000
   txnew.input count : 01
   txnew.input1_txid: c2558cdf3fe33c523f5316f55d86d233861950783643b375df1a77a9d61c8964
   txnew.input1_vout: 00000000
   txprev.output0_ScriptSigSize: 19
   txprev.output0_ScriptSig: 76a91471ba0c0827171ef16859048b5157ef1f5b7d516288ac
   txnew.input1_sequence: ffffffff
   txnew.output_count : 02
   txnew.output0_amount: ffffffffffffffff
   txnew.output0_scriptPubKeySize: 00
   txnew.output1_amount: 5f95f40000000000
   txnew.output1_scriptPubKeySize: 19
   txnew.output1_scriptPubKey: 76a9145b639f973c812eb23adda40465acabd336c6092888ac
   txnew.locktime: 00000000
   hashTypeCode : 00000083

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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