Skip to content

Commit

Permalink
Fix BTC Wallet creation on the regtest network (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
orlangur authored Aug 29, 2024
1 parent 0f0ba15 commit bfeb27d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 62 deletions.
1 change: 1 addition & 0 deletions chain/bitcoind_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions netparams/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
60 changes: 20 additions & 40 deletions run/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)"`
Expand All @@ -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)"`
Expand Down Expand Up @@ -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++
Expand Down Expand Up @@ -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
Expand All @@ -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{}{
Expand Down
34 changes: 12 additions & 22 deletions run/walletsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package run

import (
"bufio"
//"bufio"
"fmt"
"os"
"path/filepath"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit bfeb27d

Please sign in to comment.