github.com/bchainhub/blockbook@v0.3.2/bchain/coins/unobtanium/unobtaniumparser.go (about) 1 package unobtanium 2 3 import ( 4 "blockbook/bchain" 5 "blockbook/bchain/coins/btc" 6 "blockbook/bchain/coins/utils" 7 "bytes" 8 9 "github.com/martinboehm/btcd/wire" 10 "github.com/martinboehm/btcutil/chaincfg" 11 ) 12 13 // magic numbers 14 const ( 15 MainnetMagic wire.BitcoinNet = 0x03d5b503 16 ) 17 18 // chain parameters 19 var ( 20 MainNetParams chaincfg.Params 21 ) 22 23 func init() { 24 MainNetParams = chaincfg.MainNetParams 25 MainNetParams.Net = MainnetMagic 26 27 // Mainnet address encoding magics 28 MainNetParams.PubKeyHashAddrID = []byte{130} 29 MainNetParams.ScriptHashAddrID = []byte{30} 30 } 31 32 // UnobtaniumParser handle 33 type UnobtaniumParser struct { 34 *btc.BitcoinParser 35 } 36 37 // NewUnobtaniumParser returns new UnobtaniumParser instance 38 func NewUnobtaniumParser(params *chaincfg.Params, c *btc.Configuration) *UnobtaniumParser { 39 return &UnobtaniumParser{BitcoinParser: btc.NewBitcoinParser(params, c)} 40 } 41 42 // GetChainParams returns network parameters 43 func GetChainParams(chain string) *chaincfg.Params { 44 if !chaincfg.IsRegistered(&MainNetParams) { 45 err := chaincfg.Register(&MainNetParams) 46 if err != nil { 47 panic(err) 48 } 49 } 50 switch chain { 51 default: 52 return &MainNetParams 53 } 54 } 55 56 // ParseBlock parses raw block to our Block struct 57 // it has special handling for Auxpow blocks that cannot be parsed by standard btc wire parse 58 func (p *UnobtaniumParser) ParseBlock(b []byte) (*bchain.Block, error) { 59 r := bytes.NewReader(b) 60 w := wire.MsgBlock{} 61 h := wire.BlockHeader{} 62 err := h.Deserialize(r) 63 if err != nil { 64 return nil, err 65 } 66 67 if (h.Version & utils.VersionAuxpow) != 0 { 68 if err = utils.SkipAuxpow(r); err != nil { 69 return nil, err 70 } 71 } 72 73 err = utils.DecodeTransactions(r, 0, wire.WitnessEncoding, &w) 74 if err != nil { 75 return nil, err 76 } 77 78 txs := make([]bchain.Tx, len(w.Transactions)) 79 for ti, t := range w.Transactions { 80 txs[ti] = p.TxFromMsgTx(t, false) 81 } 82 83 return &bchain.Block{ 84 BlockHeader: bchain.BlockHeader{ 85 Size: len(b), 86 Time: h.Timestamp.Unix(), 87 }, 88 Txs: txs, 89 }, nil 90 }