@ -1,24 +1,24 @@
package com.sparrowwallet.sparrow.preferences ;
import com.google.common.eventbus.Subscribe ;
import com.google.common.net.HostAndPort ;
import com.sparrowwallet.drongo.Network ;
import com.sparrowwallet.sparrow.AppServices ;
import com.sparrowwallet.sparrow.EventManager ;
import com.sparrowwallet.sparrow.Mode ;
import com.sparrowwallet.sparrow.control.TextFieldValidator ;
import com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch ;
import com.sparrowwallet.sparrow.event.ConnectionEvent ;
import com.sparrowwallet.sparrow.event.RequestDisconnectEvent ;
import com.sparrowwallet.sparrow.event.* ;
import com.sparrowwallet.sparrow.glyphfont.FontAwesome5 ;
import com.sparrowwallet.sparrow.io.Config ;
import com.sparrowwallet.sparrow.net.ElectrumServer ;
import com.sparrowwallet.sparrow.net.Protocol ;
import com.sparrowwallet.sparrow.net.* ;
import javafx.application.Platform ;
import javafx.beans.value.ChangeListener ;
import javafx.concurrent.WorkerStateEvent ;
import javafx.fxml.FXML ;
import javafx.scene.control.Button ;
import javafx.scene.control.Control ;
import javafx.scene.control.TextArea ;
import javafx.scene.control.TextField ;
import javafx.scene.paint.Color ;
import javafx.scene.control.* ;
import javafx.scene.text.Font ;
import javafx.stage.DirectoryChooser ;
import javafx.stage.FileChooser ;
import javafx.stage.Stage ;
import javafx.util.Duration ;
@ -30,30 +30,76 @@ import org.controlsfx.validation.decoration.StyleClassValidationDecoration;
import org.jetbrains.annotations.NotNull ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import tornadofx.control.Field ;
import tornadofx.control.Form ;
import javax.net.ssl.SSLHandshakeException ;
import java.io.File ;
import java.io.FileInputStream ;
import java.security.cert.CertificateFactory ;
import java.text.DateFormat ;
import java.text.SimpleDateFormat ;
import java.util.List ;
public class ServerPreferencesController extends PreferencesDetailController {
private static final Logger log = LoggerFactory . getLogger ( ServerPreferencesController . class ) ;
@FXML
private TextField host ;
private ToggleGroup serverTypeToggleGroup ;
@FXML
private TextField port ;
private Form coreForm ;
@FXML
private UnlabeledToggleSwitch useSsl ;
private TextField coreHost ;
@FXML
private TextField certificate ;
private TextField cor ePo rt ;
@FXML
private Button certificateSelect ;
private ToggleGroup coreAuthToggleGroup ;
@FXML
private Field coreDataDirField ;
@FXML
private TextField coreDataDir ;
@FXML
private Button coreDataDirSelect ;
@FXML
private Field coreUserPassField ;
@FXML
private TextField coreUser ;
@FXML
private PasswordField corePass ;
@FXML
private UnlabeledToggleSwitch coreMultiWallet ;
@FXML
private TextField coreWallet ;
@FXML
private Form electrumForm ;
@FXML
private TextField electrumHost ;
@FXML
private TextField electrumPort ;
@FXML
private UnlabeledToggleSwitch electrumUseSsl ;
@FXML
private TextField electrumCertificate ;
@FXML
private Button electrumCertificateSelect ;
@FXML
private UnlabeledToggleSwitch useProxy ;
@ -75,35 +121,108 @@ public class ServerPreferencesController extends PreferencesDetailController {
private final ValidationSupport validationSupport = new ValidationSupport ( ) ;
private ElectrumServer . ConnectionService connectionService ;
@Override
public void initializeView ( Config config ) {
EventManager . get ( ) . register ( this ) ;
getMasterController ( ) . closingProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
EventManager . get ( ) . unregister ( this ) ;
} ) ;
Platform . runLater ( this : : setupValidation ) ;
port . setTextFormatter ( new TextFieldValidator ( TextFieldValidator . ValidationModus . MAX_INTEGERS , 5 ) . getFormatter ( ) ) ;
coreForm . managedProperty ( ) . bind ( coreForm . visibleProperty ( ) ) ;
electrumForm . managedProperty ( ) . bind ( electrumForm . visibleProperty ( ) ) ;
coreForm . visibleProperty ( ) . bind ( electrumForm . visibleProperty ( ) . not ( ) ) ;
serverTypeToggleGroup . selectedToggleProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
if ( serverTypeToggleGroup . getSelectedToggle ( ) ! = null ) {
ServerType existingType = config . getServerType ( ) ;
ServerType serverType = ( ServerType ) newValue . getUserData ( ) ;
electrumForm . setVisible ( serverType = = ServerType . ELECTRUM_SERVER ) ;
config . setServerType ( serverType ) ;
testConnection . setGraphic ( getGlyph ( FontAwesome5 . Glyph . QUESTION_CIRCLE , "" ) ) ;
testResults . clear ( ) ;
if ( existingType ! = serverType ) {
EventManager . get ( ) . post ( new ServerTypeChangedEvent ( serverType ) ) ;
}
} else if ( oldValue ! = null ) {
oldValue . setSelected ( true ) ;
}
} ) ;
ServerType serverType = config . getServerType ( ) ! = null ? config . getServerType ( ) : ( config . getCoreServer ( ) = = null & & config . getElectrumServer ( ) ! = null ? ServerType . ELECTRUM_SERVER : ServerType . BITCOIN_CORE ) ;
serverTypeToggleGroup . selectToggle ( serverTypeToggleGroup . getToggles ( ) . stream ( ) . filter ( toggle - > toggle . getUserData ( ) = = serverType ) . findFirst ( ) . orElse ( null ) ) ;
corePort . setTextFormatter ( new TextFieldValidator ( TextFieldValidator . ValidationModus . MAX_INTEGERS , 5 ) . getFormatter ( ) ) ;
electrumPort . setTextFormatter ( new TextFieldValidator ( TextFieldValidator . ValidationModus . MAX_INTEGERS , 5 ) . getFormatter ( ) ) ;
proxyPort . setTextFormatter ( new TextFieldValidator ( TextFieldValidator . ValidationModus . MAX_INTEGERS , 5 ) . getFormatter ( ) ) ;
host . textProperty ( ) . addListener ( getElectrumServerListener ( config ) ) ;
port . textProperty ( ) . addListener ( getElectrumServerListener ( config ) ) ;
coreHost . textProperty ( ) . addListener ( getBitcoinCoreListener ( config ) ) ;
corePort . textProperty ( ) . addListener ( getBitcoinCoreListener ( config ) ) ;
coreUser . textProperty ( ) . addListener ( getBitcoinAuthListener ( config ) ) ;
corePass . textProperty ( ) . addListener ( getBitcoinAuthListener ( config ) ) ;
coreWallet . textProperty ( ) . addListener ( getBitcoinWalletListener ( config ) ) ;
electrumHost . textProperty ( ) . addListener ( getElectrumServerListener ( config ) ) ;
electrumPort . textProperty ( ) . addListener ( getElectrumServerListener ( config ) ) ;
proxyHost . textProperty ( ) . addListener ( getProxyListener ( config ) ) ;
proxyPort . textProperty ( ) . addListener ( getProxyListener ( config ) ) ;
useSsl . selectedProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
coreDataDirField . managedProperty ( ) . bind ( coreDataDirField . visibleProperty ( ) ) ;
coreUserPassField . managedProperty ( ) . bind ( coreUserPassField . visibleProperty ( ) ) ;
coreUserPassField . visibleProperty ( ) . bind ( coreDataDirField . visibleProperty ( ) . not ( ) ) ;
coreAuthToggleGroup . selectedToggleProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
if ( coreAuthToggleGroup . getSelectedToggle ( ) ! = null ) {
CoreAuthType coreAuthType = ( CoreAuthType ) newValue . getUserData ( ) ;
coreDataDirField . setVisible ( coreAuthType = = CoreAuthType . COOKIE ) ;
config . setCoreAuthType ( coreAuthType ) ;
} else if ( oldValue ! = null ) {
oldValue . setSelected ( true ) ;
}
} ) ;
CoreAuthType coreAuthType = config . getCoreAuthType ( ) ! = null ? config . getCoreAuthType ( ) : CoreAuthType . COOKIE ;
coreAuthToggleGroup . selectToggle ( coreAuthToggleGroup . getToggles ( ) . stream ( ) . filter ( toggle - > toggle . getUserData ( ) = = coreAuthType ) . findFirst ( ) . orElse ( null ) ) ;
coreDataDir . textProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
File dataDir = getDirectory ( newValue ) ;
config . setCoreDataDir ( dataDir ) ;
} ) ;
coreDataDirSelect . setOnAction ( event - > {
Stage window = new Stage ( ) ;
DirectoryChooser directorChooser = new DirectoryChooser ( ) ;
directorChooser . setTitle ( "Select Bitcoin Core Data Directory" ) ;
directorChooser . setInitialDirectory ( config . getCoreDataDir ( ) ! = null ? config . getCoreDataDir ( ) : new File ( System . getProperty ( "user.home" ) ) ) ;
File dataDir = directorChooser . showDialog ( window ) ;
if ( dataDir ! = null ) {
coreDataDir . setText ( dataDir . getAbsolutePath ( ) ) ;
}
} ) ;
coreMultiWallet . selectedProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
coreWallet . setText ( " " ) ;
coreWallet . setText ( "" ) ;
coreWallet . setDisable ( ! newValue ) ;
coreWallet . setPromptText ( newValue ? "" : "Default" ) ;
} ) ;
electrumUseSsl . selectedProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
setElectrumServerInConfig ( config ) ;
certificate . setDisable ( ! newValue ) ;
certificateSelect . setDisable ( ! newValue ) ;
ele ctrumC ertificate. setDisable ( ! newValue ) ;
ele ctrumC ertificateSelect. setDisable ( ! newValue ) ;
} ) ;
certificate . textProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
ele ctrumC ertificate. textProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
File crtFile = getCertificate ( newValue ) ;
if ( crtFile ! = null ) {
config . setElectrumServerCert ( crtFile ) ;
} else {
config . setElectrumServerCert ( null ) ;
}
config . setElectrumServerCert ( crtFile ) ;
} ) ;
certificateSelect . setOnAction ( event - > {
electrumCertificateSelect . setOnAction ( event - > {
Stage window = new Stage ( ) ;
FileChooser fileChooser = new FileChooser ( ) ;
@ -115,7 +234,7 @@ public class ServerPreferencesController extends PreferencesDetailController {
File file = fileChooser . showOpenDialog ( window ) ;
if ( file ! = null ) {
certificate . setText ( file . getAbsolutePath ( ) ) ;
ele ctrumC ertificate. setText ( file . getAbsolutePath ( ) ) ;
}
} ) ;
@ -127,45 +246,32 @@ public class ServerPreferencesController extends PreferencesDetailController {
proxyPort . setDisable ( ! newValue ) ;
if ( newValue ) {
useSsl . setSelected ( true ) ;
useSsl . setDisable ( true ) ;
electr umU seSsl. setSelected ( true ) ;
electr umU seSsl. setDisable ( true ) ;
} else {
useSsl . setDisable ( false ) ;
electr umU seSsl. setDisable ( false ) ;
}
} ) ;
boolean isConnected = ElectrumServer . isConnected ( ) ;
boolean isConnected = AppServices . isConnecting ( ) | | AppServices . isConnected ( ) ;
setFieldsEditable ( ! isConnected ) ;
if ( AppServices . isConnecting ( ) ) {
testResults . appendText ( "Connecting to server, please wait..." ) ;
}
testConnection . managedProperty ( ) . bind ( testConnection . visibleProperty ( ) ) ;
testConnection . setVisible ( ! isConnected ) ;
setTestResultsFont ( ) ;
testConnection . setOnAction ( event - > {
testResults . setText ( "Connecting to " + config . getElectrumServer ( ) + "..." ) ;
testConnection . setGraphic ( getGlyph ( FontAwesome5 . Glyph . ELLIPSIS_H , null ) ) ;
ElectrumServer . ConnectionService connectionService = new ElectrumServer . ConnectionService ( false ) ;
connectionService . setPeriod ( Duration . ZERO ) ;
EventManager . get ( ) . register ( connectionService ) ;
connectionService . statusProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
testResults . setText ( testResults . getText ( ) + "\n" + newValue ) ;
} ) ;
connectionService . setOnSucceeded ( successEvent - > {
EventManager . get ( ) . unregister ( connectionService ) ;
ConnectionEvent connectionEvent = ( ConnectionEvent ) connectionService . getValue ( ) ;
showConnectionSuccess ( connectionEvent . getServerVersion ( ) , connectionEvent . getServerBanner ( ) ) ;
connectionService . cancel ( ) ;
} ) ;
connectionService . setOnFailed ( workerStateEvent - > {
EventManager . get ( ) . unregister ( connectionService ) ;
showConnectionFailure ( workerStateEvent ) ;
connectionService . cancel ( ) ;
} ) ;
connectionService . start ( ) ;
testResults . setText ( "Connecting to " + config . getServerAddress ( ) + "..." ) ;
startElectrumConnection ( ) ;
} ) ;
editConnection . managedProperty ( ) . bind ( editConnection . visibleProperty ( ) ) ;
editConnection . setVisible ( isConnected ) ;
editConnection . setDisable ( AppServices . isConnecting ( ) ) ;
editConnection . setOnAction ( event - > {
EventManager . get ( ) . post ( new RequestDisconnectEvent ( ) ) ;
setFieldsEditable ( true ) ;
@ -173,27 +279,61 @@ public class ServerPreferencesController extends PreferencesDetailController {
testConnection . setVisible ( true ) ;
} ) ;
String coreServer = config . getCoreServer ( ) ;
if ( coreServer ! = null ) {
Protocol protocol = Protocol . getProtocol ( coreServer ) ;
if ( protocol ! = null ) {
HostAndPort server = protocol . getServerHostAndPort ( coreServer ) ;
coreHost . setText ( server . getHost ( ) ) ;
if ( server . hasPort ( ) ) {
corePort . setText ( Integer . toString ( server . getPort ( ) ) ) ;
}
}
} else {
coreHost . setText ( "127.0.0.1" ) ;
corePort . setText ( String . valueOf ( Network . get ( ) . getDefaultPort ( ) ) ) ;
}
coreDataDir . setText ( config . getCoreDataDir ( ) ! = null ? config . getCoreDataDir ( ) . getAbsolutePath ( ) : getDefaultCoreDataDir ( ) . getAbsolutePath ( ) ) ;
if ( config . getCoreAuth ( ) ! = null ) {
String [ ] userPass = config . getCoreAuth ( ) . split ( ":" ) ;
if ( userPass . length > 0 ) {
coreUser . setText ( userPass [ 0 ] ) ;
}
if ( userPass . length > 1 ) {
corePass . setText ( userPass [ 1 ] ) ;
}
}
coreMultiWallet . setSelected ( true ) ;
coreMultiWallet . setSelected ( config . getCoreWallet ( ) ! = null ) ;
if ( config . getCoreWallet ( ) ! = null ) {
coreWallet . setText ( config . getCoreWallet ( ) ) ;
}
String electrumServer = config . getElectrumServer ( ) ;
if ( electrumServer ! = null ) {
Protocol protocol = Protocol . getProtocol ( electrumServer ) ;
if ( protocol ! = null ) {
boolean ssl = protocol . equals ( Protocol . SSL ) ;
useSsl . setSelected ( ssl ) ;
certificate . setDisable ( ! ssl ) ;
certificateSelect . setDisable ( ! ssl ) ;
electr umU seSsl. setSelected ( ssl ) ;
ele ctrumC ertificate. setDisable ( ! ssl ) ;
ele ctrumC ertificateSelect. setDisable ( ! ssl ) ;
HostAndPort server = protocol . getServerHostAndPort ( electrumServer ) ;
host . setText ( server . getHost ( ) ) ;
electrumH ost. setText ( server . getHost ( ) ) ;
if ( server . hasPort ( ) ) {
port . setText ( Integer . toString ( server . getPort ( ) ) ) ;
electrumP ort. setText ( Integer . toString ( server . getPort ( ) ) ) ;
}
}
}
File certificateFile = config . getElectrumServerCert ( ) ;
if ( certificateFile ! = null ) {
certificate . setText ( certificateFile . getAbsolutePath ( ) ) ;
ele ctrumC ertificate. setText ( certificateFile . getAbsolutePath ( ) ) ;
}
useProxy . setSelected ( config . isUseProxy ( ) ) ;
@ -201,8 +341,8 @@ public class ServerPreferencesController extends PreferencesDetailController {
proxyPort . setDisable ( ! config . isUseProxy ( ) ) ;
if ( config . isUseProxy ( ) ) {
useSsl . setSelected ( true ) ;
useSsl . setDisable ( true ) ;
electr umU seSsl. setSelected ( true ) ;
electr umU seSsl. setDisable ( true ) ;
}
String proxyServer = config . getProxyServer ( ) ;
@ -215,15 +355,55 @@ public class ServerPreferencesController extends PreferencesDetailController {
}
}
private void startElectrumConnection ( ) {
if ( connectionService ! = null & & connectionService . isRunning ( ) ) {
connectionService . cancel ( ) ;
}
connectionService = new ElectrumServer . ConnectionService ( false ) ;
connectionService . setPeriod ( Duration . hours ( 1 ) ) ;
EventManager . get ( ) . register ( connectionService ) ;
connectionService . statusProperty ( ) . addListener ( ( observable , oldValue , newValue ) - > {
testResults . setText ( testResults . getText ( ) + "\n" + newValue ) ;
} ) ;
connectionService . setOnSucceeded ( successEvent - > {
EventManager . get ( ) . unregister ( connectionService ) ;
ConnectionEvent connectionEvent = ( ConnectionEvent ) connectionService . getValue ( ) ;
showConnectionSuccess ( connectionEvent . getServerVersion ( ) , connectionEvent . getServerBanner ( ) ) ;
getMasterController ( ) . reconnectOnClosingProperty ( ) . set ( true ) ;
Config . get ( ) . setMode ( Mode . ONLINE ) ;
connectionService . cancel ( ) ;
} ) ;
connectionService . setOnFailed ( workerStateEvent - > {
EventManager . get ( ) . unregister ( connectionService ) ;
showConnectionFailure ( workerStateEvent ) ;
connectionService . cancel ( ) ;
} ) ;
connectionService . start ( ) ;
}
private void setFieldsEditable ( boolean editable ) {
host . setEditable ( editable ) ;
port . setEditable ( editable ) ;
useSsl . setDisable ( ! editable ) ;
certificate . setEditable ( editable ) ;
certificateSelect . setDisable ( ! editable ) ;
serverTypeToggleGroup . getToggles ( ) . forEach ( toggle - > ( ( ToggleButton ) toggle ) . setDisable ( ! editable ) ) ;
coreHost . setDisable ( ! editable ) ;
corePort . setDisable ( ! editable ) ;
coreAuthToggleGroup . getToggles ( ) . forEach ( toggle - > ( ( ToggleButton ) toggle ) . setDisable ( ! editable ) ) ;
coreDataDir . setDisable ( ! editable ) ;
coreDataDirSelect . setDisable ( ! editable ) ;
coreUser . setDisable ( ! editable ) ;
corePass . setDisable ( ! editable ) ;
coreMultiWallet . setDisable ( ! editable ) ;
coreWallet . setDisable ( ! editable ) ;
electrumHost . setDisable ( ! editable ) ;
electrumPort . setDisable ( ! editable ) ;
electrumUseSsl . setDisable ( ! editable ) ;
electrumCertificate . setDisable ( ! editable ) ;
electrumCertificateSelect . setDisable ( ! editable ) ;
useProxy . setDisable ( ! editable ) ;
proxyHost . setEditable ( editable ) ;
proxyPort . setEditable ( editable ) ;
proxyHost . setDisable ( ! editable ) ;
proxyPort . setDisable ( ! editable ) ;
}
private void showConnectionSuccess ( List < String > serverVersion , String serverBanner ) {
@ -252,12 +432,36 @@ public class ServerPreferencesController extends PreferencesDetailController {
}
private void setupValidation ( ) {
validationSupport . registerValidator ( host , Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Invalid host name" , getHost ( newValue ) = = null )
validationSupport . registerValidator ( coreHost , Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Invalid Core host" , getHost ( newValue ) = = null )
) ) ;
validationSupport . registerValidator ( corePort , Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Invalid Core port" , ! newValue . isEmpty ( ) & & ! isValidPort ( Integer . parseInt ( newValue ) ) )
) ) ;
validationSupport . registerValidator ( coreDataDir , Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Core Data Dir required" , coreAuthToggleGroup . getSelectedToggle ( ) . getUserData ( ) = = CoreAuthType . COOKIE & & ( newValue . isEmpty ( ) | | getDirectory ( newValue ) = = null ) )
) ) ;
validationSupport . registerValidator ( port , Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Invalid port" , ! newValue . isEmpty ( ) & & ! isValidPort ( Integer . parseInt ( newValue ) ) )
validationSupport . registerValidator ( coreUser , Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Core user required" , coreAuthToggleGroup . getSelectedToggle ( ) . getUserData ( ) = = CoreAuthType . USERPASS & & newValue . isEmpty ( ) )
) ) ;
validationSupport . registerValidator ( corePass , Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Core pass required" , coreAuthToggleGroup . getSelectedToggle ( ) . getUserData ( ) = = CoreAuthType . USERPASS & & newValue . isEmpty ( ) )
) ) ;
validationSupport . registerValidator ( coreWallet , Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Core wallet required" , coreMultiWallet . isSelected ( ) & & newValue . isEmpty ( ) )
) ) ;
validationSupport . registerValidator ( electrumHost , Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Invalid Electrum host" , getHost ( newValue ) = = null )
) ) ;
validationSupport . registerValidator ( electrumPort , Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Invalid Electrum port" , ! newValue . isEmpty ( ) & & ! isValidPort ( Integer . parseInt ( newValue ) ) )
) ) ;
validationSupport . registerValidator ( proxyHost , Validator . combine (
@ -269,13 +473,44 @@ public class ServerPreferencesController extends PreferencesDetailController {
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Invalid proxy port" , ! newValue . isEmpty ( ) & & ! isValidPort ( Integer . parseInt ( newValue ) ) )
) ) ;
validationSupport . registerValidator ( certificate , Validator . combine (
validationSupport . registerValidator ( ele ctrumC ertificate, Validator . combine (
( Control c , String newValue ) - > ValidationResult . fromErrorIf ( c , "Invalid certificate file" , newValue ! = null & & ! newValue . isEmpty ( ) & & getCertificate ( newValue ) = = null )
) ) ;
validationSupport . setValidationDecorator ( new StyleClassValidationDecoration ( ) ) ;
}
@NotNull
private ChangeListener < String > getBitcoinCoreListener ( Config config ) {
return ( observable , oldValue , newValue ) - > {
setCoreServerInConfig ( config ) ;
} ;
}
private void setCoreServerInConfig ( Config config ) {
String hostAsString = getHost ( coreHost . getText ( ) ) ;
Integer portAsInteger = getPort ( corePort . getText ( ) ) ;
if ( hostAsString ! = null & & portAsInteger ! = null & & isValidPort ( portAsInteger ) ) {
config . setCoreServer ( Protocol . HTTP . toUrlString ( hostAsString , portAsInteger ) ) ;
} else if ( hostAsString ! = null ) {
config . setCoreServer ( Protocol . HTTP . toUrlString ( hostAsString ) ) ;
}
}
@NotNull
private ChangeListener < String > getBitcoinAuthListener ( Config config ) {
return ( observable , oldValue , newValue ) - > {
config . setCoreAuth ( coreUser . getText ( ) + ":" + corePass . getText ( ) ) ;
} ;
}
@NotNull
private ChangeListener < String > getBitcoinWalletListener ( Config config ) {
return ( observable , oldValue , newValue ) - > {
config . setCoreWallet ( coreWallet . getText ( ) ) ;
} ;
}
@NotNull
private ChangeListener < String > getElectrumServerListener ( Config config ) {
return ( observable , oldValue , newValue ) - > {
@ -284,8 +519,8 @@ public class ServerPreferencesController extends PreferencesDetailController {
}
private void setElectrumServerInConfig ( Config config ) {
String hostAsString = getHost ( h ost. getText ( ) ) ;
Integer portAsInteger = getPort ( p ort. getText ( ) ) ;
String hostAsString = getHost ( electrumH ost. getText ( ) ) ;
Integer portAsInteger = getPort ( electrumP ort. getText ( ) ) ;
if ( hostAsString ! = null & & portAsInteger ! = null & & isValidPort ( portAsInteger ) ) {
config . setElectrumServer ( getProtocol ( ) . toUrlString ( hostAsString , portAsInteger ) ) ;
} else if ( hostAsString ! = null ) {
@ -311,7 +546,7 @@ public class ServerPreferencesController extends PreferencesDetailController {
}
private Protocol getProtocol ( ) {
return ( useSsl . isSelected ( ) ? Protocol . SSL : Protocol . TCP ) ;
return ( electr umU seSsl. isSelected ( ) ? Protocol . SSL : Protocol . TCP ) ;
}
private String getHost ( String text ) {
@ -330,6 +565,19 @@ public class ServerPreferencesController extends PreferencesDetailController {
}
}
private File getDirectory ( String dirLocation ) {
try {
File dirFile = new File ( dirLocation ) ;
if ( ! dirFile . exists ( ) | | ! dirFile . isDirectory ( ) ) {
return null ;
}
return dirFile ;
} catch ( Exception e ) {
return null ;
}
}
private File getCertificate ( String crtFileLocation ) {
try {
File crtFile = new File ( crtFileLocation ) ;
@ -357,4 +605,48 @@ public class ServerPreferencesController extends PreferencesDetailController {
private static boolean isValidPort ( int port ) {
return port > = 0 & & port < = 65535 ;
}
private File getDefaultCoreDataDir ( ) {
org . controlsfx . tools . Platform platform = org . controlsfx . tools . Platform . getCurrent ( ) ;
if ( platform = = org . controlsfx . tools . Platform . OSX ) {
return new File ( System . getProperty ( "user.home" ) + "/Library/Application Support/Bitcoin" ) ;
} else if ( platform = = org . controlsfx . tools . Platform . WINDOWS ) {
return new File ( System . getenv ( "APPDATA" ) + "/Bitcoin" ) ;
} else {
return new File ( System . getProperty ( "user.home" ) + "/.bitcoin" ) ;
}
}
private void setTestResultsFont ( ) {
org . controlsfx . tools . Platform platform = org . controlsfx . tools . Platform . getCurrent ( ) ;
if ( platform = = org . controlsfx . tools . Platform . OSX ) {
testResults . setFont ( Font . font ( "Monaco" , 11 ) ) ;
} else if ( platform = = org . controlsfx . tools . Platform . WINDOWS ) {
testResults . setFont ( Font . font ( "Lucida Console" , 11 ) ) ;
} else {
testResults . setFont ( Font . font ( "monospace" , 11 ) ) ;
}
}
@Subscribe
public void bwtStatus ( BwtStatusEvent event ) {
if ( ! ( event instanceof BwtSyncStatusEvent ) ) {
testResults . appendText ( "\n" + event . getStatus ( ) ) ;
}
if ( event instanceof BwtReadyStatusEvent ) {
editConnection . setDisable ( false ) ;
}
}
@Subscribe
public void bwtSyncStatus ( BwtSyncStatusEvent event ) {
editConnection . setDisable ( false ) ;
if ( connectionService ! = null & & connectionService . isRunning ( ) & & event . getProgress ( ) < 100 ) {
DateFormat dateFormat = new SimpleDateFormat ( "yyyy/MM/dd HH:mm" ) ;
testResults . appendText ( "\nThe connection to the Bitcoin Core node was successful, but it is still syncing and cannot be used yet." ) ;
testResults . appendText ( "\nCurrently " + event . getProgress ( ) + "% completed to date " + dateFormat . format ( event . getTip ( ) ) ) ;
testConnection . setGraphic ( getGlyph ( FontAwesome5 . Glyph . QUESTION_CIRCLE , null ) ) ;
connectionService . cancel ( ) ;
}
}
}