github.com/bchainhub/blockbook@v0.3.2/bchain/coins/koto/kotoparser.go (about)

     1  package koto
     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  // magic numbers
    12  const (
    13  	MainnetMagic wire.BitcoinNet = 0x6f746f4b
    14  	TestnetMagic wire.BitcoinNet = 0x6f6b6f54
    15  	RegtestMagic wire.BitcoinNet = 0x6f6b6552
    16  )
    17  
    18  // chain parameters
    19  var (
    20  	MainNetParams chaincfg.Params
    21  	TestNetParams chaincfg.Params
    22  	RegtestParams chaincfg.Params
    23  )
    24  
    25  func init() {
    26  	MainNetParams = chaincfg.MainNetParams
    27  	MainNetParams.Net = MainnetMagic
    28  
    29  	// Address encoding magics
    30  	MainNetParams.AddressMagicLen = 2
    31  	MainNetParams.PubKeyHashAddrID = []byte{0x18, 0x36} // base58 prefix: k1
    32  	MainNetParams.ScriptHashAddrID = []byte{0x18, 0x3B} // base58 prefix: k3
    33  
    34  	TestNetParams = chaincfg.TestNet3Params
    35  	TestNetParams.Net = TestnetMagic
    36  
    37  	// Address encoding magics
    38  	TestNetParams.AddressMagicLen = 2
    39  	TestNetParams.PubKeyHashAddrID = []byte{0x18, 0xA4} // base58 prefix: km
    40  	TestNetParams.ScriptHashAddrID = []byte{0x18, 0x39} // base58 prefix: k2
    41  
    42  	RegtestParams = chaincfg.RegressionNetParams
    43  	RegtestParams.Net = RegtestMagic
    44  }
    45  
    46  // KotoParser handle
    47  type KotoParser struct {
    48  	*btc.BitcoinParser
    49  	baseparser *bchain.BaseParser
    50  }
    51  
    52  // NewKotoParser returns new KotoParser instance
    53  func NewKotoParser(params *chaincfg.Params, c *btc.Configuration) *KotoParser {
    54  	return &KotoParser{
    55  		BitcoinParser: btc.NewBitcoinParser(params, c),
    56  		baseparser:    &bchain.BaseParser{},
    57  	}
    58  }
    59  
    60  // GetChainParams contains network parameters for the main Koto network,
    61  // the regression test Koto network, the test Koto network and
    62  // the simulation test Koto network, in this order
    63  func GetChainParams(chain string) *chaincfg.Params {
    64  	if !chaincfg.IsRegistered(&MainNetParams) {
    65  		err := chaincfg.Register(&MainNetParams)
    66  		if err == nil {
    67  			err = chaincfg.Register(&TestNetParams)
    68  		}
    69  		if err == nil {
    70  			err = chaincfg.Register(&RegtestParams)
    71  		}
    72  		if err != nil {
    73  			panic(err)
    74  		}
    75  	}
    76  	switch chain {
    77  	case "test":
    78  		return &TestNetParams
    79  	case "regtest":
    80  		return &RegtestParams
    81  	default:
    82  		return &MainNetParams
    83  	}
    84  }
    85  
    86  // PackTx packs transaction to byte array using protobuf
    87  func (p *KotoParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) ([]byte, error) {
    88  	return p.baseparser.PackTx(tx, height, blockTime)
    89  }
    90  
    91  // UnpackTx unpacks transaction from protobuf byte array
    92  func (p *KotoParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
    93  	return p.baseparser.UnpackTx(buf)
    94  }