Bitcoin-Core

解碼在 rpc 呼叫 getrawtransaction 中返回的交易雜湊

  • November 27, 2022

十六進制:

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

這個網站解碼得很好但是當我在 golang 或 python 中使用字節或十六進製字元串的解碼器時它輸出垃圾,我是否遺漏了什麼?

您必須使用編寫的庫來解析區塊鏈數據。

這是我自己的例子。我的圖書館未發布,請從搜尋結果中選擇其他圖書館

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)

}

使用範例

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)

引用自:https://bitcoin.stackexchange.com/questions/116151