From d8ce90306d77ac031cae7c1e1cd08bed5faae63d Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 1 Feb 2018 20:50:58 -0800 Subject: [PATCH] lnwallet/btcwallet: during initial creation catch the case of an existing wallet In this commit, due to the recent changes within lnd itself, it may be possible that a wallet already exists when the wallet has been signaled to be created. As a result, *always* open the wallet ourselves, but allow an existing wallet to already be in place. --- lnwallet/btcwallet/btcwallet.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lnwallet/btcwallet/btcwallet.go b/lnwallet/btcwallet/btcwallet.go index d5b1af00..2855d319 100644 --- a/lnwallet/btcwallet/btcwallet.go +++ b/lnwallet/btcwallet/btcwallet.go @@ -80,21 +80,33 @@ func New(cfg Config) (*BtcWallet, error) { var wallet *base.Wallet if !walletExists { // Wallet has never been created, perform initial set up. - wallet, err = loader.CreateNewWallet(pubPass, cfg.PrivatePass, - cfg.HdSeed) - if err != nil { + wallet, err = loader.CreateNewWallet( + pubPass, cfg.PrivatePass, cfg.HdSeed, + ) + + switch { + // If the wallet already exists, then we'll ignore this error + // and proceed directly to opening the wallet. + case err == base.ErrExists: + + // Otherwise, there's a greater error here, and we'll return + // early. + case err != nil: return nil, err } - } else { - // Wallet has been created and been initialized at this point, - // open it along with all the required DB namespaces, and the - // DB itself. - wallet, err = loader.OpenExistingWallet(pubPass, false) - if err != nil { + + if err := loader.UnloadWallet(); err != nil { return nil, err } } + // Wallet has been created and been initialized at this point, open it + // along with all the required DB namepsaces, and the DB itself. + wallet, err = loader.OpenExistingWallet(pubPass, false) + if err != nil { + return nil, err + } + // Create a bucket within the wallet's database dedicated to storing // our LN specific data. db := wallet.Database()