diff --git a/bchain/coins/russianbitcoin/russianbitcoinparser.go b/bchain/coins/russianbitcoin/russianbitcoinparser.go new file mode 100644 index 0000000..51a7e14 --- /dev/null +++ b/bchain/coins/russianbitcoin/russianbitcoinparser.go @@ -0,0 +1,64 @@ +package russianbitcoin + +import ( + "github.com/nbcorg/btcd/wire" + "github.com/nbcorg/btcutil/chaincfg" + "github.com/nbcorg/blockbook/bchain/coins/btc" +) + +// magic numbers +const ( + MainnetMagic wire.BitcoinNet = 0xd8b4bef8 + TestnetMagic wire.BitcoinNet = 0x0809110c + RegtestMagic wire.BitcoinNet = 0xdbd5bffb +) + +// chain parameters +var ( + MainNetParams chaincfg.Params + TestNetParams chaincfg.Params +) + +func init() { + MainNetParams = chaincfg.MainNetParams + MainNetParams.Net = MainnetMagic + MainNetParams.PubKeyHashAddrID = []byte{102} + MainNetParams.ScriptHashAddrID = []byte{11} + MainNetParams.Bech32HRPSegwit = "rubtcm" + + TestNetParams = chaincfg.TestNet3Params + TestNetParams.Net = TestnetMagic + TestNetParams.PubKeyHashAddrID = []byte{105} + TestNetParams.ScriptHashAddrID = []byte{13} + TestNetParams.Bech32HRPSegwit = "rubtct" +} + +// RussianBitcoinParser handle +type RussianBitcoinParser struct { + *btc.BitcoinParser +} + +// NewRussianBitcoinParser returns new RussianBitcoinParser instance +func NewRussianBitcoinParser(params *chaincfg.Params, c *btc.Configuration) *RussianBitcoinParser { + return &RussianBitcoinParser{BitcoinParser: btc.NewBitcoinParser(params, c)} +} + +// GetChainParams contains network parameters for the main RussianBitcoin network, +// and the test RussianBitcoin network +func GetChainParams(chain string) *chaincfg.Params { + if !chaincfg.IsRegistered(&MainNetParams) { + err := chaincfg.Register(&MainNetParams) + if err == nil { + err = chaincfg.Register(&TestNetParams) + } + if err != nil { + panic(err) + } + } + switch chain { + case "test": + return &TestNetParams + default: + return &MainNetParams + } +} diff --git a/bchain/coins/russianbitcoin/russianbitcoinrpc.go b/bchain/coins/russianbitcoin/russianbitcoinrpc.go new file mode 100644 index 0000000..92ab478 --- /dev/null +++ b/bchain/coins/russianbitcoin/russianbitcoinrpc.go @@ -0,0 +1,58 @@ +package russianbitcoin + +import ( + "encoding/json" + + "github.com/golang/glog" + "github.com/nbcorg/blockbook/bchain" + "github.com/nbcorg/blockbook/bchain/coins/btc" +) + +// RussianBitcoinRPC is an interface to JSON-RPC bitcoind service. +type RussianBitcoinRPC struct { + *btc.BitcoinRPC +} + +// NewRussianBitcoinRPC returns new RussianBitcoinRPC instance. +func NewRussianBitcoinRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) { + b, err := btc.NewBitcoinRPC(config, pushHandler) + if err != nil { + return nil, err + } + + s := &RussianBitcoinRPC{ + b.(*btc.BitcoinRPC), + } + s.RPCMarshaler = btc.JSONMarshalerV2{} + s.ChainConfig.SupportsEstimateFee = false + + return s, nil +} + +// Initialize initializes RussianBitcoinRPC instance. +func (b *RussianBitcoinRPC) Initialize() error { + ci, err := b.GetChainInfo() + if err != nil { + return err + } + chainName := ci.Chain + + glog.Info("Chain name ", chainName) + params := GetChainParams(chainName) + + // always create parser + b.Parser = NewRussianBitcoinParser(params, b.ChainConfig) + + // parameters for getInfo request + if params.Net == MainnetMagic { + b.Testnet = false + b.Network = "livenet" + } else { + b.Testnet = true + b.Network = "testnet" + } + + glog.Info("rpc: block chain ", params.Name) + + return nil +}