Bitcoin Forum
November 11, 2024, 01:38:30 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to create Tx Hash?  (Read 3600 times)
ThePiachu (OP)
Sr. Member
****
Offline Offline

Activity: 444
Merit: 313



View Profile WWW
December 13, 2011, 08: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?

1HWbVLhxj7bhewhyapMZpyhqWAeAhJd51E
My Bitcoin Calculator:
http://tpbitcalc.appspot.com/
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
December 13, 2011, 08:41:10 AM
 #2

https://en.bitcoin.it/wiki/Protocol_specification

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
ThePiachu (OP)
Sr. Member
****
Offline Offline

Activity: 444
Merit: 313



View Profile WWW
December 13, 2011, 12:00:01 PM
 #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).

1HWbVLhxj7bhewhyapMZpyhqWAeAhJd51E
My Bitcoin Calculator:
http://tpbitcalc.appspot.com/
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
December 13, 2011, 12:27:24 PM
 #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.

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
Gavin Andresen
Legendary
*
qt
Offline Offline

Activity: 1652
Merit: 2301


Chief Scientist


View Profile WWW
December 13, 2011, 04:05:42 PM
 #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.


How often do you get the chance to work on a potentially world-changing project?
ThePiachu (OP)
Sr. Member
****
Offline Offline

Activity: 444
Merit: 313



View Profile WWW
December 13, 2011, 07:09:39 PM
 #6

So I would hash the entire Tx as it would appear in a Bitcoin message. Okay, I`ll try that.

1HWbVLhxj7bhewhyapMZpyhqWAeAhJd51E
My Bitcoin Calculator:
http://tpbitcalc.appspot.com/
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
December 13, 2011, 07: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!

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
etotheipi
Legendary
*
expert
Offline Offline

Activity: 1428
Merit: 1093


Core Armory Developer


View Profile WWW
December 13, 2011, 11: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://bitcointalk.org/index.php?topic=29416.0

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).

Founder and CEO of Armory Technologies, Inc.
Armory Bitcoin Wallet: Bringing cold storage to the average user!
Only use Armory software signed by the Armory Offline Signing Key (0x98832223)

Please donate to the Armory project by clicking here!    (or donate directly via 1QBDLYTDFHHZAABYSKGKPWKLSXZWCCJQBX -- yes, it's a real address!)
ThePiachu (OP)
Sr. Member
****
Offline Offline

Activity: 444
Merit: 313



View Profile WWW
December 14, 2011, 12:51:09 AM
 #9

Thanks for all your help, I managed to properly hash the Tx from the Genesis Block.

1HWbVLhxj7bhewhyapMZpyhqWAeAhJd51E
My Bitcoin Calculator:
http://tpbitcalc.appspot.com/
kokjo
Legendary
*
Offline Offline

Activity: 1050
Merit: 1000

You are WRONG!


View Profile
December 14, 2011, 08:51:49 AM
 #10

Thanks for all your help, I managed to properly hash the Tx from the Genesis Block.
+1

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!