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

     1  package dash
     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 = 0xbd6b0cbf
    13  	// TestnetMagic is testnet network constant
    14  	TestnetMagic wire.BitcoinNet = 0xffcae2ce
    15  	// RegtestMagic is regtest network constant
    16  	RegtestMagic wire.BitcoinNet = 0xdcb7c1fc
    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.PubKeyHashAddrID = []byte{76} // base58 prefix: X
    34  	MainNetParams.ScriptHashAddrID = []byte{16} // base58 prefix: 7
    35  
    36  	TestNetParams = chaincfg.TestNet3Params
    37  	TestNetParams.Net = TestnetMagic
    38  
    39  	// Address encoding magics
    40  	TestNetParams.PubKeyHashAddrID = []byte{140} // base58 prefix: y
    41  	TestNetParams.ScriptHashAddrID = []byte{19}  // base58 prefix: 8 or 9
    42  
    43  	RegtestParams = chaincfg.RegressionNetParams
    44  	RegtestParams.Net = RegtestMagic
    45  
    46  	// Address encoding magics
    47  	RegtestParams.PubKeyHashAddrID = []byte{140} // base58 prefix: y
    48  	RegtestParams.ScriptHashAddrID = []byte{19}  // base58 prefix: 8 or 9
    49  }
    50  
    51  // DashParser handle
    52  type DashParser struct {
    53  	*btc.BitcoinLikeParser
    54  	baseparser *bchain.BaseParser
    55  }
    56  
    57  // NewDashParser returns new DashParser instance
    58  func NewDashParser(params *chaincfg.Params, c *btc.Configuration) *DashParser {
    59  	return &DashParser{
    60  		BitcoinLikeParser: btc.NewBitcoinLikeParser(params, c),
    61  		baseparser:        &bchain.BaseParser{},
    62  	}
    63  }
    64  
    65  // GetChainParams contains network parameters for the main Dash network,
    66  // the regression test Dash network, the test Dash network and
    67  // the simulation test Dash network, in this order
    68  func GetChainParams(chain string) *chaincfg.Params {
    69  	if !chaincfg.IsRegistered(&MainNetParams) {
    70  		err := chaincfg.Register(&MainNetParams)
    71  		if err == nil {
    72  			err = chaincfg.Register(&TestNetParams)
    73  		}
    74  		if err == nil {
    75  			err = chaincfg.Register(&RegtestParams)
    76  		}
    77  		if err != nil {
    78  			panic(err)
    79  		}
    80  	}
    81  	switch chain {
    82  	case "test":
    83  		return &TestNetParams
    84  	case "regtest":
    85  		return &RegtestParams
    86  	default:
    87  		return &MainNetParams
    88  	}
    89  }
    90  
    91  // PackTx packs transaction to byte array using protobuf
    92  func (p *DashParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) ([]byte, error) {
    93  	return p.baseparser.PackTx(tx, height, blockTime)
    94  }
    95  
    96  // UnpackTx unpacks transaction from protobuf byte array
    97  func (p *DashParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
    98  	return p.baseparser.UnpackTx(buf)
    99  }