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

     1  //go:build !no_walletrpc
     2  // +build !no_walletrpc
     3  
     4  package walletrpc
     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 WalletKit RPC
    13  // sub server given the main config dispatcher method. If we're unable to find
    14  // the config that is meant for us in the config dispatcher, then we'll exit
    15  // with an error.
    16  func createNewSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (
    17  	*WalletKit, 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  	walletKitServerConf, 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 := walletKitServerConf.(*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{}, walletKitServerConf)
    36  	}
    37  
    38  	// Before we try to make the new WalletKit service instance, we'll
    39  	// perform some sanity checks on the arguments to ensure that they're
    40  	// useable.
    41  	switch {
    42  	case config.MacService != nil && config.NetworkDir == "":
    43  		return nil, nil, fmt.Errorf("NetworkDir must be set to " +
    44  			"create WalletKit RPC server")
    45  
    46  	case config.FeeEstimator == nil:
    47  		return nil, nil, fmt.Errorf("FeeEstimator must be set to " +
    48  			"create WalletKit RPC server")
    49  
    50  	case config.Wallet == nil:
    51  		return nil, nil, fmt.Errorf("Wallet must be set to create " +
    52  			"WalletKit RPC server")
    53  
    54  	case config.KeyRing == nil:
    55  		return nil, nil, fmt.Errorf("KeyRing must be set to create " +
    56  			"WalletKit RPC server")
    57  
    58  	case config.Sweeper == nil:
    59  		return nil, nil, fmt.Errorf("Sweeper must be set to create " +
    60  			"WalletKit RPC server")
    61  
    62  	case config.Chain == nil:
    63  		return nil, nil, fmt.Errorf("Chain must be set to create " +
    64  			"WalletKit RPC server")
    65  	}
    66  
    67  	return New(config)
    68  }
    69  
    70  func init() {
    71  	subServer := &lnrpc.SubServerDriver{
    72  		SubServerName: SubServerName,
    73  		NewGrpcHandler: func() lnrpc.GrpcHandler {
    74  			return &ServerShell{}
    75  		},
    76  	}
    77  
    78  	// If the build tag is active, then we'll register ourselves as a
    79  	// sub-RPC server within the global lnrpc package namespace.
    80  	if err := lnrpc.RegisterSubServer(subServer); err != nil {
    81  		panic(fmt.Sprintf("failed to register sub server driver '%s': %v",
    82  			SubServerName, err))
    83  	}
    84  }