From 0085e2a38a9bf4c473affd2d903c7aa976e9a374 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 29 Aug 2016 21:39:01 -0700 Subject: [PATCH] config: add an option to pass in a hex-encoded rpc cert (--rawrpccert) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds an option to pass in a raw hex-encoded rpc cert via lnd’s configuration file. Such a change allows for programmatically creating lnd nodes which can connect to an existing btcd instance without requiring a file for the rpc cert to be specified. Additionally, this commit makes the creation of an integration testing harness easier. --- config.go | 1 + lnd.go | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/config.go b/config.go index 20e93c09..ecd448bf 100644 --- a/config.go +++ b/config.go @@ -63,6 +63,7 @@ type config struct { RPCPass string `short:"P" long:"rpcpass" default-mask:"-" description:"Password for RPC connections"` RPCCert string `long:"rpccert" description:"File containing btcd's certificate file"` + RawRPCCert string `long:"rawrpccert" description:"The raw bytes of btcd's PEM-encoded certificate chain which will be used to authenticate the RPC connection."` SPVHostAdr string `long:"spvhostadr" description:"Address of full bitcoin node. It is used in SPV mode."` TestNet3 bool `long:"testnet" description:"Use the test network"` SimNet bool `long:"simnet" description:"Use the simulation test network"` diff --git a/lnd.go b/lnd.go index a2ccc966..bb8cb928 100644 --- a/lnd.go +++ b/lnd.go @@ -1,6 +1,7 @@ package main import ( + "encoding/hex" "fmt" "io/ioutil" "net" @@ -64,26 +65,44 @@ func lndMain() error { } defer chanDB.Close() - // Read btcd's rpc cert for lnwallet's convenience. - f, err := os.Open(loadedConfig.RPCCert) + // Next load btcd's TLS cert for the RPC connection. If a raw cert was + // specified in the config, then we'll se that directly. Otherwise, we + // attempt to read the cert from the path specified in the config. + var rpcCert []byte + if loadedConfig.RawRPCCert != "" { + rpcCert, err = hex.DecodeString(loadedConfig.RawRPCCert) + if err != nil { + return err + } + } else { + certFile, err := os.Open(loadedConfig.RPCCert) + if err != nil { + return err + } + rpcCert, err = ioutil.ReadAll(certFile) + if err != nil { + return err + } + if err := certFile.Close(); err != nil { + return err + } + } + + rpcIP, err := net.LookupHost(loadedConfig.RPCHost) if err != nil { + fmt.Printf("unable to resolve rpc host: %v", err) return err } - cert, err := ioutil.ReadAll(f) - if err != nil { - return err - } - defer f.Close() // Create, and start the lnwallet, which handles the core payment channel // logic, and exposes control via proxy state machines. config := &lnwallet.Config{ PrivatePass: []byte("hello"), DataDir: filepath.Join(loadedConfig.DataDir, "lnwallet"), - RpcHost: fmt.Sprintf("%v:%v", loadedConfig.RPCHost, activeNetParams.rpcPort), + RpcHost: fmt.Sprintf("%v:%v", rpcIP[0], activeNetParams.rpcPort), RpcUser: loadedConfig.RPCUser, RpcPass: loadedConfig.RPCPass, - CACert: cert, + CACert: rpcCert, NetParams: activeNetParams.Params, } wallet, err := lnwallet.NewLightningWallet(config, chanDB)