From 250dd7eea28b18f42255c5cbe39b8b877a03356b Mon Sep 17 00:00:00 2001 From: Louis Singer <41042567+louisinger@users.noreply.github.com> Date: Mon, 19 Aug 2024 13:44:05 +0200 Subject: [PATCH] [server] fix unexpected panics when the wallet is not initialized (#264) --- .../wallet/btc-embedded/wallet.go | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/server/internal/infrastructure/wallet/btc-embedded/wallet.go b/server/internal/infrastructure/wallet/btc-embedded/wallet.go index 1a4b882..9297b59 100644 --- a/server/internal/infrastructure/wallet/btc-embedded/wallet.go +++ b/server/internal/infrastructure/wallet/btc-embedded/wallet.go @@ -68,9 +68,10 @@ const ( ) var ( - p2wpkhKeyScope = waddrmgr.KeyScopeBIP0084 - p2trKeyScope = waddrmgr.KeyScopeBIP0086 - outputLockDuration = time.Minute + ErrWalletNotInitialized = fmt.Errorf("wallet not initialized") + p2wpkhKeyScope = waddrmgr.KeyScopeBIP0084 + p2trKeyScope = waddrmgr.KeyScopeBIP0086 + outputLockDuration = time.Minute ) type service struct { @@ -221,8 +222,10 @@ func NewService(cfg WalletConfig, options ...WalletOption) (ports.WalletService, } func (s *service) Close() { - if err := s.wallet.Stop(); err != nil { - log.WithError(err).Warn("failed to gracefully stop the wallet, forcing shutdown") + if s.isInitialized() { + if err := s.wallet.Stop(); err != nil { + log.WithError(err).Warn("failed to gracefully stop the wallet, forcing shutdown") + } } } @@ -243,7 +246,7 @@ func (s *service) Restore(_ context.Context, seed, password string) error { } func (s *service) Unlock(_ context.Context, password string) error { - if s.wallet == nil { + if !s.isInitialized() { pwd := []byte(password) opt := btcwallet.LoaderWithLocalWalletDB(s.cfg.Datadir, false, time.Minute) config := btcwallet.Config{ @@ -323,6 +326,10 @@ func (s *service) Unlock(_ context.Context, password string) error { } func (s *service) Lock(_ context.Context, _ string) error { + if !s.isInitialized() { + return ErrWalletNotInitialized + } + s.wallet.InternalWallet().Lock() return nil } @@ -577,7 +584,7 @@ func (s *service) SignTransactionTapscript(ctx context.Context, partialTx string } func (s *service) Status(ctx context.Context) (ports.WalletStatus, error) { - if s.wallet == nil { + if !s.isInitialized() { return status{}, nil } @@ -900,6 +907,10 @@ func (s *service) initWallet(wallet *btcwallet.BtcWallet) error { } func (s *service) getBalance(account accountName) (uint64, error) { + if !s.isInitialized() { + return 0, ErrWalletNotInitialized + } + balance, err := s.wallet.ConfirmedBalance(0, string(account)) if err != nil { return 0, err @@ -910,9 +921,17 @@ func (s *service) getBalance(account accountName) (uint64, error) { // this only supports deriving segwit v0 accounts func (s *service) deriveNextAddress(account accountName) (btcutil.Address, error) { + if !s.isInitialized() { + return nil, ErrWalletNotInitialized + } + return s.wallet.NewAddress(lnwallet.WitnessPubKey, false, string(account)) } +func (s *service) isInitialized() bool { + return s.wallet != nil +} + func withChainSource(chainSource chain.Interface) WalletOption { return func(s *service) error { if s.chainSource != nil {