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

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