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

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