github.com/aychain/blockbook@v0.1.1-0.20181121092459-6d1fc7e07c5b/bchain/coins/grs/grsparser.go (about)

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