lndclient: NewLndServices now accepts a macaroon directory, not admin mac path

In this commit, we modify the `NewLndServices` method to accept the
directory where macaroons are stored, rather than the path for the admin
macaroon. This is a prep to moving towards a multi-macaroon system, as
the `macaroonPouch` will handle storing the various macaroons.
pull/45/head
Olaoluwa Osuntokun 5 years ago
parent ee6191f844
commit 2c97258ce7
No known key found for this signature in database
GPG Key ID: CE58F7F8E20FD9A2

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"path/filepath" "path/filepath"
"time" "time"
@ -12,11 +11,8 @@ import (
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/lightninglabs/loop/swap" "github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/macaroons"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
macaroon "gopkg.in/macaroon.v2"
) )
var rpcTimeout = 30 * time.Second var rpcTimeout = 30 * time.Second
@ -30,6 +26,8 @@ type LndServices struct {
Invoices InvoicesClient Invoices InvoicesClient
ChainParams *chaincfg.Params ChainParams *chaincfg.Params
macaroons *macaroonPouch
} }
// GrpcLndServices constitutes a set of required RPC services. // GrpcLndServices constitutes a set of required RPC services.
@ -40,13 +38,25 @@ type GrpcLndServices struct {
} }
// NewLndServices creates a set of required RPC services. // NewLndServices creates a set of required RPC services.
func NewLndServices(lndAddress string, application string, func NewLndServices(lndAddress, application, network, macaroonDir,
network string, macPath, tlsPath string) ( tlsPath string) (*GrpcLndServices, error) {
*GrpcLndServices, error) {
// If the macaroon directory isn't set, then we can't proceed as we
// need then to obtain the macaroons for all sub-servers.
if macaroonDir == "" {
return nil, fmt.Errorf("macarooon dir must be set")
}
// Now that we've ensured our macaroon directory is set properly, we
// can retrieve our full macaroon pouch from the directory.
macaroons, err := newMacaroonPouch(macaroonDir)
if err != nil {
return nil, fmt.Errorf("unable to obtain macaroons: %v", err)
}
// Setup connection with lnd // Setup connection with lnd
logger.Infof("Creating lnd connection to %v", lndAddress) logger.Infof("Creating lnd connection to %v", lndAddress)
conn, err := getClientConn(lndAddress, network, macPath, tlsPath) conn, err := getClientConn(lndAddress, network, tlsPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -101,6 +111,7 @@ func NewLndServices(lndAddress string, application string,
Signer: signerClient, Signer: signerClient,
Invoices: invoicesClient, Invoices: invoicesClient,
ChainParams: chainParams, ChainParams: chainParams,
macaroons: macaroons,
}, },
cleanup: cleanup, cleanup: cleanup,
} }
@ -135,7 +146,7 @@ var (
defaultSignerFilename = "signer.macaroon" defaultSignerFilename = "signer.macaroon"
) )
func getClientConn(address string, network string, macPath, tlsPath string) ( func getClientConn(address string, network string, tlsPath string) (
*grpc.ClientConn, error) { *grpc.ClientConn, error) {
// Load the specified TLS certificate and build transport credentials // Load the specified TLS certificate and build transport credentials
@ -154,28 +165,6 @@ func getClientConn(address string, network string, macPath, tlsPath string) (
grpc.WithTransportCredentials(creds), grpc.WithTransportCredentials(creds),
} }
if macPath == "" {
macPath = filepath.Join(
defaultLndDir, defaultDataDir, defaultChainSubDir,
"bitcoin", network, defaultAdminMacaroonFilename,
)
}
// Load the specified macaroon file.
macBytes, err := ioutil.ReadFile(macPath)
if err == nil {
// Only if file is found
mac := &macaroon.Macaroon{}
if err = mac.UnmarshalBinary(macBytes); err != nil {
return nil, fmt.Errorf("unable to decode macaroon: %v",
err)
}
// Now we append the macaroon credentials to the dial options.
cred := macaroons.NewMacaroonCredential(mac)
opts = append(opts, grpc.WithPerRPCCredentials(cred))
}
// We need to use a custom dialer so we can also connect to unix sockets // We need to use a custom dialer so we can also connect to unix sockets
// and not just TCP addresses. // and not just TCP addresses.
opts = append( opts = append(

Loading…
Cancel
Save