lnd: threads recovery window param from walletunlocker
This commit is contained in:
parent
b9df5e75f7
commit
603228ce79
98
lnd.go
98
lnd.go
@ -207,19 +207,35 @@ func lndMain() error {
|
|||||||
defer macaroonService.Close()
|
defer macaroonService.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
privateWalletPw = []byte("hello")
|
||||||
|
publicWalletPw = []byte("public")
|
||||||
|
birthday time.Time
|
||||||
|
recoveryWindow uint32
|
||||||
|
)
|
||||||
|
|
||||||
// We wait until the user provides a password over RPC. In case lnd is
|
// We wait until the user provides a password over RPC. In case lnd is
|
||||||
// started with the --noencryptwallet flag, we use the default password
|
// started with the --noencryptwallet flag, we use the default password
|
||||||
// "hello" for wallet encryption.
|
// "hello" for wallet encryption.
|
||||||
privateWalletPw := []byte("hello")
|
|
||||||
publicWalletPw := []byte("public")
|
|
||||||
if !cfg.NoEncryptWallet {
|
if !cfg.NoEncryptWallet {
|
||||||
privateWalletPw, publicWalletPw, err = waitForWalletPassword(
|
walletInitParams, err := waitForWalletPassword(
|
||||||
cfg.RPCListeners, cfg.RESTListeners, serverOpts, proxyOpts,
|
cfg.RPCListeners, cfg.RESTListeners, serverOpts,
|
||||||
tlsConf, macaroonService,
|
proxyOpts, tlsConf, macaroonService,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
privateWalletPw = walletInitParams.Password
|
||||||
|
publicWalletPw = walletInitParams.Password
|
||||||
|
birthday = walletInitParams.Birthday
|
||||||
|
recoveryWindow = walletInitParams.RecoveryWindow
|
||||||
|
|
||||||
|
if recoveryWindow > 0 {
|
||||||
|
ltndLog.Infof("Wallet recovery mode enabled with "+
|
||||||
|
"address lookahead of %d addresses",
|
||||||
|
recoveryWindow)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cfg.NoMacaroons {
|
if !cfg.NoMacaroons {
|
||||||
@ -251,8 +267,10 @@ func lndMain() error {
|
|||||||
// With the information parsed from the configuration, create valid
|
// With the information parsed from the configuration, create valid
|
||||||
// instances of the pertinent interfaces required to operate the
|
// instances of the pertinent interfaces required to operate the
|
||||||
// Lightning Network Daemon.
|
// Lightning Network Daemon.
|
||||||
activeChainControl, chainCleanUp, err := newChainControlFromConfig(cfg,
|
activeChainControl, chainCleanUp, err := newChainControlFromConfig(
|
||||||
chanDB, privateWalletPw, publicWalletPw)
|
cfg, chanDB, privateWalletPw, publicWalletPw, birthday,
|
||||||
|
recoveryWindow,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("unable to create chain control: %v\n", err)
|
fmt.Printf("unable to create chain control: %v\n", err)
|
||||||
return err
|
return err
|
||||||
@ -827,12 +845,30 @@ func genMacaroons(ctx context.Context, svc *macaroons.Service,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WalletUnlockParams holds the variables used to parameterize the unlocking of
|
||||||
|
// lnd's wallet after it has already been created.
|
||||||
|
type WalletUnlockParams struct {
|
||||||
|
// Password is the public and private wallet passphrase.
|
||||||
|
Password []byte
|
||||||
|
|
||||||
|
// Birthday specifies the approximate time that this wallet was created.
|
||||||
|
// This is used to bound any rescans on startup.
|
||||||
|
Birthday time.Time
|
||||||
|
|
||||||
|
// RecoveryWindow specifies the address lookahead when entering recovery
|
||||||
|
// mode. A recovery will be attempted if this value is non-zero.
|
||||||
|
RecoveryWindow uint32
|
||||||
|
}
|
||||||
|
|
||||||
// waitForWalletPassword will spin up gRPC and REST endpoints for the
|
// waitForWalletPassword will spin up gRPC and REST endpoints for the
|
||||||
// WalletUnlocker server, and block until a password is provided by
|
// WalletUnlocker server, and block until a password is provided by
|
||||||
// the user to this RPC server.
|
// the user to this RPC server.
|
||||||
func waitForWalletPassword(grpcEndpoints, restEndpoints []string,
|
func waitForWalletPassword(
|
||||||
serverOpts []grpc.ServerOption, proxyOpts []grpc.DialOption,
|
grpcEndpoints, restEndpoints []string,
|
||||||
tlsConf *tls.Config, macaroonService *macaroons.Service) ([]byte, []byte, error) {
|
serverOpts []grpc.ServerOption,
|
||||||
|
proxyOpts []grpc.DialOption,
|
||||||
|
tlsConf *tls.Config,
|
||||||
|
macaroonService *macaroons.Service) (*WalletUnlockParams, error) {
|
||||||
|
|
||||||
// Set up a new PasswordService, which will listen
|
// Set up a new PasswordService, which will listen
|
||||||
// for passwords provided over RPC.
|
// for passwords provided over RPC.
|
||||||
@ -857,7 +893,7 @@ func waitForWalletPassword(grpcEndpoints, restEndpoints []string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
ltndLog.Errorf("password RPC server unable to listen on %s",
|
ltndLog.Errorf("password RPC server unable to listen on %s",
|
||||||
grpcEndpoint)
|
grpcEndpoint)
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer lis.Close()
|
defer lis.Close()
|
||||||
|
|
||||||
@ -879,7 +915,7 @@ func waitForWalletPassword(grpcEndpoints, restEndpoints []string,
|
|||||||
err := lnrpc.RegisterWalletUnlockerHandlerFromEndpoint(ctx, mux,
|
err := lnrpc.RegisterWalletUnlockerHandlerFromEndpoint(ctx, mux,
|
||||||
grpcEndpoints[0], proxyOpts)
|
grpcEndpoints[0], proxyOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
srv := &http.Server{Handler: mux}
|
srv := &http.Server{Handler: mux}
|
||||||
@ -889,7 +925,7 @@ func waitForWalletPassword(grpcEndpoints, restEndpoints []string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
ltndLog.Errorf("password gRPC proxy unable to listen on %s",
|
ltndLog.Errorf("password gRPC proxy unable to listen on %s",
|
||||||
restEndpoint)
|
restEndpoint)
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer lis.Close()
|
defer lis.Close()
|
||||||
|
|
||||||
@ -920,14 +956,15 @@ func waitForWalletPassword(grpcEndpoints, restEndpoints []string,
|
|||||||
case initMsg := <-pwService.InitMsgs:
|
case initMsg := <-pwService.InitMsgs:
|
||||||
password := initMsg.Passphrase
|
password := initMsg.Passphrase
|
||||||
cipherSeed := initMsg.WalletSeed
|
cipherSeed := initMsg.WalletSeed
|
||||||
|
recoveryWindow := initMsg.RecoveryWindow
|
||||||
|
|
||||||
// Before we proceed, we'll check the internal version of the
|
// Before we proceed, we'll check the internal version of the
|
||||||
// seed. If it's greater than the current key derivation
|
// seed. If it's greater than the current key derivation
|
||||||
// version, then we'll return an error as we don't understand
|
// version, then we'll return an error as we don't understand
|
||||||
// this.
|
// this.
|
||||||
if cipherSeed.InternalVersion != keychain.KeyDerivationVersion {
|
if cipherSeed.InternalVersion != keychain.KeyDerivationVersion {
|
||||||
return nil, nil, fmt.Errorf("invalid internal seed "+
|
return nil, fmt.Errorf("invalid internal seed version "+
|
||||||
"version %v, current version is %v",
|
"%v, current version is %v",
|
||||||
cipherSeed.InternalVersion,
|
cipherSeed.InternalVersion,
|
||||||
keychain.KeyDerivationVersion)
|
keychain.KeyDerivationVersion)
|
||||||
}
|
}
|
||||||
@ -935,31 +972,42 @@ func waitForWalletPassword(grpcEndpoints, restEndpoints []string,
|
|||||||
netDir := btcwallet.NetworkDir(
|
netDir := btcwallet.NetworkDir(
|
||||||
chainConfig.ChainDir, activeNetParams.Params,
|
chainConfig.ChainDir, activeNetParams.Params,
|
||||||
)
|
)
|
||||||
loader := wallet.NewLoader(activeNetParams.Params, netDir)
|
loader := wallet.NewLoader(
|
||||||
|
activeNetParams.Params, netDir, uint32(recoveryWindow),
|
||||||
|
)
|
||||||
|
|
||||||
// With the seed, we can now use the wallet loader to create
|
// With the seed, we can now use the wallet loader to create
|
||||||
// the wallet, then unload it so it can be opened shortly
|
// the wallet, then unload it so it can be opened shortly
|
||||||
// after.
|
birthday := cipherSeed.BirthdayTime()
|
||||||
// TODO(roasbeef): extend loader to also accept birthday
|
|
||||||
_, err = loader.CreateNewWallet(
|
_, err = loader.CreateNewWallet(
|
||||||
password, password, cipherSeed.Entropy[:],
|
password, password, cipherSeed.Entropy[:], birthday,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := loader.UnloadWallet(); err != nil {
|
if err := loader.UnloadWallet(); err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return password, password, nil
|
walletInitParams := &WalletUnlockParams{
|
||||||
|
Password: password,
|
||||||
|
Birthday: birthday,
|
||||||
|
RecoveryWindow: recoveryWindow,
|
||||||
|
}
|
||||||
|
|
||||||
|
return walletInitParams, nil
|
||||||
|
|
||||||
// The wallet has already been created in the past, and is simply being
|
// The wallet has already been created in the past, and is simply being
|
||||||
// unlocked. So we'll just return these passphrases.
|
// unlocked. So we'll just return these passphrases.
|
||||||
case walletPw := <-pwService.UnlockPasswords:
|
case unlockMsg := <-pwService.UnlockMsgs:
|
||||||
return walletPw, walletPw, nil
|
walletInitParams := &WalletUnlockParams{
|
||||||
|
Password: unlockMsg.Passphrase,
|
||||||
|
RecoveryWindow: unlockMsg.RecoveryWindow,
|
||||||
|
}
|
||||||
|
return walletInitParams, nil
|
||||||
|
|
||||||
case <-shutdownChannel:
|
case <-shutdownChannel:
|
||||||
return nil, nil, fmt.Errorf("shutting down")
|
return nil, fmt.Errorf("shutting down")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user