config_builder.go 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. package lnd
  2. import (
  3. "bytes"
  4. "context"
  5. "database/sql"
  6. "errors"
  7. "fmt"
  8. "net"
  9. "os"
  10. "path/filepath"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "sync/atomic"
  15. "time"
  16. "github.com/btcsuite/btcd/chaincfg"
  17. "github.com/btcsuite/btcd/chaincfg/chainhash"
  18. "github.com/btcsuite/btcd/rpcclient"
  19. "github.com/btcsuite/btcd/wire"
  20. "github.com/btcsuite/btclog"
  21. "github.com/btcsuite/btcwallet/waddrmgr"
  22. "github.com/btcsuite/btcwallet/wallet"
  23. "github.com/btcsuite/btcwallet/walletdb"
  24. proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
  25. "github.com/lightninglabs/neutrino"
  26. "github.com/lightninglabs/neutrino/blockntfns"
  27. "github.com/lightninglabs/neutrino/headerfs"
  28. "github.com/lightninglabs/neutrino/pushtx"
  29. "github.com/lightningnetwork/lnd/blockcache"
  30. "github.com/lightningnetwork/lnd/chainntnfs"
  31. "github.com/lightningnetwork/lnd/chainreg"
  32. "github.com/lightningnetwork/lnd/channeldb"
  33. "github.com/lightningnetwork/lnd/clock"
  34. "github.com/lightningnetwork/lnd/invoices"
  35. "github.com/lightningnetwork/lnd/keychain"
  36. "github.com/lightningnetwork/lnd/kvdb"
  37. "github.com/lightningnetwork/lnd/lncfg"
  38. "github.com/lightningnetwork/lnd/lnrpc"
  39. "github.com/lightningnetwork/lnd/lnwallet"
  40. "github.com/lightningnetwork/lnd/lnwallet/btcwallet"
  41. "github.com/lightningnetwork/lnd/lnwallet/rpcwallet"
  42. "github.com/lightningnetwork/lnd/macaroons"
  43. "github.com/lightningnetwork/lnd/rpcperms"
  44. "github.com/lightningnetwork/lnd/signal"
  45. "github.com/lightningnetwork/lnd/sqldb"
  46. "github.com/lightningnetwork/lnd/walletunlocker"
  47. "github.com/lightningnetwork/lnd/watchtower"
  48. "github.com/lightningnetwork/lnd/watchtower/wtclient"
  49. "github.com/lightningnetwork/lnd/watchtower/wtdb"
  50. "google.golang.org/grpc"
  51. "gopkg.in/macaroon-bakery.v2/bakery"
  52. )
  53. // GrpcRegistrar is an interface that must be satisfied by an external subserver
  54. // that wants to be able to register its own gRPC server onto lnd's main
  55. // grpc.Server instance.
  56. type GrpcRegistrar interface {
  57. // RegisterGrpcSubserver is called for each net.Listener on which lnd
  58. // creates a grpc.Server instance. External subservers implementing this
  59. // method can then register their own gRPC server structs to the main
  60. // server instance.
  61. RegisterGrpcSubserver(*grpc.Server) error
  62. }
  63. // RestRegistrar is an interface that must be satisfied by an external subserver
  64. // that wants to be able to register its own REST mux onto lnd's main
  65. // proxy.ServeMux instance.
  66. type RestRegistrar interface {
  67. // RegisterRestSubserver is called after lnd creates the main
  68. // proxy.ServeMux instance. External subservers implementing this method
  69. // can then register their own REST proxy stubs to the main server
  70. // instance.
  71. RegisterRestSubserver(context.Context, *proxy.ServeMux, string,
  72. []grpc.DialOption) error
  73. }
  74. // ExternalValidator is an interface that must be satisfied by an external
  75. // macaroon validator.
  76. type ExternalValidator interface {
  77. macaroons.MacaroonValidator
  78. // Permissions returns the permissions that the external validator is
  79. // validating. It is a map between the full HTTP URI of each RPC and its
  80. // required macaroon permissions. If multiple action/entity tuples are
  81. // specified per URI, they are all required. See rpcserver.go for a list
  82. // of valid action and entity values.
  83. Permissions() map[string][]bakery.Op
  84. }
  85. // DatabaseBuilder is an interface that must be satisfied by the implementation
  86. // that provides lnd's main database backend instances.
  87. type DatabaseBuilder interface {
  88. // BuildDatabase extracts the current databases that we'll use for
  89. // normal operation in the daemon. A function closure that closes all
  90. // opened databases is also returned.
  91. BuildDatabase(ctx context.Context) (*DatabaseInstances, func(), error)
  92. }
  93. // WalletConfigBuilder is an interface that must be satisfied by a custom wallet
  94. // implementation.
  95. type WalletConfigBuilder interface {
  96. // BuildWalletConfig is responsible for creating or unlocking and then
  97. // fully initializing a wallet.
  98. BuildWalletConfig(context.Context, *DatabaseInstances,
  99. *rpcperms.InterceptorChain,
  100. []*ListenerWithSignal) (*chainreg.PartialChainControl,
  101. *btcwallet.Config, func(), error)
  102. }
  103. // ChainControlBuilder is an interface that must be satisfied by a custom wallet
  104. // implementation.
  105. type ChainControlBuilder interface {
  106. // BuildChainControl is responsible for creating a fully populated chain
  107. // control instance from a wallet.
  108. BuildChainControl(*chainreg.PartialChainControl,
  109. *btcwallet.Config) (*chainreg.ChainControl, func(), error)
  110. }
  111. // ImplementationCfg is a struct that holds all configuration items for
  112. // components that can be implemented outside lnd itself.
  113. type ImplementationCfg struct {
  114. // GrpcRegistrar is a type that can register additional gRPC subservers
  115. // before the main gRPC server is started.
  116. GrpcRegistrar
  117. // RestRegistrar is a type that can register additional REST subservers
  118. // before the main REST proxy is started.
  119. RestRegistrar
  120. // ExternalValidator is a type that can provide external macaroon
  121. // validation.
  122. ExternalValidator
  123. // DatabaseBuilder is a type that can provide lnd's main database
  124. // backend instances.
  125. DatabaseBuilder
  126. // WalletConfigBuilder is a type that can provide a wallet configuration
  127. // with a fully loaded and unlocked wallet.
  128. WalletConfigBuilder
  129. // ChainControlBuilder is a type that can provide a custom wallet
  130. // implementation.
  131. ChainControlBuilder
  132. }
  133. // DefaultWalletImpl is the default implementation of our normal, btcwallet
  134. // backed configuration.
  135. type DefaultWalletImpl struct {
  136. cfg *Config
  137. logger btclog.Logger
  138. interceptor signal.Interceptor
  139. watchOnly bool
  140. migrateWatchOnly bool
  141. pwService *walletunlocker.UnlockerService
  142. }
  143. // NewDefaultWalletImpl creates a new default wallet implementation.
  144. func NewDefaultWalletImpl(cfg *Config, logger btclog.Logger,
  145. interceptor signal.Interceptor, watchOnly bool) *DefaultWalletImpl {
  146. return &DefaultWalletImpl{
  147. cfg: cfg,
  148. logger: logger,
  149. interceptor: interceptor,
  150. watchOnly: watchOnly,
  151. pwService: createWalletUnlockerService(cfg),
  152. }
  153. }
  154. // RegisterRestSubserver is called after lnd creates the main proxy.ServeMux
  155. // instance. External subservers implementing this method can then register
  156. // their own REST proxy stubs to the main server instance.
  157. //
  158. // NOTE: This is part of the GrpcRegistrar interface.
  159. func (d *DefaultWalletImpl) RegisterRestSubserver(ctx context.Context,
  160. mux *proxy.ServeMux, restProxyDest string,
  161. restDialOpts []grpc.DialOption) error {
  162. return lnrpc.RegisterWalletUnlockerHandlerFromEndpoint(
  163. ctx, mux, restProxyDest, restDialOpts,
  164. )
  165. }
  166. // RegisterGrpcSubserver is called for each net.Listener on which lnd creates a
  167. // grpc.Server instance. External subservers implementing this method can then
  168. // register their own gRPC server structs to the main server instance.
  169. //
  170. // NOTE: This is part of the GrpcRegistrar interface.
  171. func (d *DefaultWalletImpl) RegisterGrpcSubserver(s *grpc.Server) error {
  172. lnrpc.RegisterWalletUnlockerServer(s, d.pwService)
  173. return nil
  174. }
  175. // ValidateMacaroon extracts the macaroon from the context's gRPC metadata,
  176. // checks its signature, makes sure all specified permissions for the called
  177. // method are contained within and finally ensures all caveat conditions are
  178. // met. A non-nil error is returned if any of the checks fail.
  179. //
  180. // NOTE: This is part of the ExternalValidator interface.
  181. func (d *DefaultWalletImpl) ValidateMacaroon(ctx context.Context,
  182. requiredPermissions []bakery.Op, fullMethod string) error {
  183. // Because the default implementation does not return any permissions,
  184. // we shouldn't be registered as an external validator at all and this
  185. // should never be invoked.
  186. return fmt.Errorf("default implementation does not support external " +
  187. "macaroon validation")
  188. }
  189. // Permissions returns the permissions that the external validator is
  190. // validating. It is a map between the full HTTP URI of each RPC and its
  191. // required macaroon permissions. If multiple action/entity tuples are specified
  192. // per URI, they are all required. See rpcserver.go for a list of valid action
  193. // and entity values.
  194. //
  195. // NOTE: This is part of the ExternalValidator interface.
  196. func (d *DefaultWalletImpl) Permissions() map[string][]bakery.Op {
  197. return nil
  198. }
  199. // BuildWalletConfig is responsible for creating or unlocking and then
  200. // fully initializing a wallet.
  201. //
  202. // NOTE: This is part of the WalletConfigBuilder interface.
  203. func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
  204. dbs *DatabaseInstances, interceptorChain *rpcperms.InterceptorChain,
  205. grpcListeners []*ListenerWithSignal) (*chainreg.PartialChainControl,
  206. *btcwallet.Config, func(), error) {
  207. // Keep track of our various cleanup functions. We use a defer function
  208. // as well to not repeat ourselves with every return statement.
  209. var (
  210. cleanUpTasks []func()
  211. earlyExit = true
  212. cleanUp = func() {
  213. for _, fn := range cleanUpTasks {
  214. if fn == nil {
  215. continue
  216. }
  217. fn()
  218. }
  219. }
  220. )
  221. defer func() {
  222. if earlyExit {
  223. cleanUp()
  224. }
  225. }()
  226. // Initialize a new block cache.
  227. blockCache := blockcache.NewBlockCache(d.cfg.BlockCacheSize)
  228. // Before starting the wallet, we'll create and start our Neutrino
  229. // light client instance, if enabled, in order to allow it to sync
  230. // while the rest of the daemon continues startup.
  231. mainChain := d.cfg.Bitcoin
  232. var neutrinoCS *neutrino.ChainService
  233. if mainChain.Node == "neutrino" {
  234. neutrinoBackend, neutrinoCleanUp, err := initNeutrinoBackend(
  235. ctx, d.cfg, mainChain.ChainDir, blockCache,
  236. )
  237. if err != nil {
  238. err := fmt.Errorf("unable to initialize neutrino "+
  239. "backend: %v", err)
  240. d.logger.Error(err)
  241. return nil, nil, nil, err
  242. }
  243. cleanUpTasks = append(cleanUpTasks, neutrinoCleanUp)
  244. neutrinoCS = neutrinoBackend
  245. }
  246. var (
  247. walletInitParams = walletunlocker.WalletUnlockParams{
  248. // In case we do auto-unlock, we need to be able to send
  249. // into the channel without blocking so we buffer it.
  250. MacResponseChan: make(chan []byte, 1),
  251. }
  252. privateWalletPw = lnwallet.DefaultPrivatePassphrase
  253. publicWalletPw = lnwallet.DefaultPublicPassphrase
  254. )
  255. // If the user didn't request a seed, then we'll manually assume a
  256. // wallet birthday of now, as otherwise the seed would've specified
  257. // this information.
  258. walletInitParams.Birthday = time.Now()
  259. d.pwService.SetLoaderOpts([]btcwallet.LoaderOption{dbs.WalletDB})
  260. d.pwService.SetMacaroonDB(dbs.MacaroonDB)
  261. walletExists, err := d.pwService.WalletExists()
  262. if err != nil {
  263. return nil, nil, nil, err
  264. }
  265. if !walletExists {
  266. interceptorChain.SetWalletNotCreated()
  267. } else {
  268. interceptorChain.SetWalletLocked()
  269. }
  270. // If we've started in auto unlock mode, then a wallet should already
  271. // exist because we don't want to enable the RPC unlocker in that case
  272. // for security reasons (an attacker could inject their seed since the
  273. // RPC is unauthenticated). Only if the user explicitly wants to allow
  274. // wallet creation we don't error out here.
  275. if d.cfg.WalletUnlockPasswordFile != "" && !walletExists &&
  276. !d.cfg.WalletUnlockAllowCreate {
  277. return nil, nil, nil, fmt.Errorf("wallet unlock password file " +
  278. "was specified but wallet does not exist; initialize " +
  279. "the wallet before using auto unlocking")
  280. }
  281. // What wallet mode are we running in? We've already made sure the no
  282. // seed backup and auto unlock aren't both set during config parsing.
  283. switch {
  284. // No seed backup means we're also using the default password.
  285. case d.cfg.NoSeedBackup:
  286. // We continue normally, the default password has already been
  287. // set above.
  288. // A password for unlocking is provided in a file.
  289. case d.cfg.WalletUnlockPasswordFile != "" && walletExists:
  290. d.logger.Infof("Attempting automatic wallet unlock with " +
  291. "password provided in file")
  292. pwBytes, err := os.ReadFile(d.cfg.WalletUnlockPasswordFile)
  293. if err != nil {
  294. return nil, nil, nil, fmt.Errorf("error reading "+
  295. "password from file %s: %v",
  296. d.cfg.WalletUnlockPasswordFile, err)
  297. }
  298. // Remove any newlines at the end of the file. The lndinit tool
  299. // won't ever write a newline but maybe the file was provisioned
  300. // by another process or user.
  301. pwBytes = bytes.TrimRight(pwBytes, "\r\n")
  302. // We have the password now, we can ask the unlocker service to
  303. // do the unlock for us.
  304. unlockedWallet, unloadWalletFn, err := d.pwService.LoadAndUnlock(
  305. pwBytes, 0,
  306. )
  307. if err != nil {
  308. return nil, nil, nil, fmt.Errorf("error unlocking "+
  309. "wallet with password from file: %v", err)
  310. }
  311. cleanUpTasks = append(cleanUpTasks, func() {
  312. if err := unloadWalletFn(); err != nil {
  313. d.logger.Errorf("Could not unload wallet: %v",
  314. err)
  315. }
  316. })
  317. privateWalletPw = pwBytes
  318. publicWalletPw = pwBytes
  319. walletInitParams.Wallet = unlockedWallet
  320. walletInitParams.UnloadWallet = unloadWalletFn
  321. // If none of the automatic startup options are selected, we fall back
  322. // to the default behavior of waiting for the wallet creation/unlocking
  323. // over RPC.
  324. default:
  325. if err := d.interceptor.Notifier.NotifyReady(false); err != nil {
  326. return nil, nil, nil, err
  327. }
  328. params, err := waitForWalletPassword(
  329. d.cfg, d.pwService, []btcwallet.LoaderOption{dbs.WalletDB},
  330. d.interceptor.ShutdownChannel(),
  331. )
  332. if err != nil {
  333. err := fmt.Errorf("unable to set up wallet password "+
  334. "listeners: %v", err)
  335. d.logger.Error(err)
  336. return nil, nil, nil, err
  337. }
  338. walletInitParams = *params
  339. privateWalletPw = walletInitParams.Password
  340. publicWalletPw = walletInitParams.Password
  341. cleanUpTasks = append(cleanUpTasks, func() {
  342. if err := walletInitParams.UnloadWallet(); err != nil {
  343. d.logger.Errorf("Could not unload wallet: %v",
  344. err)
  345. }
  346. })
  347. if walletInitParams.RecoveryWindow > 0 {
  348. d.logger.Infof("Wallet recovery mode enabled with "+
  349. "address lookahead of %d addresses",
  350. walletInitParams.RecoveryWindow)
  351. }
  352. }
  353. var macaroonService *macaroons.Service
  354. if !d.cfg.NoMacaroons {
  355. // Create the macaroon authentication/authorization service.
  356. rootKeyStore, err := macaroons.NewRootKeyStorage(dbs.MacaroonDB)
  357. if err != nil {
  358. return nil, nil, nil, err
  359. }
  360. macaroonService, err = macaroons.NewService(
  361. rootKeyStore, "lnd", walletInitParams.StatelessInit,
  362. macaroons.IPLockChecker,
  363. macaroons.CustomChecker(interceptorChain),
  364. )
  365. if err != nil {
  366. err := fmt.Errorf("unable to set up macaroon "+
  367. "authentication: %v", err)
  368. d.logger.Error(err)
  369. return nil, nil, nil, err
  370. }
  371. cleanUpTasks = append(cleanUpTasks, func() {
  372. if err := macaroonService.Close(); err != nil {
  373. d.logger.Errorf("Could not close macaroon "+
  374. "service: %v", err)
  375. }
  376. })
  377. // Try to unlock the macaroon store with the private password.
  378. // Ignore ErrAlreadyUnlocked since it could be unlocked by the
  379. // wallet unlocker.
  380. err = macaroonService.CreateUnlock(&privateWalletPw)
  381. if err != nil && err != macaroons.ErrAlreadyUnlocked {
  382. err := fmt.Errorf("unable to unlock macaroons: %w", err)
  383. d.logger.Error(err)
  384. return nil, nil, nil, err
  385. }
  386. // If we have a macaroon root key from the init wallet params,
  387. // set the root key before baking any macaroons.
  388. if len(walletInitParams.MacRootKey) > 0 {
  389. err := macaroonService.SetRootKey(
  390. walletInitParams.MacRootKey,
  391. )
  392. if err != nil {
  393. return nil, nil, nil, err
  394. }
  395. }
  396. // Send an admin macaroon to all our listeners that requested
  397. // one by setting a non-nil macaroon channel.
  398. adminMacBytes, err := bakeMacaroon(
  399. ctx, macaroonService, adminPermissions(),
  400. )
  401. if err != nil {
  402. return nil, nil, nil, err
  403. }
  404. for _, lis := range grpcListeners {
  405. if lis.MacChan != nil {
  406. lis.MacChan <- adminMacBytes
  407. }
  408. }
  409. // In case we actually needed to unlock the wallet, we now need
  410. // to create an instance of the admin macaroon and send it to
  411. // the unlocker so it can forward it to the user. In no seed
  412. // backup mode, there's nobody listening on the channel and we'd
  413. // block here forever.
  414. if !d.cfg.NoSeedBackup {
  415. // The channel is buffered by one element so writing
  416. // should not block here.
  417. walletInitParams.MacResponseChan <- adminMacBytes
  418. }
  419. // If the user requested a stateless initialization, no macaroon
  420. // files should be created.
  421. if !walletInitParams.StatelessInit {
  422. // Create default macaroon files for lncli to use if
  423. // they don't exist.
  424. err = genDefaultMacaroons(
  425. ctx, macaroonService, d.cfg.AdminMacPath,
  426. d.cfg.ReadMacPath, d.cfg.InvoiceMacPath,
  427. )
  428. if err != nil {
  429. err := fmt.Errorf("unable to create macaroons "+
  430. "%v", err)
  431. d.logger.Error(err)
  432. return nil, nil, nil, err
  433. }
  434. }
  435. // As a security service to the user, if they requested
  436. // stateless initialization and there are macaroon files on disk
  437. // we log a warning.
  438. if walletInitParams.StatelessInit {
  439. msg := "Found %s macaroon on disk (%s) even though " +
  440. "--stateless_init was requested. Unencrypted " +
  441. "state is accessible by the host system. You " +
  442. "should change the password and use " +
  443. "--new_mac_root_key with --stateless_init to " +
  444. "clean up and invalidate old macaroons."
  445. if lnrpc.FileExists(d.cfg.AdminMacPath) {
  446. d.logger.Warnf(msg, "admin", d.cfg.AdminMacPath)
  447. }
  448. if lnrpc.FileExists(d.cfg.ReadMacPath) {
  449. d.logger.Warnf(msg, "readonly", d.cfg.ReadMacPath)
  450. }
  451. if lnrpc.FileExists(d.cfg.InvoiceMacPath) {
  452. d.logger.Warnf(msg, "invoice", d.cfg.InvoiceMacPath)
  453. }
  454. }
  455. // We add the macaroon service to our RPC interceptor. This
  456. // will start checking macaroons against permissions on every
  457. // RPC invocation.
  458. interceptorChain.AddMacaroonService(macaroonService)
  459. }
  460. // Now that the wallet password has been provided, transition the RPC
  461. // state into Unlocked.
  462. interceptorChain.SetWalletUnlocked()
  463. // Since calls to the WalletUnlocker service wait for a response on the
  464. // macaroon channel, we close it here to make sure they return in case
  465. // we did not return the admin macaroon above. This will be the case if
  466. // --no-macaroons is used.
  467. close(walletInitParams.MacResponseChan)
  468. // We'll also close all the macaroon channels since lnd is done sending
  469. // macaroon data over it.
  470. for _, lis := range grpcListeners {
  471. if lis.MacChan != nil {
  472. close(lis.MacChan)
  473. }
  474. }
  475. // With the information parsed from the configuration, create valid
  476. // instances of the pertinent interfaces required to operate the
  477. // Lightning Network Daemon.
  478. //
  479. // When we create the chain control, we need storage for the height
  480. // hints and also the wallet itself, for these two we want them to be
  481. // replicated, so we'll pass in the remote channel DB instance.
  482. chainControlCfg := &chainreg.Config{
  483. Bitcoin: d.cfg.Bitcoin,
  484. HeightHintCacheQueryDisable: d.cfg.HeightHintCacheQueryDisable,
  485. NeutrinoMode: d.cfg.NeutrinoMode,
  486. BitcoindMode: d.cfg.BitcoindMode,
  487. BtcdMode: d.cfg.BtcdMode,
  488. HeightHintDB: dbs.HeightHintDB,
  489. ChanStateDB: dbs.ChanStateDB.ChannelStateDB(),
  490. NeutrinoCS: neutrinoCS,
  491. ActiveNetParams: d.cfg.ActiveNetParams,
  492. FeeURL: d.cfg.FeeURL,
  493. Fee: &lncfg.Fee{
  494. URL: d.cfg.Fee.URL,
  495. MinUpdateTimeout: d.cfg.Fee.MinUpdateTimeout,
  496. MaxUpdateTimeout: d.cfg.Fee.MaxUpdateTimeout,
  497. },
  498. Dialer: func(addr string) (net.Conn, error) {
  499. return d.cfg.net.Dial(
  500. "tcp", addr, d.cfg.ConnectionTimeout,
  501. )
  502. },
  503. BlockCache: blockCache,
  504. WalletUnlockParams: &walletInitParams,
  505. }
  506. // Let's go ahead and create the partial chain control now that is only
  507. // dependent on our configuration and doesn't require any wallet
  508. // specific information.
  509. partialChainControl, pccCleanup, err := chainreg.NewPartialChainControl(
  510. chainControlCfg,
  511. )
  512. cleanUpTasks = append(cleanUpTasks, pccCleanup)
  513. if err != nil {
  514. err := fmt.Errorf("unable to create partial chain control: %w",
  515. err)
  516. d.logger.Error(err)
  517. return nil, nil, nil, err
  518. }
  519. walletConfig := &btcwallet.Config{
  520. PrivatePass: privateWalletPw,
  521. PublicPass: publicWalletPw,
  522. Birthday: walletInitParams.Birthday,
  523. RecoveryWindow: walletInitParams.RecoveryWindow,
  524. NetParams: d.cfg.ActiveNetParams.Params,
  525. CoinType: d.cfg.ActiveNetParams.CoinType,
  526. Wallet: walletInitParams.Wallet,
  527. LoaderOptions: []btcwallet.LoaderOption{dbs.WalletDB},
  528. ChainSource: partialChainControl.ChainSource,
  529. WatchOnly: d.watchOnly,
  530. MigrateWatchOnly: d.migrateWatchOnly,
  531. }
  532. // Parse coin selection strategy.
  533. switch d.cfg.CoinSelectionStrategy {
  534. case "largest":
  535. walletConfig.CoinSelectionStrategy = wallet.CoinSelectionLargest
  536. case "random":
  537. walletConfig.CoinSelectionStrategy = wallet.CoinSelectionRandom
  538. default:
  539. return nil, nil, nil, fmt.Errorf("unknown coin selection "+
  540. "strategy %v", d.cfg.CoinSelectionStrategy)
  541. }
  542. earlyExit = false
  543. return partialChainControl, walletConfig, cleanUp, nil
  544. }
  545. // proxyBlockEpoch proxies a block epoch subsections to the underlying neutrino
  546. // rebroadcaster client.
  547. func proxyBlockEpoch(notifier chainntnfs.ChainNotifier,
  548. ) func() (*blockntfns.Subscription, error) {
  549. return func() (*blockntfns.Subscription, error) {
  550. blockEpoch, err := notifier.RegisterBlockEpochNtfn(
  551. nil,
  552. )
  553. if err != nil {
  554. return nil, err
  555. }
  556. sub := blockntfns.Subscription{
  557. Notifications: make(chan blockntfns.BlockNtfn, 6),
  558. Cancel: blockEpoch.Cancel,
  559. }
  560. go func() {
  561. for blk := range blockEpoch.Epochs {
  562. ntfn := blockntfns.NewBlockConnected(
  563. *blk.BlockHeader,
  564. uint32(blk.Height),
  565. )
  566. sub.Notifications <- ntfn
  567. }
  568. }()
  569. return &sub, nil
  570. }
  571. }
  572. // walletReBroadcaster is a simple wrapper around the pushtx.Broadcaster
  573. // interface to adhere to the expanded lnwallet.Rebroadcaster interface.
  574. type walletReBroadcaster struct {
  575. started atomic.Bool
  576. *pushtx.Broadcaster
  577. }
  578. // newWalletReBroadcaster creates a new instance of the walletReBroadcaster.
  579. func newWalletReBroadcaster(
  580. broadcaster *pushtx.Broadcaster) *walletReBroadcaster {
  581. return &walletReBroadcaster{
  582. Broadcaster: broadcaster,
  583. }
  584. }
  585. // Start launches all goroutines the rebroadcaster needs to operate.
  586. func (w *walletReBroadcaster) Start() error {
  587. defer w.started.Store(true)
  588. return w.Broadcaster.Start()
  589. }
  590. // Started returns true if the broadcaster is already active.
  591. func (w *walletReBroadcaster) Started() bool {
  592. return w.started.Load()
  593. }
  594. // BuildChainControl is responsible for creating a fully populated chain
  595. // control instance from a wallet.
  596. //
  597. // NOTE: This is part of the ChainControlBuilder interface.
  598. func (d *DefaultWalletImpl) BuildChainControl(
  599. partialChainControl *chainreg.PartialChainControl,
  600. walletConfig *btcwallet.Config) (*chainreg.ChainControl, func(), error) {
  601. walletController, err := btcwallet.New(
  602. *walletConfig, partialChainControl.Cfg.BlockCache,
  603. )
  604. if err != nil {
  605. err := fmt.Errorf("unable to create wallet controller: %w", err)
  606. d.logger.Error(err)
  607. return nil, nil, err
  608. }
  609. keyRing := keychain.NewBtcWalletKeyRing(
  610. walletController.InternalWallet(), walletConfig.CoinType,
  611. )
  612. // Create, and start the lnwallet, which handles the core payment
  613. // channel logic, and exposes control via proxy state machines.
  614. lnWalletConfig := lnwallet.Config{
  615. Database: partialChainControl.Cfg.ChanStateDB,
  616. Notifier: partialChainControl.ChainNotifier,
  617. WalletController: walletController,
  618. Signer: walletController,
  619. FeeEstimator: partialChainControl.FeeEstimator,
  620. SecretKeyRing: keyRing,
  621. ChainIO: walletController,
  622. NetParams: *walletConfig.NetParams,
  623. CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
  624. }
  625. // The broadcast is already always active for neutrino nodes, so we
  626. // don't want to create a rebroadcast loop.
  627. if partialChainControl.Cfg.NeutrinoCS == nil {
  628. broadcastCfg := pushtx.Config{
  629. Broadcast: func(tx *wire.MsgTx) error {
  630. cs := partialChainControl.ChainSource
  631. _, err := cs.SendRawTransaction(
  632. tx, true,
  633. )
  634. return err
  635. },
  636. SubscribeBlocks: proxyBlockEpoch(
  637. partialChainControl.ChainNotifier,
  638. ),
  639. RebroadcastInterval: pushtx.DefaultRebroadcastInterval,
  640. // In case the backend is different from neutrino we
  641. // make sure that broadcast backend errors are mapped
  642. // to the neutrino broadcastErr.
  643. MapCustomBroadcastError: broadcastErrorMapper,
  644. }
  645. lnWalletConfig.Rebroadcaster = newWalletReBroadcaster(
  646. pushtx.NewBroadcaster(&broadcastCfg),
  647. )
  648. }
  649. // We've created the wallet configuration now, so we can finish
  650. // initializing the main chain control.
  651. activeChainControl, cleanUp, err := chainreg.NewChainControl(
  652. lnWalletConfig, walletController, partialChainControl,
  653. )
  654. if err != nil {
  655. err := fmt.Errorf("unable to create chain control: %w", err)
  656. d.logger.Error(err)
  657. return nil, nil, err
  658. }
  659. return activeChainControl, cleanUp, nil
  660. }
  661. // RPCSignerWalletImpl is a wallet implementation that uses a remote signer over
  662. // an RPC interface.
  663. type RPCSignerWalletImpl struct {
  664. // DefaultWalletImpl is the embedded instance of the default
  665. // implementation that the remote signer uses as its watch-only wallet
  666. // for keeping track of addresses and UTXOs.
  667. *DefaultWalletImpl
  668. }
  669. // NewRPCSignerWalletImpl creates a new instance of the remote signing wallet
  670. // implementation.
  671. func NewRPCSignerWalletImpl(cfg *Config, logger btclog.Logger,
  672. interceptor signal.Interceptor,
  673. migrateWatchOnly bool) *RPCSignerWalletImpl {
  674. return &RPCSignerWalletImpl{
  675. DefaultWalletImpl: &DefaultWalletImpl{
  676. cfg: cfg,
  677. logger: logger,
  678. interceptor: interceptor,
  679. watchOnly: true,
  680. migrateWatchOnly: migrateWatchOnly,
  681. pwService: createWalletUnlockerService(cfg),
  682. },
  683. }
  684. }
  685. // BuildChainControl is responsible for creating or unlocking and then fully
  686. // initializing a wallet and returning it as part of a fully populated chain
  687. // control instance.
  688. //
  689. // NOTE: This is part of the ChainControlBuilder interface.
  690. func (d *RPCSignerWalletImpl) BuildChainControl(
  691. partialChainControl *chainreg.PartialChainControl,
  692. walletConfig *btcwallet.Config) (*chainreg.ChainControl, func(), error) {
  693. walletController, err := btcwallet.New(
  694. *walletConfig, partialChainControl.Cfg.BlockCache,
  695. )
  696. if err != nil {
  697. err := fmt.Errorf("unable to create wallet controller: %w", err)
  698. d.logger.Error(err)
  699. return nil, nil, err
  700. }
  701. baseKeyRing := keychain.NewBtcWalletKeyRing(
  702. walletController.InternalWallet(), walletConfig.CoinType,
  703. )
  704. rpcKeyRing, err := rpcwallet.NewRPCKeyRing(
  705. baseKeyRing, walletController,
  706. d.DefaultWalletImpl.cfg.RemoteSigner, walletConfig.NetParams,
  707. )
  708. if err != nil {
  709. err := fmt.Errorf("unable to create RPC remote signing wallet "+
  710. "%v", err)
  711. d.logger.Error(err)
  712. return nil, nil, err
  713. }
  714. // Create, and start the lnwallet, which handles the core payment
  715. // channel logic, and exposes control via proxy state machines.
  716. lnWalletConfig := lnwallet.Config{
  717. Database: partialChainControl.Cfg.ChanStateDB,
  718. Notifier: partialChainControl.ChainNotifier,
  719. WalletController: rpcKeyRing,
  720. Signer: rpcKeyRing,
  721. FeeEstimator: partialChainControl.FeeEstimator,
  722. SecretKeyRing: rpcKeyRing,
  723. ChainIO: walletController,
  724. NetParams: *walletConfig.NetParams,
  725. CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
  726. }
  727. // We've created the wallet configuration now, so we can finish
  728. // initializing the main chain control.
  729. activeChainControl, cleanUp, err := chainreg.NewChainControl(
  730. lnWalletConfig, rpcKeyRing, partialChainControl,
  731. )
  732. if err != nil {
  733. err := fmt.Errorf("unable to create chain control: %w", err)
  734. d.logger.Error(err)
  735. return nil, nil, err
  736. }
  737. return activeChainControl, cleanUp, nil
  738. }
  739. // DatabaseInstances is a struct that holds all instances to the actual
  740. // databases that are used in lnd.
  741. type DatabaseInstances struct {
  742. // GraphDB is the database that stores the channel graph used for path
  743. // finding.
  744. //
  745. // NOTE/TODO: This currently _needs_ to be the same instance as the
  746. // ChanStateDB below until the separation of the two databases is fully
  747. // complete!
  748. GraphDB *channeldb.DB
  749. // ChanStateDB is the database that stores all of our node's channel
  750. // state.
  751. //
  752. // NOTE/TODO: This currently _needs_ to be the same instance as the
  753. // GraphDB above until the separation of the two databases is fully
  754. // complete!
  755. ChanStateDB *channeldb.DB
  756. // HeightHintDB is the database that stores height hints for spends.
  757. HeightHintDB kvdb.Backend
  758. // InvoiceDB is the database that stores information about invoices.
  759. InvoiceDB invoices.InvoiceDB
  760. // MacaroonDB is the database that stores macaroon root keys.
  761. MacaroonDB kvdb.Backend
  762. // DecayedLogDB is the database that stores p2p related encryption
  763. // information.
  764. DecayedLogDB kvdb.Backend
  765. // TowerClientDB is the database that stores the watchtower client's
  766. // configuration.
  767. TowerClientDB wtclient.DB
  768. // TowerServerDB is the database that stores the watchtower server's
  769. // configuration.
  770. TowerServerDB watchtower.DB
  771. // WalletDB is the configuration for loading the wallet database using
  772. // the btcwallet's loader.
  773. WalletDB btcwallet.LoaderOption
  774. // NativeSQLStore is a pointer to a native SQL store that can be used
  775. // for native SQL queries for tables that already support it. This may
  776. // be nil if the use-native-sql flag was not set.
  777. NativeSQLStore *sqldb.BaseDB
  778. }
  779. // DefaultDatabaseBuilder is a type that builds the default database backends
  780. // for lnd, using the given configuration to decide what actual implementation
  781. // to use.
  782. type DefaultDatabaseBuilder struct {
  783. cfg *Config
  784. logger btclog.Logger
  785. }
  786. // NewDefaultDatabaseBuilder returns a new instance of the default database
  787. // builder.
  788. func NewDefaultDatabaseBuilder(cfg *Config,
  789. logger btclog.Logger) *DefaultDatabaseBuilder {
  790. return &DefaultDatabaseBuilder{
  791. cfg: cfg,
  792. logger: logger,
  793. }
  794. }
  795. // BuildDatabase extracts the current databases that we'll use for normal
  796. // operation in the daemon. A function closure that closes all opened databases
  797. // is also returned.
  798. func (d *DefaultDatabaseBuilder) BuildDatabase(
  799. ctx context.Context) (*DatabaseInstances, func(), error) {
  800. d.logger.Infof("Opening the main database, this might take a few " +
  801. "minutes...")
  802. cfg := d.cfg
  803. if cfg.DB.Backend == lncfg.BoltBackend {
  804. d.logger.Infof("Opening bbolt database, sync_freelist=%v, "+
  805. "auto_compact=%v", !cfg.DB.Bolt.NoFreelistSync,
  806. cfg.DB.Bolt.AutoCompact)
  807. }
  808. startOpenTime := time.Now()
  809. databaseBackends, err := cfg.DB.GetBackends(
  810. ctx, cfg.graphDatabaseDir(), cfg.networkDir, filepath.Join(
  811. cfg.Watchtower.TowerDir, BitcoinChainName,
  812. lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
  813. ), cfg.WtClient.Active, cfg.Watchtower.Active, d.logger,
  814. )
  815. if err != nil {
  816. return nil, nil, fmt.Errorf("unable to obtain database "+
  817. "backends: %v", err)
  818. }
  819. // With the full remote mode we made sure both the graph and channel
  820. // state DB point to the same local or remote DB and the same namespace
  821. // within that DB.
  822. dbs := &DatabaseInstances{
  823. HeightHintDB: databaseBackends.HeightHintDB,
  824. MacaroonDB: databaseBackends.MacaroonDB,
  825. DecayedLogDB: databaseBackends.DecayedLogDB,
  826. WalletDB: databaseBackends.WalletDB,
  827. NativeSQLStore: databaseBackends.NativeSQLStore,
  828. }
  829. cleanUp := func() {
  830. // We can just close the returned close functions directly. Even
  831. // if we decorate the channel DB with an additional struct, its
  832. // close function still just points to the kvdb backend.
  833. for name, closeFunc := range databaseBackends.CloseFuncs {
  834. if err := closeFunc(); err != nil {
  835. d.logger.Errorf("Error closing %s "+
  836. "database: %v", name, err)
  837. }
  838. }
  839. }
  840. if databaseBackends.Remote {
  841. d.logger.Infof("Using remote %v database! Creating "+
  842. "graph and channel state DB instances", cfg.DB.Backend)
  843. } else {
  844. d.logger.Infof("Creating local graph and channel state DB " +
  845. "instances")
  846. }
  847. dbOptions := []channeldb.OptionModifier{
  848. channeldb.OptionSetRejectCacheSize(cfg.Caches.RejectCacheSize),
  849. channeldb.OptionSetChannelCacheSize(
  850. cfg.Caches.ChannelCacheSize,
  851. ),
  852. channeldb.OptionSetBatchCommitInterval(
  853. cfg.DB.BatchCommitInterval,
  854. ),
  855. channeldb.OptionDryRunMigration(cfg.DryRunMigration),
  856. channeldb.OptionSetUseGraphCache(!cfg.DB.NoGraphCache),
  857. channeldb.OptionKeepFailedPaymentAttempts(
  858. cfg.KeepFailedPaymentAttempts,
  859. ),
  860. channeldb.OptionStoreFinalHtlcResolutions(
  861. cfg.StoreFinalHtlcResolutions,
  862. ),
  863. channeldb.OptionPruneRevocationLog(cfg.DB.PruneRevocation),
  864. channeldb.OptionNoRevLogAmtData(cfg.DB.NoRevLogAmtData),
  865. }
  866. // We want to pre-allocate the channel graph cache according to what we
  867. // expect for mainnet to speed up memory allocation.
  868. if cfg.ActiveNetParams.Name == chaincfg.MainNetParams.Name {
  869. dbOptions = append(
  870. dbOptions, channeldb.OptionSetPreAllocCacheNumNodes(
  871. channeldb.DefaultPreAllocCacheNumNodes,
  872. ),
  873. )
  874. }
  875. // Otherwise, we'll open two instances, one for the state we only need
  876. // locally, and the other for things we want to ensure are replicated.
  877. dbs.GraphDB, err = channeldb.CreateWithBackend(
  878. databaseBackends.GraphDB, dbOptions...,
  879. )
  880. switch {
  881. // Give the DB a chance to dry run the migration. Since we know that
  882. // both the channel state and graph DBs are still always behind the same
  883. // backend, we know this would be applied to both of those DBs.
  884. case err == channeldb.ErrDryRunMigrationOK:
  885. d.logger.Infof("Graph DB dry run migration successful")
  886. return nil, nil, err
  887. case err != nil:
  888. cleanUp()
  889. err := fmt.Errorf("unable to open graph DB: %w", err)
  890. d.logger.Error(err)
  891. return nil, nil, err
  892. }
  893. // For now, we don't _actually_ split the graph and channel state DBs on
  894. // the code level. Since they both are based upon the *channeldb.DB
  895. // struct it will require more refactoring to fully separate them. With
  896. // the full remote mode we at least know for now that they both point to
  897. // the same DB backend (and also namespace within that) so we only need
  898. // to apply any migration once.
  899. //
  900. // TODO(guggero): Once the full separation of anything graph related
  901. // from the channeldb.DB is complete, the decorated instance of the
  902. // channel state DB should be created here individually instead of just
  903. // using the same struct (and DB backend) instance.
  904. dbs.ChanStateDB = dbs.GraphDB
  905. // Instantiate a native SQL invoice store if the flag is set.
  906. if d.cfg.DB.UseNativeSQL {
  907. // KV invoice db resides in the same database as the graph and
  908. // channel state DB. Let's query the database to see if we have
  909. // any invoices there. If we do, we won't allow the user to
  910. // start lnd with native SQL enabled, as we don't currently
  911. // migrate the invoices to the new database schema.
  912. invoiceSlice, err := dbs.GraphDB.QueryInvoices(
  913. ctx, invoices.InvoiceQuery{
  914. NumMaxInvoices: 1,
  915. },
  916. )
  917. if err != nil {
  918. cleanUp()
  919. d.logger.Errorf("Unable to query KV invoice DB: %v",
  920. err)
  921. return nil, nil, err
  922. }
  923. if len(invoiceSlice.Invoices) > 0 {
  924. cleanUp()
  925. err := fmt.Errorf("found invoices in the KV invoice " +
  926. "DB, migration to native SQL is not yet " +
  927. "supported")
  928. d.logger.Error(err)
  929. return nil, nil, err
  930. }
  931. executor := sqldb.NewTransactionExecutor(
  932. dbs.NativeSQLStore,
  933. func(tx *sql.Tx) invoices.SQLInvoiceQueries {
  934. return dbs.NativeSQLStore.WithTx(tx)
  935. },
  936. )
  937. dbs.InvoiceDB = invoices.NewSQLStore(
  938. executor, clock.NewDefaultClock(),
  939. )
  940. } else {
  941. dbs.InvoiceDB = dbs.GraphDB
  942. }
  943. // Wrap the watchtower client DB and make sure we clean up.
  944. if cfg.WtClient.Active {
  945. dbs.TowerClientDB, err = wtdb.OpenClientDB(
  946. databaseBackends.TowerClientDB,
  947. )
  948. if err != nil {
  949. cleanUp()
  950. err := fmt.Errorf("unable to open %s database: %w",
  951. lncfg.NSTowerClientDB, err)
  952. d.logger.Error(err)
  953. return nil, nil, err
  954. }
  955. }
  956. // Wrap the watchtower server DB and make sure we clean up.
  957. if cfg.Watchtower.Active {
  958. dbs.TowerServerDB, err = wtdb.OpenTowerDB(
  959. databaseBackends.TowerServerDB,
  960. )
  961. if err != nil {
  962. cleanUp()
  963. err := fmt.Errorf("unable to open %s database: %w",
  964. lncfg.NSTowerServerDB, err)
  965. d.logger.Error(err)
  966. return nil, nil, err
  967. }
  968. }
  969. openTime := time.Since(startOpenTime)
  970. d.logger.Infof("Database(s) now open (time_to_open=%v)!", openTime)
  971. return dbs, cleanUp, nil
  972. }
  973. // waitForWalletPassword blocks until a password is provided by the user to
  974. // this RPC server.
  975. func waitForWalletPassword(cfg *Config,
  976. pwService *walletunlocker.UnlockerService,
  977. loaderOpts []btcwallet.LoaderOption, shutdownChan <-chan struct{}) (
  978. *walletunlocker.WalletUnlockParams, error) {
  979. // Wait for user to provide the password.
  980. ltndLog.Infof("Waiting for wallet encryption password. Use `lncli " +
  981. "create` to create a wallet, `lncli unlock` to unlock an " +
  982. "existing wallet, or `lncli changepassword` to change the " +
  983. "password of an existing wallet and unlock it.")
  984. // We currently don't distinguish between getting a password to be used
  985. // for creation or unlocking, as a new wallet db will be created if
  986. // none exists when creating the chain control.
  987. select {
  988. // The wallet is being created for the first time, we'll check to see
  989. // if the user provided any entropy for seed creation. If so, then
  990. // we'll create the wallet early to load the seed.
  991. case initMsg := <-pwService.InitMsgs:
  992. password := initMsg.Passphrase
  993. cipherSeed := initMsg.WalletSeed
  994. extendedKey := initMsg.WalletExtendedKey
  995. watchOnlyAccounts := initMsg.WatchOnlyAccounts
  996. recoveryWindow := initMsg.RecoveryWindow
  997. // Before we proceed, we'll check the internal version of the
  998. // seed. If it's greater than the current key derivation
  999. // version, then we'll return an error as we don't understand
  1000. // this.
  1001. if cipherSeed != nil &&
  1002. !keychain.IsKnownVersion(cipherSeed.InternalVersion) {
  1003. return nil, fmt.Errorf("invalid internal "+
  1004. "seed version %v, current max version is %v",
  1005. cipherSeed.InternalVersion,
  1006. keychain.CurrentKeyDerivationVersion)
  1007. }
  1008. loader, err := btcwallet.NewWalletLoader(
  1009. cfg.ActiveNetParams.Params, recoveryWindow,
  1010. loaderOpts...,
  1011. )
  1012. if err != nil {
  1013. return nil, err
  1014. }
  1015. // With the seed, we can now use the wallet loader to create
  1016. // the wallet, then pass it back to avoid unlocking it again.
  1017. var (
  1018. birthday time.Time
  1019. newWallet *wallet.Wallet
  1020. )
  1021. switch {
  1022. // A normal cipher seed was given, use the birthday encoded in
  1023. // it and create the wallet from that.
  1024. case cipherSeed != nil:
  1025. birthday = cipherSeed.BirthdayTime()
  1026. newWallet, err = loader.CreateNewWallet(
  1027. password, password, cipherSeed.Entropy[:],
  1028. birthday,
  1029. )
  1030. // No seed was given, we're importing a wallet from its extended
  1031. // private key.
  1032. case extendedKey != nil:
  1033. birthday = initMsg.ExtendedKeyBirthday
  1034. newWallet, err = loader.CreateNewWalletExtendedKey(
  1035. password, password, extendedKey, birthday,
  1036. )
  1037. // Neither seed nor extended private key was given, so maybe the
  1038. // third option was chosen, the watch-only initialization. In
  1039. // this case we need to import each of the xpubs individually.
  1040. case watchOnlyAccounts != nil:
  1041. if !cfg.RemoteSigner.Enable {
  1042. return nil, fmt.Errorf("cannot initialize " +
  1043. "watch only wallet with remote " +
  1044. "signer config disabled")
  1045. }
  1046. birthday = initMsg.WatchOnlyBirthday
  1047. newWallet, err = loader.CreateNewWatchingOnlyWallet(
  1048. password, birthday,
  1049. )
  1050. if err != nil {
  1051. break
  1052. }
  1053. err = importWatchOnlyAccounts(newWallet, initMsg)
  1054. default:
  1055. // The unlocker service made sure either the cipher seed
  1056. // or the extended key is set so, we shouldn't get here.
  1057. // The default case is just here for readability and
  1058. // completeness.
  1059. err = fmt.Errorf("cannot create wallet, neither seed " +
  1060. "nor extended key was given")
  1061. }
  1062. if err != nil {
  1063. // Don't leave the file open in case the new wallet
  1064. // could not be created for whatever reason.
  1065. if err := loader.UnloadWallet(); err != nil {
  1066. ltndLog.Errorf("Could not unload new "+
  1067. "wallet: %v", err)
  1068. }
  1069. return nil, err
  1070. }
  1071. // For new wallets, the ResetWalletTransactions flag is a no-op.
  1072. if cfg.ResetWalletTransactions {
  1073. ltndLog.Warnf("Ignoring reset-wallet-transactions " +
  1074. "flag for new wallet as it has no effect")
  1075. }
  1076. return &walletunlocker.WalletUnlockParams{
  1077. Password: password,
  1078. Birthday: birthday,
  1079. RecoveryWindow: recoveryWindow,
  1080. Wallet: newWallet,
  1081. ChansToRestore: initMsg.ChanBackups,
  1082. UnloadWallet: loader.UnloadWallet,
  1083. StatelessInit: initMsg.StatelessInit,
  1084. MacResponseChan: pwService.MacResponseChan,
  1085. MacRootKey: initMsg.MacRootKey,
  1086. }, nil
  1087. // The wallet has already been created in the past, and is simply being
  1088. // unlocked. So we'll just return these passphrases.
  1089. case unlockMsg := <-pwService.UnlockMsgs:
  1090. // Resetting the transactions is something the user likely only
  1091. // wants to do once so we add a prominent warning to the log to
  1092. // remind the user to turn off the setting again after
  1093. // successful completion.
  1094. if cfg.ResetWalletTransactions {
  1095. ltndLog.Warnf("Dropped all transaction history from " +
  1096. "on-chain wallet. Remember to disable " +
  1097. "reset-wallet-transactions flag for next " +
  1098. "start of lnd")
  1099. }
  1100. return &walletunlocker.WalletUnlockParams{
  1101. Password: unlockMsg.Passphrase,
  1102. RecoveryWindow: unlockMsg.RecoveryWindow,
  1103. Wallet: unlockMsg.Wallet,
  1104. ChansToRestore: unlockMsg.ChanBackups,
  1105. UnloadWallet: unlockMsg.UnloadWallet,
  1106. StatelessInit: unlockMsg.StatelessInit,
  1107. MacResponseChan: pwService.MacResponseChan,
  1108. }, nil
  1109. // If we got a shutdown signal we just return with an error immediately
  1110. case <-shutdownChan:
  1111. return nil, fmt.Errorf("shutting down")
  1112. }
  1113. }
  1114. // importWatchOnlyAccounts imports all individual account xpubs into our wallet
  1115. // which we created as watch-only.
  1116. func importWatchOnlyAccounts(wallet *wallet.Wallet,
  1117. initMsg *walletunlocker.WalletInitMsg) error {
  1118. scopes := make([]waddrmgr.ScopedIndex, 0, len(initMsg.WatchOnlyAccounts))
  1119. for scope := range initMsg.WatchOnlyAccounts {
  1120. scopes = append(scopes, scope)
  1121. }
  1122. // We need to import the accounts in the correct order, otherwise the
  1123. // indices will be incorrect.
  1124. sort.Slice(scopes, func(i, j int) bool {
  1125. return scopes[i].Scope.Purpose < scopes[j].Scope.Purpose ||
  1126. scopes[i].Index < scopes[j].Index
  1127. })
  1128. for _, scope := range scopes {
  1129. addrSchema := waddrmgr.ScopeAddrMap[waddrmgr.KeyScopeBIP0084]
  1130. // We want witness pubkey hash by default, except for BIP49
  1131. // where we want mixed and BIP86 where we want taproot address
  1132. // formats.
  1133. switch scope.Scope.Purpose {
  1134. case waddrmgr.KeyScopeBIP0049Plus.Purpose,
  1135. waddrmgr.KeyScopeBIP0086.Purpose:
  1136. addrSchema = waddrmgr.ScopeAddrMap[scope.Scope]
  1137. }
  1138. // We want a human-readable account name. But for the default
  1139. // on-chain wallet we actually need to call it "default" to make
  1140. // sure everything works correctly.
  1141. name := fmt.Sprintf("%s/%d'", scope.Scope.String(), scope.Index)
  1142. if scope.Index == 0 {
  1143. name = "default"
  1144. }
  1145. _, err := wallet.ImportAccountWithScope(
  1146. name, initMsg.WatchOnlyAccounts[scope],
  1147. initMsg.WatchOnlyMasterFingerprint, scope.Scope,
  1148. addrSchema,
  1149. )
  1150. if err != nil {
  1151. return fmt.Errorf("could not import account %v: %w",
  1152. name, err)
  1153. }
  1154. }
  1155. return nil
  1156. }
  1157. // initNeutrinoBackend inits a new instance of the neutrino light client
  1158. // backend given a target chain directory to store the chain state.
  1159. func initNeutrinoBackend(ctx context.Context, cfg *Config, chainDir string,
  1160. blockCache *blockcache.BlockCache) (*neutrino.ChainService,
  1161. func(), error) {
  1162. // Both channel validation flags are false by default but their meaning
  1163. // is the inverse of each other. Therefore both cannot be true. For
  1164. // every other case, the neutrino.validatechannels overwrites the
  1165. // routing.assumechanvalid value.
  1166. if cfg.NeutrinoMode.ValidateChannels && cfg.Routing.AssumeChannelValid {
  1167. return nil, nil, fmt.Errorf("can't set both " +
  1168. "neutrino.validatechannels and routing." +
  1169. "assumechanvalid to true at the same time")
  1170. }
  1171. cfg.Routing.AssumeChannelValid = !cfg.NeutrinoMode.ValidateChannels
  1172. // First we'll open the database file for neutrino, creating the
  1173. // database if needed. We append the normalized network name here to
  1174. // match the behavior of btcwallet.
  1175. dbPath := filepath.Join(
  1176. chainDir, lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
  1177. )
  1178. // Ensure that the neutrino db path exists.
  1179. if err := os.MkdirAll(dbPath, 0700); err != nil {
  1180. return nil, nil, err
  1181. }
  1182. var (
  1183. db walletdb.DB
  1184. err error
  1185. )
  1186. switch {
  1187. case cfg.DB.Backend == kvdb.SqliteBackendName:
  1188. sqliteConfig := lncfg.GetSqliteConfigKVDB(cfg.DB.Sqlite)
  1189. db, err = kvdb.Open(
  1190. kvdb.SqliteBackendName, ctx, sqliteConfig, dbPath,
  1191. lncfg.SqliteNeutrinoDBName, lncfg.NSNeutrinoDB,
  1192. )
  1193. default:
  1194. dbName := filepath.Join(dbPath, "neutrino.db")
  1195. db, err = walletdb.Create(
  1196. "bdb", dbName, !cfg.SyncFreelist, cfg.DB.Bolt.DBTimeout,
  1197. )
  1198. }
  1199. if err != nil {
  1200. return nil, nil, fmt.Errorf("unable to create "+
  1201. "neutrino database: %v", err)
  1202. }
  1203. headerStateAssertion, err := parseHeaderStateAssertion(
  1204. cfg.NeutrinoMode.AssertFilterHeader,
  1205. )
  1206. if err != nil {
  1207. db.Close()
  1208. return nil, nil, err
  1209. }
  1210. // With the database open, we can now create an instance of the
  1211. // neutrino light client. We pass in relevant configuration parameters
  1212. // required.
  1213. config := neutrino.Config{
  1214. DataDir: dbPath,
  1215. Database: db,
  1216. ChainParams: *cfg.ActiveNetParams.Params,
  1217. AddPeers: cfg.NeutrinoMode.AddPeers,
  1218. ConnectPeers: cfg.NeutrinoMode.ConnectPeers,
  1219. Dialer: func(addr net.Addr) (net.Conn, error) {
  1220. return cfg.net.Dial(
  1221. addr.Network(), addr.String(),
  1222. cfg.ConnectionTimeout,
  1223. )
  1224. },
  1225. NameResolver: func(host string) ([]net.IP, error) {
  1226. addrs, err := cfg.net.LookupHost(host)
  1227. if err != nil {
  1228. return nil, err
  1229. }
  1230. ips := make([]net.IP, 0, len(addrs))
  1231. for _, strIP := range addrs {
  1232. ip := net.ParseIP(strIP)
  1233. if ip == nil {
  1234. continue
  1235. }
  1236. ips = append(ips, ip)
  1237. }
  1238. return ips, nil
  1239. },
  1240. AssertFilterHeader: headerStateAssertion,
  1241. BlockCache: blockCache.Cache,
  1242. BroadcastTimeout: cfg.NeutrinoMode.BroadcastTimeout,
  1243. PersistToDisk: cfg.NeutrinoMode.PersistFilters,
  1244. }
  1245. neutrino.MaxPeers = 8
  1246. neutrino.BanDuration = time.Hour * 48
  1247. neutrino.UserAgentName = cfg.NeutrinoMode.UserAgentName
  1248. neutrino.UserAgentVersion = cfg.NeutrinoMode.UserAgentVersion
  1249. neutrinoCS, err := neutrino.NewChainService(config)
  1250. if err != nil {
  1251. db.Close()
  1252. return nil, nil, fmt.Errorf("unable to create neutrino light "+
  1253. "client: %v", err)
  1254. }
  1255. if err := neutrinoCS.Start(); err != nil {
  1256. db.Close()
  1257. return nil, nil, err
  1258. }
  1259. cleanUp := func() {
  1260. if err := neutrinoCS.Stop(); err != nil {
  1261. ltndLog.Infof("Unable to stop neutrino light client: "+
  1262. "%v", err)
  1263. }
  1264. db.Close()
  1265. }
  1266. return neutrinoCS, cleanUp, nil
  1267. }
  1268. // parseHeaderStateAssertion parses the user-specified neutrino header state
  1269. // into a headerfs.FilterHeader.
  1270. func parseHeaderStateAssertion(state string) (*headerfs.FilterHeader, error) {
  1271. if len(state) == 0 {
  1272. return nil, nil
  1273. }
  1274. split := strings.Split(state, ":")
  1275. if len(split) != 2 {
  1276. return nil, fmt.Errorf("header state assertion %v in "+
  1277. "unexpected format, expected format height:hash", state)
  1278. }
  1279. height, err := strconv.ParseUint(split[0], 10, 32)
  1280. if err != nil {
  1281. return nil, fmt.Errorf("invalid filter header height: %w", err)
  1282. }
  1283. hash, err := chainhash.NewHashFromStr(split[1])
  1284. if err != nil {
  1285. return nil, fmt.Errorf("invalid filter header hash: %w", err)
  1286. }
  1287. return &headerfs.FilterHeader{
  1288. Height: uint32(height),
  1289. FilterHash: *hash,
  1290. }, nil
  1291. }
  1292. // broadcastErrorMapper maps errors from bitcoin backends other than neutrino to
  1293. // the neutrino BroadcastError which allows the Rebroadcaster which currently
  1294. // resides in the neutrino package to use all of its functionalities.
  1295. func broadcastErrorMapper(err error) error {
  1296. returnErr := rpcclient.MapRPCErr(err)
  1297. // We only filter for specific backend errors which are relevant for the
  1298. // Rebroadcaster.
  1299. switch {
  1300. // This makes sure the tx is removed from the rebroadcaster once it is
  1301. // confirmed.
  1302. case errors.Is(returnErr, rpcclient.ErrTxAlreadyKnown),
  1303. errors.Is(err, rpcclient.ErrTxAlreadyConfirmed):
  1304. returnErr = &pushtx.BroadcastError{
  1305. Code: pushtx.Confirmed,
  1306. Reason: returnErr.Error(),
  1307. }
  1308. // Transactions which are still in mempool but might fall out because
  1309. // of low fees are rebroadcasted despite of their backend error.
  1310. case errors.Is(returnErr, rpcclient.ErrTxAlreadyInMempool):
  1311. returnErr = &pushtx.BroadcastError{
  1312. Code: pushtx.Mempool,
  1313. Reason: returnErr.Error(),
  1314. }
  1315. // Transactions which are not accepted into mempool because of low fees
  1316. // in the first place are rebroadcasted despite of their backend error.
  1317. // Mempool conditions change over time so it makes sense to retry
  1318. // publishing the transaction. Moreover we log the detailed error so the
  1319. // user can intervene and increase the size of his mempool.
  1320. case errors.Is(err, rpcclient.ErrMempoolMinFeeNotMet):
  1321. ltndLog.Warnf("Error while broadcasting transaction: %v",
  1322. returnErr)
  1323. returnErr = &pushtx.BroadcastError{
  1324. Code: pushtx.Mempool,
  1325. Reason: returnErr.Error(),
  1326. }
  1327. }
  1328. return returnErr
  1329. }