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