github.com/decred/dcrlnd@v0.7.6/lnrpc/signrpc/driver.go (about)

     1  //go:build !no_signrpc
     2  // +build !no_signrpc
     3  
     4  package signrpc
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/decred/dcrlnd/lnrpc"
    10  )
    11  
    12  // createNewSubServer is a helper method that will create the new signer sub
    13  // server given the main config dispatcher method. If we're unable to find the
    14  // config that is meant for us in the config dispatcher, then we'll exit with
    15  // an error.
    16  func createNewSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (
    17  	*Server, lnrpc.MacaroonPerms, error) {
    18  
    19  	// We'll attempt to look up the config that we expect, according to our
    20  	// subServerName name. If we can't find this, then we'll exit with an
    21  	// error, as we're unable to properly initialize ourselves without this
    22  	// config.
    23  	signServerConf, ok := configRegistry.FetchConfig(subServerName)
    24  	if !ok {
    25  		return nil, nil, fmt.Errorf("unable to find config for "+
    26  			"subserver type %s", subServerName)
    27  	}
    28  
    29  	// Now that we've found an object mapping to our service name, we'll
    30  	// ensure that it's the type we need.
    31  	config, ok := signServerConf.(*Config)
    32  	if !ok {
    33  		return nil, nil, fmt.Errorf("wrong type of config for "+
    34  			"subserver %s, expected %T got %T", subServerName,
    35  			&Config{}, signServerConf)
    36  	}
    37  
    38  	// Before we try to make the new signer service instance, we'll perform
    39  	// some sanity checks on the arguments to ensure that they're useable.
    40  
    41  	switch {
    42  	// If the macaroon service is set (we should use macaroons), then
    43  	// ensure that we know where to look for them, or create them if not
    44  	// found.
    45  	case config.MacService != nil && config.NetworkDir == "":
    46  		return nil, nil, fmt.Errorf("NetworkDir must be set to create " +
    47  			"Signrpc")
    48  	case config.Signer == nil:
    49  		return nil, nil, fmt.Errorf("Signer must be set to create " +
    50  			"Signrpc")
    51  	}
    52  
    53  	return New(config)
    54  }
    55  
    56  func init() {
    57  	subServer := &lnrpc.SubServerDriver{
    58  		SubServerName: subServerName,
    59  		NewGrpcHandler: func() lnrpc.GrpcHandler {
    60  			return &ServerShell{}
    61  		},
    62  	}
    63  
    64  	// If the build tag is active, then we'll register ourselves as a
    65  	// sub-RPC server within the global lnrpc package namespace.
    66  	if err := lnrpc.RegisterSubServer(subServer); err != nil {
    67  		panic(fmt.Sprintf("failed to register sub server driver '%s': %v",
    68  			subServerName, err))
    69  	}
    70  }