diff --git a/src/main/java/io/bitsquare/app/BitsquareEnvironment.java b/src/main/java/io/bitsquare/app/BitsquareEnvironment.java index 7ddf15c5a1..936e1dfc88 100644 --- a/src/main/java/io/bitsquare/app/BitsquareEnvironment.java +++ b/src/main/java/io/bitsquare/app/BitsquareEnvironment.java @@ -22,13 +22,13 @@ import io.bitsquare.btc.UserAgent; import io.bitsquare.btc.WalletService; import io.bitsquare.gui.ViewCB; import io.bitsquare.persistence.Persistence; +import io.bitsquare.util.spring.JOptCommandLinePropertySource; import java.nio.file.Paths; import java.util.Properties; import joptsimple.OptionSet; -import org.springframework.core.env.JOptCommandLinePropertySource; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; @@ -64,7 +64,7 @@ public class BitsquareEnvironment extends StandardEnvironment { private final String appDataDir; public BitsquareEnvironment(OptionSet options) { - this(new JOptCommandLinePropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME, checkNotNull(options))); + this(new JOptCommandLinePropertySource(BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME, checkNotNull(options))); } BitsquareEnvironment(PropertySource commandLineProperties) { diff --git a/src/main/java/io/bitsquare/app/cli/BootstrapNodeMain.java b/src/main/java/io/bitsquare/app/cli/BootstrapNodeMain.java index b45849e4f6..c3ff1d1d7e 100644 --- a/src/main/java/io/bitsquare/app/cli/BootstrapNodeMain.java +++ b/src/main/java/io/bitsquare/app/cli/BootstrapNodeMain.java @@ -32,8 +32,8 @@ public class BootstrapNodeMain extends BitsquareExecutable { protected void customizeOptionParsing(OptionParser parser) { parser.accepts(Node.NAME_KEY, "Name of this node").withRequiredArg().isRequired(); - parser.accepts(Node.PORT_KEY, "Port to listen on").withRequiredArg() - .defaultsTo(String.valueOf(Node.DEFAULT_PORT)); + parser.accepts(Node.PORT_KEY, "Port to listen on").withRequiredArg().ofType(int.class) + .defaultsTo(Node.DEFAULT_PORT); } protected void doExecute(OptionSet options) { diff --git a/src/main/java/io/bitsquare/app/gui/BitsquareAppMain.java b/src/main/java/io/bitsquare/app/gui/BitsquareAppMain.java index 0681871f1f..e69680e0c8 100644 --- a/src/main/java/io/bitsquare/app/gui/BitsquareAppMain.java +++ b/src/main/java/io/bitsquare/app/gui/BitsquareAppMain.java @@ -19,15 +19,15 @@ package io.bitsquare.app.gui; import io.bitsquare.app.BitsquareEnvironment; import io.bitsquare.app.BitsquareExecutable; -import io.bitsquare.btc.BitcoinModule; +import io.bitsquare.btc.BitcoinNetwork; import io.bitsquare.network.BootstrapNodes; import io.bitsquare.network.Node; +import io.bitsquare.util.joptsimple.EnumValueConverter; import joptsimple.OptionParser; import joptsimple.OptionSet; import static io.bitsquare.app.BitsquareEnvironment.*; -import static io.bitsquare.btc.BitcoinModule.BITCOIN_NETWORK_KEY; import static io.bitsquare.msg.tomp2p.TomP2PMessageModule.*; import static io.bitsquare.network.Node.*; @@ -44,11 +44,14 @@ public class BitsquareAppMain extends BitsquareExecutable { parser.accepts(APP_DATA_DIR_KEY, "Application data directory").withRequiredArg() .defaultsTo(DEFAULT_APP_DATA_DIR); parser.accepts(NAME_KEY, "Name of this node").withRequiredArg(); - parser.accepts(PORT_KEY, "Port to listen on").withRequiredArg().defaultsTo(String.valueOf(Node.DEFAULT_PORT)); - parser.accepts(BITCOIN_NETWORK_KEY).withRequiredArg().defaultsTo(BitcoinModule.DEFAULT_BITCOIN_NETWORK); + parser.accepts(PORT_KEY, "Port to listen on").withRequiredArg().ofType(int.class).defaultsTo(Node.DEFAULT_PORT); + parser.accepts(BitcoinNetwork.KEY).withRequiredArg().ofType(BitcoinNetwork.class) + .withValuesConvertedBy(new EnumValueConverter(BitcoinNetwork.class)) + .defaultsTo(BitcoinNetwork.DEFAULT); parser.accepts(BOOTSTRAP_NODE_NAME_KEY).withRequiredArg().defaultsTo(BootstrapNodes.DEFAULT.getName()); parser.accepts(BOOTSTRAP_NODE_IP_KEY).withRequiredArg().defaultsTo(BootstrapNodes.DEFAULT.getIp()); - parser.accepts(BOOTSTRAP_NODE_PORT_KEY).withRequiredArg().defaultsTo(BootstrapNodes.DEFAULT.getPortAsString()); + parser.accepts(BOOTSTRAP_NODE_PORT_KEY).withRequiredArg().ofType(int.class) + .defaultsTo(BootstrapNodes.DEFAULT.getPort()); parser.accepts(NETWORK_INTERFACE_KEY, "Network interface").withRequiredArg(); } diff --git a/src/main/java/io/bitsquare/btc/BitcoinModule.java b/src/main/java/io/bitsquare/btc/BitcoinModule.java index 2105829d3a..48eae71bfb 100644 --- a/src/main/java/io/bitsquare/btc/BitcoinModule.java +++ b/src/main/java/io/bitsquare/btc/BitcoinModule.java @@ -34,8 +34,6 @@ import static com.google.inject.name.Names.named; public class BitcoinModule extends BitsquareModule { - public static final String BITCOIN_NETWORK_KEY = "bitcoin.network"; - public static final String DEFAULT_BITCOIN_NETWORK = BitcoinNetwork.TESTNET.toString(); public BitcoinModule(Environment env) { super(env); @@ -43,7 +41,8 @@ public class BitcoinModule extends BitsquareModule { @Override protected void configure() { - bind(NetworkParameters.class).toInstance(network()); + bind(BitcoinNetwork.class).toInstance( + env.getProperty(BitcoinNetwork.KEY, BitcoinNetwork.class, BitcoinNetwork.DEFAULT)); bind(FeePolicy.class).asEagerSingleton(); bindConstant().annotatedWith(named(UserAgent.NAME_KEY)).to(env.getRequiredProperty(UserAgent.NAME_KEY)); @@ -63,21 +62,5 @@ public class BitcoinModule extends BitsquareModule { protected void doClose(Injector injector) { injector.getInstance(WalletService.class).shutDown(); } - - private NetworkParameters network() { - BitcoinNetwork network = BitcoinNetwork.valueOf( - env.getProperty(BITCOIN_NETWORK_KEY, DEFAULT_BITCOIN_NETWORK).toUpperCase()); - - switch (network) { - case MAINNET: - return MainNetParams.get(); - case TESTNET: - return TestNet3Params.get(); - case REGTEST: - return RegTestParams.get(); - default: - throw new IllegalArgumentException("Unknown bitcoin network: " + network); - } - } } diff --git a/src/main/java/io/bitsquare/btc/BitcoinNetwork.java b/src/main/java/io/bitsquare/btc/BitcoinNetwork.java index d0a1fa2007..152d01db6e 100644 --- a/src/main/java/io/bitsquare/btc/BitcoinNetwork.java +++ b/src/main/java/io/bitsquare/btc/BitcoinNetwork.java @@ -17,6 +17,27 @@ package io.bitsquare.btc; +import org.bitcoinj.core.NetworkParameters; +import org.bitcoinj.params.MainNetParams; +import org.bitcoinj.params.RegTestParams; +import org.bitcoinj.params.TestNet3Params; + public enum BitcoinNetwork { - MAINNET, TESTNET, REGTEST; + + MAINNET(MainNetParams.get()), + TESTNET(TestNet3Params.get()), + REGTEST(RegTestParams.get()); + + public static final String KEY = "bitcoin.network"; + public static final BitcoinNetwork DEFAULT = TESTNET; + + private final NetworkParameters parameters; + + BitcoinNetwork(NetworkParameters parameters) { + this.parameters = parameters; + } + + public NetworkParameters getParameters() { + return parameters; + } } diff --git a/src/main/java/io/bitsquare/btc/FeePolicy.java b/src/main/java/io/bitsquare/btc/FeePolicy.java index 1e3d2b92f1..2d96c19b75 100644 --- a/src/main/java/io/bitsquare/btc/FeePolicy.java +++ b/src/main/java/io/bitsquare/btc/FeePolicy.java @@ -17,14 +17,12 @@ package io.bitsquare.btc; +import io.bitsquare.BitsquareException; + import org.bitcoinj.core.Address; import org.bitcoinj.core.AddressFormatException; import org.bitcoinj.core.Coin; -import org.bitcoinj.core.NetworkParameters; import org.bitcoinj.core.Transaction; -import org.bitcoinj.params.MainNetParams; -import org.bitcoinj.params.RegTestParams; -import org.bitcoinj.params.TestNet3Params; import javax.inject.Inject; @@ -32,77 +30,60 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class FeePolicy { + public static final Coin TX_FEE = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE; - // The min. REGISTRATION_FEE calculated with Transaction.MIN_NONDUST_OUTPUT would be 0.00015460 which might lead - // to problems for the spending wallet. + // The min. REGISTRATION_FEE calculated with Transaction.MIN_NONDUST_OUTPUT would be + // 0.00015460 which might lead to problems for the spending wallet. // Some web wallets don't allow more then 4 decimal places (need more investigation) // So we use 0.0002 as that fits also to our 4 decimal places restriction for BTC values. // The remaining 0.0000454 BTC is given to miners at the moment as it is lower then dust. public static final Coin REGISTRATION_FEE = TX_FEE.add(TX_FEE); - public static final Coin CREATE_OFFER_FEE = REGISTRATION_FEE; // 0.0002 public static final Coin TAKE_OFFER_FEE = CREATE_OFFER_FEE; - private static final Logger log = LoggerFactory.getLogger(FeePolicy.class); - // those are just dummy yet. trading fees will go probably to arbiters - // Not used at the moment - // private static final String registrationFeeAddress = "mvkDXt4QmN4Nq9dRUsRigBCaovde9nLkZR"; - - // - private static String createOfferFeeAddress; - private static String takeOfferFeeAddress; - - private final NetworkParameters params; + private final BitcoinNetwork bitcoinNetwork; + private final String createOfferFeeAddress; + private final String takeOfferFeeAddress; @Inject - public FeePolicy(NetworkParameters params) { - this.params = params; + public FeePolicy(BitcoinNetwork bitcoinNetwork) { + this.bitcoinNetwork = bitcoinNetwork; - if (params.equals(TestNet3Params.get())) { - createOfferFeeAddress = "mmm8BdTcHoc5wi75RmiQYsJ2Tr1NoZmM84"; - takeOfferFeeAddress = "mmm8BdTcHoc5wi75RmiQYsJ2Tr1NoZmM84"; - } - else if (params.equals(MainNetParams.get())) { - // bitsquare donation address used for the moment... - createOfferFeeAddress = "1BVxNn3T12veSK6DgqwU4Hdn7QHcDDRag7"; - takeOfferFeeAddress = "1BVxNn3T12veSK6DgqwU4Hdn7QHcDDRag7"; - } - else if (params.equals(RegTestParams.get())) { - createOfferFeeAddress = "n2upbsaKAe4PD3cc4JfS7UCqPC5oNd7Ckg"; - takeOfferFeeAddress = "n2upbsaKAe4PD3cc4JfS7UCqPC5oNd7Ckg"; + switch (bitcoinNetwork) { + case TESTNET: + createOfferFeeAddress = "mmm8BdTcHoc5wi75RmiQYsJ2Tr1NoZmM84"; + takeOfferFeeAddress = "mmm8BdTcHoc5wi75RmiQYsJ2Tr1NoZmM84"; + break; + case MAINNET: + // bitsquare donation address used for the moment... + createOfferFeeAddress = "1BVxNn3T12veSK6DgqwU4Hdn7QHcDDRag7"; + takeOfferFeeAddress = "1BVxNn3T12veSK6DgqwU4Hdn7QHcDDRag7"; + break; + case REGTEST: + createOfferFeeAddress = "n2upbsaKAe4PD3cc4JfS7UCqPC5oNd7Ckg"; + takeOfferFeeAddress = "n2upbsaKAe4PD3cc4JfS7UCqPC5oNd7Ckg"; + break; + default: + throw new BitsquareException("Unknown bitcoin network: %s", bitcoinNetwork); } } - //TODO who is receiver? other users or dev address? use donation option list? - // Not used at the moment - // (dev, other users, wikileaks, tor, sub projects (bitcoinj, tomp2p,...)...) - /* public Address getAddressForRegistrationFee() { - try { - return new Address(params, registrationFeeAddress); - } catch (AddressFormatException e) { - e.printStackTrace(); - return null; - } - }*/ - //TODO get address form arbitrator list public Address getAddressForCreateOfferFee() { try { - return new Address(params, createOfferFeeAddress); - } catch (AddressFormatException e) { - e.printStackTrace(); - return null; + return new Address(bitcoinNetwork.getParameters(), createOfferFeeAddress); + } catch (AddressFormatException ex) { + throw new BitsquareException(ex); } } //TODO get address form the intersection of both traders arbitrator lists public Address getAddressForTakeOfferFee() { try { - return new Address(params, takeOfferFeeAddress); - } catch (AddressFormatException e) { - e.printStackTrace(); - return null; + return new Address(bitcoinNetwork.getParameters(), takeOfferFeeAddress); + } catch (AddressFormatException ex) { + throw new BitsquareException(ex); } } } diff --git a/src/main/java/io/bitsquare/btc/WalletService.java b/src/main/java/io/bitsquare/btc/WalletService.java index acaa1410b1..fcfb24a8db 100644 --- a/src/main/java/io/bitsquare/btc/WalletService.java +++ b/src/main/java/io/bitsquare/btc/WalletService.java @@ -123,10 +123,10 @@ public class WalletService { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public WalletService(NetworkParameters params, FeePolicy feePolicy, SignatureService signatureService, + public WalletService(BitcoinNetwork bitcoinNetwork, FeePolicy feePolicy, SignatureService signatureService, Persistence persistence, UserAgent userAgent, @Named(DIR_KEY) File walletDir, @Named(PREFIX_KEY) String walletPrefix) { - this.params = params; + this.params = bitcoinNetwork.getParameters(); this.feePolicy = feePolicy; this.signatureService = signatureService; this.persistence = persistence; diff --git a/src/main/java/io/bitsquare/gui/main/preferences/PreferencesViewCB.java b/src/main/java/io/bitsquare/gui/main/preferences/PreferencesViewCB.java index c16d48e61b..86316b84b7 100644 --- a/src/main/java/io/bitsquare/gui/main/preferences/PreferencesViewCB.java +++ b/src/main/java/io/bitsquare/gui/main/preferences/PreferencesViewCB.java @@ -141,7 +141,8 @@ public class PreferencesViewCB extends CachedViewCB { tab.setContent(view); ((TabPane) root).getSelectionModel().select(tab); Initializable childController = loader.getController(); - ((ViewCB) childController).setParent(this); + if (childController instanceof ViewCB) + ((ViewCB) childController).setParent(this); return childController; } diff --git a/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesPM.java b/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesPM.java deleted file mode 100644 index e0625e61ef..0000000000 --- a/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesPM.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * This file is part of Bitsquare. - * - * Bitsquare is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bitsquare is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bitsquare. If not, see . - */ - -package io.bitsquare.gui.main.preferences.network; - -import io.bitsquare.BitsquareException; -import io.bitsquare.gui.PresentationModel; -import io.bitsquare.msg.tomp2p.BootstrappedPeerFactory; -import io.bitsquare.msg.tomp2p.TomP2PNode; -import io.bitsquare.network.BootstrapState; -import io.bitsquare.network.Node; - -import org.bitcoinj.core.NetworkParameters; - -import com.google.inject.Inject; -import com.google.inject.name.Named; - -import net.tomp2p.peers.PeerSocketAddress; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class NetworkPreferencesPM extends PresentationModel { - private static final Logger log = LoggerFactory.getLogger(NetworkPreferencesPM.class); - - final String bitcoinNetworkType; - final String p2pNetworkConnection; - final String p2pNetworkAddress; - final String bootstrapAddress; - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Constructor - /////////////////////////////////////////////////////////////////////////////////////////// - - @Inject - NetworkPreferencesPM(NetworkParameters networkParameters, - BootstrappedPeerFactory bootstrappedPeerFactory, - TomP2PNode tomP2PNode, - @Named(BootstrappedPeerFactory.BOOTSTRAP_NODE_KEY) Node bootstrapNode) { - - switch (networkParameters.getId()) { - case NetworkParameters.ID_REGTEST: - bitcoinNetworkType = "Regtest"; - break; - case NetworkParameters.ID_TESTNET: - bitcoinNetworkType = "Testnet"; - break; - case NetworkParameters.ID_MAINNET: - bitcoinNetworkType = "Mainnet"; - break; - default: - bitcoinNetworkType = "Undefined"; - throw new BitsquareException("Invalid networkParameters " + networkParameters.getId()); - } - - PeerSocketAddress socketAddress = tomP2PNode.getPeerDHT().peerAddress().peerSocketAddress(); - p2pNetworkAddress = "IP: " + socketAddress.inetAddress().getHostAddress() - + ", TCP port: " + socketAddress.tcpPort() - + ", UDP port: " + socketAddress.udpPort(); - - bootstrapAddress = "ID: " + bootstrapNode.getName() - + ", IP: " + bootstrapNode.getIp() - + ", Port: " + bootstrapNode.getPortAsString(); - - BootstrapState state = bootstrappedPeerFactory.bootstrapState.get(); - if (state == BootstrapState.DIRECT_SUCCESS) - p2pNetworkConnection = "Direct connection"; - else if (state == BootstrapState.NAT_SUCCESS) - p2pNetworkConnection = "Connected with automatic port forwarding"; - else if (state == BootstrapState.RELAY_SUCCESS) - p2pNetworkConnection = "Relayed by other peers"; - else - throw new BitsquareException("Invalid BootstrapState " + state); - } - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Lifecycle - /////////////////////////////////////////////////////////////////////////////////////////// - - @SuppressWarnings("EmptyMethod") - @Override - public void initialize() { - super.initialize(); - } - - @SuppressWarnings("EmptyMethod") - @Override - public void activate() { - super.activate(); - } - - @SuppressWarnings("EmptyMethod") - @Override - public void deactivate() { - super.deactivate(); - } - - @SuppressWarnings("EmptyMethod") - @Override - public void terminate() { - super.terminate(); - } - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Methods - /////////////////////////////////////////////////////////////////////////////////////////// - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Getters - /////////////////////////////////////////////////////////////////////////////////////////// - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Private - /////////////////////////////////////////////////////////////////////////////////////////// - - -} diff --git a/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesView.fxml b/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesView.fxml index 2f8dd60c8b..e9a326e0dd 100644 --- a/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesView.fxml +++ b/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesView.fxml @@ -38,7 +38,7 @@ - @@ -46,11 +46,11 @@