2013-08-22 18:30:38 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Conformal Systems LLC <info@conformal.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-11-12 21:25:56 +01:00
|
|
|
"github.com/conformal/btcutil"
|
2013-10-16 23:29:48 +02:00
|
|
|
"github.com/conformal/btcwire"
|
2013-08-21 16:37:30 +02:00
|
|
|
"github.com/conformal/go-flags"
|
2013-11-19 18:21:54 +01:00
|
|
|
"net"
|
2013-08-21 16:37:30 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-12-03 16:58:57 +01:00
|
|
|
defaultCAFilename = "btcd.cert"
|
2013-08-21 16:37:30 +02:00
|
|
|
defaultConfigFilename = "btcwallet.conf"
|
2013-10-16 23:29:48 +02:00
|
|
|
defaultBtcNet = btcwire.TestNet3
|
2013-08-21 16:37:30 +02:00
|
|
|
defaultLogLevel = "info"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2013-12-03 16:52:09 +01:00
|
|
|
btcwalletHomeDir = btcutil.AppDataDir("btcwallet", false)
|
|
|
|
defaultCAFile = filepath.Join(btcwalletHomeDir, defaultCAFilename)
|
|
|
|
defaultConfigFile = filepath.Join(btcwalletHomeDir, defaultConfigFilename)
|
|
|
|
defaultDataDir = btcwalletHomeDir
|
|
|
|
defaultRPCKeyFile = filepath.Join(btcwalletHomeDir, "rpc.key")
|
|
|
|
defaultRPCCertFile = filepath.Join(btcwalletHomeDir, "rpc.cert")
|
2013-08-21 16:37:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
|
2013-11-19 18:21:54 +01:00
|
|
|
CAFile string `long:"cafile" description:"File containing root certificates to authenticate a TLS connections with btcd"`
|
|
|
|
Connect string `short:"c" long:"connect" description:"Server and port of btcd instance to connect to"`
|
2013-08-21 16:37:30 +02:00
|
|
|
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"`
|
|
|
|
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
|
2013-10-16 23:29:48 +02:00
|
|
|
SvrPort string `short:"p" long:"serverport" description:"Port to serve frontend websocket connections on (default: 18332, mainnet: 8332)"`
|
2013-09-03 06:10:32 +02:00
|
|
|
DataDir string `short:"D" long:"datadir" description:"Directory to store wallets and transactions"`
|
2013-10-03 15:11:35 +02:00
|
|
|
Username string `short:"u" long:"username" description:"Username for btcd authorization"`
|
|
|
|
Password string `short:"P" long:"password" description:"Password for btcd authorization"`
|
2013-12-03 16:52:09 +01:00
|
|
|
RPCCert string `long:"rpccert" description:"File containing the certificate file"`
|
|
|
|
RPCKey string `long:"rpckey" description:"File containing the certificate key"`
|
2013-10-15 22:06:13 +02:00
|
|
|
MainNet bool `long:"mainnet" description:"*DISABLED* Use the main Bitcoin network (default testnet3)"`
|
2013-11-20 02:47:15 +01:00
|
|
|
Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"`
|
|
|
|
ProxyUser string `long:"proxyuser" description:"Username for proxy server"`
|
|
|
|
ProxyPass string `long:"proxypass" default-mask:"-" description:"Password for proxy server"`
|
2013-11-21 17:27:26 +01:00
|
|
|
Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"`
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2013-11-21 16:02:27 +01:00
|
|
|
// cleanAndExpandPath expands environement variables and leading ~ in the
|
|
|
|
// passed path, cleans the result, and returns it.
|
|
|
|
func cleanAndExpandPath(path string) string {
|
|
|
|
// Expand initial ~ to OS specific home directory.
|
|
|
|
if strings.HasPrefix(path, "~") {
|
|
|
|
homeDir := filepath.Dir(btcwalletHomeDir)
|
|
|
|
path = strings.Replace(path, "~", homeDir, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%,
|
|
|
|
// but they variables can still be expanded via POSIX-style $VARIABLE.
|
|
|
|
return filepath.Clean(os.ExpandEnv(path))
|
|
|
|
}
|
|
|
|
|
2013-10-16 23:29:48 +02:00
|
|
|
// updateConfigWithActiveParams update the passed config with parameters
|
|
|
|
// from the active net params if the relevant options in the passed config
|
|
|
|
// object are the default so options specified by the user on the command line
|
|
|
|
// are not overridden.
|
|
|
|
func updateConfigWithActiveParams(cfg *config) {
|
2013-11-19 18:21:54 +01:00
|
|
|
if cfg.Connect == netParams(defaultBtcNet).connect {
|
|
|
|
cfg.Connect = activeNetParams.connect
|
2013-10-16 23:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.SvrPort == netParams(defaultBtcNet).svrPort {
|
|
|
|
cfg.SvrPort = activeNetParams.svrPort
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
// filesExists reports whether the named file or directory exists.
|
|
|
|
func fileExists(name string) bool {
|
|
|
|
if _, err := os.Stat(name); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-11-19 18:21:54 +01:00
|
|
|
// normalizeAddress returns addr with the passed default port appended if
|
|
|
|
// there is not already a port specified.
|
|
|
|
func normalizeAddress(addr, defaultPort string) string {
|
|
|
|
_, _, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return net.JoinHostPort(addr, defaultPort)
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
// loadConfig initializes and parses the config using a config file and command
|
|
|
|
// line options.
|
|
|
|
//
|
|
|
|
// The configuration proceeds as follows:
|
|
|
|
// 1) Start with a default config with sane settings
|
|
|
|
// 2) Pre-parse the command line to check for an alternative config file
|
|
|
|
// 3) Load configuration file overwriting defaults with any specified options
|
|
|
|
// 4) Parse CLI options and overwrite/add any specified options
|
|
|
|
//
|
|
|
|
// The above results in btcwallet functioning properly without any config
|
|
|
|
// settings while still allowing the user to override settings with config files
|
|
|
|
// and command line options. Command line options always take precedence.
|
|
|
|
func loadConfig() (*config, []string, error) {
|
|
|
|
// Default config.
|
|
|
|
cfg := config{
|
|
|
|
DebugLevel: defaultLogLevel,
|
2013-11-19 18:21:54 +01:00
|
|
|
CAFile: defaultCAFile,
|
2013-08-21 16:37:30 +02:00
|
|
|
ConfigFile: defaultConfigFile,
|
2013-11-19 18:21:54 +01:00
|
|
|
Connect: netParams(defaultBtcNet).connect,
|
2013-10-16 23:29:48 +02:00
|
|
|
SvrPort: netParams(defaultBtcNet).svrPort,
|
2013-09-03 06:10:32 +02:00
|
|
|
DataDir: defaultDataDir,
|
2013-12-03 16:52:09 +01:00
|
|
|
RPCKey: defaultRPCKeyFile,
|
|
|
|
RPCCert: defaultRPCCertFile,
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A config file in the current directory takes precedence.
|
|
|
|
if fileExists(defaultConfigFilename) {
|
|
|
|
cfg.ConfigFile = defaultConfigFile
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pre-parse the command line options to see if an alternative config
|
|
|
|
// file or the version flag was specified.
|
|
|
|
preCfg := cfg
|
|
|
|
preParser := flags.NewParser(&preCfg, flags.Default)
|
|
|
|
_, err := preParser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
|
|
|
|
preParser.WriteHelp(os.Stderr)
|
|
|
|
}
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show the version and exit if the version flag was specified.
|
|
|
|
if preCfg.ShowVersion {
|
|
|
|
appName := filepath.Base(os.Args[0])
|
|
|
|
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
|
|
|
|
fmt.Println(appName, "version", version())
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load additional config from file.
|
2013-11-25 18:20:37 +01:00
|
|
|
var configFileError error
|
2013-08-21 16:37:30 +02:00
|
|
|
parser := flags.NewParser(&cfg, flags.Default)
|
2013-11-25 18:20:37 +01:00
|
|
|
err = flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
|
2013-08-21 16:37:30 +02:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*os.PathError); !ok {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
parser.WriteHelp(os.Stderr)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2013-11-25 18:20:37 +01:00
|
|
|
configFileError = err
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse command line options again to ensure they take precedence.
|
|
|
|
remainingArgs, err := parser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
|
|
|
|
parser.WriteHelp(os.Stderr)
|
|
|
|
}
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2013-11-25 18:20:37 +01:00
|
|
|
// Warn about missing config file after the final command line parse
|
|
|
|
// succeeds. This prevents the warning on help messages and invalid
|
|
|
|
// options.
|
|
|
|
if configFileError != nil {
|
|
|
|
log.Warnf("%v", configFileError)
|
|
|
|
}
|
|
|
|
|
2013-10-15 22:06:13 +02:00
|
|
|
// TODO(jrick): Enable mainnet support again when ready.
|
|
|
|
cfg.MainNet = false
|
|
|
|
|
2013-10-16 23:29:48 +02:00
|
|
|
// Choose the active network params based on the mainnet net flag.
|
|
|
|
if cfg.MainNet {
|
|
|
|
activeNetParams = netParams(btcwire.MainNet)
|
|
|
|
}
|
|
|
|
updateConfigWithActiveParams(&cfg)
|
|
|
|
|
2013-10-29 15:38:51 +01:00
|
|
|
// Validate debug log level
|
|
|
|
if !validLogLevel(cfg.DebugLevel) {
|
|
|
|
str := "%s: The specified debug level [%v] is invalid"
|
|
|
|
err := fmt.Errorf(str, "loadConfig", cfg.DebugLevel)
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
parser.WriteHelp(os.Stderr)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2013-11-19 18:21:54 +01:00
|
|
|
// Add default port to connect flag if missing.
|
|
|
|
cfg.Connect = normalizeAddress(cfg.Connect, activeNetParams.btcdPort)
|
|
|
|
|
2013-11-21 16:02:27 +01:00
|
|
|
// Expand environment variable and leading ~ for filepaths.
|
|
|
|
cfg.CAFile = cleanAndExpandPath(cfg.CAFile)
|
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
return &cfg, remainingArgs, nil
|
|
|
|
}
|
2013-10-29 15:38:51 +01:00
|
|
|
|
Introduce new account file structure.
This changes the locations that account files (wallet.bin, utxo.bin,
and tx.bin) are searched for when opening or disk syncing accounts.
Previously, files were saved in the following layout:
~/.btcwallet/
- btcwallet/
- wallet.bin
- tx.bin
- utxo.bin
- btcwallet-AccountA/
- wallet.bin
- tx.bin
- utxo.bin
This format had two issues. First, each account would require its own
directory, causing a scalability issue on unix (and perhaps other)
platforms. Second, there was no distinction between testnet and
mainnet wallets, and if mainnet support was enabled, btcwallet would
attempt to open accounts with testnet wallets.
Instead, the following file structure is now used:
~/.btcwallet/
- testnet/
- wallet.bin
- tx.bin
- utxo.bin
- AccountA-wallet.bin
- AccountA-tx.bin
- AccountA-utxo.bin
This solves both of the previously-mentioned issues by requiring only
two subdirectories (one each for the testnet and mainnet bitcoin
networks), and by separating the locations to open and save testnet
and mainnet account files.
At startup, a check for the old account file structure is performed.
If found, files are moved to the new locations, and the old account
directories are removed. Account files are moved to the testnet
directory, as only testnet support is currently enabled.
The version has been bumped to 0.1.1 to reflect this change.
Fixes #16.
2013-12-05 02:16:50 +01:00
|
|
|
func (c *config) Net() btcwire.BitcoinNet {
|
|
|
|
if cfg.MainNet {
|
|
|
|
return btcwire.MainNet
|
|
|
|
}
|
|
|
|
return btcwire.TestNet3
|
|
|
|
}
|
|
|
|
|
2013-10-29 15:38:51 +01:00
|
|
|
// validLogLevel returns whether or not logLevel is a valid debug log level.
|
|
|
|
func validLogLevel(logLevel string) bool {
|
|
|
|
switch logLevel {
|
|
|
|
case "trace":
|
|
|
|
fallthrough
|
|
|
|
case "debug":
|
|
|
|
fallthrough
|
|
|
|
case "info":
|
|
|
|
fallthrough
|
|
|
|
case "warn":
|
|
|
|
fallthrough
|
|
|
|
case "error":
|
|
|
|
fallthrough
|
|
|
|
case "critical":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|