<p>You must use a library written to parse Blockchain data.</p>
<p>Here's my own example. My library is unpublished, pick a different one from <a href="https://www.google.com/search?q=blockchain+parser" rel="nofollow noreferrer">search results</a></p>
<pre class="lang-go prettyprint-override"><code>package main

import (
    &quot;encoding/hex&quot;
    &quot;fmt&quot;
    &quot;os&quot;
)

// Read transaction data in hex from a file, parse &amp; print it
func txFromFile(fileName string) {
    txHex, err := os.ReadFile(fileName)
    if err != nil { panic(err) }
    fmt.Println(string(txHex))

    b, err := hex.DecodeString(string(txHex))
    if err != nil { panic(err) }

    tx := parseTx(NewMyBuffer(b), 1)

    // Arbitrary print to show we parsed to a struct
    fmt.Printf(&quot;\nThe first output is worth %d Satoshi\n&quot;,
        tx.Outputs[0].Value)

    // tx is a Stringer (has String() method)
    fmt.Println(tx)

}
</code></pre>
<p>Example use</p>
<pre><code>C&gt; blockchain -txfile tx.hex
010000000001026a8da33916366b884349772dc022a39c4cf8d60acc45fd2527a71e5c80cc6da2000000000000000000208a35c4425160d006ed968606485258a1ecca80550ea48def5d44511bab553200000000000000000002e847791400000000160014885f1f476949438e34d3928fcff3449c339f934dc8633b0300000000160014618736c1f42c5029a5887a5ebac292720a4ccd1802483045022100f3d721d184ad19e50ca085fedb0c2654b1c5bd3cedb4b5f1d502ca7f21df125402204d87cda1511ebc1d943b0ee223aa2ade62410c51028d04ffabb3dfd7c1120273012102826930ee848903793b57d2e253284dfa0df86de1103bace30b84ab619e663fb402463043021f718f2adabc9214c196942861bfe93c8e96dd3517c6d4988898a901485ed0c1022007fee4bfd89d6d1f936932c3dd37fcad44cdcd6ec3ad9daebc2b8d8d7da1d5e1012102826930ee848903793b57d2e253284dfa0df86de1103bace30b84ab619e663fb400000000

The first output is worth 343492584 Satoshi

  Version:................01000000 (1)
  Type:...................0001 (Segwit)
  Inputs:.................02 (2)
  Input:1
    Previous Transaction:.A26DCC805C1EA72725FD45CC0AD6F84C9CA322C02D774943886B361639A38D6A
    Previous Output Index:0000 (0)
    Script Length:........00 (0)
    Unlocking Script:.....
    Sequence:.............00000000
  Input:2
    Previous Transaction:.3255AB1B51445DEF8DA40E5580CAECA1585248068696ED06D0605142C4358A20
    Previous Output Index:0000 (0)
    Script Length:........00 (0)
    Unlocking Script:.....
    Sequence:.............00000000
  Outputs:................02 (2)
  Output 1
    Value:................E847791400000000 (343492584)
    Script Length:........16 (22)
    Locking Script:.......0014885F1F476949438E34D3928FCFF3449C339F934D
  Output 2
    Value:................C8633B0300000000 (54223816)
    Script Length:........16 (22)
    Locking Script:.......0014618736C1F42C5029A5887A5EBAC292720A4CCD18
  Witness 1
    Item Count:...........02 (2)
    Item 1
      Length:.............48 (72)
      Data:...............3045022100F3D721D184AD19E50CA085FEDB0C2654B1C5BD3CEDB4B5F1D502CA
                          7F21DF125402204D87CDA1511EBC1D943B0EE223AA2ADE62410C51028D04FFAB
                          B3DFD7C112027301
    Item 2
      Length:.............21 (33)
      Data:...............02826930EE848903793B57D2E253284DFA0DF86DE1103BACE30B84AB619E663F
                          B4
  Witness 2
    Item Count:...........02 (2)
    Item 1
      Length:.............46 (70)
      Data:...............3043021F718F2ADABC9214C196942861BFE93C8E96DD3517C6D4988898A90148
                          5ED0C1022007FEE4BFD89D6D1F936932C3DD37FCAD44CDCD6EC3AD9DAEBC2B8D
                          8D7DA1D5E101
    Item 2
      Length:.............21 (33)
      Data:...............02826930EE848903793B57D2E253284DFA0DF86DE1103BACE30B84AB619E663F
                          B4
  LockTime:...............00000000 (0)
  (Calculated hash is 2C1DEA91142889092E41F0617250F83E0601E87290FAF0676F813F0F55063270)

</code></pre>

