walletunlocker: extract method to create the Loader

This commit is contained in:
Andras Banki-Horvath 2021-03-04 23:06:47 +01:00
parent c190c3d582
commit 08be03367a
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8

@ -160,14 +160,23 @@ func New(chainDir string, params *chaincfg.Params, noFreelistSync bool,
} }
} }
func (u *UnlockerService) newLoader(recoveryWindow uint32) (*wallet.Loader,
error) {
netDir := btcwallet.NetworkDir(u.chainDir, u.netParams)
return wallet.NewLoader(
u.netParams, netDir, u.noFreelistSync, u.dbTimeout,
recoveryWindow,
), nil
}
// WalletExists returns whether a wallet exists on the file path the // WalletExists returns whether a wallet exists on the file path the
// UnlockerService is using. // UnlockerService is using.
func (u *UnlockerService) WalletExists() (bool, error) { func (u *UnlockerService) WalletExists() (bool, error) {
netDir := btcwallet.NetworkDir(u.chainDir, u.netParams) loader, err := u.newLoader(0)
loader := wallet.NewLoader( if err != nil {
u.netParams, netDir, u.noFreelistSync, u.dbTimeout, 0, return false, err
) }
return loader.WalletExists() return loader.WalletExists()
} }
@ -184,10 +193,11 @@ func (u *UnlockerService) GenSeed(_ context.Context,
// Before we start, we'll ensure that the wallet hasn't already created // Before we start, we'll ensure that the wallet hasn't already created
// so we don't show a *new* seed to the user if one already exists. // so we don't show a *new* seed to the user if one already exists.
netDir := btcwallet.NetworkDir(u.chainDir, u.netParams) loader, err := u.newLoader(0)
loader := wallet.NewLoader( if err != nil {
u.netParams, netDir, u.noFreelistSync, u.dbTimeout, 0, return nil, err
) }
walletExists, err := loader.WalletExists() walletExists, err := loader.WalletExists()
if err != nil { if err != nil {
return nil, err return nil, err
@ -315,11 +325,10 @@ func (u *UnlockerService) InitWallet(ctx context.Context,
// We'll then open up the directory that will be used to store the // We'll then open up the directory that will be used to store the
// wallet's files so we can check if the wallet already exists. // wallet's files so we can check if the wallet already exists.
netDir := btcwallet.NetworkDir(u.chainDir, u.netParams) loader, err := u.newLoader(uint32(recoveryWindow))
loader := wallet.NewLoader( if err != nil {
u.netParams, netDir, u.noFreelistSync, return nil, err
u.dbTimeout, uint32(recoveryWindow), }
)
walletExists, err := loader.WalletExists() walletExists, err := loader.WalletExists()
if err != nil { if err != nil {
@ -392,11 +401,10 @@ func (u *UnlockerService) UnlockWallet(ctx context.Context,
password := in.WalletPassword password := in.WalletPassword
recoveryWindow := uint32(in.RecoveryWindow) recoveryWindow := uint32(in.RecoveryWindow)
netDir := btcwallet.NetworkDir(u.chainDir, u.netParams) loader, err := u.newLoader(recoveryWindow)
loader := wallet.NewLoader( if err != nil {
u.netParams, netDir, u.noFreelistSync, return nil, err
u.dbTimeout, recoveryWindow, }
)
// Check if wallet already exists. // Check if wallet already exists.
walletExists, err := loader.WalletExists() walletExists, err := loader.WalletExists()
@ -492,10 +500,10 @@ func (u *UnlockerService) UnlockWallet(ctx context.Context,
func (u *UnlockerService) ChangePassword(ctx context.Context, func (u *UnlockerService) ChangePassword(ctx context.Context,
in *lnrpc.ChangePasswordRequest) (*lnrpc.ChangePasswordResponse, error) { in *lnrpc.ChangePasswordRequest) (*lnrpc.ChangePasswordResponse, error) {
netDir := btcwallet.NetworkDir(u.chainDir, u.netParams) loader, err := u.newLoader(0)
loader := wallet.NewLoader( if err != nil {
u.netParams, netDir, u.noFreelistSync, u.dbTimeout, 0, return nil, err
) }
// First, we'll make sure the wallet exists for the specific chain and // First, we'll make sure the wallet exists for the specific chain and
// network. // network.
@ -572,6 +580,7 @@ func (u *UnlockerService) ChangePassword(ctx context.Context,
// then close it again. // then close it again.
// Attempt to open the macaroon DB, unlock it and then change // Attempt to open the macaroon DB, unlock it and then change
// the passphrase. // the passphrase.
netDir := btcwallet.NetworkDir(u.chainDir, u.netParams)
macaroonService, err := macaroons.NewService( macaroonService, err := macaroons.NewService(
netDir, "lnd", in.StatelessInit, u.dbTimeout, netDir, "lnd", in.StatelessInit, u.dbTimeout,
) )