walletunlocker: extract LoadAndUnlock
As a preparation to do auto-unlock without the unlock RPC, we extract the relevant part into its own method in the unlocker service.
This commit is contained in:
parent
4685341dcb
commit
8224de599b
@ -401,29 +401,27 @@ func (u *UnlockerService) InitWallet(ctx context.Context,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnlockWallet sends the password provided by the incoming UnlockWalletRequest
|
// LoadAndUnlock creates a loader for the wallet and tries to unlock the wallet
|
||||||
// over the UnlockMsgs channel in case it successfully decrypts an existing
|
// with the given password and recovery window. If the drop wallet transactions
|
||||||
// wallet found in the chain's wallet database directory.
|
// flag is set, the history state drop is performed before unlocking the wallet
|
||||||
func (u *UnlockerService) UnlockWallet(ctx context.Context,
|
// yet again.
|
||||||
in *lnrpc.UnlockWalletRequest) (*lnrpc.UnlockWalletResponse, error) {
|
func (u *UnlockerService) LoadAndUnlock(password []byte,
|
||||||
|
recoveryWindow uint32) (*wallet.Wallet, func() error, error) {
|
||||||
password := in.WalletPassword
|
|
||||||
recoveryWindow := uint32(in.RecoveryWindow)
|
|
||||||
|
|
||||||
loader, err := u.newLoader(recoveryWindow)
|
loader, err := u.newLoader(recoveryWindow)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if wallet already exists.
|
// Check if wallet already exists.
|
||||||
walletExists, err := loader.WalletExists()
|
walletExists, err := loader.WalletExists()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !walletExists {
|
if !walletExists {
|
||||||
// Cannot unlock a wallet that does not exist!
|
// Cannot unlock a wallet that does not exist!
|
||||||
return nil, fmt.Errorf("wallet not found")
|
return nil, nil, fmt.Errorf("wallet not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try opening the existing wallet with the provided password.
|
// Try opening the existing wallet with the provided password.
|
||||||
@ -431,7 +429,7 @@ func (u *UnlockerService) UnlockWallet(ctx context.Context,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
// Could not open wallet, most likely this means that provided
|
// Could not open wallet, most likely this means that provided
|
||||||
// password was incorrect.
|
// password was incorrect.
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// The user requested to drop their whole wallet transaction state to
|
// The user requested to drop their whole wallet transaction state to
|
||||||
@ -447,7 +445,7 @@ func (u *UnlockerService) UnlockWallet(ctx context.Context,
|
|||||||
// wallet. If unloading fails, that error is probably more
|
// wallet. If unloading fails, that error is probably more
|
||||||
// important to be returned to the user anyway.
|
// important to be returned to the user anyway.
|
||||||
if err := loader.UnloadWallet(); err != nil {
|
if err := loader.UnloadWallet(); err != nil {
|
||||||
return nil, fmt.Errorf("could not unload "+
|
return nil, nil, fmt.Errorf("could not unload "+
|
||||||
"wallet (tx history drop err: %v): %v", dropErr,
|
"wallet (tx history drop err: %v): %v", dropErr,
|
||||||
err)
|
err)
|
||||||
}
|
}
|
||||||
@ -455,23 +453,42 @@ func (u *UnlockerService) UnlockWallet(ctx context.Context,
|
|||||||
// If dropping failed but unloading didn't, we'll still abort
|
// If dropping failed but unloading didn't, we'll still abort
|
||||||
// and inform the user.
|
// and inform the user.
|
||||||
if dropErr != nil {
|
if dropErr != nil {
|
||||||
return nil, dropErr
|
return nil, nil, dropErr
|
||||||
}
|
}
|
||||||
|
|
||||||
// All looks good, let's now open the wallet again.
|
// All looks good, let's now open the wallet again.
|
||||||
unlockedWallet, err = loader.OpenExistingWallet(password, false)
|
unlockedWallet, err = loader.OpenExistingWallet(password, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return unlockedWallet, loader.UnloadWallet, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnlockWallet sends the password provided by the incoming UnlockWalletRequest
|
||||||
|
// over the UnlockMsgs channel in case it successfully decrypts an existing
|
||||||
|
// wallet found in the chain's wallet database directory.
|
||||||
|
func (u *UnlockerService) UnlockWallet(ctx context.Context,
|
||||||
|
in *lnrpc.UnlockWalletRequest) (*lnrpc.UnlockWalletResponse, error) {
|
||||||
|
|
||||||
|
password := in.WalletPassword
|
||||||
|
recoveryWindow := uint32(in.RecoveryWindow)
|
||||||
|
|
||||||
|
unlockedWallet, unloadFn, err := u.LoadAndUnlock(
|
||||||
|
password, recoveryWindow,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// We successfully opened the wallet and pass the instance back to
|
// We successfully opened the wallet and pass the instance back to
|
||||||
// avoid it needing to be unlocked again.
|
// avoid it needing to be unlocked again.
|
||||||
walletUnlockMsg := &WalletUnlockMsg{
|
walletUnlockMsg := &WalletUnlockMsg{
|
||||||
Passphrase: password,
|
Passphrase: password,
|
||||||
RecoveryWindow: recoveryWindow,
|
RecoveryWindow: recoveryWindow,
|
||||||
Wallet: unlockedWallet,
|
Wallet: unlockedWallet,
|
||||||
UnloadWallet: loader.UnloadWallet,
|
UnloadWallet: unloadFn,
|
||||||
StatelessInit: in.StatelessInit,
|
StatelessInit: in.StatelessInit,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user