# Gavin Andresen # 2011-12-13 16:05:42 # https://bitcointalk.org/index.php?topic=54451.msg649253#msg649253 Here's how to figure it out from the Satoshi client code: @p{par} The IMPLEMENT_SERIALIZE macro is used to both store transactions on disk and to serialize them into a byte-array that can be hashed. @p{par} For class CTransaction, that looks like: @p{brk} Code: IMPLEMENT_SERIALIZE @p{brk} ( @p{brk} READWRITE(this-@s{gt}nVersion); @p{brk} nVersion = this-@s{gt}nVersion; @p{brk} READWRITE(vin); @p{brk} READWRITE(vout); @p{brk} READWRITE(nLockTime); @p{brk} ) @p{brk} @p{brk} READWRITE is a wrapper that is overloaded to Do The Right Thing for all the types bitcoin deals with; for complex types like CTxOut, IMPLEMENT_SERIALIZE is (essentially) called recursively. @p{par} Expand out all of the types and, assuming I didn't screw up (always an iffy assumption), it looks like a CTransaction is serialized as: @p{par} Code: nVersion @p{brk} vin.size (vectors are serialized as a compressed count immediately followed by their contents) @p{brk} vin[].prevout (vin-@s{gt}prevout-@s{gt}hash followed immediately by vin-@s{gt}prevout-@s{gt}n, as 36 bytes) @p{brk} vin[].scriptSig (CScripts are serialized as a vector of bytes) @p{brk} vin[].nSequence @p{brk} ... repeated for each vin @p{brk} vout.size @p{brk} vout[].nValue @p{brk} vout[].scriptPubKey @p{brk} ... repeated for each vout @p{brk} nLockTime @p{brk} @p{brk} String all those bytes together, SHA256 them twice, and you should get the transaction hash for the merkle chain. @p{par}