btcwallet: use already unlocked wallet if available

This commit is contained in:
Oliver Gugger 2018-05-22 09:28:51 +02:00 committed by Olaoluwa Osuntokun
parent 8f2a8d6682
commit 4cefb6b8dc

@ -81,37 +81,45 @@ func New(cfg Config) (*BtcWallet, error) {
Coin: cfg.CoinType, Coin: cfg.CoinType,
} }
// Maybe the wallet has already been opened and unlocked by the
// WalletUnlocker. So if we get a non-nil value from the config,
// we assume everything is in order.
var wallet = cfg.Wallet
if wallet == nil {
// No ready wallet was passed, so try to open an existing one.
var pubPass []byte var pubPass []byte
if cfg.PublicPass == nil { if cfg.PublicPass == nil {
pubPass = defaultPubPassphrase pubPass = defaultPubPassphrase
} else { } else {
pubPass = cfg.PublicPass pubPass = cfg.PublicPass
} }
loader := base.NewLoader(cfg.NetParams, netDir,
loader := base.NewLoader(cfg.NetParams, netDir, cfg.RecoveryWindow) cfg.RecoveryWindow)
walletExists, err := loader.WalletExists() walletExists, err := loader.WalletExists()
if err != nil { if err != nil {
return nil, err return nil, err
} }
var wallet *base.Wallet
if !walletExists { if !walletExists {
// Wallet has never been created, perform initial set up. // Wallet has never been created, perform initial
// set up.
wallet, err = loader.CreateNewWallet( wallet, err = loader.CreateNewWallet(
pubPass, cfg.PrivatePass, cfg.HdSeed, cfg.Birthday, pubPass, cfg.PrivatePass, cfg.HdSeed,
cfg.Birthday,
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else { } else {
// Wallet has been created and been initialized at this point, // Wallet has been created and been initialized at
// open it along with all the required DB namespaces, and the // this point, open it along with all the required DB
// DB itself. // namespaces, and the DB itself.
wallet, err = loader.OpenExistingWallet(pubPass, false) wallet, err = loader.OpenExistingWallet(pubPass, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
}
return &BtcWallet{ return &BtcWallet{
cfg: &cfg, cfg: &cfg,