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

     1  package myriad
     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 = 0xee7645af
    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  	MainNetParams.Bech32HRPSegwit = "my"
    28  
    29  	MainNetParams.PubKeyHashAddrID = []byte{50} // 0x32 - starts with M
    30  	MainNetParams.ScriptHashAddrID = []byte{9}  // 0x09 - starts with 4
    31  	MainNetParams.PrivateKeyID = []byte{178}    // 0xB2
    32  
    33  	MainNetParams.HDCoinType = 90
    34  }
    35  
    36  // MyriadParser handle
    37  type MyriadParser struct {
    38  	*btc.BitcoinParser
    39  }
    40  
    41  // NewMyriadParser returns new MyriadParser instance
    42  func NewMyriadParser(params *chaincfg.Params, c *btc.Configuration) *MyriadParser {
    43  	return &MyriadParser{BitcoinParser: btc.NewBitcoinParser(params, c)}
    44  }
    45  
    46  // GetChainParams contains network parameters for the main Myriad network
    47  func GetChainParams(chain string) *chaincfg.Params {
    48  	if !chaincfg.IsRegistered(&MainNetParams) {
    49  		err := chaincfg.Register(&MainNetParams)
    50  		if err != nil {
    51  			panic(err)
    52  		}
    53  	}
    54  	switch chain {
    55  	default:
    56  		return &MainNetParams
    57  	}
    58  }
    59  
    60  // ParseBlock parses raw block to our Block struct
    61  // it has special handling for Auxpow blocks that cannot be parsed by standard btc wire parser
    62  func (p *MyriadParser) ParseBlock(b []byte) (*bchain.Block, error) {
    63  	r := bytes.NewReader(b)
    64  	w := wire.MsgBlock{}
    65  	h := wire.BlockHeader{}
    66  	err := h.Deserialize(r)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	if (h.Version & utils.VersionAuxpow) != 0 {
    71  		if err = utils.SkipAuxpow(r); err != nil {
    72  			return nil, err
    73  		}
    74  	}
    75  
    76  	err = utils.DecodeTransactions(r, 0, wire.WitnessEncoding, &w)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	txs := make([]bchain.Tx, len(w.Transactions))
    82  	for ti, t := range w.Transactions {
    83  		txs[ti] = p.TxFromMsgTx(t, false)
    84  	}
    85  
    86  	return &bchain.Block{
    87  		BlockHeader: bchain.BlockHeader{
    88  			Size: len(b),
    89  			Time: h.Timestamp.Unix(),
    90  		},
    91  		Txs: txs,
    92  	}, nil
    93  }