github.com/trezor/blockbook@v0.4.1-0.20240328132726-e9a08582ee2c/bchain/coins/dogecoin/dogecoinparser.go (about)

     1  package dogecoin
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/martinboehm/btcd/wire"
     7  	"github.com/martinboehm/btcutil/chaincfg"
     8  	"github.com/trezor/blockbook/bchain"
     9  	"github.com/trezor/blockbook/bchain/coins/btc"
    10  	"github.com/trezor/blockbook/bchain/coins/utils"
    11  )
    12  
    13  // magic numbers
    14  const (
    15  	MainnetMagic wire.BitcoinNet = 0xc0c0c0c0
    16  	TestnetMagic wire.BitcoinNet = 0xfcc1b7dc // See https://github.com/dogecoin/dogecoin/blob/f80bfe9068ac1a0619d48dad0d268894d926941e/qa/rpc-tests/test_framework/mininode.py#L1620
    17  )
    18  
    19  // chain parameters
    20  var (
    21  	MainNetParams chaincfg.Params
    22  	TestNetParams chaincfg.Params
    23  )
    24  
    25  func init() {
    26  	MainNetParams = chaincfg.MainNetParams
    27  	MainNetParams.Net = MainnetMagic
    28  	MainNetParams.PubKeyHashAddrID = []byte{30}
    29  	MainNetParams.ScriptHashAddrID = []byte{22}
    30  
    31  	TestNetParams = chaincfg.TestNet3Params
    32  	TestNetParams.Net = TestnetMagic
    33  	TestNetParams.PubKeyHashAddrID = []byte{113} // See https://github.com/dogecoin/dogecoin/blob/f80bfe9068ac1a0619d48dad0d268894d926941e/contrib/testgen/gen_base58_test_vectors.py#L23
    34  	TestNetParams.ScriptHashAddrID = []byte{196} // See https://github.com/dogecoin/dogecoin/blob/f80bfe9068ac1a0619d48dad0d268894d926941e/contrib/testgen/gen_base58_test_vectors.py#L24
    35  }
    36  
    37  // DogecoinParser handle
    38  type DogecoinParser struct {
    39  	*btc.BitcoinLikeParser
    40  }
    41  
    42  // NewDogecoinParser returns new DogecoinParser instance
    43  func NewDogecoinParser(params *chaincfg.Params, c *btc.Configuration) *DogecoinParser {
    44  	return &DogecoinParser{BitcoinLikeParser: btc.NewBitcoinLikeParser(params, c)}
    45  }
    46  
    47  // GetChainParams contains network parameters for the main Dogecoin network,
    48  // and the test Dogecoin network
    49  func GetChainParams(chain string) *chaincfg.Params {
    50  	if !chaincfg.IsRegistered(&MainNetParams) {
    51  		err := chaincfg.Register(&MainNetParams)
    52  		if err == nil {
    53  			err = chaincfg.Register(&TestNetParams)
    54  		}
    55  		if err != nil {
    56  			panic(err)
    57  		}
    58  	}
    59  	switch chain {
    60  	case "test":
    61  		return &TestNetParams
    62  	default:
    63  		return &MainNetParams
    64  	}
    65  }
    66  
    67  // ParseBlock parses raw block to our Block struct
    68  // it has special handling for Auxpow blocks that cannot be parsed by standard btc wire parser
    69  func (p *DogecoinParser) ParseBlock(b []byte) (*bchain.Block, error) {
    70  	r := bytes.NewReader(b)
    71  	w := wire.MsgBlock{}
    72  	h := wire.BlockHeader{}
    73  	err := h.Deserialize(r)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	if (h.Version & utils.VersionAuxpow) != 0 {
    78  		if err = utils.SkipAuxpow(r); err != nil {
    79  			return nil, err
    80  		}
    81  	}
    82  
    83  	err = utils.DecodeTransactions(r, 0, wire.WitnessEncoding, &w)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	txs := make([]bchain.Tx, len(w.Transactions))
    89  	for ti, t := range w.Transactions {
    90  		txs[ti] = p.TxFromMsgTx(t, false)
    91  	}
    92  
    93  	return &bchain.Block{
    94  		BlockHeader: bchain.BlockHeader{
    95  			Size: len(b),
    96  			Time: h.Timestamp.Unix(),
    97  		},
    98  		Txs: txs,
    99  	}, nil
   100  }