1

I've previously been using bitcoin-cli decoderawtransaction to get human readable bitcoin transaction data but am just learning that it's not only decoding the raw transaction but also adding extra derived fields (e.g. txid, hash) that aren't explicitly stated within the transaction held in the blockchain.

Purely as a means of education I want to be able to interact with exactly the transaction data that's stored by bitcoin in blocks/mempool with no extra extrapolated data.

What's the best way to get this data?

2

Why not take that raw hex and decode it yourself? The serialization is documented here: https://en.bitcoin.it/wiki/Protocol_documentation#tx

For example: https://blockstream.info/tx/597d47f1c1653b6e067d8a323c07e0d7702cc92445922e4ba1a61748e0e6fee8

Raw hex:

01000000018a6b50504336d298af1bef59f4292afdbac8ad29a0217ef6d67eb70120f33704010
000006b483045022100b0c1bbc3ab2b716f388fe9136cbed2a1e1562fe7a9b8b9847f27284340
85e9e602206351a3b9fb8e04258730856792237e98b9e8d4a89ca10cd5c1dfa68295b9f409012
103ddc5d4f8c82c81304a409200095f26ce3f51847c6f11793b3a744818edff7ad3ffffffff02
204e0000000000001976a914d2212980d7adf3596d4db7be218d4c0e95944be488ac098033000
00000001976a91460d6cdf16948d0a40667b7af195f3d233a35b0e288ac00000000

Decoded:

01000000 version

01 # of inputs

(input 0:)

8a6b50504336d298af1bef59f4292afdbac8ad29a0217ef6d67eb70120f33704 prev out txid

01000000 prev out index

6b length of script (= 107 bytes)

483045022100b0c1bbc3ab2b716f388fe9136cbed2a1e1562fe7a9b8b9847f2728434085e9e6022
06351a3b9fb8e04258730856792237e98b9e8d4a89ca10cd5c1dfa68295b9f409012103ddc5d4f8
c82c81304a409200095f26ce3f51847c6f11793b3a744818edff7ad3 script

ffffffff nSequence

02 # of outputs

(output 0:)

204e000000000000 value (20,000 satoshis)

19 length of script (=25 bytes)

76a914d2212980d7adf3596d4db7be218d4c0e95944be488ac script

(output 1:)

0980330000000000 value (3,375,113 satoshis)

19 length of script (=25 bytes)

76a91460d6cdf16948d0a40667b7af195f3d233a35b0e288ac script

00000000 locktime

  • thanks - I'm accepting as the answer, but how did you generate that - via a library or with your own code? If it's the latter, any pseudocode you could share for decoding would be very helpful. – d3wannabe Jul 19 at 7:09
  • @d3wannabe By hand! With the raw hex and the serialization spec you can sort it all out byte by byte. – pinhead Jul 19 at 13:41
  • thanks @pinhead - so "field size" is literally # of characters/bytes? If so, the wiki says version is 4, but you knew to take 8 - how? Also a field like tx_in count (which has a size of 1+) - how do you know it wasn't 018 instead of 01? Think I might be misunderstanding the idea of field size – d3wannabe Jul 22 at 4:31
  • @d3wannabe You might be mistaking bytes for hex characters. For example, 01234567 is a hex string that represents 4 bytes. – pinhead Jul 22 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.