diff --git a/backend/electrum/blockchain.go b/backend/electrum/blockchain.go index 6795adc..f313dcc 100644 --- a/backend/electrum/blockchain.go +++ b/backend/electrum/blockchain.go @@ -139,6 +139,7 @@ func NewNode(addr, port string, network utils.Network) (*Node, error) { } if port[0] == 't' { + // TCP var p string if len(port) == 1 { p = defaultTCP @@ -147,6 +148,7 @@ func NewNode(addr, port string, network utils.Network) (*Node, error) { } t, err = NewTCPTransport(fmt.Sprintf("%s:%s", a, p)) } else if port[0] == 's' { + // TLS var p string if len(port) == 1 { p = defaultSSL @@ -154,6 +156,8 @@ func NewNode(addr, port string, network utils.Network) (*Node, error) { p = port[1:] } t, err = NewSSLTransport(fmt.Sprintf("%s:%s", a, p)) + } else { + panic(fmt.Sprintf("port (%s) must start with t or s", port)) } if err != nil { diff --git a/main.go b/main.go index 89548d5..3e798d3 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ import ( "math" "net" "os" - "strconv" "strings" "time" @@ -37,7 +36,7 @@ var ( findBlock = app.Command("find-block", "Finds the block height for a given date/time.") findBlockTimestamp = findBlock.Arg("timestamp", "Date/time to resolve. E.g. \"2006-01-02 15:04:05 MST\"").Required().String() findBlockBackend = findBlock.Flag("backend", "electrum | btcd | electrum-recorder | btcd-recorder | fixture").Default("electrum").Enum("electrum", "btcd", "electrum-recorder", "btcd-recorder", "fixture") - findBlockAddr = findBlock.Flag("addr", "Backend to connect to initially. Defaults to a hardcoded node for Electrum and localhost for Btcd.").PlaceHolder("HOST:PORT").TCP() + findBlockAddr = findBlock.Flag("addr", "Backend to connect to initially. Defaults to a hardcoded node for Electrum and localhost for Btcd.").PlaceHolder("HOST:PORT").String() findBlockRpcUser = findBlock.Flag("rpcuser", "RPC username").PlaceHolder("USER").String() findBlockRpcPass = findBlock.Flag("rpcpass", "RPC password").PlaceHolder("PASSWORD").String() findBlockFixtureFile = findBlock.Flag("fixture-file", "Fixture file to use for recording or replaying data.").PlaceHolder("FILEPATH").String() @@ -48,7 +47,7 @@ var ( computeBalanceM = computeBalance.Flag("m", "number of signatures (quorum)").Short('m').Default("1").Int() computeBalanceN = computeBalance.Flag("n", "number of public keys").Short('n').Default("1").Int() computeBalanceBackend = computeBalance.Flag("backend", "electrum | btcd | electrum-recorder | btcd-recorder | fixture").Default("electrum").Enum("electrum", "btcd", "electrum-recorder", "btcd-recorder", "fixture") - computeBalanceAddr = computeBalance.Flag("addr", "Backend to connect to initially. Defaults to a hardcoded node for Electrum and localhost for Btcd.").PlaceHolder("HOST:PORT").TCP() + computeBalanceAddr = computeBalance.Flag("addr", "Backend to connect to initially. Defaults to a hardcoded node for Electrum and localhost for Btcd.").PlaceHolder("HOST:PORT").String() computeBalanceRpcUser = computeBalance.Flag("rpcuser", "RPC username").PlaceHolder("USER").String() computeBalanceRpcPass = computeBalance.Flag("rpcpass", "RPC password").PlaceHolder("PASSWORD").String() computeBalanceFixtureFile = computeBalance.Flag("fixture-file", "Fixture file to use for recording or replaying data.").PlaceHolder("FILEPATH").String() @@ -303,13 +302,14 @@ func computeBalanceBuildBackend(network Network) (backend.Backend, error) { var err error switch *computeBalanceBackend { case "electrum": - addr, port := getServer(network, *computeBalanceAddr) + addr, port := getServer(network, "electrum", *computeBalanceAddr) b, err = backend.NewElectrumBackend(addr, port, network) if err != nil { return nil, err } case "btcd": - b, err = backend.NewBtcdBackend((*computeBalanceAddr).String(), *computeBalanceRpcUser, + addr, port := getServer(network, "btcd", *computeBalanceAddr) + b, err = backend.NewBtcdBackend(fmt.Sprintf("%s:%s", addr, port), *computeBalanceRpcUser, *computeBalanceRpcPass, network) if err != nil { return nil, err @@ -318,7 +318,7 @@ func computeBalanceBuildBackend(network Network) (backend.Backend, error) { if *computeBalanceFixtureFile == "" { panic("electrum-recorder backend requires output --fixture-file.") } - addr, port := getServer(network, *computeBalanceAddr) + addr, port := getServer(network, "electrum", *computeBalanceAddr) b, err = backend.NewElectrumBackend(addr, port, network) if err != nil { return nil, err @@ -328,7 +328,8 @@ func computeBalanceBuildBackend(network Network) (backend.Backend, error) { if *computeBalanceFixtureFile == "" { panic("btcd-recorder backend requires output --fixture-file.") } - b, err = backend.NewBtcdBackend((*computeBalanceAddr).String(), *computeBalanceRpcUser, + addr, port := getServer(network, "btcd", *computeBalanceAddr) + b, err = backend.NewBtcdBackend(fmt.Sprintf("%s:%s", addr, port), *computeBalanceRpcUser, *computeBalanceRpcPass, network) if err != nil { return nil, err @@ -348,17 +349,32 @@ func computeBalanceBuildBackend(network Network) (backend.Backend, error) { return b, err } -// pick a default server for each network if none provided -// TODO: default server should be localhost for Btcd backend. -func getServer(network Network, addr *net.TCPAddr) (string, string) { - if addr != nil { - return addr.IP.String(), strconv.Itoa(addr.Port) +// pick a default server for electrum or localhost for btcd +func getServer(network Network, backend, addr string) (string, string) { + if addr != "" { + host, port, err := net.SplitHostPort(addr) + PanicOnError(err) + return host, port } - switch network { - case "mainnet": - return "electrum.petrkr.net", "s50002" - case "testnet": - return "electrum_testnet_unlimited.criptolayer.net", "s50102" + switch backend { + case "electrum": + switch network { + case "mainnet": + return "electrum.petrkr.net", "s50002" + case "testnet": + return "electrum_testnet_unlimited.criptolayer.net", "s50102" + default: + panic("unreachable") + } + case "btcd": + switch network { + case "mainnet": + return "localhost", "8334" + case "testnet": + return "localhost", "18334" + default: + panic("unreachable") + } default: panic("unreachable") }