Skip to content

Commit

Permalink
construct bitcoin rpc params
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Nov 17, 2023
1 parent 18964c6 commit 691fd81
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
42 changes: 42 additions & 0 deletions bitcoin/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package bitcoin

import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/rpcclient"
"github.com/evmos/ethermint/server/config"
)

// Config this param use for holding bitcoin rpc config
var Config RpcConfig

// RpcConfig this struct use for storing bitcoin rpc config
type RpcConfig struct {
ConnConfig rpcclient.ConnConfig
Params chaincfg.Params
}

// SetBitcoinConfig this method uses for setting bitcoin rpc
func SetBitcoinConfig(config config.BITCOINConfig) RpcConfig {
var params chaincfg.Params
switch config.NetworkName {
case chaincfg.SigNetParams.Name:
params = chaincfg.SigNetParams
case chaincfg.MainNetParams.Name:
params = chaincfg.MainNetParams
case chaincfg.TestNet3Params.Name:
params = chaincfg.TestNet3Params
case chaincfg.SimNetParams.Name:
params = chaincfg.SimNetParams
case chaincfg.RegressionNetParams.Name:
params = chaincfg.RegressionNetParams
default:
params = chaincfg.MainNetParams
}
Config = RpcConfig{
ConnConfig: rpcclient.ConnConfig{
Host: config.RpcHost,
},
Params: params,
}
return Config
}
27 changes: 26 additions & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const (
DefaultMaxOpenConnections = 0

DefaultBitcoinNetworkName = "mainnet"

DefaultBitcoinRpchost = "localhost:8332"
DefaultBitcoinRpcUser = "b2node"
DefaultBitcoinRpcPass = "b2node"
DefaultBitcoinWalletName = "b2node"
)

var evmTracers = []string{"json", "markdown", "struct", "access_list"}
Expand Down Expand Up @@ -365,6 +370,10 @@ func GetConfig(v *viper.Viper) (Config, error) {
},
BITCOIN: BITCOINConfig{
NetworkName: v.GetString("bitcoin.network-name"),
RpcHost: v.GetString("bitcoin.rpc-host"),
RpcUser: v.GetString("bitcoin.rpc-user"),
RpcPass: v.GetString("bitcoin.rpc-pass"),
WalletName: v.GetString("bitcoin.wallet-name"),
},
}, nil
}
Expand Down Expand Up @@ -403,12 +412,20 @@ func (c Config) ValidateBasic() error {
type BITCOINConfig struct {
// NetworkName defines the bitcoin network name
NetworkName string `mapstructure:"network-name"`
RpcHost string `mapstructure:"rpc-host"`
RpcUser string `mapstructure:"rpc-user"`
RpcPass string `mapstructure:"rpc-pass"`
WalletName string `mapstructrue:"wallet-name"`
}

// DefaultBitcoinConfig returns the default Bitcon configuration
func DefaultBitcoinConfig() *BITCOINConfig {
return &BITCOINConfig{
NetworkName: DefaultBitcoinNetworkName,
RpcHost: DefaultBitcoinRpchost,
RpcUser: DefaultBitcoinRpcUser,
RpcPass: DefaultBitcoinRpcPass,
WalletName: DefaultBitcoinWalletName,
}
}

Expand All @@ -417,6 +434,14 @@ func (c BITCOINConfig) Validate() error {
if c.NetworkName != "" && !strings.StringInSlice(c.NetworkName, bitcoinNetworkNames) {
return fmt.Errorf("invalid network name %s, available names: %v", c.NetworkName, bitcoinNetworkNames)
}

if c.RpcHost != "" {
return fmt.Errorf("rpc host is not allowed empty")
}
if c.RpcPass != "" {
return fmt.Errorf("rpc pass is not allowed empty")
}
if c.WalletName != "" {
return fmt.Errorf("rpc wallet name is not allowed empty")
}
return nil
}
4 changes: 4 additions & 0 deletions server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,8 @@ key-path = "{{ .TLS.KeyPath }}"
# Network defines the bitcoin network name to use.
# Names: "mainnet", "testnet", "regtest", "simnet", "testnet3", "signet"
network-name = "{{ .BITCOIN.NetworkName }}"
rpc-host = "{{ .BITCOIN.RpcHost }}"
rpc-user = "{{ .BITCOIN.RpcUser }}"
rpc-pass = "{{ .BITCOIN.RpcPass }}"
wallet-name = "{{ .BITCOIN.WalletName }}"
`
9 changes: 9 additions & 0 deletions server/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ const (
TLSKeyPath = "tls.key-path"
)

// BITCOIN flags
const (
BITCOINNetworkName = "bitcoin.network-name"
BITCOINRpcHost = "bitcoin.rpc-host"
BITCOINRpcUser = "bitcoin.rpc-user"
BITCOINRpcPass = "bitcoin.rpc-pass"

Check failure

Code scanning / gosec

Potential hardcoded credentials Error

Potential hardcoded credentials
BITCOINWalletName = "bitcoin.wallet-name"
)

// AddTxFlags adds common flags for commands to post tx
func AddTxFlags(cmd *cobra.Command) (*cobra.Command, error) {
cmd.PersistentFlags().String(flags.FlagChainID, "testnet", "Specify Chain ID for sending Tx")
Expand Down
8 changes: 8 additions & 0 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package server
import (
"context"
"fmt"
"github.com/evmos/ethermint/bitcoin"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -217,6 +218,11 @@ which accepts a path for the resulting pprof file.
cmd.Flags().String(srvflags.TLSCertPath, "", "the cert.pem file path for the server TLS configuration")
cmd.Flags().String(srvflags.TLSKeyPath, "", "the key.pem file path for the server TLS configuration")

cmd.Flags().String(srvflags.BITCOINNetworkName, config.DefaultBitcoinNetworkName, "Sets the bitcoin network type")
cmd.Flags().String(srvflags.BITCOINRpcHost, config.DefaultBitcoinRpchost, "Sets the bitcoin network rpc host")
cmd.Flags().String(srvflags.BITCOINRpcUser, config.DefaultBitcoinRpcUser, "Sets the bitcoin network rpc user")
cmd.Flags().String(srvflags.BITCOINRpcPass, config.DefaultBitcoinRpcPass, "Sets the bitcoin network rpc password")

cmd.Flags().Uint64(server.FlagStateSyncSnapshotInterval, 0, "State sync snapshot interval")
cmd.Flags().Uint32(server.FlagStateSyncSnapshotKeepRecent, 2, "State sync snapshot to keep")

Expand Down Expand Up @@ -645,6 +651,8 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, opts StartOpt
// bitcoin indexer run go routine handle bitcoin transaction
// or bitcoin commiter logic

// construct bitcoin rpc params
bitcoin.SetBitcoinConfig(config.BITCOIN)
// Wait for SIGINT or SIGTERM signal
return server.WaitForQuitSignals()
}
Expand Down

0 comments on commit 691fd81

Please sign in to comment.