forked from nbcorg/blockbook
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
National Bitcoiner
committed
Jun 20, 2020
1 parent
8288495
commit e56bf6d
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |