From b5301c455636d17801cb4d77adf9218faca9c3cc Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 3 Nov 2021 12:27:45 +0200 Subject: [PATCH] add public testnet electrum server --- .../com/sparrowwallet/sparrow/MainApp.java | 3 +- .../com/sparrowwallet/sparrow/io/Config.java | 2 +- .../sparrow/net/PublicElectrumServer.java | 37 +++++++++++++++---- .../ServerPreferencesController.java | 11 ++++-- .../sparrow/preferences/server.fxml | 12 +----- 5 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/MainApp.java b/src/main/java/com/sparrowwallet/sparrow/MainApp.java index 45ce9e6e..26ff588e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/MainApp.java +++ b/src/main/java/com/sparrowwallet/sparrow/MainApp.java @@ -77,7 +77,8 @@ public class MainApp extends Application { createNewWallet = optNewWallet.isPresent() && optNewWallet.get(); } else if(Network.get() == Network.MAINNET) { Config.get().setServerType(ServerType.PUBLIC_ELECTRUM_SERVER); - Config.get().setPublicElectrumServer(PublicElectrumServer.values()[new Random().nextInt(PublicElectrumServer.values().length)].getUrl()); + List servers = PublicElectrumServer.getServers(); + Config.get().setPublicElectrumServer(servers.get(new Random().nextInt(servers.size())).getUrl()); } } } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Config.java b/src/main/java/com/sparrowwallet/sparrow/io/Config.java index b27afe17..fbd2e695 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Config.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Config.java @@ -384,7 +384,7 @@ public class Config { } public void changePublicServer() { - List otherServers = Arrays.stream(PublicElectrumServer.values()).map(PublicElectrumServer::getUrl).filter(url -> !url.equals(getPublicElectrumServer())).collect(Collectors.toList()); + List otherServers = PublicElectrumServer.getServers().stream().map(PublicElectrumServer::getUrl).filter(url -> !url.equals(getPublicElectrumServer())).collect(Collectors.toList()); setPublicElectrumServer(otherServers.get(new Random().nextInt(otherServers.size()))); } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/PublicElectrumServer.java b/src/main/java/com/sparrowwallet/sparrow/net/PublicElectrumServer.java index 3490beb8..d8768b9c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/PublicElectrumServer.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/PublicElectrumServer.java @@ -1,19 +1,30 @@ package com.sparrowwallet.sparrow.net; -public enum PublicElectrumServer { - BLOCKSTREAM_INFO("blockstream.info", "ssl://blockstream.info:700"), - ELECTRUM_BLOCKSTREAM_INFO("electrum.blockstream.info", "ssl://electrum.blockstream.info:50002"), - LUKECHILDS_CO("bitcoin.lukechilds.co", "ssl://bitcoin.lukechilds.co:50002"), - EMZY_DE("electrum.emzy.de", "ssl://electrum.emzy.de:50002"), - BITAROO_NET("electrum.bitaroo.net", "ssl://electrum.bitaroo.net:50002"); +import com.sparrowwallet.drongo.Network; - PublicElectrumServer(String name, String url) { +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public enum PublicElectrumServer { + BLOCKSTREAM_INFO("blockstream.info", "ssl://blockstream.info:700", Network.MAINNET), + ELECTRUM_BLOCKSTREAM_INFO("electrum.blockstream.info", "ssl://electrum.blockstream.info:50002", Network.MAINNET), + LUKECHILDS_CO("bitcoin.lukechilds.co", "ssl://bitcoin.lukechilds.co:50002", Network.MAINNET), + EMZY_DE("electrum.emzy.de", "ssl://electrum.emzy.de:50002", Network.MAINNET), + BITAROO_NET("electrum.bitaroo.net", "ssl://electrum.bitaroo.net:50002", Network.MAINNET), + TESTNET_ARANGUREN_ORG("testnet.aranguren.org", "ssl://testnet.aranguren.org:51002", Network.TESTNET); + + PublicElectrumServer(String name, String url, Network network) { this.name = name; this.url = url; + this.network = network; } + public static final List SUPPORTED_NETWORKS = List.of(Network.MAINNET, Network.TESTNET); + private final String name; private final String url; + private final Network network; public String getName() { return name; @@ -23,6 +34,18 @@ public enum PublicElectrumServer { return url; } + public Network getNetwork() { + return network; + } + + public static List getServers() { + return Arrays.stream(values()).filter(server -> server.network == Network.get()).collect(Collectors.toList()); + } + + public static boolean supportedNetwork() { + return SUPPORTED_NETWORKS.contains(Network.get()); + } + public static PublicElectrumServer fromUrl(String url) { for(PublicElectrumServer server : values()) { if(server.url.equals(url)) { diff --git a/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java b/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java index 643d5c74..0b3a6385 100644 --- a/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java @@ -15,6 +15,7 @@ import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.net.*; import javafx.application.Platform; import javafx.beans.value.ChangeListener; +import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.text.Font; @@ -186,15 +187,16 @@ public class ServerPreferencesController extends PreferencesDetailController { } }); ServerType serverType = config.getServerType() != null ? - (config.getServerType() == ServerType.PUBLIC_ELECTRUM_SERVER && Network.get() != Network.MAINNET ? ServerType.BITCOIN_CORE : config.getServerType()) : + (config.getServerType() == ServerType.PUBLIC_ELECTRUM_SERVER && !PublicElectrumServer.supportedNetwork() ? ServerType.BITCOIN_CORE : config.getServerType()) : (config.getCoreServer() == null && config.getElectrumServer() != null ? ServerType.ELECTRUM_SERVER : - (config.getCoreServer() != null || Network.get() != Network.MAINNET ? ServerType.BITCOIN_CORE : ServerType.PUBLIC_ELECTRUM_SERVER)); - if(Network.get() != Network.MAINNET) { + (config.getCoreServer() != null || !PublicElectrumServer.supportedNetwork() ? ServerType.BITCOIN_CORE : ServerType.PUBLIC_ELECTRUM_SERVER)); + if(!PublicElectrumServer.supportedNetwork()) { serverTypeSegmentedButton.getButtons().remove(publicElectrumToggle); serverTypeToggleGroup.getToggles().remove(publicElectrumToggle); } serverTypeToggleGroup.selectToggle(serverTypeToggleGroup.getToggles().stream().filter(toggle -> toggle.getUserData() == serverType).findFirst().orElse(null)); + publicElectrumServer.setItems(FXCollections.observableList(PublicElectrumServer.getServers())); publicElectrumServer.getSelectionModel().selectedItemProperty().addListener(getPublicElectrumServerListener(config)); publicUseProxy.selectedProperty().bindBidirectional(useProxy.selectedProperty()); @@ -326,7 +328,8 @@ public class ServerPreferencesController extends PreferencesDetailController { PublicElectrumServer configPublicElectrumServer = PublicElectrumServer.fromUrl(config.getPublicElectrumServer()); if(configPublicElectrumServer == null) { - publicElectrumServer.setValue(PublicElectrumServer.values()[new Random().nextInt(PublicElectrumServer.values().length)]); + List servers = PublicElectrumServer.getServers(); + publicElectrumServer.setValue(servers.get(new Random().nextInt(servers.size()))); } else { publicElectrumServer.setValue(configPublicElectrumServer); } diff --git a/src/main/resources/com/sparrowwallet/sparrow/preferences/server.fxml b/src/main/resources/com/sparrowwallet/sparrow/preferences/server.fxml index 86c3e975..ff4e2305 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/preferences/server.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/preferences/server.fxml @@ -85,17 +85,7 @@ - - - - - - - - - - - +