diff --git a/walletunlocker/service.go b/walletunlocker/service.go index d0966a7a7..c334e9713 100644 --- a/walletunlocker/service.go +++ b/walletunlocker/service.go @@ -52,6 +52,13 @@ type WalletUnlockMsg struct { // recovery should be attempted, such as after the wallet's initial // creation, but before any addresses have been created. RecoveryWindow uint32 + + // Wallet is the loaded and unlocked Wallet. This is returned + // through the channel to avoid it being unlocked twice (once to check + // if the password is correct, here in the WalletUnlocker and again + // later when lnd actually uses it). Because unlocking involves scrypt + // which is resource intensive, we want to avoid doing it twice. + Wallet *wallet.Wallet } // UnlockerService implements the WalletUnlocker service used to provide lnd @@ -255,23 +262,19 @@ func (u *UnlockerService) UnlockWallet(ctx context.Context, } // Try opening the existing wallet with the provided password. - _, err = loader.OpenExistingWallet(password, false) + unlockedWallet, err := loader.OpenExistingWallet(password, false) if err != nil { // Could not open wallet, most likely this means that provided // password was incorrect. return nil, err } - // We successfully opened the wallet, but we'll need to unload it to - // make sure lnd can open it later. - if err := loader.UnloadWallet(); err != nil { - // TODO: not return error here? - return nil, err - } - + // We successfully opened the wallet and pass the instance back to + // avoid it needing to be unlocked again. walletUnlockMsg := &WalletUnlockMsg{ Passphrase: password, RecoveryWindow: recoveryWindow, + Wallet: unlockedWallet, } // At this point we was able to open the existing wallet with the