github.com/trezor/blockbook@v0.4.1-0.20240328132726-e9a08582ee2c/bchain/coins/zec/zcashparser.go (about)

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