Bitcoin Forum
May 06, 2024, 03:49:01 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: txid and hash question  (Read 185 times)
warrior4jesus (OP)
Newbie
*
Offline Offline

Activity: 6
Merit: 0


View Profile
October 03, 2019, 02:31:00 PM
 #1

On some transactions the txid and hash have the same value as each other and on other transactions they have different values. Why is this?
1714967341
Hero Member
*
Offline Offline

Posts: 1714967341

View Profile Personal Message (Offline)

Ignore
1714967341
Reply with quote  #2

1714967341
Report to moderator
1714967341
Hero Member
*
Offline Offline

Posts: 1714967341

View Profile Personal Message (Offline)

Ignore
1714967341
Reply with quote  #2

1714967341
Report to moderator
1714967341
Hero Member
*
Offline Offline

Posts: 1714967341

View Profile Personal Message (Offline)

Ignore
1714967341
Reply with quote  #2

1714967341
Report to moderator
"Governments are good at cutting off the heads of a centrally controlled networks like Napster, but pure P2P networks like Gnutella and Tor seem to be holding their own." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714967341
Hero Member
*
Offline Offline

Posts: 1714967341

View Profile Personal Message (Offline)

Ignore
1714967341
Reply with quote  #2

1714967341
Report to moderator
1714967341
Hero Member
*
Offline Offline

Posts: 1714967341

View Profile Personal Message (Offline)

Ignore
1714967341
Reply with quote  #2

1714967341
Report to moderator
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
October 03, 2019, 02:37:53 PM
 #2

"Hash" is the result that you get from the hash function (double SHA256) and transaction ID is the hexadecimal representation of the that but in bitcoin we reverse it first and then encode it using base-16. So they are not exactly the same although they both represent the same value.
For example if your hash is { 1, 2, 3 } your txid is going to be 0x030201 (3 bytes is chosen for brevity otherwise length is 32 bytes).

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
warrior4jesus (OP)
Newbie
*
Offline Offline

Activity: 6
Merit: 0


View Profile
October 03, 2019, 02:48:22 PM
 #3

"Hash" is the result that you get from the hash function (double SHA256) and transaction ID is the hexadecimal representation of the that but in bitcoin we reverse it first and then encode it using base-16. So they are not exactly the same although they both represent the same value.
For example if your hash is { 1, 2, 3 } your txid is going to be 0x030201 (3 bytes is chosen for brevity otherwise length is 32 bytes).

Thanks for your response.

Ok, they represent the same value. But sometimes they look the same. Reversed and encoded in base 16 just happens to get the same result as double SHA256?
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6581


Just writing some code


View Profile WWW
October 03, 2019, 03:11:26 PM
Merited by bones261 (4), Coding Enthusiast (2), ABCbits (1)
 #4

"Hash" is the result that you get from the hash function (double SHA256) and transaction ID is the hexadecimal representation of the that but in bitcoin we reverse it first and then encode it using base-16. So they are not exactly the same although they both represent the same value.
For example if your hash is { 1, 2, 3 } your txid is going to be 0x030201 (3 bytes is chosen for brevity otherwise length is 32 bytes).

Thanks for your response.

Ok, they represent the same value. But sometimes they look the same. Reversed and encoded in base 16 just happens to get the same result as double SHA256?
No, that's completely wrong. It has nothing to do with the encoding.

Before segwit, the txid was the hash of the entire transaction. For non-segwit transactions, this is still true. But after segwit, some new data was added to transactions. This is not hashed as part of the txid, only those parts that were part of the non-segwit transaction format are hashed. The hash field was introduced for segwit as the hash of the entire segwit transaction.

When you see the txid and hash are the same, the transaction is non-segwit and both the txid and hash are hashing the entire transaction. When they are different, the transaction is a segwit transaction and txid is hashing some parts of the transaction while hash is hashing all of it.

Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
October 03, 2019, 03:20:48 PM
Last edit: October 03, 2019, 03:31:37 PM by Coding Enthusiast
Merited by bones261 (4), ABCbits (1)
 #5

Ok, they represent the same value. But sometimes they look the same. Reversed and encoded in base 16 just happens to get the same result as double SHA256?
Are you sure you are looking at correct values?
Here is an example transaction link on block explorer

The raw bytes of this transaction is:
Code:
01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000
You can use this online tool to compute SHA256 of hexadecimal input: https://cryptii.com/pipes/hash-function click on the drop down dots and select duplicate to compute it twice. It should look like this: https://i.imgur.com/LWXpRIA.jpg
As you can see the result is going to be:
Code:
982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e
Now if you look at that link from block explorer you can see that the txid is:
Code:
0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098
that is the hash result in reverse.

edit: Of course I forgot about SegWit, if the transaction has any witness, it has to be stripped off before hashing it as @achow101 pointed out.

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
warrior4jesus (OP)
Newbie
*
Offline Offline

Activity: 6
Merit: 0


View Profile
October 03, 2019, 03:32:52 PM
 #6

I am looking at my own node via rpc and printing the entire tx object and this is what I am seeing. Sometimes the values are the same and sometimes not. I am writing my own blockchain explorer and want to understand what the data means.
Ok, they represent the same value. But sometimes they look the same. Reversed and encoded in base 16 just happens to get the same result as double SHA256?
Are you sure you are looking at correct values?
Here is an example transaction link on block explorer

The raw bytes of this transaction is:
Code:
01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000
You can use this online tool to compute SHA256 of hexadecimal input: https://cryptii.com/pipes/hash-function click on the drop down dots and select duplicate to compute it twice. It should look like this: https://i.imgur.com/LWXpRIA.jpg
As you can see the result is going to be:
Code:
982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e
Now if you look at that link from block explorer you can see that the txid is:
Code:
0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098
that is the hash result in reverse.
warrior4jesus (OP)
Newbie
*
Offline Offline

Activity: 6
Merit: 0


View Profile
October 03, 2019, 03:40:54 PM
 #7

Yes. The tx's that have different values have an array in the object called [txinwitness]. The tx's where they have the same value do not have a [txinwitness] array. Thank you.

"Hash" is the result that you get from the hash function (double SHA256) and transaction ID is the hexadecimal representation of the that but in bitcoin we reverse it first and then encode it using base-16. So they are not exactly the same although they both represent the same value.
For example if your hash is { 1, 2, 3 } your txid is going to be 0x030201 (3 bytes is chosen for brevity otherwise length is 32 bytes).

Thanks for your response.

Ok, they represent the same value. But sometimes they look the same. Reversed and encoded in base 16 just happens to get the same result as double SHA256?
No, that's completely wrong. It has nothing to do with the encoding.

Before segwit, the txid was the hash of the entire transaction. For non-segwit transactions, this is still true. But after segwit, some new data was added to transactions. This is not hashed as part of the txid, only those parts that were part of the non-segwit transaction format are hashed. The hash field was introduced for segwit as the hash of the entire segwit transaction.

When you see the txid and hash are the same, the transaction is non-segwit and both the txid and hash are hashing the entire transaction. When they are different, the transaction is a segwit transaction and txid is hashing some parts of the transaction while hash is hashing all of it.
Dabs
Legendary
*
Offline Offline

Activity: 3416
Merit: 1912


The Concierge of Crypto


View Profile
October 03, 2019, 07:23:32 PM
 #8

Can you tell if an address that has received coins is a segwit address? I'm talking about those that begin with 3, also known as Legacy compatible segwit address, or wrapped segwit address.

I understand any coins sent to addresses that begin with bc1q are segwit transactions because that is the native segwit address format.

There are still some services that can't send to those, so if you use them, you have to give the addresses that begin with 3.

achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6581


Just writing some code


View Profile WWW
October 03, 2019, 08:38:45 PM
 #9

Can you tell if an address that has received coins is a segwit address? I'm talking about those that begin with 3, also known as Legacy compatible segwit address, or wrapped segwit address.

I understand any coins sent to addresses that begin with bc1q are segwit transactions because that is the native segwit address format.

There are still some services that can't send to those, so if you use them, you have to give the addresses that begin with 3.
Only if coins have been sent from that address. Only then can you look at a spending transaction and see if the redeemScript is a segwit script. Otherwise, you cannot.

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!