github.com/cerberus-wallet/blockbook@v0.3.2/bchain/coins/viacoin/viacoinparser.go (about)

     1  package viacoin
     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 = 0xcbc6680f
    16  	RegtestMagic wire.BitcoinNet = 0x377b972d
    17  )
    18  
    19  // chain parameters
    20  var (
    21  	MainNetParams chaincfg.Params
    22  	RegtestParams chaincfg.Params
    23  )
    24  
    25  func init() {
    26  	MainNetParams = chaincfg.MainNetParams
    27  	MainNetParams.Net = MainnetMagic
    28  
    29  	// Mainnet address encoding magics
    30  	MainNetParams.PubKeyHashAddrID = []byte{71} // base58 prefix: v
    31  	MainNetParams.ScriptHashAddrID = []byte{33} // base68 prefix: E
    32  	MainNetParams.Bech32HRPSegwit = "via"
    33  
    34  	RegtestParams = chaincfg.RegressionNetParams
    35  	RegtestParams.Net = RegtestMagic
    36  
    37  	// Regtest address encoding magics
    38  	RegtestParams.PubKeyHashAddrID = []byte{111} // base58 prefix: m or n
    39  	RegtestParams.ScriptHashAddrID = []byte{196} // base58 prefix: 2
    40  	RegtestParams.Bech32HRPSegwit = "tvia"
    41  }
    42  
    43  // ViacoinParser handle
    44  type ViacoinParser struct {
    45  	*btc.BitcoinParser
    46  }
    47  
    48  // NewViacoinParser returns new VertcoinParser instance
    49  func NewViacoinParser(params *chaincfg.Params, c *btc.Configuration) *ViacoinParser {
    50  	return &ViacoinParser{BitcoinParser: btc.NewBitcoinParser(params, c)}
    51  }
    52  
    53  // GetChainParams returns network parameters
    54  func GetChainParams(chain string) *chaincfg.Params {
    55  	if !chaincfg.IsRegistered(&MainNetParams) {
    56  		err := chaincfg.Register(&MainNetParams)
    57  		if err == nil {
    58  			err = chaincfg.Register(&RegtestParams)
    59  		}
    60  		if err != nil {
    61  			panic(err)
    62  		}
    63  	}
    64  
    65  	switch chain {
    66  	case "regtest":
    67  		return &RegtestParams
    68  	default:
    69  		return &MainNetParams
    70  	}
    71  }
    72  
    73  // ParseBlock parses raw block to our Block struct
    74  // it has special handling for Auxpow blocks that cannot be parsed by standard btc wire parse
    75  func (p *ViacoinParser) ParseBlock(b []byte) (*bchain.Block, error) {
    76  	r := bytes.NewReader(b)
    77  	w := wire.MsgBlock{}
    78  	h := wire.BlockHeader{}
    79  	err := h.Deserialize(r)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	if (h.Version & utils.VersionAuxpow) != 0 {
    85  		if err = utils.SkipAuxpow(r); err != nil {
    86  			return nil, err
    87  		}
    88  	}
    89  
    90  	err = utils.DecodeTransactions(r, 0, wire.WitnessEncoding, &w)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	txs := make([]bchain.Tx, len(w.Transactions))
    96  	for ti, t := range w.Transactions {
    97  		txs[ti] = p.TxFromMsgTx(t, false)
    98  	}
    99  
   100  	return &bchain.Block{
   101  		BlockHeader: bchain.BlockHeader{
   102  			Size: len(b),
   103  			Time: h.Timestamp.Unix(),
   104  		},
   105  		Txs: txs,
   106  	}, nil
   107  }