mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 20:54:20 +01:00
[server] fix unexpected panics when the wallet is not initialized (#264)
This commit is contained in:
@@ -68,9 +68,10 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
p2wpkhKeyScope = waddrmgr.KeyScopeBIP0084
|
ErrWalletNotInitialized = fmt.Errorf("wallet not initialized")
|
||||||
p2trKeyScope = waddrmgr.KeyScopeBIP0086
|
p2wpkhKeyScope = waddrmgr.KeyScopeBIP0084
|
||||||
outputLockDuration = time.Minute
|
p2trKeyScope = waddrmgr.KeyScopeBIP0086
|
||||||
|
outputLockDuration = time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
@@ -221,8 +222,10 @@ func NewService(cfg WalletConfig, options ...WalletOption) (ports.WalletService,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Close() {
|
func (s *service) Close() {
|
||||||
if err := s.wallet.Stop(); err != nil {
|
if s.isInitialized() {
|
||||||
log.WithError(err).Warn("failed to gracefully stop the wallet, forcing shutdown")
|
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 {
|
func (s *service) Unlock(_ context.Context, password string) error {
|
||||||
if s.wallet == nil {
|
if !s.isInitialized() {
|
||||||
pwd := []byte(password)
|
pwd := []byte(password)
|
||||||
opt := btcwallet.LoaderWithLocalWalletDB(s.cfg.Datadir, false, time.Minute)
|
opt := btcwallet.LoaderWithLocalWalletDB(s.cfg.Datadir, false, time.Minute)
|
||||||
config := btcwallet.Config{
|
config := btcwallet.Config{
|
||||||
@@ -323,6 +326,10 @@ func (s *service) Unlock(_ context.Context, password string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Lock(_ context.Context, _ string) error {
|
func (s *service) Lock(_ context.Context, _ string) error {
|
||||||
|
if !s.isInitialized() {
|
||||||
|
return ErrWalletNotInitialized
|
||||||
|
}
|
||||||
|
|
||||||
s.wallet.InternalWallet().Lock()
|
s.wallet.InternalWallet().Lock()
|
||||||
return nil
|
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) {
|
func (s *service) Status(ctx context.Context) (ports.WalletStatus, error) {
|
||||||
if s.wallet == nil {
|
if !s.isInitialized() {
|
||||||
return status{}, nil
|
return status{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -900,6 +907,10 @@ func (s *service) initWallet(wallet *btcwallet.BtcWallet) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) getBalance(account accountName) (uint64, error) {
|
func (s *service) getBalance(account accountName) (uint64, error) {
|
||||||
|
if !s.isInitialized() {
|
||||||
|
return 0, ErrWalletNotInitialized
|
||||||
|
}
|
||||||
|
|
||||||
balance, err := s.wallet.ConfirmedBalance(0, string(account))
|
balance, err := s.wallet.ConfirmedBalance(0, string(account))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -910,9 +921,17 @@ func (s *service) getBalance(account accountName) (uint64, error) {
|
|||||||
|
|
||||||
// this only supports deriving segwit v0 accounts
|
// this only supports deriving segwit v0 accounts
|
||||||
func (s *service) deriveNextAddress(account accountName) (btcutil.Address, error) {
|
func (s *service) deriveNextAddress(account accountName) (btcutil.Address, error) {
|
||||||
|
if !s.isInitialized() {
|
||||||
|
return nil, ErrWalletNotInitialized
|
||||||
|
}
|
||||||
|
|
||||||
return s.wallet.NewAddress(lnwallet.WitnessPubKey, false, string(account))
|
return s.wallet.NewAddress(lnwallet.WitnessPubKey, false, string(account))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *service) isInitialized() bool {
|
||||||
|
return s.wallet != nil
|
||||||
|
}
|
||||||
|
|
||||||
func withChainSource(chainSource chain.Interface) WalletOption {
|
func withChainSource(chainSource chain.Interface) WalletOption {
|
||||||
return func(s *service) error {
|
return func(s *service) error {
|
||||||
if s.chainSource != nil {
|
if s.chainSource != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user