blockbook/bchain/coins/koto/kotoparser.go
Martin Boehm eae9b6b613 Handle Bitcoin vout addresses after upgrade to bitcoind 22.0.0
It was necessary to split the composition chain in other coins
from BitcoinParser to BitcoinLikeParser to keep backwards compatibility
2021-09-21 23:59:20 +02:00

94 lines
2.4 KiB
Go

package koto
import (
"github.com/martinboehm/btcd/wire"
"github.com/martinboehm/btcutil/chaincfg"
"github.com/trezor/blockbook/bchain"
"github.com/trezor/blockbook/bchain/coins/btc"
)
// magic numbers
const (
MainnetMagic wire.BitcoinNet = 0x6f746f4b
TestnetMagic wire.BitcoinNet = 0x6f6b6f54
RegtestMagic wire.BitcoinNet = 0x6f6b6552
)
// chain parameters
var (
MainNetParams chaincfg.Params
TestNetParams chaincfg.Params
RegtestParams chaincfg.Params
)
func init() {
MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic
// Address encoding magics
MainNetParams.AddressMagicLen = 2
MainNetParams.PubKeyHashAddrID = []byte{0x18, 0x36} // base58 prefix: k1
MainNetParams.ScriptHashAddrID = []byte{0x18, 0x3B} // base58 prefix: k3
TestNetParams = chaincfg.TestNet3Params
TestNetParams.Net = TestnetMagic
// Address encoding magics
TestNetParams.AddressMagicLen = 2
TestNetParams.PubKeyHashAddrID = []byte{0x18, 0xA4} // base58 prefix: km
TestNetParams.ScriptHashAddrID = []byte{0x18, 0x39} // base58 prefix: k2
RegtestParams = chaincfg.RegressionNetParams
RegtestParams.Net = RegtestMagic
}
// KotoParser handle
type KotoParser struct {
*btc.BitcoinLikeParser
baseparser *bchain.BaseParser
}
// NewKotoParser returns new KotoParser instance
func NewKotoParser(params *chaincfg.Params, c *btc.Configuration) *KotoParser {
return &KotoParser{
BitcoinLikeParser: btc.NewBitcoinLikeParser(params, c),
baseparser: &bchain.BaseParser{},
}
}
// GetChainParams contains network parameters for the main Koto network,
// the regression test Koto network, the test Koto network and
// the simulation test Koto network, in this order
func GetChainParams(chain string) *chaincfg.Params {
if !chaincfg.IsRegistered(&MainNetParams) {
err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err == nil {
err = chaincfg.Register(&RegtestParams)
}
if err != nil {
panic(err)
}
}
switch chain {
case "test":
return &TestNetParams
case "regtest":
return &RegtestParams
default:
return &MainNetParams
}
}
// PackTx packs transaction to byte array using protobuf
func (p *KotoParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) ([]byte, error) {
return p.baseparser.PackTx(tx, height, blockTime)
}
// UnpackTx unpacks transaction from protobuf byte array
func (p *KotoParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
return p.baseparser.UnpackTx(buf)
}