Skip to content

Commit 32fe002

Browse files
authored
Merge pull request #284 from zcash/zcash-rpc-from-flags
Added option to provide Zcashd RPC parameters from flags
2 parents fd94d8b + efe90fd commit 32fe002

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

cmd/root.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"syscall"
1212
"time"
1313

14+
"github.com/btcsuite/btcd/rpcclient"
1415
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
1516
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
1617
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -45,6 +46,10 @@ var rootCmd = &cobra.Command{
4546
LogLevel: viper.GetUint64("log-level"),
4647
LogFile: viper.GetString("log-file"),
4748
ZcashConfPath: viper.GetString("zcash-conf-path"),
49+
RPCUser: viper.GetString("rpcuser"),
50+
RPCPassword: viper.GetString("rpcpassword"),
51+
RPCHost: viper.GetString("rpchost"),
52+
RPCPort: viper.GetString("rpcport"),
4853
NoTLSVeryInsecure: viper.GetBool("no-tls-very-insecure"),
4954
GenCertVeryInsecure: viper.GetBool("gen-cert-very-insecure"),
5055
DataDir: viper.GetString("data-dir"),
@@ -61,7 +66,7 @@ var rootCmd = &cobra.Command{
6166
if !fileExists(opts.LogFile) {
6267
os.OpenFile(opts.LogFile, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
6368
}
64-
if !opts.Darkside {
69+
if !opts.Darkside && (opts.RPCUser == "" || opts.RPCPassword == "" || opts.RPCHost == "" || opts.RPCPort == "") {
6570
filesThatShouldExist = append(filesThatShouldExist, opts.ZcashConfPath)
6671
}
6772
if !opts.NoTLSVeryInsecure && !opts.GenCertVeryInsecure {
@@ -176,10 +181,16 @@ func startServer(opts *common.Options) error {
176181
var blockHeight int
177182
var chainName string
178183
var branchID string
184+
var rpcClient *rpcclient.Client
185+
var err error
179186
if opts.Darkside {
180187
chainName = "darkside"
181188
} else {
182-
rpcClient, err := frontend.NewZRPCFromConf(opts.ZcashConfPath)
189+
if opts.RPCUser != "" && opts.RPCPassword != "" && opts.RPCHost != "" && opts.RPCPort != "" {
190+
rpcClient, err = frontend.NewZRPCFromFlags(opts)
191+
} else {
192+
rpcClient, err = frontend.NewZRPCFromConf(opts.ZcashConfPath)
193+
}
183194
if err != nil {
184195
common.Log.WithFields(logrus.Fields{
185196
"error": err,
@@ -287,6 +298,10 @@ func init() {
287298
rootCmd.Flags().Int("log-level", int(logrus.InfoLevel), "log level (logrus 1-7)")
288299
rootCmd.Flags().String("log-file", "./server.log", "log file to write to")
289300
rootCmd.Flags().String("zcash-conf-path", "./zcash.conf", "conf file to pull RPC creds from")
301+
rootCmd.Flags().String("rpcuser", "", "RPC user name")
302+
rootCmd.Flags().String("rpcpassword", "", "RPC password")
303+
rootCmd.Flags().String("rpchost", "", "RPC host")
304+
rootCmd.Flags().String("rpcport", "", "RPC host port")
290305
rootCmd.Flags().Bool("no-tls-very-insecure", false, "run without the required TLS certificate, only for debugging, DO NOT use in production")
291306
rootCmd.Flags().Bool("gen-cert-very-insecure", false, "run with self-signed TLS certificate, only for debugging, DO NOT use in production")
292307
rootCmd.Flags().Bool("redownload", false, "re-fetch all blocks from zcashd; reinitialize local cache files")
@@ -308,6 +323,10 @@ func init() {
308323
viper.SetDefault("log-file", "./server.log")
309324
viper.BindPFlag("zcash-conf-path", rootCmd.Flags().Lookup("zcash-conf-path"))
310325
viper.SetDefault("zcash-conf-path", "./zcash.conf")
326+
viper.BindPFlag("rpcuser", rootCmd.Flags().Lookup("rpcuser"))
327+
viper.BindPFlag("rpcpassword", rootCmd.Flags().Lookup("rpcpassword"))
328+
viper.BindPFlag("rpchost", rootCmd.Flags().Lookup("rpchost"))
329+
viper.BindPFlag("rpcport", rootCmd.Flags().Lookup("rpcport"))
311330
viper.BindPFlag("no-tls-very-insecure", rootCmd.Flags().Lookup("no-tls-very-insecure"))
312331
viper.SetDefault("no-tls-very-insecure", false)
313332
viper.BindPFlag("gen-cert-very-insecure", rootCmd.Flags().Lookup("gen-cert-very-insecure"))

common/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ type Options struct {
3333
LogLevel uint64 `json:"log_level,omitempty"`
3434
LogFile string `json:"log_file,omitempty"`
3535
ZcashConfPath string `json:"zcash_conf,omitempty"`
36+
RPCUser string `json:"rpcuser"`
37+
RPCPassword string `json:"rpcpassword"`
38+
RPCHost string `json:"rpchost"`
39+
RPCPort string `json:"rpcport"`
3640
NoTLSVeryInsecure bool `json:"no_tls_very_insecure,omitempty"`
3741
GenCertVeryInsecure bool `json:"gen_cert_very_insecure,omitempty"`
3842
Redownload bool `json:"redownload"`

frontend/rpc_client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/btcsuite/btcd/rpcclient"
1111
"github.com/pkg/errors"
12+
"github.com/zcash/lightwalletd/common"
1213
ini "gopkg.in/ini.v1"
1314
)
1415

@@ -21,6 +22,19 @@ func NewZRPCFromConf(confPath interface{}) (*rpcclient.Client, error) {
2122
return rpcclient.New(connCfg, nil)
2223
}
2324

25+
// NewZRPCFromFlags gets zcashd rpc connection information from provided flags.
26+
func NewZRPCFromFlags(opts *common.Options) (*rpcclient.Client, error) {
27+
// Connect to local Zcash RPC server using HTTP POST mode.
28+
connCfg := &rpcclient.ConnConfig{
29+
Host: net.JoinHostPort(opts.RPCHost, opts.RPCPort),
30+
User: opts.RPCUser,
31+
Pass: opts.RPCPassword,
32+
HTTPPostMode: true, // Zcash only supports HTTP POST mode
33+
DisableTLS: true, // Zcash does not provide TLS by default
34+
}
35+
return rpcclient.New(connCfg, nil)
36+
}
37+
2438
// If passed a string, interpret as a path, open and read; if passed
2539
// a byte slice, interpret as the config file content (used in testing).
2640
func connFromConf(confPath interface{}) (*rpcclient.ConnConfig, error) {

0 commit comments

Comments
 (0)