2020-07-15 15:19:25 +02:00
|
|
|
// +build mobile
|
2019-01-24 14:51:06 +01:00
|
|
|
|
|
|
|
package lndmobile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
flags "github.com/jessevdk/go-flags"
|
|
|
|
"github.com/lightningnetwork/lnd"
|
2020-07-15 15:05:32 +02:00
|
|
|
"github.com/lightningnetwork/lnd/signal"
|
2019-01-24 14:51:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Start starts lnd in a new goroutine.
|
|
|
|
//
|
|
|
|
// extraArgs can be used to pass command line arguments to lnd that will
|
|
|
|
// override what is found in the config file. Example:
|
|
|
|
// extraArgs = "--bitcoin.testnet --lnddir=\"/tmp/folder name/\" --profile=5050"
|
2019-11-29 11:51:50 +01:00
|
|
|
//
|
|
|
|
// The unlockerReady callback is called when the WalletUnlocker service is
|
|
|
|
// ready, and rpcReady is called after the wallet has been unlocked and lnd is
|
|
|
|
// ready to accept RPC calls.
|
2020-01-13 12:30:59 +01:00
|
|
|
//
|
|
|
|
// NOTE: On mobile platforms the '--lnddir` argument should be set to the
|
|
|
|
// current app directory in order to ensure lnd has the permissions needed to
|
|
|
|
// write to it.
|
2019-11-29 11:51:50 +01:00
|
|
|
func Start(extraArgs string, unlockerReady, rpcReady Callback) {
|
2019-01-24 14:51:06 +01:00
|
|
|
// Split the argument string on "--" to get separated command line
|
|
|
|
// arguments.
|
|
|
|
var splitArgs []string
|
|
|
|
for _, a := range strings.Split(extraArgs, "--") {
|
2020-01-13 13:25:14 +01:00
|
|
|
// Trim any whitespace space, and ignore empty params.
|
|
|
|
a := strings.TrimSpace(a)
|
2019-01-24 14:51:06 +01:00
|
|
|
if a == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-13 13:25:14 +01:00
|
|
|
|
|
|
|
// Finally we prefix any non-empty string with -- to mimic the
|
|
|
|
// regular command line arguments.
|
|
|
|
splitArgs = append(splitArgs, "--"+a)
|
2019-01-24 14:51:06 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 15:05:32 +02:00
|
|
|
// Add the extra arguments to os.Args, as that will be parsed in
|
|
|
|
// LoadConfig below.
|
2019-01-24 14:51:06 +01:00
|
|
|
os.Args = append(os.Args, splitArgs...)
|
|
|
|
|
2020-07-15 15:05:32 +02:00
|
|
|
// Load the configuration, and parse the extra arguments as command
|
|
|
|
// line options. This function will also set up logging properly.
|
|
|
|
loadedConfig, err := lnd.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, err)
|
2021-03-09 14:25:43 +01:00
|
|
|
rpcReady.OnError(err)
|
|
|
|
return
|
2020-07-15 15:05:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hook interceptor for os signals.
|
2020-08-24 08:54:34 +02:00
|
|
|
if err := signal.Intercept(); err != nil {
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, err)
|
2021-03-09 14:25:43 +01:00
|
|
|
rpcReady.OnError(err)
|
|
|
|
return
|
2020-08-24 08:54:34 +02:00
|
|
|
}
|
2020-07-15 15:05:32 +02:00
|
|
|
|
2019-11-29 11:51:50 +01:00
|
|
|
// Set up channels that will be notified when the RPC servers are ready
|
|
|
|
// to accept calls.
|
|
|
|
var (
|
|
|
|
unlockerListening = make(chan struct{})
|
|
|
|
rpcListening = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
2019-01-24 14:51:06 +01:00
|
|
|
// We call the main method with the custom in-memory listeners called
|
|
|
|
// by the mobile APIs, such that the grpc server will use these.
|
|
|
|
cfg := lnd.ListenerCfg{
|
2019-11-29 11:51:50 +01:00
|
|
|
WalletUnlocker: &lnd.ListenerWithSignal{
|
|
|
|
Listener: walletUnlockerLis,
|
|
|
|
Ready: unlockerListening,
|
|
|
|
},
|
|
|
|
RPCListener: &lnd.ListenerWithSignal{
|
|
|
|
Listener: lightningLis,
|
|
|
|
Ready: rpcListening,
|
|
|
|
},
|
2019-01-24 14:51:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call the "real" main in a nested manner so the defers will properly
|
|
|
|
// be executed in the case of a graceful shutdown.
|
|
|
|
go func() {
|
2020-07-15 15:05:32 +02:00
|
|
|
if err := lnd.Main(
|
|
|
|
loadedConfig, cfg, signal.ShutdownChannel(),
|
|
|
|
); err != nil {
|
2019-01-24 14:51:06 +01:00
|
|
|
if e, ok := err.(*flags.Error); ok &&
|
|
|
|
e.Type == flags.ErrHelp {
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2021-03-09 14:25:43 +01:00
|
|
|
rpcReady.OnError(err)
|
|
|
|
return
|
2019-01-24 14:51:06 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-29 11:51:50 +01:00
|
|
|
// Finally we start two go routines that will call the provided
|
|
|
|
// callbacks when the RPC servers are ready to accept calls.
|
|
|
|
go func() {
|
|
|
|
<-unlockerListening
|
2020-01-13 12:21:39 +01:00
|
|
|
|
|
|
|
// We must set the TLS certificates in order to properly
|
|
|
|
// authenticate with the wallet unlocker service.
|
2020-07-15 15:05:32 +02:00
|
|
|
auth, err := lnd.WalletUnlockerAuthOptions(loadedConfig)
|
2020-01-13 12:21:39 +01:00
|
|
|
if err != nil {
|
|
|
|
unlockerReady.OnError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the auth options to the listener's dial options.
|
|
|
|
addWalletUnlockerLisDialOption(auth...)
|
|
|
|
|
2019-11-29 11:51:50 +01:00
|
|
|
unlockerReady.OnResponse([]byte{})
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-rpcListening
|
2019-12-02 13:26:06 +01:00
|
|
|
|
|
|
|
// Now that the RPC server is ready, we can get the needed
|
|
|
|
// authentication options, and add them to the global dial
|
|
|
|
// options.
|
2020-07-15 15:05:32 +02:00
|
|
|
auth, err := lnd.AdminAuthOptions(loadedConfig)
|
2019-12-02 13:26:06 +01:00
|
|
|
if err != nil {
|
|
|
|
rpcReady.OnError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the auth options to the listener's dial options.
|
|
|
|
addLightningLisDialOption(auth...)
|
|
|
|
|
2019-11-29 11:51:50 +01:00
|
|
|
rpcReady.OnResponse([]byte{})
|
|
|
|
}()
|
2019-01-24 14:51:06 +01:00
|
|
|
}
|