From bfeb27d1779ec33a1d5ad7e663473e716411b43d Mon Sep 17 00:00:00 2001 From: Vlad Veselov Date: Thu, 29 Aug 2024 23:58:44 +0300 Subject: [PATCH] Fix BTC Wallet creation on the regtest network (#21) --- chain/bitcoind_events_test.go | 1 + netparams/params.go | 6 ++++ run/config.go | 60 ++++++++++++----------------------- run/walletsetup.go | 34 +++++++------------- 4 files changed, 39 insertions(+), 62 deletions(-) diff --git a/chain/bitcoind_events_test.go b/chain/bitcoind_events_test.go index 3056cc495d..478e22d662 100644 --- a/chain/bitcoind_events_test.go +++ b/chain/bitcoind_events_test.go @@ -23,6 +23,7 @@ import ( // block notifications for both the case where a ZMQ subscription is used and // for the case where RPC polling is used. func TestBitcoindEvents(t *testing.T) { + tests := []struct { name string rpcPolling bool diff --git a/netparams/params.go b/netparams/params.go index f54781e206..78e31307bd 100644 --- a/netparams/params.go +++ b/netparams/params.go @@ -25,6 +25,12 @@ var MainNetParams = Params{ RPCServerPort: "8332", } +var RegtestParams = Params{ + Params: &chaincfg.RegressionNetParams, + RPCClientPort: "18444", + RPCServerPort: "18443", +} + // TestNet3Params contains parameters specific running btcwallet and // btcd on the test network (version 3) (wire.TestNet3). var TestNet3Params = Params{ diff --git a/run/config.go b/run/config.go index 37225bbbef..d20720667f 100644 --- a/run/config.go +++ b/run/config.go @@ -21,7 +21,6 @@ import ( flags "github.com/jessevdk/go-flags" "github.com/lightninglabs/neutrino" "github.com/stroomnetwork/btcwallet/cfgutil" - "github.com/stroomnetwork/btcwallet/internal/legacy/keystore" "github.com/stroomnetwork/btcwallet/netparams" "github.com/stroomnetwork/btcwallet/wallet" ) @@ -52,6 +51,7 @@ type Config struct { Create bool `long:"create" description:"Create the wallet if it does not exist"` CreateTemp bool `long:"createtemp" description:"Create a temporary simulation wallet (pass=password) in the data directory indicated; must call with --datadir"` AppDataDir *cfgutil.ExplicitString `short:"A" long:"appdata" description:"Application data directory for wallet config, databases and logs"` + Regtest bool `long:"regtest" description:"Use the test Bitcoin regtest network (default mainnet)"` TestNet3 bool `long:"testnet" description:"Use the test Bitcoin network (version 3) (default mainnet)"` SimNet bool `long:"simnet" description:"Use the simulation test network (default mainnet)"` SigNet bool `long:"signet" description:"Use the signet test network (default mainnet)"` @@ -64,7 +64,9 @@ type Config struct { DBTimeout time.Duration `long:"dbtimeout" description:"The timeout value to use when opening the wallet database."` // Wallet options - WalletPass string `long:"walletpass" default-mask:"-" description:"The public wallet password -- Only required if the wallet was created with one"` + WalletPrivatePass string `long:"walletprivatepass" default-mask:"-" description:"The private wallet passphrase"` + WalletPass string `long:"walletpass" default-mask:"-" description:"The public wallet passphrase -- Only required if the wallet was created with one"` + WalletSeed string `long:"walletseed" default-mask:"-" description:"The wallet seed"` // RPC client options RPCConnect string `short:"c" long:"rpcconnect" description:"Hostname/IP and port of btcd RPC server to connect to (default localhost:8334, testnet: localhost:18334, simnet: localhost:18556)"` @@ -382,6 +384,11 @@ func loadConfig(cfg *Config) error { // Choose the active network params based on the selected network. // Multiple networks can't be selected simultaneously. numNets := 0 + if cfg.Regtest { + activeNet = &netparams.RegtestParams + numNets++ + } + if cfg.TestNet3 { activeNet = &netparams.TestNet3Params numNets++ @@ -467,11 +474,6 @@ func loadConfig(cfg *Config) error { netDir := networkDir(cfg.AppDataDir.Value, activeNet.Params) dbPath := filepath.Join(netDir, wallet.WalletDBName) - if cfg.CreateTemp && cfg.Create { - return fmt.Errorf("the flags --create and --createtemp can not " + - "be specified together. Use --help for more information") - } - dbFileExists, err := cfgutil.FileExists(dbPath) if err != nil { return err @@ -498,41 +500,19 @@ func loadConfig(cfg *Config) error { return fmt.Errorf("unable to create wallet: %w", err) } } - } else if cfg.Create { - // Error if the create flag is set and the wallet already - // exists. - if dbFileExists { - return fmt.Errorf("the wallet database file `%v` "+ - "already exists", dbPath) - } - - // Ensure the data directory for the network exists. - if err := checkCreateDir(netDir); err != nil { - return err - } - - // Perform the initial wallet creation wizard. - if err := createWallet(cfg); err != nil { - - return fmt.Errorf("unable to create wallet: %w", err) - } + } else { + // We consider create flag is always set and create the wallet whenever it does not exist + if !dbFileExists { + // Ensure the data directory for the specified network exists. + if err := checkCreateDir(netDir); err != nil { + return err + } - // Created successfully, so exit now with success. - os.Exit(0) - } else if !dbFileExists && !cfg.NoInitialLoad { - keystorePath := filepath.Join(netDir, keystore.Filename) - keystoreExists, err := cfgutil.FileExists(keystorePath) - if err != nil { - return err - } - if !keystoreExists { - err = fmt.Errorf("the wallet does not exist, run with " + - "the --create option to initialize and create it") - } else { - err = fmt.Errorf("the wallet is in legacy format, run " + - "with the --create option to import it") + // Create the wallet using the initial wallet creation wizard. + if err := createWallet(cfg); err != nil { + return fmt.Errorf("unable to create wallet: %w", err) + } } - return err } localhostListeners := map[string]struct{}{ diff --git a/run/walletsetup.go b/run/walletsetup.go index fb9a86ce2c..5252f59c5f 100644 --- a/run/walletsetup.go +++ b/run/walletsetup.go @@ -5,7 +5,7 @@ package run import ( - "bufio" + //"bufio" "fmt" "os" "path/filepath" @@ -17,7 +17,7 @@ import ( "github.com/btcsuite/btcwallet/walletdb" _ "github.com/btcsuite/btcwallet/walletdb/bdb" "github.com/stroomnetwork/btcwallet/internal/legacy/keystore" - "github.com/stroomnetwork/btcwallet/internal/prompt" + //"github.com/stroomnetwork/btcwallet/internal/prompt" "github.com/stroomnetwork/btcwallet/waddrmgr" "github.com/stroomnetwork/btcwallet/wallet" ) @@ -95,7 +95,7 @@ func convertLegacyKeystore(legacyKeyStore *keystore.Store, w *wallet.Wallet) { } } -// createWallet prompts the user for information needed to generate a new wallet +// createWallet obtains information needed to generate a new wallet from config instead of prompting user // and generates the wallet accordingly. The new wallet will reside at the // provided path. func createWallet(cfg *Config) error { @@ -104,6 +104,8 @@ func createWallet(cfg *Config) error { activeNet.Params, dbDir, true, cfg.DBTimeout, 250, ) + privPass := []byte(cfg.WalletPrivatePass) + // When there is a legacy keystore, open it now to ensure any errors // don't end up exiting the process after the user has spent time // entering a bunch of information. @@ -123,15 +125,6 @@ func createWallet(cfg *Config) error { } } - // Start by prompting for the private passphrase. When there is an - // existing keystore, the user will be promped for that passphrase, - // otherwise they will be prompted for a new one. - reader := bufio.NewReader(os.Stdin) - privPass, err := prompt.PrivatePass(reader, legacyKeyStore) - if err != nil { - return err - } - // When there exists a legacy keystore, unlock it now and set up a // callback to import all keystore keys into the new walletdb // wallet @@ -173,22 +166,19 @@ func createWallet(cfg *Config) error { // Ascertain the public passphrase. This will either be a value // specified by the user or the default hard-coded public passphrase if // the user does not want the additional public data encryption. - pubPass, err := prompt.PublicPass(reader, privPass, - []byte(wallet.InsecurePubPassphrase), []byte(cfg.WalletPass)) - if err != nil { - return err + pubPass := []byte(cfg.WalletPass) + seed := []byte(nil) + if len(cfg.WalletSeed) != 0 { + seed = []byte(cfg.WalletSeed) } - // 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, err := prompt.Seed(reader) + fmt.Println("Creating the wallet...") + w, err := loader.CreateNewWallet(pubPass, privPass, seed, time.Now()) if err != nil { return err } - fmt.Println("Creating the wallet...") - w, err := loader.CreateNewWallet(pubPass, privPass, seed, time.Now()) + err = loader.UnloadWallet() if err != nil { return err }