github.com/cryptohub-digital/blockbook-fork@v0.0.0-20230713133354-673c927af7f1/bchain/coins/koto/kotoparser.go (about)

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