Author

Topic: How to create Tx Hash? (Read 3603 times)

legendary
Activity: 1050
Merit: 1000
You are WRONG!
December 14, 2011, 03:51:49 AM
#10
Thanks for all your help, I managed to properly hash the Tx from the Genesis Block.
+1
sr. member
Activity: 444
Merit: 313
December 13, 2011, 07:51:09 PM
#9
Thanks for all your help, I managed to properly hash the Tx from the Genesis Block.
legendary
Activity: 1428
Merit: 1093
Core Armory Developer
December 13, 2011, 06:31:22 PM
#8
So I would hash the entire Tx as it would appear in a Bitcoin message. Okay, I`ll try that.

Here's exactly how the Tx is laid out: 
https://bitcointalksearch.org/topic/on-the-wire-byte-map-opchecksig-diagram-knowledge-donation-29416

That should be the same as show on the protocol wiki, and is the same as it's stored in the blk0001.dat file and also as it's sent over the network.  Once you have that in binary form, apply sha256 twice to get your final answer.

If you want to hash a Tx for ECDSA signing/verification, that's a whole different story (although, the link above also shows how to do that, too).
legendary
Activity: 1050
Merit: 1000
You are WRONG!
December 13, 2011, 02:14:59 PM
#7
So I would hash the entire Tx as it would appear in a Bitcoin message. Okay, I`ll try that.
twice!
sr. member
Activity: 444
Merit: 313
December 13, 2011, 02:09:39 PM
#6
So I would hash the entire Tx as it would appear in a Bitcoin message. Okay, I`ll try that.
legendary
Activity: 1652
Merit: 2301
Chief Scientist
December 13, 2011, 11:05:42 AM
#5
Here's how to figure it out from the Satoshi client code:

The IMPLEMENT_SERIALIZE macro is used to both store transactions on disk and to serialize them into a byte-array that can be hashed.

For class CTransaction, that looks like:
Code:
   IMPLEMENT_SERIALIZE
    (
        READWRITE(this->nVersion);
        nVersion = this->nVersion;
        READWRITE(vin);
        READWRITE(vout);
        READWRITE(nLockTime);
    )

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.

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:

Code:
nVersion
vin.size  (vectors are serialized as a compressed count immediately followed by their contents)
 vin[].prevout    (vin->prevout->hash followed immediately by vin->prevout->n, as 36 bytes)
 vin[].scriptSig   (CScripts are serialized as a vector of bytes)
 vin[].nSequence
 ... repeated for each vin
vout.size
 vout[].nValue
 vout[].scriptPubKey
 ... repeated for each vout
nLockTime

String all those bytes together, SHA256 them twice, and you should get the transaction hash for the merkle chain.

legendary
Activity: 1050
Merit: 1000
You are WRONG!
December 13, 2011, 07:27:24 AM
#4
Yes, I know how to create a Tx message, hash a block, I know of the ScriptSig RIPEMD use, and how to construct the Merkle Tree given the Tx hashes, but what I can't seem to find how does one create the hash used in the Merkle Tree. Do I process the entire Tx, or just parts of it (like in case of block hashes).
it deppends on what you need it for... if its for signing, it just some of it. but if its for locating the full transaction, you hash the whole transaction with alle the scripts included.
sr. member
Activity: 444
Merit: 313
December 13, 2011, 07:00:01 AM
#3
Yes, I know how to create a Tx message, hash a block, I know of the ScriptSig RIPEMD use, and how to construct the Merkle Tree given the Tx hashes, but what I can't seem to find how does one create the hash used in the Merkle Tree. Do I process the entire Tx, or just parts of it (like in case of block hashes).
legendary
Activity: 1050
Merit: 1000
You are WRONG!
sr. member
Activity: 444
Merit: 313
December 13, 2011, 03:17:02 AM
#1
What fields does one need to hash from a Tx in order to create the Hash that is used in the Merkle Tree?
Jump to: