Originally posted to StackOverflow but with no response. Thought I might have better luck here.
I've successfully created a Bitcoin transaction in C# according to the protocol specifications. Here's the transaction in binary:
010000000162EF5D87E91090453009860C6F135EA2A490133B2633F8F7BA014889F5FF7E4A000000008A4730440220497E2D3F9BBBC0C122D839968FF880514440CED08A71D28E614FF4C871964024022049687008E4AF72682992325F17E95DA382D63E3FB14605A81B4498DFF9210E310141040BCF1BECF4EF6A5C24FD790CA86E8DCDFA3538CB97E99C254F84D209CFE47BFC6BF3B11ACA05DB7C7638301711070F176E848B650A406EB7AFC30AA9EA242489FFFFFFFF02404B4C00000000001976A9140A24F0387F05A6EECD1F8B327BB442EAB767CBC888ACCA165E00000000001976A914F8FB3B4E5D75652795DD13B209E0BE7BBD66B5D488AC00000000
I know the transaction is solid because the BTCs were transmitted properly. Furthermore, pasting my transaction above into this validator indicates the correct values.
What's stumping me is how the official transaction ID is generated. Everything on the Bitcoin and StackOverflow StackExchange sites indicate that it's generated by hashing the transaction bytes twice. I tried to do so as follows:
const String txHex="010000000162EF5D87E91090453009860C6F135EA2A490133B2633F8F7BA014889F5FF7E4A000000008A4730440220497E2D3F9BBBC0C122D839968FF880514440CED08A71D28E614FF4C871964024022049687008E4AF72682992325F17E95DA382D63E3FB14605A81B4498DFF9210E310141040BCF1BECF4EF6A5C24FD790CA86E8DCDFA3538CB97E99C254F84D209CFE47BFC6BF3B11ACA05DB7C7638301711070F176E848B650A406EB7AFC30AA9EA242489FFFFFFFF02404B4C00000000001976A9140A24F0387F05A6EECD1F8B327BB442EAB767CBC888ACCA165E00000000001976A914F8FB3B4E5D75652795DD13B209E0BE7BBD66B5D488AC00000000";
SHA256Managed sha256Hasher=new SHA256Managed();
Func<byte[],byte[]> doubleHash=(bytes)=>{
return sha256Hasher.ComputeHash((sha256Hasher.ComputeHash(bytes)));
};
String txidHex=Utils.ToHex(doubleHash(Utils.FromHex(txHex)));
Utils.ToHex()
and Utils.FromHex()
are helper functions copied from StackOverflow which convert between byte[]
and a hex string.
The code above yields a txidHex
value of 27E6C3EF18D14FB49CBAC182BBDB47FC9D6F79DA65C101873207B60A957852A7
but searching BlockExplorer.com for that particular txid yields no results.
I mentioned earlier that my transaction did execute properly. It turns out that it did so using a txid of 11172aed3a61fe64ae82cb02e886757d352a4452f956a0703d0809880538ba58
. If you paste that txid into BlockExplorer you'll see my transaction.
Which leads me to my question: Given a valid transaction byte stream (byte[]), how does one correctly compute a transaction ID?
Thanks.