lightwalletd/frontend/rpc_client.go

50 lines
1.4 KiB
Go
Raw Normal View History

2019-01-22 11:05:49 -08:00
package frontend
import (
"net"
"github.com/btcsuite/btcd/rpcclient"
"github.com/pkg/errors"
ini "gopkg.in/ini.v1"
)
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)
}
// 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 == "" {
rpcport = "8232" // mainnet
}
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
}