// Copyright (c) 2014-2015 The btcsuite developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. package main import ( "bufio" "fmt" "os" "path/filepath" "time" "github.com/lbryio/lbcd/chaincfg" "github.com/lbryio/lbcd/wire" "github.com/lbryio/lbcwallet/internal/prompt" "github.com/lbryio/lbcwallet/wallet" "github.com/lbryio/lbcwallet/walletdb" _ "github.com/lbryio/lbcwallet/walletdb/bdb" ) // networkDir returns the directory name of a network directory to hold wallet // files. func networkDir(dataDir string, chainParams *chaincfg.Params) string { netname := chainParams.Name // For now, we must always name the testnet data directory as "testnet" // and not "testnet3" or any other version, as the chaincfg testnet3 // parameters will likely be switched to being named "testnet3" in the // future. This is done to future proof that change, and an upgrade // plan to move the testnet3 data directory can be worked out later. if chainParams.Net == wire.TestNet3 { netname = "testnet" } return filepath.Join(dataDir, netname) } // createWallet prompts the user for information needed to generate a new wallet // and generates the wallet accordingly. The new wallet will reside at the // provided path. func createWallet(cfg *config) error { dbDir := networkDir(cfg.AppDataDir.Value, activeNet.Params) loader := wallet.NewLoader( activeNet.Params, dbDir, true, cfg.DBTimeout, 250, ) // Start by prompting for the passphrase. reader := bufio.NewReader(os.Stdin) privPass, err := prompt.Passphrase(true) if err != nil { return err } // Ascertain the wallet generation seed. This will either be an // automatically generated value the user has already confirmed or a // value the user has entered which has already been validated. seed, bday, err := prompt.Seed(reader) if err != nil { return err } fmt.Println("Creating the wallet...") w, err := loader.CreateNewWallet(privPass, seed, bday) if err != nil { return err } w.Manager.Close() fmt.Println("The wallet has been created successfully with birthday:", bday.Format(time.UnixDate)) return nil } // createSimulationWallet is intended to be called from the rpcclient // and used to create a wallet for actors involved in simulations. func createSimulationWallet(cfg *config) error { // Simulation wallet password is 'password'. privPass := []byte("password") netDir := networkDir(cfg.AppDataDir.Value, activeNet.Params) // Create the wallet. dbPath := filepath.Join(netDir, wallet.WalletDBName) fmt.Println("Creating the wallet...") // Create the wallet database backed by bolt db. db, err := walletdb.Create("bdb", dbPath, true, cfg.DBTimeout) if err != nil { return err } defer db.Close() // Create the wallet. err = wallet.Create(db, privPass, nil, activeNet.Params, time.Now()) if err != nil { return err } fmt.Println("The wallet has been created successfully.") return nil } // checkCreateDir checks that the path exists and is a directory. // If path does not exist, it is created. func checkCreateDir(path string) error { if fi, err := os.Stat(path); err != nil { if os.IsNotExist(err) { // Attempt data directory creation if err = os.MkdirAll(path, 0700); err != nil { return fmt.Errorf("cannot create directory: %s", err) } } else { return fmt.Errorf("error checking directory: %s", err) } } else { if !fi.IsDir() { return fmt.Errorf("path '%s' is not a directory", path) } } return nil }