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

     1  package bitcore
     2  
     3  import (
     4  	"blockbook/bchain"
     5  	"blockbook/bchain/coins/btc"
     6  	"github.com/martinboehm/btcd/wire"
     7  	"github.com/martinboehm/btcutil/chaincfg"
     8  )
     9  
    10  const (
    11  	MainnetMagic wire.BitcoinNet = 0xf9beb4d9
    12  	TestnetMagic wire.BitcoinNet = 0xfdd2c8f1
    13  	RegtestMagic wire.BitcoinNet = 0xfabfb5da
    14  )
    15  
    16  var (
    17  	MainNetParams chaincfg.Params
    18  	TestNetParams chaincfg.Params
    19  )
    20  
    21  func init() {
    22  	MainNetParams = chaincfg.MainNetParams
    23  	MainNetParams.Net = MainnetMagic
    24  	MainNetParams.PubKeyHashAddrID = []byte{3}
    25  	MainNetParams.ScriptHashAddrID = []byte{125}
    26  	MainNetParams.Bech32HRPSegwit = "btx"
    27  
    28  	TestNetParams = chaincfg.TestNet3Params
    29  	TestNetParams.Net = TestnetMagic
    30  	TestNetParams.PubKeyHashAddrID = []byte{111}
    31  	TestNetParams.ScriptHashAddrID = []byte{196}
    32  	TestNetParams.Bech32HRPSegwit = "tbtx"
    33  
    34  	err := chaincfg.Register(&MainNetParams)
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  }
    39  
    40  // BitcoreParser handle
    41  type BitcoreParser struct {
    42  	*btc.BitcoinParser
    43  	baseparser *bchain.BaseParser
    44  }
    45  
    46  // NewBitcoreParser returns new BitcoreParser instance
    47  func NewBitcoreParser(params *chaincfg.Params, c *btc.Configuration) *BitcoreParser {
    48  	return &BitcoreParser{
    49  		BitcoinParser: btc.NewBitcoinParser(params, c),
    50  		baseparser:    &bchain.BaseParser{},
    51  	}
    52  }
    53  
    54  // GetChainParams contains network parameters for the main Bitcore network,
    55  // and the test Bitcore network
    56  func GetChainParams(chain string) *chaincfg.Params {
    57  	if !chaincfg.IsRegistered(&MainNetParams) {
    58  		err := chaincfg.Register(&MainNetParams)
    59  		if err == nil {
    60  			err = chaincfg.Register(&TestNetParams)
    61  		}
    62  		if err != nil {
    63  			panic(err)
    64  		}
    65  	}
    66  	switch chain {
    67  	case "test":
    68  		return &TestNetParams
    69  	default:
    70  		return &MainNetParams
    71  	}
    72  }
    73  
    74  // PackTx packs transaction to byte array using protobuf
    75  func (p *BitcoreParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) ([]byte, error) {
    76  	return p.baseparser.PackTx(tx, height, blockTime)
    77  }
    78  
    79  // UnpackTx unpacks transaction from protobuf byte array
    80  func (p *BitcoreParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
    81  	return p.baseparser.UnpackTx(buf)
    82  }