CC BY-SA 4.0
2023-03-01T10:53:58.247Z
Related: https://bitcoin.stackexchange.com/q/117397/13866
RedGrittyBrick
CC BY-SA 4.0
2022-11-27T21:48:57.747Z
2022-11-27T23:01:24.470Z
2022-11-27T23:01:24.470Z
RedGrittyBrick
<p>hex: 010000000001026a8da33916366b884349772dc022a39c4cf8d60acc45fd2527a71e5c80cc6da2000000000000000000208a35c4425160d006ed968606485258a1ecca80550ea48def5d44511bab553200000000000000000002e847791400000000160014885f1f476949438e34d3928fcff3449c339f934dc8633b0300000000160014618736c1f42c5029a5887a5ebac292720a4ccd1802483045022100f3d721d184ad19e50ca085fedb0c2654b1c5bd3cedb4b5f1d502ca7f21df125402204d87cda1511ebc1d943b0ee223aa2ade62410c51028d04ffabb3dfd7c1120273012102826930ee848903793b57d2e253284dfa0df86de1103bace30b84ab619e663fb402463043021f718f2adabc9214c196942861bfe93c8e96dd3517c6d4988898a901485ed0c1022007fee4bfd89d6d1f936932c3dd37fcad44cdcd6ec3ad9daebc2b8d8d7da1d5e1012102826930ee848903793b57d2e253284dfa0df86de1103bace30b84ab619e663fb400000000</p>
<p><a href="https://live.blockcypher.com/btc/decodetx/" rel="nofollow noreferrer">https://live.blockcypher.com/btc/decodetx/</a></p>
<p>This site decodes it fine but when I use decoder in golang or python either from bytes or a hex string it outputs garbage, is there something I am missing?</p>

