diff --git a/lnrpc/walletrpc/config_active.go b/lnrpc/walletrpc/config_active.go new file mode 100644 index 00000000..6f7a207e --- /dev/null +++ b/lnrpc/walletrpc/config_active.go @@ -0,0 +1,41 @@ +// +build walletrpc + +package walletrpc + +import ( + "github.com/lightningnetwork/lnd/keychain" + "github.com/lightningnetwork/lnd/lnwallet" + "github.com/lightningnetwork/lnd/macaroons" +) + +// Config is the primary configuration struct for the WalletKit RPC server. It +// contains all the items required for the signer rpc server to carry out its +// duties. The fields with struct tags are meant to be parsed as normal +// configuration options, while if able to be populated, the latter fields MUST +// also be specified. +type Config struct { + // WalletKitMacPath is the path for the signer macaroon. If unspecified + // then we assume that the macaroon will be found under the network + // directory, named DefaultWalletKitMacFilename. + WalletKitMacPath string `long:"walletkitmacaroonpath" description:"Path to the wallet kit macaroon"` + + // NetworkDir is the main network directory wherein the signer rpc + // server will find the macaroon named DefaultWalletKitMacFilename. + NetworkDir string + + // MacService is the main macaroon service that we'll use to handle + // authentication for the signer rpc server. + MacService *macaroons.Service + + // FeeEstimator is an instance of the primary fee estimator instance + // the WalletKit will use to respond to fee estimation requests. + FeeEstimator lnwallet.FeeEstimator + + // Wallet is the primary wallet that the WalletKit will use to proxy + // any relevant requests to. + Wallet lnwallet.WalletController + + // KeyRing is an interface that the WalletKit will use to derive any + // keys due to incoming client requests. + KeyRing keychain.KeyRing +} diff --git a/lnrpc/walletrpc/config_default.go b/lnrpc/walletrpc/config_default.go new file mode 100644 index 00000000..16bbbcc8 --- /dev/null +++ b/lnrpc/walletrpc/config_default.go @@ -0,0 +1,8 @@ +// +build !walletrpc + +package walletrpc + +// Config is the primary configuration struct for the WalletKit RPC server. +// When the server isn't active (via the build flag), callers outside this +// package will see this shell of a config file. +type Config struct{} diff --git a/lnrpc/walletrpc/driver.go b/lnrpc/walletrpc/driver.go new file mode 100644 index 00000000..4135d3aa --- /dev/null +++ b/lnrpc/walletrpc/driver.go @@ -0,0 +1,73 @@ +// +build walletrpc + +package walletrpc + +import ( + "fmt" + + "github.com/lightningnetwork/lnd/lnrpc" +) + +// createNewSubServer is a helper method that will create the new WalletKit RPC +// sub server given the main config dispatcher method. If we're unable to find +// the config that is meant for us in the config dispatcher, then we'll exit +// with an error. +func createNewSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (lnrpc.SubServer, lnrpc.MacaroonPerms, error) { + // We'll attempt to look up the config that we expect, according to our + // subServerName name. If we can't find this, then we'll exit with an + // error, as we're unable to properly initialize ourselves without this + // config. + walletKitServerConf, ok := configRegistry.FetchConfig(subServerName) + if !ok { + return nil, nil, fmt.Errorf("unable to find config for "+ + "subserver type %s", subServerName) + } + + // Now that we've found an object mapping to our service name, we'll + // ensure that it's the type we need. + config, ok := walletKitServerConf.(*Config) + if !ok { + return nil, nil, fmt.Errorf("wrong type of config for "+ + "subserver %s, expected %T got %T", subServerName, + &Config{}, walletKitServerConf) + } + + // Before we try to make the new WalletKit service instance, we'll + // perform some sanity checks on the arguments to ensure that they're + // useable. + switch { + case config.MacService != nil && config.NetworkDir == "": + return nil, nil, fmt.Errorf("NetworkDir must be set to " + + "create WalletKit RPC server") + + case config.FeeEstimator == nil: + return nil, nil, fmt.Errorf("FeeEstimator must be set to " + + "create WalletKit RPC server") + + case config.Wallet == nil: + return nil, nil, fmt.Errorf("Wallet must be set to create " + + "WalletKit RPC server") + + case config.KeyRing == nil: + return nil, nil, fmt.Errorf("KeyRing must be set to create " + + "WalletKit RPC server") + } + + return New(config) +} + +func init() { + subServer := &lnrpc.SubServerDriver{ + SubServerName: subServerName, + New: func(c lnrpc.SubServerConfigDispatcher) (lnrpc.SubServer, lnrpc.MacaroonPerms, error) { + return createNewSubServer(c) + }, + } + + // If the build tag is active, then we'll register ourselves as a + // sub-RPC server within the global lnrpc package namespace. + if err := lnrpc.RegisterSubServer(subServer); err != nil { + panic(fmt.Sprintf("failed to register sub server driver '%s': %v", + subServerName, err)) + } +} diff --git a/lnrpc/walletrpc/log.go b/lnrpc/walletrpc/log.go new file mode 100644 index 00000000..6a0a5048 --- /dev/null +++ b/lnrpc/walletrpc/log.go @@ -0,0 +1,45 @@ +package walletrpc + +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) + +// log is a logger that is initialized with no output filters. This means the +// package will not perform any logging by default until the caller requests +// it. +var log btclog.Logger + +// The default amount of logging is none. +func init() { + UseLogger(build.NewSubLogger("WLKT", nil)) +} + +// DisableLog disables all library log output. Logging output is disabled by +// by default until UseLogger is called. +func DisableLog() { + UseLogger(btclog.Disabled) +} + +// UseLogger uses a specified Logger to output package logging info. This +// should be used in preference to SetLogWriter if the caller is also using +// btclog. +func UseLogger(logger btclog.Logger) { + log = logger +} + +// logClosure is used to provide a closure over expensive logging operations so +// don't have to be performed when the logging level doesn't warrant it. +type logClosure func() string + +// String invokes the underlying function and returns the result. +func (c logClosure) String() string { + return c() +} + +// newLogClosure returns a new closure over a function that returns a string +// which itself provides a Stringer interface so that it can be used with the +// logging system. +func newLogClosure(c func() string) logClosure { + return logClosure(c) +} diff --git a/lnrpc/walletrpc/walletkit_server.go b/lnrpc/walletrpc/walletkit_server.go new file mode 100644 index 00000000..18f5d319 --- /dev/null +++ b/lnrpc/walletrpc/walletkit_server.go @@ -0,0 +1,345 @@ +// +build walletrpc + +package walletrpc + +import ( + "bytes" + fmt "fmt" + "io/ioutil" + "os" + "path/filepath" + + "github.com/btcsuite/btcd/wire" + "github.com/lightningnetwork/lnd/keychain" + "github.com/lightningnetwork/lnd/lnrpc" + signrpc "github.com/lightningnetwork/lnd/lnrpc/signrpc" + "github.com/lightningnetwork/lnd/lnwallet" + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" + "gopkg.in/macaroon-bakery.v2/bakery" +) + +const ( + // subServerName is the name of the sub rpc server. We'll use this name + // to register ourselves, and we also require that the main + // SubServerConfigDispatcher instance recognize as the name of our + subServerName = "WalletKitRPC" +) + +var ( + // macaroonOps are the set of capabilities that our minted macaroon (if + // it doesn't already exist) will have. + macaroonOps = []bakery.Op{ + { + Entity: "address", + Action: "write", + }, + { + Entity: "address", + Action: "read", + }, + { + Entity: "onchain", + Action: "write", + }, + { + Entity: "onchain", + Action: "read", + }, + } + + // macPermissions maps RPC calls to the permissions they require. + macPermissions = map[string][]bakery.Op{ + "/walletrpc.WalletKit/DeriveNextKey": {{ + Entity: "address", + Action: "read", + }}, + "/walletrpc.WalletKit/DeriveKey": {{ + Entity: "address", + Action: "read", + }}, + "/walletrpc.WalletKit/NextAddr": {{ + Entity: "address", + Action: "read", + }}, + "/walletrpc.WalletKit/PublishTransaction": {{ + Entity: "onchain", + Action: "write", + }}, + "/walletrpc.WalletKit/SendOutputs": {{ + Entity: "onchain", + Action: "write", + }}, + "/walletrpc.WalletKit/EstimateFee": {{ + Entity: "onchain", + Action: "read", + }}, + } + + // DefaultWalletKitMacFilename is the default name of the wallet kit + // macaroon that we expect to find via a file handle within the main + // configuration file in this package. + DefaultWalletKitMacFilename = "walletkit.macaroon" +) + +// WalletKit is a sub-RPC server that exposes a tool kit which allows clients +// to execute common wallet operations. This includes requesting new addresses, +// keys (for contracts!), and publishing transactions. +type WalletKit struct { + cfg *Config +} + +// A compile time check to ensure that WalletKit fully implements the +// WalletKitServer gRPC service. +var _ WalletKitServer = (*WalletKit)(nil) + +// fileExists reports whether the named file or directory exists. +func fileExists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +} + +// New creates a new instance of the WalletKit sub-RPC server. +func New(cfg *Config) (*WalletKit, lnrpc.MacaroonPerms, error) { + // If the path of the wallet kit macaroon wasn't specified, then we'll + // assume that it's found at the default network directory. + if cfg.WalletKitMacPath == "" { + cfg.WalletKitMacPath = filepath.Join( + cfg.NetworkDir, DefaultWalletKitMacFilename, + ) + } + + // Now that we know the full path of the wallet kit macaroon, we can + // check to see if we need to create it or not. + macFilePath := cfg.WalletKitMacPath + if !fileExists(macFilePath) && cfg.MacService != nil { + log.Infof("Baking macaroons for WaleltKit RPC Server at: %v", + macFilePath) + + // At this point, we know that the wallet kit macaroon doesn't + // yet, exist, so we need to create it with the help of the + // main macaroon service. + walletKitMac, err := cfg.MacService.Oven.NewMacaroon( + context.Background(), bakery.LatestVersion, nil, + macaroonOps..., + ) + if err != nil { + return nil, nil, err + } + walletKitMacBytes, err := walletKitMac.M().MarshalBinary() + if err != nil { + return nil, nil, err + } + err = ioutil.WriteFile(macFilePath, walletKitMacBytes, 0644) + if err != nil { + os.Remove(macFilePath) + return nil, nil, err + } + } + + walletKit := &WalletKit{ + cfg: cfg, + } + + return walletKit, macPermissions, nil +} + +// Start launches any helper goroutines required for the sub-server to function. +// +// NOTE: This is part of the lnrpc.SubServer interface. +func (w *WalletKit) Start() error { + return nil +} + +// Stop signals any active goroutines for a graceful closure. +// +// NOTE: This is part of the lnrpc.SubServer interface. +func (w *WalletKit) Stop() error { + return nil +} + +// Name returns a unique string representation of the sub-server. This can be +// used to identify the sub-server and also de-duplicate them. +// +// NOTE: This is part of the lnrpc.SubServer interface. +func (w *WalletKit) Name() string { + return subServerName +} + +// RegisterWithRootServer will be called by the root gRPC server to direct a +// sub RPC server to register itself with the main gRPC root server. Until this +// is called, each sub-server won't be able to have requests routed towards it. +// +// NOTE: This is part of the lnrpc.SubServer interface. +func (w *WalletKit) RegisterWithRootServer(grpcServer *grpc.Server) error { + // We make sure that we register it with the main gRPC server to ensure + // all our methods are routed properly. + RegisterWalletKitServer(grpcServer, w) + + log.Debugf("WalletKit RPC server successfully registered with " + + "root gRPC server") + + return nil +} + +// DeriveNextKey attempts to derive the *next* key within the key family +// (account in BIP43) specified. This method should return the next external +// child within this branch. +func (w *WalletKit) DeriveNextKey(ctx context.Context, + req *KeyReq) (*signrpc.KeyDescriptor, error) { + + nextKeyDesc, err := w.cfg.KeyRing.DeriveNextKey( + keychain.KeyFamily(req.KeyFamily), + ) + if err != nil { + return nil, err + } + + return &signrpc.KeyDescriptor{ + Key: &signrpc.KeyDescriptor_KeyLoc{ + KeyLoc: &signrpc.KeyLocator{ + KeyFamily: int32(nextKeyDesc.Family), + KeyIndex: int32(nextKeyDesc.Index), + }, + }, + }, nil +} + +// DeriveKey attempts to derive an arbitrary key specified by the passed +// KeyLocator. +func (w *WalletKit) DeriveKey(ctx context.Context, + req *signrpc.KeyLocator) (*signrpc.KeyDescriptor, error) { + + keyDesc, err := w.cfg.KeyRing.DeriveKey(keychain.KeyLocator{ + Family: keychain.KeyFamily(req.KeyFamily), + Index: uint32(req.KeyIndex), + }) + if err != nil { + return nil, err + } + + return &signrpc.KeyDescriptor{ + Key: &signrpc.KeyDescriptor_KeyLoc{ + KeyLoc: &signrpc.KeyLocator{ + KeyFamily: int32(keyDesc.Family), + KeyIndex: int32(keyDesc.Index), + }, + }, + }, nil +} + +// NextAddr returns the next unused address within the wallet. +func (w *WalletKit) NextAddr(ctx context.Context, + req *AddrRequest) (*AddrResponse, error) { + + addr, err := w.cfg.Wallet.NewAddress(lnwallet.WitnessPubKey, false) + if err != nil { + return nil, err + } + + return &AddrResponse{ + Addr: addr.String(), + }, nil +} + +// Attempts to publish the passed transaction to the network. Once this returns +// without an error, the wallet will continually attempt to re-broadcast the +// transaction on start up, until it enters the chain. +func (w *WalletKit) PublishTransaction(ctx context.Context, + req *Transaction) (*PublishResponse, error) { + + switch { + // If the client doesn't specify a transaction, then there's nothing to + // publish. + case len(req.TxHex) == 0: + return nil, fmt.Errorf("must provide a transaction to " + + "publish") + } + + tx := &wire.MsgTx{} + txReader := bytes.NewReader(req.TxHex) + if err := tx.Deserialize(txReader); err != nil { + return nil, err + } + + err := w.cfg.Wallet.PublishTransaction(tx) + if err != nil { + return nil, err + } + + return &PublishResponse{}, nil +} + +// SendOutputs is similar to the existing sendmany call in Bitcoind, and allows +// the caller to create a transaction that sends to several outputs at once. +// This is ideal when wanting to batch create a set of transactions. +func (w *WalletKit) SendOutputs(ctx context.Context, + req *SendOutputsRequest) (*SendOutputsResponse, error) { + + switch { + // If the client didn't specify any outputs to create, then we can't + // proceed . + case len(req.Outputs) == 0: + return nil, fmt.Errorf("must specify at least one output " + + "to create") + } + + // Before we can request this transaction to be created, we'll need to + // amp the protos back into the format that the internal wallet will + // recognize. + outputsToCreate := make([]*wire.TxOut, 0, len(req.Outputs)) + for _, output := range req.Outputs { + outputsToCreate = append(outputsToCreate, &wire.TxOut{ + Value: output.Value, + PkScript: output.PkScript, + }) + } + + // Now that we have the outputs mapped, we can request that the wallet + // attempt to create this transaction. + tx, err := w.cfg.Wallet.SendOutputs( + outputsToCreate, lnwallet.SatPerKWeight(req.SatPerKw), + ) + if err != nil { + return nil, err + } + + var b bytes.Buffer + if err := tx.Serialize(&b); err != nil { + return nil, err + } + + return &SendOutputsResponse{ + RawTx: b.Bytes(), + }, nil +} + +// EstimateFee attempts to query the internal fee estimator of the wallet to +// determine the fee (in sat/kw) to attach to a transaction in order to achieve +// the confirmation target. +func (w *WalletKit) EstimateFee(ctx context.Context, + req *EstimateFeeRequest) (*EstimateFeeResponse, error) { + + switch { + // A confirmation target of zero doesn't make any sense. Similarly, we + // reject confirmation targets of 1 as they're unreasonable. + case req.ConfTarget == 0 || req.ConfTarget == 1: + return nil, fmt.Errorf("confirmation target must be greater " + + "than 1") + } + + satPerKw, err := w.cfg.FeeEstimator.EstimateFeePerKW( + uint32(req.ConfTarget), + ) + if err != nil { + return nil, err + } + + return &EstimateFeeResponse{ + SatPerKw: int64(satPerKw), + }, nil +}