diff --git a/cmd.go b/cmd.go index 5e21c85..68e0900 100644 --- a/cmd.go +++ b/cmd.go @@ -26,6 +26,7 @@ import ( "github.com/conformal/btcwallet/wallet" "github.com/conformal/btcwire" "github.com/conformal/seelog" + "github.com/davecgh/go-spew/spew" "os" "path/filepath" "sync" @@ -526,6 +527,7 @@ func main() { wallets.m[""] = w wallets.Unlock() } + spew.Dump(cfg) go func() { // Start HTTP server to listen and send messages to frontend and btcd diff --git a/config.go b/config.go index d65a301..4452604 100644 --- a/config.go +++ b/config.go @@ -18,6 +18,7 @@ package main import ( "fmt" + "github.com/conformal/btcwire" "github.com/conformal/go-flags" "os" "path/filepath" @@ -26,9 +27,8 @@ import ( const ( defaultConfigFilename = "btcwallet.conf" - defaultBtcdPort = 8334 + defaultBtcNet = btcwire.TestNet3 defaultLogLevel = "info" - defaultServerPort = 8332 ) var ( @@ -38,10 +38,10 @@ var ( type config struct { ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` - BtcdPort int `short:"b" long:"btcdport" description:"Port to connect to btcd on"` + BtcdPort string `short:"b" long:"btcdport" description:"Port to connect to btcd on (default: 18334, mainnet: 18332)"` 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"` - SvrPort int `short:"p" long:"serverport" description:"Port to serve frontend websocket connections on"` + SvrPort string `short:"p" long:"serverport" description:"Port to serve frontend websocket connections on (default: 18332, mainnet: 8332)"` DataDir string `short:"D" long:"datadir" description:"Directory to store wallets and transactions"` Username string `short:"u" long:"username" description:"Username for btcd authorization"` Password string `short:"P" long:"password" description:"Password for btcd authorization"` @@ -66,6 +66,20 @@ func btcwalletHomeDir() string { return "." } +// 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) { + if cfg.BtcdPort == netParams(defaultBtcNet).btcdPort { + cfg.BtcdPort = activeNetParams.btcdPort + } + + if cfg.SvrPort == netParams(defaultBtcNet).svrPort { + cfg.SvrPort = activeNetParams.svrPort + } +} + // filesExists reports whether the named file or directory exists. func fileExists(name string) bool { if _, err := os.Stat(name); err != nil { @@ -93,8 +107,8 @@ func loadConfig() (*config, []string, error) { cfg := config{ DebugLevel: defaultLogLevel, ConfigFile: defaultConfigFile, - BtcdPort: defaultBtcdPort, - SvrPort: defaultServerPort, + BtcdPort: netParams(defaultBtcNet).btcdPort, + SvrPort: netParams(defaultBtcNet).svrPort, DataDir: defaultDataDir, } @@ -147,5 +161,11 @@ func loadConfig() (*config, []string, error) { // TODO(jrick): Enable mainnet support again when ready. cfg.MainNet = false + // Choose the active network params based on the mainnet net flag. + if cfg.MainNet { + activeNetParams = netParams(btcwire.MainNet) + } + updateConfigWithActiveParams(&cfg) + return &cfg, remainingArgs, nil } diff --git a/params.go b/params.go new file mode 100644 index 0000000..a1877d2 --- /dev/null +++ b/params.go @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2013 Conformal Systems LLC + * + * 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. + */ + +package main + +import ( + "github.com/conformal/btcwire" +) + +var activeNetParams = netParams(defaultBtcNet) + +// params is used to group parameters for various networks such as the main +// network and test networks. +type params struct { + btcdPort string + svrPort string +} + +// mainNetParams contains parameters specific running btcwallet and +// btcd on the main network (btcwire.MainNet). +var mainNetParams = params{ + btcdPort: "8334", + svrPort: "8332", +} + +// testNet3Params contains parameters specific running btcwallet and +// btcd on the test network (version 3) (btcwire.TestNet3). +var testNet3Params = params{ + btcdPort: "18334", + svrPort: "18332", +} + +// netParams returns parameters specific to the passed bitcoin network. +func netParams(btcnet btcwire.BitcoinNet) *params { + switch btcnet { + case btcwire.TestNet3: + return &testNet3Params + + // Return main net by default. + case btcwire.MainNet: + fallthrough + default: + return &mainNetParams + } +} diff --git a/sockets.go b/sockets.go index 4f925bd..016b994 100644 --- a/sockets.go +++ b/sockets.go @@ -441,7 +441,7 @@ func FrontendListenAndServe() error { // TODO(jrick): We need some sort of authentication before websocket // connections are allowed, and perhaps TLS on the server as well. http.Handle("/frontend", websocket.Handler(frontendReqsNotifications)) - return http.ListenAndServe(fmt.Sprintf(":%d", cfg.SvrPort), nil) + return http.ListenAndServe(fmt.Sprintf(":%s", cfg.SvrPort), nil) } // BtcdConnect connects to a running btcd instance over a websocket @@ -450,7 +450,7 @@ func FrontendListenAndServe() error { func BtcdConnect(reply chan error) { // btcd requires basic authorization, so we use a custom config with // the Authorization header set. - server := fmt.Sprintf("ws://localhost:%d/wallet", cfg.BtcdPort) + server := fmt.Sprintf("ws://localhost:%s/wallet", cfg.BtcdPort) login := cfg.Username + ":" + cfg.Password auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login)) config, err := websocket.NewConfig(server, "http://localhost/")