CC BY-SA 4.0
2022-11-27T21:32:51.707Z
The hex data you posted is a perfectly cromulent SegWit transaction ([this one](https://blockchair.com/bitcoin/transaction/2c1dea91142889092e41f0617250f83e0601e87290faf0676f813f0f55063270)). I can decode your hex data in go without any obvious problem. Maybe you can post the decoder you are using, full details of how you are using it and full error messages it produces
RedGrittyBrick
CC BY-SA 4.0
2022-11-27T21:41:45.927Z
thanks for the reply just like that `str :="hex posted"`, `hex.DecodeString(str).   `dst := make([]byte, hex.DecodedLen(len([]byte(str))))
 n, err := hex.Decode(dst, []byte(str))`
meanthatmuchtoyou
CC BY-SA 4.0
2022-11-27T21:46:50.507Z
In Go that just converts hex to binary. You need to also subsequently invoke methods from a proper blockchain parsing libary.
RedGrittyBrick
<bitcoin-core><bitcoind><development>
decoding transaction hash returned in rpc call getrawtransaction
meanthatmuchtoyou
# decoding transaction hash returned in rpc call getrawtransaction

(Asked by: username_0 on 2022-11-27)

hex: 010000000001026a8da33916366b884349772dc022a39c4cf8d60acc45fd2527a71e5c80cc6da2000000000000000000208a35c4425160d006ed968606485258a1ecca80550ea48def5d44511bab553200000000000000000002e847791400000000160014885f1f476949438e34d3928fcff3449c339f934dc8633b0300000000160014618736c1f42c5029a5887a5ebac292720a4ccd1802483045022100f3d721d184ad19e50ca085fedb0c2654b1c5bd3cedb4b5f1d502ca7f21df125402204d87cda1511ebc1d943b0ee223aa2ade62410c51028d04ffabb3dfd7c1120273012102826930ee848903793b57d2e253284dfa0df86de1103bace30b84ab619e663fb402463043021f718f2adabc9214c196942861bfe93c8e96dd3517c6d4988898a901485ed0c1022007fee4bfd89d6d1f936932c3dd37fcad44cdcd6ec3ad9daebc2b8d8d7da1d5e1012102826930ee848903793b57d2e253284dfa0df86de1103bace30b84ab619e663fb400000000

<https://live.blockcypher.com/btc/decodetx/>

This site decodes it fine but when I use decoder in golang or python either from bytes or a hex string it outputs garbage, is there something I am missing?

**Tags:** bitcoin-core, bitcoind, development

> **Comments:**
> **username_1**: The hex data you posted is a perfectly cromulent SegWit transaction ([this one](https://blockchair.com/bitcoin/transaction/2c1dea91142889092e41f0617250f83e0601e87290faf0676f813f0f55063270)). I can decode your hex data in go without any obvious problem. Maybe you can post the decoder you are using, full details of how you are using it and full error messages it produces  
> **username_0**: thanks for the reply just like that `str :="hex posted"`, `hex.DecodeString(str).   `dst := make([]byte, hex.DecodedLen(len([]byte(str))))  n, err := hex.Decode(dst, []byte(str))`  
> **username_1**: In Go that just converts hex to binary. You need to also subsequently invoke methods from a proper blockchain parsing libary.  

---

## 1 answers

### Answer by username_1 on 2022-11-27. Score: 0 | Accepted answer

You must use a library written to parse Blockchain data.

Here's my own example. My library is unpublished, pick a different one from [search results](https://www.google.com/search?q=blockchain+parser)

```go
package main

import (
    "encoding/hex"
    "fmt"
    "os"
)

// Read transaction data in hex from a file, parse & print it
func txFromFile(fileName string) {
    txHex, err := os.ReadFile(fileName)
    if err != nil { panic(err) }
    fmt.Println(string(txHex))

    b, err := hex.DecodeString(string(txHex))
    if err != nil { panic(err) }

    tx := parseTx(NewMyBuffer(b), 1)

    // Arbitrary print to show we parsed to a struct
    fmt.Printf("\nThe first output is worth %d Satoshi\n",
        tx.Outputs[0].Value)

    // tx is a Stringer (has String() method)
    fmt.Println(tx)

}

```

Example use

```
C> blockchain -txfile tx.hex
010000000001026a8da33916366b884349772dc022a39c4cf8d60acc45fd2527a71e5c80cc6da2000000000000000000208a35c4425160d006ed968606485258a1ecca80550ea48def5d44511bab553200000000000000000002e847791400000000160014885f1f476949438e34d3928fcff3449c339f934dc8633b0300000000160014618736c1f42c5029a5887a5ebac292720a4ccd1802483045022100f3d721d184ad19e50ca085fedb0c2654b1c5bd3cedb4b5f1d502ca7f21df125402204d87cda1511ebc1d943b0ee223aa2ade62410c51028d04ffabb3dfd7c1120273012102826930ee848903793b57d2e253284dfa0df86de1103bace30b84ab619e663fb402463043021f718f2adabc9214c196942861bfe93c8e96dd3517c6d4988898a901485ed0c1022007fee4bfd89d6d1f936932c3dd37fcad44cdcd6ec3ad9daebc2b8d8d7da1d5e1012102826930ee848903793b57d2e253284dfa0df86de1103bace30b84ab619e663fb400000000

The first output is worth 343492584 Satoshi

  Version:................01000000 (1)
  Type:...................0001 (Segwit)
  Inputs:.................02 (2)
  Input:1
    Previous Transaction:.A26DCC805C1EA72725FD45CC0AD6F84C9CA322C02D774943886B361639A38D6A
    Previous Output Index:0000 (0)
    Script Length:........00 (0)
    Unlocking Script:.....
    Sequence:.............00000000
  Input:2
    Previous Transaction:.3255AB1B51445DEF8DA40E5580CAECA1585248068696ED06D0605142C4358A20
    Previous Output Index:0000 (0)
    Script Length:........00 (0)
    Unlocking Script:.....
    Sequence:.............00000000
  Outputs:................02 (2)
  Output 1
    Value:................E847791400000000 (343492584)
    Script Length:........16 (22)
    Locking Script:.......0014885F1F476949438E34D3928FCFF3449C339F934D
  Output 2
    Value:................C8633B0300000000 (54223816)
    Script Length:........16 (22)
    Locking Script:.......0014618736C1F42C5029A5887A5EBAC292720A4CCD18
  Witness 1
    Item Count:...........02 (2)
    Item 1
      Length:.............48 (72)
      Data:...............3045022100F3D721D184AD19E50CA085FEDB0C2654B1C5BD3CEDB4B5F1D502CA
                          7F21DF125402204D87CDA1511EBC1D943B0EE223AA2ADE62410C51028D04FFAB
                          B3DFD7C112027301
    Item 2
      Length:.............21 (33)
      Data:...............02826930EE848903793B57D2E253284DFA0DF86DE1103BACE30B84AB619E663F
                          B4
  Witness 2
    Item Count:...........02 (2)
    Item 1
      Length:.............46 (70)
      Data:...............3043021F718F2ADABC9214C196942861BFE93C8E96DD3517C6D4988898A90148
                          5ED0C1022007FEE4BFD89D6D1F936932C3DD37FCAD44CDCD6EC3AD9DAEBC2B8D
                          8D7DA1D5E101
    Item 2
      Length:.............21 (33)
      Data:...............02826930EE848903793B57D2E253284DFA0DF86DE1103BACE30B84AB619E663F
                          B4
  LockTime:...............00000000 (0)
  (Calculated hash is 2C1DEA91142889092E41F0617250F83E0601E87290FAF0676F813F0F55063270)

```

> **Comments:**
> **username_1**: Related: https://bitcoin.stackexchange.com/q/117397/13866  

---

