lightwalletd/frontend/rpc_client.go

74 lines
2.4 KiB
Go
Raw Normal View History

2020-03-12 10:57:02 -07:00
// Copyright (c) 2019-2020 The Zcash developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
2020-05-15 10:52:13 -07:00
2019-01-22 11:05:49 -08:00
package frontend
import (
"net"
"github.com/btcsuite/btcd/rpcclient"
"github.com/pkg/errors"
"github.com/zcash/lightwalletd/common"
2019-01-22 11:05:49 -08:00
ini "gopkg.in/ini.v1"
)
2020-05-15 10:52:13 -07:00
// NewZRPCFromConf reads the zcashd configuration file.
2020-01-31 14:17:03 -08:00
func NewZRPCFromConf(confPath interface{}) (*rpcclient.Client, error) {
connCfg, err := connFromConf(confPath)
if err != nil {
return nil, err
}
return rpcclient.New(connCfg, nil)
}
// NewZRPCFromFlags gets zcashd rpc connection information from provided flags.
func NewZRPCFromFlags(opts *common.Options) (*rpcclient.Client, error) {
// Connect to local Zcash RPC server using HTTP POST mode.
connCfg := &rpcclient.ConnConfig{
Host: net.JoinHostPort(opts.RPCHost, opts.RPCPort),
User: opts.RPCUser,
Pass: opts.RPCPassword,
HTTPPostMode: true, // Zcash only supports HTTP POST mode
DisableTLS: true, // Zcash does not provide TLS by default
}
return rpcclient.New(connCfg, nil)
}
2020-01-31 14:17:03 -08:00
// If passed a string, interpret as a path, open and read; if passed
// a byte slice, interpret as the config file content (used in testing).
func connFromConf(confPath interface{}) (*rpcclient.ConnConfig, error) {
2019-01-22 11:05:49 -08:00
cfg, err := ini.Load(confPath)
if err != nil {
return nil, errors.Wrap(err, "failed to read config file")
}
rpcaddr := cfg.Section("").Key("rpcbind").String()
2020-01-31 14:17:03 -08:00
if rpcaddr == "" {
rpcaddr = "127.0.0.1"
}
2019-01-22 11:05:49 -08:00
rpcport := cfg.Section("").Key("rpcport").String()
2020-01-31 14:17:03 -08:00
if rpcport == "" {
2020-04-13 21:03:54 -07:00
rpcport = "8232" // default mainnet
testnet, _ := cfg.Section("").Key("testnet").Int()
regtest, _ := cfg.Section("").Key("regtest").Int()
if testnet > 0 || regtest > 0 {
rpcport = "18232"
}
2020-01-31 14:17:03 -08:00
}
2019-01-22 11:05:49 -08:00
username := cfg.Section("").Key("rpcuser").String()
password := cfg.Section("").Key("rpcpassword").String()
2019-09-25 05:01:46 -07:00
// Connect to local Zcash RPC server using HTTP POST mode.
2019-01-22 11:05:49 -08:00
connCfg := &rpcclient.ConnConfig{
Host: net.JoinHostPort(rpcaddr, rpcport),
2019-01-22 11:05:49 -08:00
User: username,
Pass: password,
HTTPPostMode: true, // Zcash only supports HTTP POST mode
DisableTLS: true, // Zcash does not provide TLS by default
}
// Notice the notification parameter is nil since notifications are
// not supported in HTTP POST mode.
2020-01-31 14:17:03 -08:00
return connCfg, nil
2019-01-22 11:05:49 -08:00
}