From e736ae125d3c9aacd7ae44af3e6aed3e57945920 Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Tue, 1 Aug 2017 18:16:42 +0100 Subject: [PATCH] Read RPC username/pass from correct config file for btcctl defaults. If no existing btcctl.conf file exists, btcctl creates a default one using the RPC username and password from the btcd.conf. If the --wallet flag is passed, however, it should read from btcwallet.conf instead. https://github.com/btcsuite/btcd/issues/875. --- cmd/btcctl/config.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/cmd/btcctl/config.go b/cmd/btcctl/config.go index 9d08a0e2..cd232a9e 100644 --- a/cmd/btcctl/config.go +++ b/cmd/btcctl/config.go @@ -211,7 +211,15 @@ func loadConfig() (*config, []string, error) { } if _, err := os.Stat(preCfg.ConfigFile); os.IsNotExist(err) { - err := createDefaultConfigFile(preCfg.ConfigFile) + // Use config file for RPC server to create default btcctl config + var serverConfigPath string + if preCfg.Wallet { + serverConfigPath = filepath.Join(btcwalletHomeDir, "btcwallet.conf") + } else { + serverConfigPath = filepath.Join(btcdHomeDir, "btcd.conf") + } + + err := createDefaultConfigFile(preCfg.ConfigFile, serverConfigPath) if err != nil { fmt.Fprintf(os.Stderr, "Error creating a default config file: %v\n", err) } @@ -272,17 +280,16 @@ func loadConfig() (*config, []string, error) { } // createDefaultConfig creates a basic config file at the given destination path. -// For this it tries to read the btcd config file at its default path, and extract -// the RPC user and password from it. -func createDefaultConfigFile(destinationPath string) error { - // Read btcd.conf from its default path - btcdConfigPath := filepath.Join(btcdHomeDir, "btcd.conf") - btcdConfigFile, err := os.Open(btcdConfigPath) +// For this it tries to read the config file for the RPC server (either btcd or +// btcwallet), and extract the RPC user and password from it. +func createDefaultConfigFile(destinationPath, serverConfigPath string) error { + // Read the RPC server config + serverConfigFile, err := os.Open(serverConfigPath) if err != nil { return err } - defer btcdConfigFile.Close() - content, err := ioutil.ReadAll(btcdConfigFile) + defer serverConfigFile.Close() + content, err := ioutil.ReadAll(serverConfigFile) if err != nil { return err }