Merge branch 'bwt'

This commit is contained in:
Craig Raw 2021-01-13 09:12:01 +02:00
commit a2ead56593
66 changed files with 2035 additions and 286 deletions

View file

@ -69,6 +69,7 @@ dependencies {
exclude group: 'org.openjfx', module: 'javafx-web' exclude group: 'org.openjfx', module: 'javafx-web'
exclude group: 'org.openjfx', module: 'javafx-media' exclude group: 'org.openjfx', module: 'javafx-media'
} }
implementation('dev.bwt:bwt-jni:0.1.6')
testImplementation('junit:junit:4.12') testImplementation('junit:junit:4.12')
} }

View file

@ -22,4 +22,8 @@ public class AboutController {
public void close(ActionEvent event) { public void close(ActionEvent event) {
stage.close(); stage.close();
} }
public void openDonate(ActionEvent event) {
AppServices.get().getApplication().getHostServices().showDocument("https://sparrowwallet.com/donate");
}
} }

View file

@ -23,6 +23,7 @@ import com.sparrowwallet.sparrow.control.*;
import com.sparrowwallet.sparrow.event.*; import com.sparrowwallet.sparrow.event.*;
import com.sparrowwallet.sparrow.io.*; import com.sparrowwallet.sparrow.io.*;
import com.sparrowwallet.sparrow.net.ElectrumServer; import com.sparrowwallet.sparrow.net.ElectrumServer;
import com.sparrowwallet.sparrow.net.ServerType;
import com.sparrowwallet.sparrow.preferences.PreferencesDialog; import com.sparrowwallet.sparrow.preferences.PreferencesDialog;
import com.sparrowwallet.sparrow.transaction.TransactionController; import com.sparrowwallet.sparrow.transaction.TransactionController;
import com.sparrowwallet.sparrow.transaction.TransactionData; import com.sparrowwallet.sparrow.transaction.TransactionData;
@ -110,6 +111,8 @@ public class AppController implements Initializable {
@FXML @FXML
private UnlabeledToggleSwitch serverToggle; private UnlabeledToggleSwitch serverToggle;
private PauseTransition wait;
private Timeline statusTimeline; private Timeline statusTimeline;
@Override @Override
@ -170,7 +173,7 @@ public class AppController implements Initializable {
boolean walletAdded = c.getAddedSubList().stream().anyMatch(tab -> ((TabData)tab.getUserData()).getType() == TabData.TabType.WALLET); boolean walletAdded = c.getAddedSubList().stream().anyMatch(tab -> ((TabData)tab.getUserData()).getType() == TabData.TabType.WALLET);
boolean walletRemoved = c.getRemoved().stream().anyMatch(tab -> ((TabData)tab.getUserData()).getType() == TabData.TabType.WALLET); boolean walletRemoved = c.getRemoved().stream().anyMatch(tab -> ((TabData)tab.getUserData()).getType() == TabData.TabType.WALLET);
if(walletAdded || walletRemoved) { if(walletAdded || walletRemoved) {
EventManager.get().post(new OpenWalletsEvent(tabs.getScene().getWindow(), getOpenWallets())); EventManager.get().post(new OpenWalletsEvent(tabs.getScene().getWindow(), getOpenWalletTabData()));
} }
List<WalletTabData> closedWalletTabs = c.getRemoved().stream().map(tab -> (TabData)tab.getUserData()) List<WalletTabData> closedWalletTabs = c.getRemoved().stream().map(tab -> (TabData)tab.getUserData())
@ -194,7 +197,7 @@ public class AppController implements Initializable {
tabs.getScene().getWindow().setOnCloseRequest(event -> { tabs.getScene().getWindow().setOnCloseRequest(event -> {
EventManager.get().unregister(this); EventManager.get().unregister(this);
EventManager.get().post(new OpenWalletsEvent(tabs.getScene().getWindow(), Collections.emptyMap())); EventManager.get().post(new OpenWalletsEvent(tabs.getScene().getWindow(), Collections.emptyList()));
}); });
BitcoinUnit unit = Config.get().getBitcoinUnit(); BitcoinUnit unit = Config.get().getBitcoinUnit();
@ -221,11 +224,15 @@ public class AppController implements Initializable {
showTxHex.setSelected(Config.get().isShowTransactionHex()); showTxHex.setSelected(Config.get().isShowTransactionHex());
exportWallet.setDisable(true); exportWallet.setDisable(true);
serverToggle.setSelected(isOnline()); setServerType(Config.get().getServerType());
serverToggle.setSelected(isConnected());
onlineProperty().bindBidirectional(serverToggle.selectedProperty()); onlineProperty().bindBidirectional(serverToggle.selectedProperty());
onlineProperty().addListener((observable, oldValue, newValue) -> { onlineProperty().addListener((observable, oldValue, newValue) -> {
Platform.runLater(() -> setServerToggleTooltip(getCurrentBlockHeight())); Platform.runLater(() -> setServerToggleTooltip(getCurrentBlockHeight()));
}); });
serverToggle.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
Config.get().setMode(serverToggle.isSelected() ? Mode.ONLINE : Mode.OFFLINE);
});
openTransactionIdItem.disableProperty().bind(onlineProperty().not()); openTransactionIdItem.disableProperty().bind(onlineProperty().not());
} }
@ -451,21 +458,30 @@ public class AppController implements Initializable {
} }
} }
public Map<Wallet, Storage> getOpenWallets() {
Map<Wallet, Storage> openWallets = new LinkedHashMap<>(); public List<WalletTabData> getOpenWalletTabData() {
List<WalletTabData> openWalletTabData = new ArrayList<>();
for(Tab tab : tabs.getTabs()) { for(Tab tab : tabs.getTabs()) {
TabData tabData = (TabData)tab.getUserData(); TabData tabData = (TabData)tab.getUserData();
if(tabData.getType() == TabData.TabType.WALLET) { if(tabData.getType() == TabData.TabType.WALLET) {
WalletTabData walletTabData = (WalletTabData) tabData; openWalletTabData.add((WalletTabData)tabData);
openWallets.put(walletTabData.getWallet(), walletTabData.getStorage());
} }
} }
return openWalletTabData;
}
public Map<Wallet, Storage> getOpenWallets() {
Map<Wallet, Storage> openWallets = new LinkedHashMap<>();
for(WalletTabData walletTabData : getOpenWalletTabData()){
openWallets.put(walletTabData.getWallet(), walletTabData.getStorage());
}
return openWallets; return openWallets;
} }
public void selectTab(Wallet wallet) { public void selectTab(Wallet wallet) {
for(Tab tab : tabs.getTabs()) { for(Tab tab : tabs.getTabs()) {
TabData tabData = (TabData) tab.getUserData(); TabData tabData = (TabData) tab.getUserData();
@ -521,16 +537,17 @@ public class AppController implements Initializable {
} }
private void setServerToggleTooltip(Integer currentBlockHeight) { private void setServerToggleTooltip(Integer currentBlockHeight) {
serverToggle.setTooltip(new Tooltip(AppServices.isOnline() ? "Connected to " + Config.get().getElectrumServer() + (currentBlockHeight != null ? " at height " + currentBlockHeight : "") : "Disconnected")); serverToggle.setTooltip(new Tooltip(AppServices.isConnected() ? "Connected to " + Config.get().getServerAddress() + (currentBlockHeight != null ? " at height " + currentBlockHeight : "") : "Disconnected"));
} }
public void newWallet(ActionEvent event) { public void newWallet(ActionEvent event) {
WalletNameDialog dlg = new WalletNameDialog(); WalletNameDialog dlg = new WalletNameDialog();
Optional<String> walletName = dlg.showAndWait(); Optional<WalletNameDialog.NameAndBirthDate> optNameAndBirthDate = dlg.showAndWait();
if(walletName.isPresent()) { if(optNameAndBirthDate.isPresent()) {
File walletFile = Storage.getWalletFile(walletName.get()); WalletNameDialog.NameAndBirthDate nameAndBirthDate = optNameAndBirthDate.get();
File walletFile = Storage.getWalletFile(nameAndBirthDate.getName());
Storage storage = new Storage(walletFile); Storage storage = new Storage(walletFile);
Wallet wallet = new Wallet(walletName.get(), PolicyType.SINGLE, ScriptType.P2WPKH); Wallet wallet = new Wallet(nameAndBirthDate.getName(), PolicyType.SINGLE, ScriptType.P2WPKH, nameAndBirthDate.getBirthDate());
addWalletTabOrWindow(storage, wallet, false); addWalletTabOrWindow(storage, wallet, false);
} }
} }
@ -695,8 +712,17 @@ public class AppController implements Initializable {
} }
private void addImportedWallet(Wallet wallet) { private void addImportedWallet(Wallet wallet) {
File walletFile = Storage.getExistingWallet(wallet.getName()); WalletNameDialog nameDlg = new WalletNameDialog(wallet.getName());
Optional<WalletNameDialog.NameAndBirthDate> optNameAndBirthDate = nameDlg.showAndWait();
if(optNameAndBirthDate.isPresent()) {
WalletNameDialog.NameAndBirthDate nameAndBirthDate = optNameAndBirthDate.get();
wallet.setName(nameAndBirthDate.getName());
wallet.setBirthDate(nameAndBirthDate.getBirthDate());
} else {
return;
}
File walletFile = Storage.getExistingWallet(wallet.getName());
if(walletFile != null) { if(walletFile != null) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION); Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Existing wallet found"); alert.setTitle("Existing wallet found");
@ -835,6 +861,8 @@ public class AppController implements Initializable {
tab.setContent(walletLoader.load()); tab.setContent(walletLoader.load());
WalletController controller = walletLoader.getController(); WalletController controller = walletLoader.getController();
EventManager.get().post(new WalletOpeningEvent(storage, wallet));
//Note that only one WalletForm is created per wallet tab, and registered to listen for events. All wallet controllers (except SettingsController) share this instance. //Note that only one WalletForm is created per wallet tab, and registered to listen for events. All wallet controllers (except SettingsController) share this instance.
WalletForm walletForm = new WalletForm(storage, wallet); WalletForm walletForm = new WalletForm(storage, wallet);
EventManager.get().register(walletForm); EventManager.get().register(walletForm);
@ -1019,6 +1047,14 @@ public class AppController implements Initializable {
return contextMenu; return contextMenu;
} }
public void setServerType(ServerType serverType) {
if(serverType == ServerType.BITCOIN_CORE && !serverToggle.getStyleClass().contains("core-server")) {
serverToggle.getStyleClass().add("core-server");
} else {
serverToggle.getStyleClass().remove("core-server");
}
}
public void setTheme(ActionEvent event) { public void setTheme(ActionEvent event) {
Theme selectedTheme = (Theme)theme.getSelectedToggle().getUserData(); Theme selectedTheme = (Theme)theme.getSelectedToggle().getUserData();
if(Config.get().getTheme() != selectedTheme) { if(Config.get().getTheme() != selectedTheme) {
@ -1040,6 +1076,11 @@ public class AppController implements Initializable {
} }
} }
@Subscribe
public void serverTypeChanged(ServerTypeChangedEvent event) {
setServerType(event.getServerType());
}
@Subscribe @Subscribe
public void tabSelected(TabSelectedEvent event) { public void tabSelected(TabSelectedEvent event) {
if(tabs.getTabs().contains(event.getTab())) { if(tabs.getTabs().contains(event.getTab())) {
@ -1152,7 +1193,10 @@ public class AppController implements Initializable {
public void statusUpdated(StatusEvent event) { public void statusUpdated(StatusEvent event) {
statusBar.setText(event.getStatus()); statusBar.setText(event.getStatus());
PauseTransition wait = new PauseTransition(Duration.seconds(20)); if(wait != null && wait.getStatus() == Animation.Status.RUNNING) {
wait.stop();
}
wait = new PauseTransition(Duration.seconds(20));
wait.setOnFinished((e) -> { wait.setOnFinished((e) -> {
if(statusBar.getText().equals(event.getStatus())) { if(statusBar.getText().equals(event.getStatus())) {
statusBar.setText(""); statusBar.setText("");
@ -1225,6 +1269,41 @@ public class AppController implements Initializable {
} }
} }
@Subscribe
public void bwtBootStatus(BwtBootStatusEvent event) {
serverToggle.setDisable(true);
statusUpdated(new StatusEvent(event.getStatus()));
}
@Subscribe
public void bwtSyncStatus(BwtSyncStatusEvent event) {
serverToggle.setDisable(false);
if((AppServices.isConnecting() || AppServices.isConnected()) && !event.isCompleted()) {
statusUpdated(new StatusEvent(event.getStatus()));
}
}
@Subscribe
public void bwtScanStatus(BwtScanStatusEvent event) {
serverToggle.setDisable(true);
if((AppServices.isConnecting() || AppServices.isConnected()) && !event.isCompleted()) {
statusUpdated(new StatusEvent(event.getStatus()));
}
}
@Subscribe
public void bwtReadyStatus(BwtReadyStatusEvent event) {
serverToggle.setDisable(false);
}
@Subscribe
public void disconnection(DisconnectionEvent event) {
serverToggle.setDisable(false);
if(!AppServices.isConnecting() && !AppServices.isConnected() && !statusBar.getText().startsWith("Connection error")) {
statusUpdated(new StatusEvent("Disconnected"));
}
}
@Subscribe @Subscribe
public void newBlock(NewBlockEvent event) { public void newBlock(NewBlockEvent event) {
setServerToggleTooltip(event.getHeight()); setServerToggleTooltip(event.getHeight());
@ -1278,7 +1357,7 @@ public class AppController implements Initializable {
@Subscribe @Subscribe
public void requestOpenWallets(RequestOpenWalletsEvent event) { public void requestOpenWallets(RequestOpenWalletsEvent event) {
EventManager.get().post(new OpenWalletsEvent(tabs.getScene().getWindow(), getOpenWallets())); EventManager.get().post(new OpenWalletsEvent(tabs.getScene().getWindow(), getOpenWalletTabData()));
} }
@Subscribe @Subscribe

View file

@ -12,10 +12,7 @@ import com.sparrowwallet.sparrow.io.Config;
import com.sparrowwallet.sparrow.io.Device; import com.sparrowwallet.sparrow.io.Device;
import com.sparrowwallet.sparrow.io.Hwi; import com.sparrowwallet.sparrow.io.Hwi;
import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.io.Storage;
import com.sparrowwallet.sparrow.net.ElectrumServer; import com.sparrowwallet.sparrow.net.*;
import com.sparrowwallet.sparrow.net.ExchangeSource;
import com.sparrowwallet.sparrow.net.MempoolRateSize;
import com.sparrowwallet.sparrow.net.VersionCheckService;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleBooleanProperty;
@ -27,6 +24,7 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import javafx.stage.Stage; import javafx.stage.Stage;
@ -57,7 +55,7 @@ public class AppServices {
private final MainApp application; private final MainApp application;
private final Map<Window, Map<Wallet, Storage>> walletWindows = new LinkedHashMap<>(); private final Map<Window, List<WalletTabData>> walletWindows = new LinkedHashMap<>();
private static final BooleanProperty onlineProperty = new SimpleBooleanProperty(false); private static final BooleanProperty onlineProperty = new SimpleBooleanProperty(false);
@ -86,7 +84,6 @@ public class AppServices {
private final ChangeListener<Boolean> onlineServicesListener = new ChangeListener<>() { private final ChangeListener<Boolean> onlineServicesListener = new ChangeListener<>() {
@Override @Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean online) { public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean online) {
Config.get().setMode(online ? Mode.ONLINE : Mode.OFFLINE);
if(online) { if(online) {
restartService(connectionService); restartService(connectionService);
@ -110,7 +107,7 @@ public class AppServices {
service.cancel(); service.cancel();
} }
if(service.getState() == Worker.State.CANCELLED) { if(service.getState() == Worker.State.CANCELLED || service.getState() == Worker.State.FAILED) {
service.reset(); service.reset();
} }
@ -127,7 +124,7 @@ public class AppServices {
public void start() { public void start() {
Config config = Config.get(); Config config = Config.get();
connectionService = createConnectionService(); connectionService = createConnectionService();
if(config.getMode() == Mode.ONLINE && config.getElectrumServer() != null && !config.getElectrumServer().isEmpty()) { if(config.getMode() == Mode.ONLINE && config.getServerAddress() != null && !config.getServerAddress().isEmpty()) {
connectionService.start(); connectionService.start();
} }
@ -146,6 +143,20 @@ public class AppServices {
onlineProperty.addListener(onlineServicesListener); onlineProperty.addListener(onlineServicesListener);
} }
public void stop() {
if(connectionService != null) {
connectionService.cancel();
}
if(ratesService != null) {
ratesService.cancel();
}
if(versionCheckService != null) {
versionCheckService.cancel();
}
}
private ElectrumServer.ConnectionService createConnectionService() { private ElectrumServer.ConnectionService createConnectionService() {
ElectrumServer.ConnectionService connectionService = new ElectrumServer.ConnectionService(); ElectrumServer.ConnectionService connectionService = new ElectrumServer.ConnectionService();
connectionService.setPeriod(new Duration(SERVER_PING_PERIOD)); connectionService.setPeriod(new Duration(SERVER_PING_PERIOD));
@ -159,6 +170,8 @@ public class AppServices {
}); });
connectionService.setOnSucceeded(successEvent -> { connectionService.setOnSucceeded(successEvent -> {
connectionService.setRestartOnFailure(true);
onlineProperty.removeListener(onlineServicesListener); onlineProperty.removeListener(onlineServicesListener);
onlineProperty.setValue(true); onlineProperty.setValue(true);
onlineProperty.addListener(onlineServicesListener); onlineProperty.addListener(onlineServicesListener);
@ -171,6 +184,10 @@ public class AppServices {
//Close connection here to create a new transport next time we try //Close connection here to create a new transport next time we try
connectionService.resetConnection(); connectionService.resetConnection();
if(failEvent.getSource().getException() instanceof ServerConfigException) {
connectionService.setRestartOnFailure(false);
}
onlineProperty.removeListener(onlineServicesListener); onlineProperty.removeListener(onlineServicesListener);
onlineProperty.setValue(false); onlineProperty.setValue(false);
onlineProperty.addListener(onlineServicesListener); onlineProperty.addListener(onlineServicesListener);
@ -265,17 +282,24 @@ public class AppServices {
return application; return application;
} }
public Map<Wallet, Storage> getOpenWallets(Window window) { public Map<Wallet, Storage> getOpenWallets() {
return walletWindows.get(window); Map<Wallet, Storage> openWallets = new LinkedHashMap<>();
for(List<WalletTabData> walletTabDataList : walletWindows.values()) {
for(WalletTabData walletTabData : walletTabDataList) {
openWallets.put(walletTabData.getWallet(), walletTabData.getStorage());
}
}
return openWallets;
} }
public Window getWindowForWallet(Storage storage) { public Window getWindowForWallet(Storage storage) {
Optional<Window> optWindow = walletWindows.entrySet().stream().filter(entry -> entry.getValue().values().stream().anyMatch(storage1 -> storage1.getWalletFile().equals(storage.getWalletFile()))).map(Map.Entry::getKey).findFirst(); Optional<Window> optWindow = walletWindows.entrySet().stream().filter(entry -> entry.getValue().stream().anyMatch(walletTabData -> walletTabData.getStorage().getWalletFile().equals(storage.getWalletFile()))).map(Map.Entry::getKey).findFirst();
return optWindow.orElse(null); return optWindow.orElse(null);
} }
public Window getWindowForPSBT(PSBT psbt) { public Window getWindowForPSBT(PSBT psbt) {
Optional<Window> optWindow = walletWindows.entrySet().stream().filter(entry -> entry.getValue().keySet().stream().anyMatch(wallet -> wallet.canSign(psbt))).map(Map.Entry::getKey).findFirst(); Optional<Window> optWindow = walletWindows.entrySet().stream().filter(entry -> entry.getValue().stream().anyMatch(walletTabData -> walletTabData.getWallet().canSign(psbt))).map(Map.Entry::getKey).findFirst();
return optWindow.orElse(null); return optWindow.orElse(null);
} }
@ -283,8 +307,12 @@ public class AppServices {
return walletWindows.keySet().stream().mapToDouble(Window::getX).max().orElse(0d); return walletWindows.keySet().stream().mapToDouble(Window::getX).max().orElse(0d);
} }
public static boolean isOnline() { public static boolean isConnecting() {
return onlineProperty.get(); return get().connectionService != null && get().connectionService.isConnecting();
}
public static boolean isConnected() {
return onlineProperty.get() && get().connectionService.isConnected();
} }
public static BooleanProperty onlineProperty() { public static BooleanProperty onlineProperty() {
@ -335,14 +363,21 @@ public class AppServices {
payjoinURIs.put(bitcoinURI.getAddress(), bitcoinURI); payjoinURIs.put(bitcoinURI.getAddress(), bitcoinURI);
} }
public static void showErrorDialog(String title, String content) { public static Optional<ButtonType> showWarningDialog(String title, String content, ButtonType... buttons) {
Alert alert = new Alert(Alert.AlertType.ERROR); return showAlertDialog(title, content, Alert.AlertType.WARNING, buttons);
}
public static Optional<ButtonType> showErrorDialog(String title, String content, ButtonType... buttons) {
return showAlertDialog(title, content, Alert.AlertType.ERROR, buttons);
}
public static Optional<ButtonType> showAlertDialog(String title, String content, Alert.AlertType alertType, ButtonType... buttons) {
Alert alert = new Alert(alertType, content, buttons);
setStageIcon(alert.getDialogPane().getScene().getWindow()); setStageIcon(alert.getDialogPane().getScene().getWindow());
alert.getDialogPane().getScene().getStylesheets().add(AppServices.class.getResource("general.css").toExternalForm()); alert.getDialogPane().getScene().getStylesheets().add(AppServices.class.getResource("general.css").toExternalForm());
alert.setTitle(title); alert.setTitle(title);
alert.setHeaderText(title); alert.setHeaderText(title);
alert.setContentText(content); return alert.showAndWait();
alert.showAndWait();
} }
public static void setStageIcon(Window window) { public static void setStageIcon(Window window) {
@ -365,7 +400,7 @@ public class AppServices {
addMempoolRateSizes(event.getMempoolRateSizes()); addMempoolRateSizes(event.getMempoolRateSizes());
minimumRelayFeeRate = event.getMinimumRelayFeeRate(); minimumRelayFeeRate = event.getMinimumRelayFeeRate();
String banner = event.getServerBanner(); String banner = event.getServerBanner();
String status = "Connected to " + Config.get().getElectrumServer() + " at height " + event.getBlockHeight(); String status = "Connected to " + Config.get().getServerAddress() + " at height " + event.getBlockHeight();
EventManager.get().post(new StatusEvent(status)); EventManager.get().post(new StatusEvent(status));
} }
@ -431,25 +466,25 @@ public class AppServices {
@Subscribe @Subscribe
public void openWallets(OpenWalletsEvent event) { public void openWallets(OpenWalletsEvent event) {
if(event.getWalletsMap().isEmpty()) { if(event.getWalletTabDataList().isEmpty()) {
walletWindows.remove(event.getWindow()); walletWindows.remove(event.getWindow());
} else { } else {
walletWindows.put(event.getWindow(), event.getWalletsMap()); walletWindows.put(event.getWindow(), event.getWalletTabDataList());
} }
List<Map.Entry<Wallet, Storage>> allWallets = walletWindows.values().stream().flatMap(map -> map.entrySet().stream()).collect(Collectors.toList()); List<WalletTabData> allWallets = walletWindows.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
Platform.runLater(() -> { Platform.runLater(() -> {
if(!Window.getWindows().isEmpty()) { if(!Window.getWindows().isEmpty()) {
List<File> walletFiles = allWallets.stream().map(entry -> entry.getValue().getWalletFile()).collect(Collectors.toList()); List<File> walletFiles = allWallets.stream().map(walletTabData -> walletTabData.getStorage().getWalletFile()).collect(Collectors.toList());
Config.get().setRecentWalletFiles(walletFiles); Config.get().setRecentWalletFiles(walletFiles);
} }
}); });
boolean usbWallet = false; boolean usbWallet = false;
for(Map.Entry<Wallet, Storage> entry : allWallets) { for(WalletTabData walletTabData : allWallets) {
Wallet wallet = entry.getKey(); Wallet wallet = walletTabData.getWallet();
Storage storage = entry.getValue(); Storage storage = walletTabData.getStorage();
if(!storage.getWalletFile().exists() || wallet.containsSource(KeystoreSource.HW_USB)) { if(!storage.getWalletFile().exists() || wallet.containsSource(KeystoreSource.HW_USB)) {
usbWallet = true; usbWallet = true;
@ -485,4 +520,28 @@ public class AppServices {
public void requestDisconnect(RequestDisconnectEvent event) { public void requestDisconnect(RequestDisconnectEvent event) {
onlineProperty.set(false); onlineProperty.set(false);
} }
@Subscribe
public void walletSettingsChanged(WalletSettingsChangedEvent event) {
restartBwt(event.getWallet());
}
@Subscribe
public void walletOpening(WalletOpeningEvent event) {
restartBwt(event.getWallet());
}
private void restartBwt(Wallet wallet) {
if(Config.get().getServerType() == ServerType.BITCOIN_CORE && isConnected() && wallet.isValid()) {
connectionService.cancel();
}
}
@Subscribe
public void bwtShutdown(BwtShutdownEvent event) {
if(onlineProperty().get() && !connectionService.isRunning()) {
connectionService.reset();
connectionService.start();
}
}
} }

View file

@ -9,14 +9,10 @@ import com.sparrowwallet.sparrow.io.Config;
import com.sparrowwallet.sparrow.io.FileType; import com.sparrowwallet.sparrow.io.FileType;
import com.sparrowwallet.sparrow.io.IOUtils; import com.sparrowwallet.sparrow.io.IOUtils;
import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.io.Storage;
import com.sparrowwallet.sparrow.net.ServerType;
import com.sparrowwallet.sparrow.preferences.PreferenceGroup; import com.sparrowwallet.sparrow.preferences.PreferenceGroup;
import com.sparrowwallet.sparrow.preferences.PreferencesDialog; import com.sparrowwallet.sparrow.preferences.PreferencesDialog;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.controlsfx.glyphfont.GlyphFontRegistry; import org.controlsfx.glyphfont.GlyphFontRegistry;
@ -66,6 +62,10 @@ public class MainApp extends Application {
} }
} }
if(Config.get().getServerType() == null && Config.get().getCoreServer() == null && Config.get().getElectrumServer() != null) {
Config.get().setServerType(ServerType.ELECTRUM_SERVER);
}
AppServices.initialize(this); AppServices.initialize(this);
AppController appController = AppServices.newAppWindow(stage); AppController appController = AppServices.newAppWindow(stage);
@ -94,6 +94,7 @@ public class MainApp extends Application {
@Override @Override
public void stop() throws Exception { public void stop() throws Exception {
AppServices.get().stop();
mainStage.close(); mainStage.close();
} }

View file

@ -2,12 +2,27 @@ package com.sparrowwallet.sparrow.control;
import com.sparrowwallet.drongo.BitcoinUnit; import com.sparrowwallet.drongo.BitcoinUnit;
import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.event.WalletDataChangedEvent;
import com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent; import com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent;
import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent;
import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.io.Config;
import com.sparrowwallet.sparrow.io.Storage;
import com.sparrowwallet.sparrow.net.ServerType;
import com.sparrowwallet.sparrow.wallet.Entry; import com.sparrowwallet.sparrow.wallet.Entry;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.TreeTableView; import javafx.scene.control.TreeTableView;
import javafx.scene.layout.StackPane;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
public class CoinTreeTable extends TreeTableView<Entry> { public class CoinTreeTable extends TreeTableView<Entry> {
private BitcoinUnit bitcoinUnit; private BitcoinUnit bitcoinUnit;
@ -51,10 +66,46 @@ public class CoinTreeTable extends TreeTableView<Entry> {
setPlaceholder(new Label("Loading transactions...")); setPlaceholder(new Label("Loading transactions..."));
} }
} else { } else {
setPlaceholder(new Label("No transactions")); setPlaceholder(getDefaultPlaceholder(event.getWallet()));
} }
}); });
} }
} }
} }
protected Node getDefaultPlaceholder(Wallet wallet) {
StackPane stackPane = new StackPane();
stackPane.getChildren().add(AppServices.isConnecting() ? new Label("Loading transactions...") : new Label("No transactions"));
if(Config.get().getServerType() == ServerType.BITCOIN_CORE && !AppServices.isConnecting()) {
Hyperlink hyperlink = new Hyperlink();
hyperlink.setTranslateY(30);
hyperlink.setOnAction(event -> {
WalletBirthDateDialog dlg = new WalletBirthDateDialog(wallet.getBirthDate());
Optional<Date> optDate = dlg.showAndWait();
if(optDate.isPresent()) {
wallet.setBirthDate(optDate.get());
Storage storage = AppServices.get().getOpenWallets().get(wallet);
if(storage != null) {
//Trigger background save of birthdate
EventManager.get().post(new WalletDataChangedEvent(wallet));
//Trigger full wallet rescan
wallet.clearHistory();
EventManager.get().post(new WalletSettingsChangedEvent(wallet, storage.getWalletFile()));
}
}
});
if(wallet.getBirthDate() == null) {
hyperlink.setText("Scan for previous transactions?");
} else {
DateFormat dateFormat = new SimpleDateFormat(DateStringConverter.FORMAT_PATTERN);
hyperlink.setText("Scan for transactions earlier than " + dateFormat.format(wallet.getBirthDate()) + "?");
}
stackPane.getChildren().add(hyperlink);
}
stackPane.setAlignment(Pos.CENTER);
return stackPane;
}
} }

View file

@ -0,0 +1,29 @@
package com.sparrowwallet.sparrow.control;
import javafx.util.StringConverter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateStringConverter extends StringConverter<LocalDate> {
public static final String FORMAT_PATTERN = "yyyy/MM/dd";
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(FORMAT_PATTERN);
@Override
public String toString(LocalDate date) {
if (date != null) {
return DATE_FORMATTER.format(date);
} else {
return "";
}
}
@Override
public LocalDate fromString(String string) {
if (string != null && !string.isEmpty()) {
return LocalDate.parse(string, DATE_FORMATTER);
} else {
return null;
}
}
}

View file

@ -5,7 +5,6 @@ import com.sparrowwallet.sparrow.wallet.Entry;
import com.sparrowwallet.sparrow.wallet.TransactionEntry; import com.sparrowwallet.sparrow.wallet.TransactionEntry;
import com.sparrowwallet.sparrow.wallet.WalletTransactionsEntry; import com.sparrowwallet.sparrow.wallet.WalletTransactionsEntry;
import javafx.beans.property.ReadOnlyObjectWrapper; import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.scene.control.Label;
import javafx.scene.control.TreeTableColumn; import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView; import javafx.scene.control.TreeTableView;
@ -51,7 +50,7 @@ public class TransactionsTreeTable extends CoinTreeTable {
balanceCol.setSortable(true); balanceCol.setSortable(true);
getColumns().add(balanceCol); getColumns().add(balanceCol);
setPlaceholder(new Label("No transactions")); setPlaceholder(getDefaultPlaceholder(rootEntry.getWallet()));
setEditable(true); setEditable(true);
setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY); setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);
dateCol.setSortType(TreeTableColumn.SortType.DESCENDING); dateCol.setSortType(TreeTableColumn.SortType.DESCENDING);

View file

@ -3,7 +3,6 @@ package com.sparrowwallet.sparrow.control;
import com.sparrowwallet.drongo.wallet.WalletNode; import com.sparrowwallet.drongo.wallet.WalletNode;
import com.sparrowwallet.sparrow.wallet.*; import com.sparrowwallet.sparrow.wallet.*;
import javafx.beans.property.ReadOnlyObjectWrapper; import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode; import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeTableColumn; import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView; import javafx.scene.control.TreeTableView;
@ -69,7 +68,7 @@ public class UtxosTreeTable extends CoinTreeTable {
getColumns().add(amountCol); getColumns().add(amountCol);
setTreeColumn(amountCol); setTreeColumn(amountCol);
setPlaceholder(new Label("No unspent outputs")); setPlaceholder(getDefaultPlaceholder(rootEntry.getWallet()));
setEditable(true); setEditable(true);
setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY); setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);
amountCol.setSortType(TreeTableColumn.SortType.DESCENDING); amountCol.setSortType(TreeTableColumn.SortType.DESCENDING);

View file

@ -0,0 +1,68 @@
package com.sparrowwallet.sparrow.control;
import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.glyphfont.FontAwesome5;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import org.controlsfx.glyphfont.Glyph;
import org.controlsfx.validation.ValidationResult;
import org.controlsfx.validation.ValidationSupport;
import org.controlsfx.validation.Validator;
import org.controlsfx.validation.decoration.StyleClassValidationDecoration;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class WalletBirthDateDialog extends Dialog<Date> {
private final DatePicker birthDatePicker;
public WalletBirthDateDialog(Date birthDate) {
final DialogPane dialogPane = getDialogPane();
AppServices.setStageIcon(dialogPane.getScene().getWindow());
setTitle("Wallet Birth Date");
dialogPane.setHeaderText("Select an approximate date earlier than the first wallet transaction:");
dialogPane.getStylesheets().add(AppServices.class.getResource("general.css").toExternalForm());
dialogPane.getButtonTypes().addAll(ButtonType.CANCEL);
dialogPane.setPrefWidth(420);
dialogPane.setPrefHeight(200);
Glyph wallet = new Glyph(FontAwesome5.FONT_NAME, FontAwesome5.Glyph.HISTORY);
wallet.setFontSize(50);
dialogPane.setGraphic(wallet);
HBox datePickerBox = new HBox(10);
Label label = new Label("Start scanning from:");
label.setPadding(new Insets(5, 0, 0, 8));
datePickerBox.getChildren().add(label);
birthDatePicker = birthDate == null ? new DatePicker() : new DatePicker(birthDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
birthDatePicker.setEditable(false);
birthDatePicker.setConverter(new DateStringConverter());
datePickerBox.getChildren().add(birthDatePicker);
dialogPane.setContent(datePickerBox);
ValidationSupport validationSupport = new ValidationSupport();
Platform.runLater( () -> {
validationSupport.registerValidator(birthDatePicker, Validator.combine(
(Control c, LocalDate newValue) -> ValidationResult.fromErrorIf( c, "Birth date not specified", newValue == null)
));
validationSupport.setValidationDecorator(new StyleClassValidationDecoration());
});
final ButtonType okButtonType = new javafx.scene.control.ButtonType("Rescan Wallet", ButtonBar.ButtonData.OK_DONE);
dialogPane.getButtonTypes().addAll(okButtonType);
Button okButton = (Button) dialogPane.lookupButton(okButtonType);
BooleanBinding isInvalid = Bindings.createBooleanBinding(() -> birthDatePicker.getValue() == null, birthDatePicker.valueProperty());
okButton.disableProperty().bind(isInvalid);
setResultConverter(dialogButton -> dialogButton == okButtonType ? Date.from(birthDatePicker.getValue().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()) : null);
}
}

View file

@ -2,11 +2,15 @@ package com.sparrowwallet.sparrow.control;
import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.glyphfont.FontAwesome5; import com.sparrowwallet.sparrow.glyphfont.FontAwesome5;
import com.sparrowwallet.sparrow.io.Config;
import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.io.Storage;
import com.sparrowwallet.sparrow.net.ServerType;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.binding.Bindings; import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding; import javafx.beans.binding.BooleanBinding;
import javafx.geometry.Insets;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import org.controlsfx.control.textfield.CustomTextField; import org.controlsfx.control.textfield.CustomTextField;
import org.controlsfx.control.textfield.TextFields; import org.controlsfx.control.textfield.TextFields;
@ -16,28 +20,74 @@ import org.controlsfx.validation.ValidationSupport;
import org.controlsfx.validation.Validator; import org.controlsfx.validation.Validator;
import org.controlsfx.validation.decoration.StyleClassValidationDecoration; import org.controlsfx.validation.decoration.StyleClassValidationDecoration;
public class WalletNameDialog extends Dialog<String> { import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class WalletNameDialog extends Dialog<WalletNameDialog.NameAndBirthDate> {
private final CustomTextField name; private final CustomTextField name;
private final CheckBox existingCheck;
private final DatePicker existingPicker;
public WalletNameDialog() { public WalletNameDialog() {
this.name = (CustomTextField)TextFields.createClearableTextField(); this("");
}
public WalletNameDialog(String initialName) {
final DialogPane dialogPane = getDialogPane(); final DialogPane dialogPane = getDialogPane();
AppServices.setStageIcon(dialogPane.getScene().getWindow()); AppServices.setStageIcon(dialogPane.getScene().getWindow());
boolean requestBirthDate = (Config.get().getServerType() == null || Config.get().getServerType() == ServerType.BITCOIN_CORE);
setTitle("Wallet Name"); setTitle("Wallet Name");
dialogPane.setHeaderText("Enter a name for this wallet:"); dialogPane.setHeaderText("Enter a name for this wallet:");
dialogPane.getStylesheets().add(AppServices.class.getResource("general.css").toExternalForm()); dialogPane.getStylesheets().add(AppServices.class.getResource("general.css").toExternalForm());
dialogPane.getButtonTypes().addAll(ButtonType.CANCEL); dialogPane.getButtonTypes().addAll(ButtonType.CANCEL);
dialogPane.setPrefWidth(380); dialogPane.setPrefWidth(460);
dialogPane.setPrefHeight(200); dialogPane.setPrefHeight(requestBirthDate ? 250 : 200);
Glyph wallet = new Glyph(FontAwesome5.FONT_NAME, FontAwesome5.Glyph.WALLET); Glyph wallet = new Glyph(FontAwesome5.FONT_NAME, FontAwesome5.Glyph.WALLET);
wallet.setFontSize(50); wallet.setFontSize(50);
dialogPane.setGraphic(wallet); dialogPane.setGraphic(wallet);
final VBox content = new VBox(10); final VBox content = new VBox(20);
name = (CustomTextField)TextFields.createClearableTextField();
name.setText(initialName);
content.getChildren().add(name); content.getChildren().add(name);
HBox existingBox = new HBox(10);
existingCheck = new CheckBox("Has existing transactions");
existingCheck.setPadding(new Insets(5, 0, 0, 0));
existingBox.getChildren().add(existingCheck);
existingPicker = new DatePicker();
existingPicker.setConverter(new DateStringConverter());
existingPicker.setEditable(false);
existingPicker.setPrefWidth(130);
existingPicker.managedProperty().bind(existingPicker.visibleProperty());
existingPicker.setVisible(false);
existingBox.getChildren().add(existingPicker);
HelpLabel helpLabel = new HelpLabel();
helpLabel.setHelpText("Select an approximate date earlier than the first wallet transaction.");
helpLabel.setTranslateY(5);
helpLabel.managedProperty().bind(helpLabel.visibleProperty());
helpLabel.visibleProperty().bind(existingPicker.visibleProperty());
existingBox.getChildren().add(helpLabel);
existingCheck.selectedProperty().addListener((observable, oldValue, newValue) -> {
if(newValue) {
existingCheck.setText("Has existing transactions starting from");
existingPicker.setVisible(true);
} else {
existingCheck.setText("Has existing transactions");
existingPicker.setVisible(false);
}
});
if(requestBirthDate) {
content.getChildren().add(existingBox);
}
dialogPane.setContent(content); dialogPane.setContent(content);
ValidationSupport validationSupport = new ValidationSupport(); ValidationSupport validationSupport = new ValidationSupport();
@ -46,18 +96,39 @@ public class WalletNameDialog extends Dialog<String> {
Validator.createEmptyValidator("Wallet name is required"), Validator.createEmptyValidator("Wallet name is required"),
(Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Wallet name is not unique", Storage.walletExists(newValue)) (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Wallet name is not unique", Storage.walletExists(newValue))
)); ));
validationSupport.registerValidator(existingPicker, Validator.combine(
(Control c, LocalDate newValue) -> ValidationResult.fromErrorIf( c, "Birth date not specified", existingCheck.isSelected() && newValue == null)
));
validationSupport.setValidationDecorator(new StyleClassValidationDecoration()); validationSupport.setValidationDecorator(new StyleClassValidationDecoration());
}); });
final ButtonType okButtonType = new javafx.scene.control.ButtonType("New Wallet", ButtonBar.ButtonData.OK_DONE); final ButtonType okButtonType = new javafx.scene.control.ButtonType("Create Wallet", ButtonBar.ButtonData.OK_DONE);
dialogPane.getButtonTypes().addAll(okButtonType); dialogPane.getButtonTypes().addAll(okButtonType);
Button okButton = (Button) dialogPane.lookupButton(okButtonType); Button okButton = (Button) dialogPane.lookupButton(okButtonType);
BooleanBinding isInvalid = Bindings.createBooleanBinding(() -> BooleanBinding isInvalid = Bindings.createBooleanBinding(() ->
name.getText().length() == 0 || Storage.walletExists(name.getText()), name.textProperty()); name.getText().length() == 0 || Storage.walletExists(name.getText()) || (existingCheck.isSelected() && existingPicker.getValue() == null), name.textProperty(), existingCheck.selectedProperty(), existingPicker.valueProperty());
okButton.disableProperty().bind(isInvalid); okButton.disableProperty().bind(isInvalid);
name.setPromptText("Wallet Name"); name.setPromptText("Wallet Name");
Platform.runLater(name::requestFocus); Platform.runLater(name::requestFocus);
setResultConverter(dialogButton -> dialogButton == okButtonType ? name.getText() : null); setResultConverter(dialogButton -> dialogButton == okButtonType ? new NameAndBirthDate(name.getText(), existingPicker.getValue()) : null);
}
public static class NameAndBirthDate {
private final String name;
private final Date birthDate;
public NameAndBirthDate(String name, LocalDate birthLocalDate) {
this.name = name;
this.birthDate = (birthLocalDate == null ? null : Date.from(birthLocalDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
}
public String getName() {
return name;
}
public Date getBirthDate() {
return birthDate;
}
} }
} }

View file

@ -2,6 +2,7 @@ package com.sparrowwallet.sparrow.control;
import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.Mode; import com.sparrowwallet.sparrow.Mode;
import com.sparrowwallet.sparrow.net.ServerType;
import javafx.application.HostServices; import javafx.application.HostServices;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.scene.control.*; import javafx.scene.control.*;
@ -13,13 +14,10 @@ import org.controlsfx.control.StatusBar;
import org.controlsfx.control.ToggleSwitch; import org.controlsfx.control.ToggleSwitch;
public class WelcomeDialog extends Dialog<Mode> { public class WelcomeDialog extends Dialog<Mode> {
private static final String[] ELECTRUM_SERVERS = new String[]{
"ElectrumX (Recommended)", "https://github.com/spesmilo/electrumx",
"electrs", "https://github.com/romanz/electrs",
"esplora-electrs", "https://github.com/Blockstream/electrs"};
private final HostServices hostServices; private final HostServices hostServices;
private ServerType serverType = ServerType.ELECTRUM_SERVER;
public WelcomeDialog(HostServices services) { public WelcomeDialog(HostServices services) {
this.hostServices = services; this.hostServices = services;
@ -27,10 +25,11 @@ public class WelcomeDialog extends Dialog<Mode> {
setTitle("Welcome to Sparrow"); setTitle("Welcome to Sparrow");
dialogPane.setHeaderText("Welcome to Sparrow!"); dialogPane.setHeaderText("Welcome to Sparrow!");
dialogPane.getStylesheets().add(AppServices.class.getResource("app.css").toExternalForm());
dialogPane.getStylesheets().add(AppServices.class.getResource("general.css").toExternalForm()); dialogPane.getStylesheets().add(AppServices.class.getResource("general.css").toExternalForm());
AppServices.setStageIcon(dialogPane.getScene().getWindow()); AppServices.setStageIcon(dialogPane.getScene().getWindow());
dialogPane.setPrefWidth(600); dialogPane.setPrefWidth(600);
dialogPane.setPrefHeight(480); dialogPane.setPrefHeight(520);
Image image = new Image("image/sparrow-small.png", 50, 50, false, false); Image image = new Image("image/sparrow-small.png", 50, 50, false, false);
if (!image.isError()) { if (!image.isError()) {
@ -46,16 +45,10 @@ public class WelcomeDialog extends Dialog<Mode> {
final VBox content = new VBox(20); final VBox content = new VBox(20);
content.setPadding(new Insets(20, 20, 20, 20)); content.setPadding(new Insets(20, 20, 20, 20));
content.getChildren().add(createParagraph("Sparrow can operate in both an online and offline mode. In the online mode it connects to your Electrum server to display transaction history. In the offline mode it is useful as a transaction editor and as an airgapped multisig coordinator.")); content.getChildren().add(createParagraph("Sparrow can operate in both an online and offline mode. In the online mode it connects to your Bitcoin Core node or Electrum server to display transaction history. In the offline mode it is useful as a transaction editor and as an airgapped multisig coordinator."));
content.getChildren().add(createParagraph("For privacy and security reasons it is not recommended to use a public Electrum server. Install an Electrum server that connects to your full node to index the blockchain and provide full privacy. Examples include:")); content.getChildren().add(createParagraph("Connecting Sparrow to your Bitcoin Core node ensures your privacy, while connecting Sparrow to your own Electrum server ensures wallets load quicker, you have access to a full blockchain explorer, and your public keys are always encrypted on disk. Examples of Electrum servers include ElectrumX and electrs."));
content.getChildren().add(createParagraph("It's also possible to connect Sparrow to a public Electrum server (such as blockstream.info:700) but this is not recommended as you will share your public key information with that server."));
VBox linkBox = new VBox(); content.getChildren().add(createParagraph("You can change your mode at any time using the toggle in the status bar. A blue toggle indicates you are connected to an Electrum server, while a green toggle indicates you are connected to a Bitcoin Code node."));
for(int i = 0; i < ELECTRUM_SERVERS.length; i+=2) {
linkBox.getChildren().add(createBulletedLink(ELECTRUM_SERVERS[i], ELECTRUM_SERVERS[i+1]));
}
content.getChildren().add(linkBox);
content.getChildren().add(createParagraph("You can change your mode at any time using the toggle in the status bar:"));
content.getChildren().add(createStatusBar(onlineButtonType, offlineButtonType)); content.getChildren().add(createStatusBar(onlineButtonType, offlineButtonType));
dialogPane.setContent(content); dialogPane.setContent(content);
@ -70,16 +63,6 @@ public class WelcomeDialog extends Dialog<Mode> {
return label; return label;
} }
private HyperlinkLabel createBulletedLink(String name, String url) {
String[] nameParts = name.split(" ");
HyperlinkLabel label = new HyperlinkLabel(" \u2022 [" + nameParts[0] + "] " + (nameParts.length > 1 ? nameParts[1] : ""));
label.setOnAction(event -> {
hostServices.showDocument(url);
});
return label;
}
private StatusBar createStatusBar(ButtonType onlineButtonType, ButtonType offlineButtonType) { private StatusBar createStatusBar(ButtonType onlineButtonType, ButtonType offlineButtonType) {
StatusBar statusBar = new StatusBar(); StatusBar statusBar = new StatusBar();
statusBar.setText("Online Mode"); statusBar.setText("Online Mode");
@ -97,7 +80,18 @@ public class WelcomeDialog extends Dialog<Mode> {
onlineButton.setDefaultButton(newValue); onlineButton.setDefaultButton(newValue);
Button offlineButton = (Button) getDialogPane().lookupButton(offlineButtonType); Button offlineButton = (Button) getDialogPane().lookupButton(offlineButtonType);
offlineButton.setDefaultButton(!newValue); offlineButton.setDefaultButton(!newValue);
statusBar.setText(newValue ? "Online Mode" : "Offline Mode");
if(!newValue) {
serverType = (serverType == ServerType.BITCOIN_CORE ? ServerType.ELECTRUM_SERVER : ServerType.BITCOIN_CORE);
if(serverType == ServerType.BITCOIN_CORE && !toggleSwitch.getStyleClass().contains("core-server")) {
toggleSwitch.getStyleClass().add("core-server");
} else {
toggleSwitch.getStyleClass().remove("core-server");
}
}
statusBar.setText(newValue ? "Online Mode: " + serverType.getName() : "Offline Mode");
}); });
toggleSwitch.setSelected(true); toggleSwitch.setSelected(true);

View file

@ -0,0 +1,7 @@
package com.sparrowwallet.sparrow.event;
public class BwtBootStatusEvent extends BwtStatusEvent {
public BwtBootStatusEvent(String status) {
super(status);
}
}

View file

@ -0,0 +1,14 @@
package com.sparrowwallet.sparrow.event;
public class BwtElectrumReadyStatusEvent extends BwtStatusEvent {
private final String electrumAddr;
public BwtElectrumReadyStatusEvent(String status, String electrumAddr) {
super(status);
this.electrumAddr = electrumAddr;
}
public String getElectrumAddr() {
return electrumAddr;
}
}

View file

@ -0,0 +1,7 @@
package com.sparrowwallet.sparrow.event;
public class BwtReadyStatusEvent extends BwtStatusEvent {
public BwtReadyStatusEvent(String status) {
super(status);
}
}

View file

@ -0,0 +1,26 @@
package com.sparrowwallet.sparrow.event;
import java.util.Date;
public class BwtScanStatusEvent extends BwtStatusEvent {
private final int progress;
private final Date eta;
public BwtScanStatusEvent(String status, int progress, Date eta) {
super(status);
this.progress = progress;
this.eta = eta;
}
public int getProgress() {
return progress;
}
public boolean isCompleted() {
return progress == 100;
}
public Date getEta() {
return eta;
}
}

View file

@ -0,0 +1,9 @@
package com.sparrowwallet.sparrow.event;
/**
* Empty class used to notify the bwt has shut down.
* Note this extends from DisconnectionEvent, which is the more general event fired on any type of disconnection.
*/
public class BwtShutdownEvent extends DisconnectionEvent {
}

View file

@ -0,0 +1,13 @@
package com.sparrowwallet.sparrow.event;
public class BwtStatusEvent {
private final String status;
public BwtStatusEvent(String status) {
this.status = status;
}
public String getStatus() {
return status;
}
}

View file

@ -0,0 +1,26 @@
package com.sparrowwallet.sparrow.event;
import java.util.Date;
public class BwtSyncStatusEvent extends BwtStatusEvent {
private final int progress;
private final Date tip;
public BwtSyncStatusEvent(String status, int progress, Date tip) {
super(status);
this.progress = progress;
this.tip = tip;
}
public int getProgress() {
return progress;
}
public boolean isCompleted() {
return progress == 100;
}
public Date getTip() {
return tip;
}
}

View file

@ -0,0 +1,8 @@
package com.sparrowwallet.sparrow.event;
/**
* Empty class used to signal that the server has been disconnected from.
*/
public class DisconnectionEvent {
}

View file

@ -1,35 +1,47 @@
package com.sparrowwallet.sparrow.event; package com.sparrowwallet.sparrow.event;
import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.WalletTabData;
import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.io.Storage;
import javafx.stage.Window; import javafx.stage.Window;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class OpenWalletsEvent { public class OpenWalletsEvent {
private final Window window; private final Window window;
private final Map<Wallet, Storage> walletsMap; private final List<WalletTabData> walletTabDataList;
public OpenWalletsEvent(Window window, Map<Wallet, Storage> walletsMap) { public OpenWalletsEvent(Window window, List<WalletTabData> walletTabDataList) {
this.window = window; this.window = window;
this.walletsMap = walletsMap; this.walletTabDataList = walletTabDataList;
} }
public Window getWindow() { public Window getWindow() {
return window; return window;
} }
public List<Wallet> getWallets() { public List<WalletTabData> getWalletTabDataList() {
return new ArrayList<>(walletsMap.keySet()); return walletTabDataList;
}
public Storage getStorage(Wallet wallet) {
return walletsMap.get(wallet);
} }
public Map<Wallet, Storage> getWalletsMap() { public Map<Wallet, Storage> getWalletsMap() {
return walletsMap; Map<Wallet, Storage> openWallets = new LinkedHashMap<>();
for(WalletTabData walletTabData : walletTabDataList){
openWallets.put(walletTabData.getWallet(), walletTabData.getStorage());
}
return openWallets;
}
public List<Wallet> getWallets() {
return new ArrayList<>(getWalletsMap().keySet());
}
public Storage getStorage(Wallet wallet) {
return getWalletsMap().get(wallet);
} }
} }

View file

@ -0,0 +1,15 @@
package com.sparrowwallet.sparrow.event;
import com.sparrowwallet.sparrow.net.ServerType;
public class ServerTypeChangedEvent {
private final ServerType serverType;
public ServerTypeChangedEvent(ServerType serverType) {
this.serverType = serverType;
}
public ServerType getServerType() {
return serverType;
}
}

View file

@ -20,6 +20,6 @@ public class SettingsChangedEvent {
} }
public enum Type { public enum Type {
POLICY, SCRIPT_TYPE, MUTLISIG_THRESHOLD, MULTISIG_TOTAL, KEYSTORE_LABEL, KEYSTORE_FINGERPRINT, KEYSTORE_DERIVATION, KEYSTORE_XPUB, GAP_LIMIT; POLICY, SCRIPT_TYPE, MUTLISIG_THRESHOLD, MULTISIG_TOTAL, KEYSTORE_LABEL, KEYSTORE_FINGERPRINT, KEYSTORE_DERIVATION, KEYSTORE_XPUB, GAP_LIMIT, BIRTH_DATE;
} }
} }

View file

@ -3,7 +3,9 @@ package com.sparrowwallet.sparrow.event;
import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.KeyPurpose;
import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.drongo.wallet.WalletNode; import com.sparrowwallet.drongo.wallet.WalletNode;
import com.sparrowwallet.sparrow.io.Storage;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -12,13 +14,19 @@ import java.util.stream.Collectors;
* *
*/ */
public class WalletHistoryChangedEvent extends WalletChangedEvent { public class WalletHistoryChangedEvent extends WalletChangedEvent {
private final Storage storage;
private final List<WalletNode> historyChangedNodes; private final List<WalletNode> historyChangedNodes;
public WalletHistoryChangedEvent(Wallet wallet, List<WalletNode> historyChangedNodes) { public WalletHistoryChangedEvent(Wallet wallet, Storage storage, List<WalletNode> historyChangedNodes) {
super(wallet); super(wallet);
this.storage = storage;
this.historyChangedNodes = historyChangedNodes; this.historyChangedNodes = historyChangedNodes;
} }
public File getWalletFile() {
return storage.getWalletFile();
}
public List<WalletNode> getHistoryChangedNodes() { public List<WalletNode> getHistoryChangedNodes() {
return historyChangedNodes; return historyChangedNodes;
} }

View file

@ -4,27 +4,27 @@ import com.sparrowwallet.drongo.wallet.Wallet;
public class WalletHistoryStatusEvent { public class WalletHistoryStatusEvent {
private final Wallet wallet; private final Wallet wallet;
private final boolean loaded; private final boolean loading;
private final String statusMessage; private final String statusMessage;
private final String errorMessage; private final String errorMessage;
public WalletHistoryStatusEvent(Wallet wallet, boolean loaded) { public WalletHistoryStatusEvent(Wallet wallet, boolean loading) {
this.wallet = wallet; this.wallet = wallet;
this.loaded = loaded; this.loading = loading;
this.statusMessage = null; this.statusMessage = null;
this.errorMessage = null; this.errorMessage = null;
} }
public WalletHistoryStatusEvent(Wallet wallet,boolean loaded, String statusMessage) { public WalletHistoryStatusEvent(Wallet wallet, boolean loading, String statusMessage) {
this.wallet = wallet; this.wallet = wallet;
this.loaded = false; this.loading = loading;
this.statusMessage = statusMessage; this.statusMessage = statusMessage;
this.errorMessage = null; this.errorMessage = null;
} }
public WalletHistoryStatusEvent(Wallet wallet,String errorMessage) { public WalletHistoryStatusEvent(Wallet wallet, String errorMessage) {
this.wallet = wallet; this.wallet = wallet;
this.loaded = false; this.loading = true;
this.statusMessage = null; this.statusMessage = null;
this.errorMessage = errorMessage; this.errorMessage = errorMessage;
} }
@ -34,11 +34,7 @@ public class WalletHistoryStatusEvent {
} }
public boolean isLoading() { public boolean isLoading() {
return !loaded; return loading;
}
public boolean isLoaded() {
return loaded;
} }
public String getStatusMessage() { public String getStatusMessage() {

View file

@ -0,0 +1,22 @@
package com.sparrowwallet.sparrow.event;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.io.Storage;
public class WalletOpeningEvent {
private final Storage storage;
private final Wallet wallet;
public WalletOpeningEvent(Storage storage, Wallet wallet) {
this.storage = storage;
this.wallet = wallet;
}
public Storage getStorage() {
return storage;
}
public Wallet getWallet() {
return wallet;
}
}

View file

@ -31,6 +31,7 @@ public class FontAwesome5 extends GlyphFont {
EYE('\uf06e'), EYE('\uf06e'),
HAND_HOLDING('\uf4bd'), HAND_HOLDING('\uf4bd'),
HAND_HOLDING_MEDICAL('\ue05c'), HAND_HOLDING_MEDICAL('\ue05c'),
HISTORY('\uf1da'),
KEY('\uf084'), KEY('\uf084'),
LAPTOP('\uf109'), LAPTOP('\uf109'),
LOCK('\uf023'), LOCK('\uf023'),

View file

@ -48,8 +48,8 @@ public class ColdcardMultisig implements WalletImport, KeystoreFileImport, Walle
keystore.setKeyDerivation(new KeyDerivation(cck.xfp, cck.p2sh_deriv)); keystore.setKeyDerivation(new KeyDerivation(cck.xfp, cck.p2sh_deriv));
keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(cck.p2sh)); keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(cck.p2sh));
} else if(scriptType.equals(ScriptType.P2SH_P2WSH)) { } else if(scriptType.equals(ScriptType.P2SH_P2WSH)) {
keystore.setKeyDerivation(new KeyDerivation(cck.xfp, cck.p2wsh_p2sh_deriv)); keystore.setKeyDerivation(new KeyDerivation(cck.xfp, cck.p2wsh_p2sh_deriv != null ? cck.p2wsh_p2sh_deriv : cck.p2sh_p2wsh_deriv));
keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(cck.p2wsh_p2sh)); keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(cck.p2wsh_p2sh != null ? cck.p2wsh_p2sh : cck.p2sh_p2wsh));
} else if(scriptType.equals(ScriptType.P2WSH)) { } else if(scriptType.equals(ScriptType.P2WSH)) {
keystore.setKeyDerivation(new KeyDerivation(cck.xfp, cck.p2wsh_deriv)); keystore.setKeyDerivation(new KeyDerivation(cck.xfp, cck.p2wsh_deriv));
keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(cck.p2wsh)); keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(cck.p2wsh));
@ -65,6 +65,8 @@ public class ColdcardMultisig implements WalletImport, KeystoreFileImport, Walle
public String p2sh; public String p2sh;
public String p2wsh_p2sh_deriv; public String p2wsh_p2sh_deriv;
public String p2wsh_p2sh; public String p2wsh_p2sh;
public String p2sh_p2wsh_deriv;
public String p2sh_p2wsh;
public String p2wsh_deriv; public String p2wsh_deriv;
public String p2wsh; public String p2wsh;
public String xpub; public String xpub;

View file

@ -66,7 +66,7 @@ public class ColdcardSinglesig implements KeystoreFileImport, WalletImport {
ColdcardKeystore ck = gson.fromJson(map.get(key), ColdcardKeystore.class); ColdcardKeystore ck = gson.fromJson(map.get(key), ColdcardKeystore.class);
if(ck.name != null) { if(ck.name != null) {
ScriptType ckScriptType = ScriptType.valueOf(ck.name.replace("p2wpkh-p2sh", "p2sh_p2wpkh").toUpperCase()); ScriptType ckScriptType = ScriptType.valueOf(ck.name.replace("p2wpkh-p2sh", "p2sh_p2wpkh").replace("p2sh-p2wpkh", "p2sh_p2wpkh").toUpperCase());
if(ckScriptType.equals(scriptType)) { if(ckScriptType.equals(scriptType)) {
Keystore keystore = new Keystore(); Keystore keystore = new Keystore();
keystore.setLabel(getName()); keystore.setLabel(getName());

View file

@ -4,8 +4,10 @@ import com.google.gson.*;
import com.sparrowwallet.drongo.BitcoinUnit; import com.sparrowwallet.drongo.BitcoinUnit;
import com.sparrowwallet.sparrow.Mode; import com.sparrowwallet.sparrow.Mode;
import com.sparrowwallet.sparrow.Theme; import com.sparrowwallet.sparrow.Theme;
import com.sparrowwallet.sparrow.net.CoreAuthType;
import com.sparrowwallet.sparrow.net.ExchangeSource; import com.sparrowwallet.sparrow.net.ExchangeSource;
import com.sparrowwallet.sparrow.net.FeeRatesSource; import com.sparrowwallet.sparrow.net.FeeRatesSource;
import com.sparrowwallet.sparrow.net.ServerType;
import com.sparrowwallet.sparrow.wallet.FeeRatesSelection; import com.sparrowwallet.sparrow.wallet.FeeRatesSelection;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -37,6 +39,12 @@ public class Config {
private List<File> recentWalletFiles; private List<File> recentWalletFiles;
private Integer keyDerivationPeriod; private Integer keyDerivationPeriod;
private File hwi; private File hwi;
private ServerType serverType;
private String coreServer;
private CoreAuthType coreAuthType;
private File coreDataDir;
private String coreAuth;
private String coreWallet;
private String electrumServer; private String electrumServer;
private File electrumServerCert; private File electrumServerCert;
private boolean useProxy; private boolean useProxy;
@ -241,6 +249,64 @@ public class Config {
flush(); flush();
} }
public ServerType getServerType() {
return serverType;
}
public void setServerType(ServerType serverType) {
this.serverType = serverType;
flush();
}
public String getServerAddress() {
return getServerType() == ServerType.BITCOIN_CORE ? getCoreServer() : getElectrumServer();
}
public String getCoreServer() {
return coreServer;
}
public void setCoreServer(String coreServer) {
this.coreServer = coreServer;
flush();
}
public CoreAuthType getCoreAuthType() {
return coreAuthType;
}
public void setCoreAuthType(CoreAuthType coreAuthType) {
this.coreAuthType = coreAuthType;
flush();
}
public File getCoreDataDir() {
return coreDataDir;
}
public void setCoreDataDir(File coreDataDir) {
this.coreDataDir = coreDataDir;
flush();
}
public String getCoreAuth() {
return coreAuth;
}
public void setCoreAuth(String coreAuth) {
this.coreAuth = coreAuth;
flush();
}
public String getCoreWallet() {
return coreWallet;
}
public void setCoreWallet(String coreWallet) {
this.coreWallet = (coreWallet == null || coreWallet.isEmpty() ? null : coreWallet);
flush();
}
public String getElectrumServer() { public String getElectrumServer() {
return electrumServer; return electrumServer;
} }

View file

@ -74,7 +74,7 @@ public class BatchedElectrumServerRpc implements ElectrumServerRpc {
public Map<String, ScriptHashTx[]> getScriptHashHistory(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes, boolean failOnError) { public Map<String, ScriptHashTx[]> getScriptHashHistory(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes, boolean failOnError) {
JsonRpcClient client = new JsonRpcClient(transport); JsonRpcClient client = new JsonRpcClient(transport);
BatchRequestBuilder<String, ScriptHashTx[]> batchRequest = client.createBatchRequest().keysType(String.class).returnType(ScriptHashTx[].class); BatchRequestBuilder<String, ScriptHashTx[]> batchRequest = client.createBatchRequest().keysType(String.class).returnType(ScriptHashTx[].class);
EventManager.get().post(new WalletHistoryStatusEvent(wallet, false, "Loading transactions")); EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Loading transactions"));
for(String path : pathScriptHashes.keySet()) { for(String path : pathScriptHashes.keySet()) {
batchRequest.add(path, "blockchain.scripthash.get_history", pathScriptHashes.get(path)); batchRequest.add(path, "blockchain.scripthash.get_history", pathScriptHashes.get(path));
@ -130,7 +130,7 @@ public class BatchedElectrumServerRpc implements ElectrumServerRpc {
public Map<String, String> subscribeScriptHashes(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes) { public Map<String, String> subscribeScriptHashes(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes) {
JsonRpcClient client = new JsonRpcClient(transport); JsonRpcClient client = new JsonRpcClient(transport);
BatchRequestBuilder<String, String> batchRequest = client.createBatchRequest().keysType(String.class).returnType(String.class); BatchRequestBuilder<String, String> batchRequest = client.createBatchRequest().keysType(String.class).returnType(String.class);
EventManager.get().post(new WalletHistoryStatusEvent(wallet, false, "Finding transactions")); EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Finding transactions"));
for(String path : pathScriptHashes.keySet()) { for(String path : pathScriptHashes.keySet()) {
batchRequest.add(path, "blockchain.scripthash.subscribe", pathScriptHashes.get(path)); batchRequest.add(path, "blockchain.scripthash.subscribe", pathScriptHashes.get(path));
@ -151,7 +151,7 @@ public class BatchedElectrumServerRpc implements ElectrumServerRpc {
public Map<Integer, String> getBlockHeaders(Transport transport, Wallet wallet, Set<Integer> blockHeights) { public Map<Integer, String> getBlockHeaders(Transport transport, Wallet wallet, Set<Integer> blockHeights) {
JsonRpcClient client = new JsonRpcClient(transport); JsonRpcClient client = new JsonRpcClient(transport);
BatchRequestBuilder<Integer, String> batchRequest = client.createBatchRequest().keysType(Integer.class).returnType(String.class); BatchRequestBuilder<Integer, String> batchRequest = client.createBatchRequest().keysType(Integer.class).returnType(String.class);
EventManager.get().post(new WalletHistoryStatusEvent(wallet, false, "Retrieving blocks")); EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Retrieving blocks"));
for(Integer height : blockHeights) { for(Integer height : blockHeights) {
batchRequest.add(height, "blockchain.block.header", height); batchRequest.add(height, "blockchain.block.header", height);
@ -171,7 +171,7 @@ public class BatchedElectrumServerRpc implements ElectrumServerRpc {
public Map<String, String> getTransactions(Transport transport, Wallet wallet, Set<String> txids) { public Map<String, String> getTransactions(Transport transport, Wallet wallet, Set<String> txids) {
JsonRpcClient client = new JsonRpcClient(transport); JsonRpcClient client = new JsonRpcClient(transport);
BatchRequestBuilder<String, String> batchRequest = client.createBatchRequest().keysType(String.class).returnType(String.class); BatchRequestBuilder<String, String> batchRequest = client.createBatchRequest().keysType(String.class).returnType(String.class);
EventManager.get().post(new WalletHistoryStatusEvent(wallet, false, "Retrieving transactions")); EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Retrieving transactions"));
for(String txid : txids) { for(String txid : txids) {
batchRequest.add(txid, "blockchain.transaction.get", txid); batchRequest.add(txid, "blockchain.transaction.get", txid);
@ -204,7 +204,8 @@ public class BatchedElectrumServerRpc implements ElectrumServerRpc {
} }
try { try {
return new RetryLogic<Map<String, VerboseTransaction>>(MAX_RETRIES, RETRY_DELAY, IllegalStateException.class).getResult(batchRequest::execute); //The server may return an error if the transaction has not yet been broadcasted - this is a valid state so only try once
return new RetryLogic<Map<String, VerboseTransaction>>(1, RETRY_DELAY, IllegalStateException.class).getResult(batchRequest::execute);
} catch(JsonRpcBatchException e) { } catch(JsonRpcBatchException e) {
log.warn("Some errors retrieving transactions: " + e.getErrors()); log.warn("Some errors retrieving transactions: " + e.getErrors());
return (Map<String, VerboseTransaction>)e.getSuccesses(); return (Map<String, VerboseTransaction>)e.getSuccesses();

View file

@ -0,0 +1,309 @@
package com.sparrowwallet.sparrow.net;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.sparrowwallet.drongo.KeyPurpose;
import com.sparrowwallet.drongo.Network;
import com.sparrowwallet.drongo.OutputDescriptor;
import com.sparrowwallet.drongo.wallet.BlockTransactionHash;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.event.*;
import com.sparrowwallet.sparrow.io.Config;
import dev.bwt.libbwt.daemon.CallbackNotifier;
import dev.bwt.libbwt.daemon.NativeBwtDaemon;
import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
public class Bwt {
private static final Logger log = LoggerFactory.getLogger(Bwt.class);
private static final int IMPORT_BATCH_SIZE = 350;
private Long shutdownPtr;
private boolean terminating;
private boolean ready;
static {
try {
org.controlsfx.tools.Platform platform = org.controlsfx.tools.Platform.getCurrent();
if(platform == org.controlsfx.tools.Platform.OSX) {
NativeUtils.loadLibraryFromJar("/native/osx/x64/libbwt_jni.dylib");
} else if(platform == org.controlsfx.tools.Platform.WINDOWS) {
NativeUtils.loadLibraryFromJar("/native/windows/x64/bwt_jni.dll");
} else {
NativeUtils.loadLibraryFromJar("/native/linux/x64/libbwt_jni.so");
}
} catch(IOException e) {
log.error("Error loading bwt library", e);
}
}
private void start(CallbackNotifier callback) {
start(Collections.emptyList(), null, null, null, callback);
}
private void start(Collection<Wallet> wallets, CallbackNotifier callback) {
List<String> outputDescriptors = new ArrayList<>();
for(Wallet wallet : wallets) {
OutputDescriptor receiveOutputDescriptor = OutputDescriptor.getOutputDescriptor(wallet, KeyPurpose.RECEIVE);
outputDescriptors.add(receiveOutputDescriptor.toString(false, false));
OutputDescriptor changeOutputDescriptor = OutputDescriptor.getOutputDescriptor(wallet, KeyPurpose.CHANGE);
outputDescriptors.add(changeOutputDescriptor.toString(false, false));
}
int rescanSince = wallets.stream().filter(wallet -> wallet.getBirthDate() != null).mapToInt(wallet -> (int)(wallet.getBirthDate().getTime() / 1000)).min().orElse(-1);
int gapLimit = wallets.stream().filter(wallet -> wallet.getGapLimit() > 0).mapToInt(Wallet::getGapLimit).max().orElse(Wallet.DEFAULT_LOOKAHEAD);
boolean forceRescan = false;
for(Wallet wallet :wallets) {
Date txBirthDate = wallet.getTransactions().values().stream().map(BlockTransactionHash::getDate).filter(Objects::nonNull).min(Date::compareTo).orElse(null);
if((wallet.getBirthDate() != null && txBirthDate != null && wallet.getBirthDate().before(txBirthDate)) || (txBirthDate == null && wallet.getStoredBlockHeight() == 0)) {
forceRescan = true;
}
}
start(outputDescriptors, rescanSince, forceRescan, gapLimit, callback);
}
/**
* Start the bwt daemon with the provided wallets
* Blocks until the daemon is shut down.
*
* @param outputDescriptors descriptors of keys to add to Bitcoin Core
* @param rescanSince seconds since epoch to start scanning keys
* @param gapLimit desired gap limit beyond last used address
* @param callback object receiving notifications
*/
private void start(List<String> outputDescriptors, Integer rescanSince, Boolean forceRescan, Integer gapLimit, CallbackNotifier callback) {
BwtConfig bwtConfig = new BwtConfig();
bwtConfig.network = Network.get() == Network.MAINNET ? "bitcoin" : Network.get().getName();
if(!outputDescriptors.isEmpty()) {
bwtConfig.descriptors = outputDescriptors;
bwtConfig.rescanSince = (rescanSince == null || rescanSince < 0 ? "now" : rescanSince);
bwtConfig.forceRescan = forceRescan;
bwtConfig.gapLimit = gapLimit;
} else {
bwtConfig.requireAddresses = false;
}
bwtConfig.verbose = log.isDebugEnabled() ? 2 : 0;
if(!log.isInfoEnabled()) {
bwtConfig.setupLogger = false;
}
bwtConfig.electrumAddr = "127.0.0.1:0";
bwtConfig.electrumSkipMerkle = true;
Config config = Config.get();
bwtConfig.bitcoindUrl = config.getCoreServer();
if(config.getCoreAuthType() == CoreAuthType.COOKIE) {
bwtConfig.bitcoindDir = config.getCoreDataDir().getAbsolutePath() + "/";
} else {
bwtConfig.bitcoindAuth = config.getCoreAuth();
}
if(config.getCoreWallet() != null && !config.getCoreWallet().isEmpty()) {
bwtConfig.bitcoindWallet = config.getCoreWallet();
}
Gson gson = new Gson();
String jsonConfig = gson.toJson(bwtConfig);
log.debug("Configuring bwt: " + jsonConfig);
NativeBwtDaemon.start(jsonConfig, callback);
}
/**
* Shut down the BWT daemon
*
*/
private void shutdown() {
if(shutdownPtr == null) {
terminating = true;
return;
}
NativeBwtDaemon.shutdown(shutdownPtr);
this.terminating = false;
this.ready = false;
this.shutdownPtr = null;
Platform.runLater(() -> EventManager.get().post(new BwtShutdownEvent()));
}
public boolean isRunning() {
return shutdownPtr != null;
}
public boolean isReady() {
return ready;
}
public boolean isTerminating() {
return terminating;
}
public ConnectionService getConnectionService(Collection<Wallet> wallets) {
return wallets != null ? new ConnectionService(wallets) : new ConnectionService();
}
public DisconnectionService getDisconnectionService() {
return new DisconnectionService();
}
private static class BwtConfig {
@SerializedName("network")
public String network;
@SerializedName("bitcoind_url")
public String bitcoindUrl;
@SerializedName("bitcoind_auth")
public String bitcoindAuth;
@SerializedName("bitcoind_dir")
public String bitcoindDir;
@SerializedName("bitcoind_cookie")
public String bitcoindCookie;
@SerializedName("bitcoind_wallet")
public String bitcoindWallet;
@SerializedName("descriptors")
public List<String> descriptors;
@SerializedName("xpubs")
public String xpubs;
@SerializedName("rescan_since")
public Object rescanSince;
@SerializedName("force_rescan")
public Boolean forceRescan;
@SerializedName("gap_limit")
public Integer gapLimit;
@SerializedName("initial_import_size")
public Integer initialImportSize;
@SerializedName("verbose")
public Integer verbose;
@SerializedName("electrum_addr")
public String electrumAddr;
@SerializedName("electrum_skip_merkle")
public Boolean electrumSkipMerkle;
@SerializedName("require_addresses")
public Boolean requireAddresses;
@SerializedName("setup_logger")
public Boolean setupLogger;
@SerializedName("http_addr")
public String httpAddr;
}
public final class ConnectionService extends Service<Void> {
private final Collection<Wallet> wallets;
public ConnectionService() {
this.wallets = null;
}
public ConnectionService(Collection<Wallet> wallets) {
this.wallets = wallets;
}
@Override
protected Task<Void> createTask() {
return new Task<>() {
protected Void call() {
CallbackNotifier notifier = new CallbackNotifier() {
@Override
public void onBooting(long shutdownPtr) {
log.debug("Booting bwt");
Bwt.this.shutdownPtr = shutdownPtr;
if(terminating) {
Bwt.this.shutdown();
terminating = false;
} else {
Platform.runLater(() -> EventManager.get().post(new BwtBootStatusEvent("Connecting to Bitcoin Core node at " + Config.get().getCoreServer() + "...")));
}
}
@Override
public void onSyncProgress(float progress, int tip) {
int percent = (int) (progress * 100.0);
Date tipDate = new Date((long)tip * 1000);
log.debug("Syncing " + percent + "%");
if(!terminating) {
Platform.runLater(() -> EventManager.get().post(new BwtSyncStatusEvent("Syncing" + (percent < 100 ? " (" + percent + "%)" : ""), percent, tipDate)));
}
}
@Override
public void onScanProgress(float progress, int eta) {
int percent = (int) (progress * 100.0);
Date date = new Date((long) eta * 1000);
log.debug("Scanning " + percent + "%");
if(!terminating) {
Platform.runLater(() -> EventManager.get().post(new BwtScanStatusEvent("Scanning" + (percent < 100 ? " (" + percent + "%)" : ""), percent, date)));
}
}
@Override
public void onElectrumReady(String addr) {
log.debug("Electrum ready");
if(!terminating) {
Platform.runLater(() -> EventManager.get().post(new BwtElectrumReadyStatusEvent("Electrum server ready", addr)));
}
}
@Override
public void onHttpReady(String addr) {
log.info("http ready at " + addr);
}
@Override
public void onReady() {
log.debug("Bwt ready");
ready = true;
if(!terminating) {
Platform.runLater(() -> EventManager.get().post(new BwtReadyStatusEvent("Server ready")));
}
}
};
if(wallets == null) {
Bwt.this.start(notifier);
} else {
Bwt.this.start(wallets, notifier);
}
return null;
}
};
}
}
public final class DisconnectionService extends Service<Void> {
@Override
protected Task<Void> createTask() {
return new Task<>() {
protected Void call() {
Bwt.this.shutdown();
return null;
}
};
}
}
}

View file

@ -0,0 +1,5 @@
package com.sparrowwallet.sparrow.net;
public enum CoreAuthType {
COOKIE, USERPASS;
}

View file

@ -9,11 +9,12 @@ import com.sparrowwallet.drongo.Network;
import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.Utils;
import com.sparrowwallet.drongo.protocol.*; import com.sparrowwallet.drongo.protocol.*;
import com.sparrowwallet.drongo.wallet.*; import com.sparrowwallet.drongo.wallet.*;
import com.sparrowwallet.sparrow.event.ConnectionEvent; import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.event.FeeRatesUpdatedEvent; import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.event.TorStatusEvent; import com.sparrowwallet.sparrow.event.*;
import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.io.Config;
import com.sparrowwallet.sparrow.wallet.SendController; import com.sparrowwallet.sparrow.wallet.SendController;
import javafx.application.Platform;
import javafx.beans.property.IntegerProperty; import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
@ -26,6 +27,8 @@ import org.slf4j.LoggerFactory;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ElectrumServer { public class ElectrumServer {
@ -41,24 +44,37 @@ public class ElectrumServer {
private static ElectrumServerRpc electrumServerRpc = new SimpleElectrumServerRpc(); private static ElectrumServerRpc electrumServerRpc = new SimpleElectrumServerRpc();
private static String bwtElectrumServer;
private static synchronized Transport getTransport() throws ServerException { private static synchronized Transport getTransport() throws ServerException {
if(transport == null) { if(transport == null) {
try { try {
String electrumServer = Config.get().getElectrumServer(); String electrumServer = null;
File electrumServerCert = Config.get().getElectrumServerCert(); File electrumServerCert = null;
String proxyServer = Config.get().getProxyServer(); String proxyServer = null;
if(Config.get().getServerType() == ServerType.BITCOIN_CORE) {
if(bwtElectrumServer == null) {
throw new ServerConfigException("Could not connect to Bitcoin Core RPC");
}
electrumServer = bwtElectrumServer;
} else if(Config.get().getServerType() == ServerType.ELECTRUM_SERVER) {
electrumServer = Config.get().getElectrumServer();
electrumServerCert = Config.get().getElectrumServerCert();
proxyServer = Config.get().getProxyServer();
}
if(electrumServer == null) { if(electrumServer == null) {
throw new ServerException("Electrum server URL not specified"); throw new ServerConfigException("Electrum server URL not specified");
} }
if(electrumServerCert != null && !electrumServerCert.exists()) { if(electrumServerCert != null && !electrumServerCert.exists()) {
throw new ServerException("Electrum server certificate file not found"); throw new ServerConfigException("Electrum server certificate file not found");
} }
Protocol protocol = Protocol.getProtocol(electrumServer); Protocol protocol = Protocol.getProtocol(electrumServer);
if(protocol == null) { if(protocol == null) {
throw new ServerException("Electrum server URL must start with " + Protocol.TCP.toUrlString() + " or " + Protocol.SSL.toUrlString()); throw new ServerConfigException("Electrum server URL must start with " + Protocol.TCP.toUrlString() + " or " + Protocol.SSL.toUrlString());
} }
HostAndPort server = protocol.getServerHostAndPort(electrumServer); HostAndPort server = protocol.getServerHostAndPort(electrumServer);
@ -78,7 +94,7 @@ public class ElectrumServer {
} }
} }
} catch (Exception e) { } catch (Exception e) {
throw new ServerException(e); throw new ServerConfigException(e);
} }
} }
@ -760,7 +776,10 @@ public class ElectrumServer {
private boolean firstCall = true; private boolean firstCall = true;
private Thread reader; private Thread reader;
private long feeRatesRetrievedAt; private long feeRatesRetrievedAt;
private StringProperty statusProperty = new SimpleStringProperty(); private final Bwt bwt = new Bwt();
private final ReentrantLock bwtStartLock = new ReentrantLock();
private final Condition bwtStartCondition = bwtStartLock.newCondition();
private final StringProperty statusProperty = new SimpleStringProperty();
public ConnectionService() { public ConnectionService() {
this(true); this(true);
@ -775,6 +794,37 @@ public class ElectrumServer {
return new Task<>() { return new Task<>() {
protected FeeRatesUpdatedEvent call() throws ServerException { protected FeeRatesUpdatedEvent call() throws ServerException {
ElectrumServer electrumServer = new ElectrumServer(); ElectrumServer electrumServer = new ElectrumServer();
if(Config.get().getServerType() == ServerType.BITCOIN_CORE) {
if(!bwt.isRunning()) {
Bwt.ConnectionService bwtConnectionService = bwt.getConnectionService(subscribe ? AppServices.get().getOpenWallets().keySet() : null);
bwtConnectionService.setOnFailed(workerStateEvent -> {
log.error("Failed to start BWT", workerStateEvent.getSource().getException());
try {
bwtStartLock.lock();
bwtStartCondition.signal();
} finally {
bwtStartLock.unlock();
}
});
Platform.runLater(bwtConnectionService::start);
try {
bwtStartLock.lock();
bwtStartCondition.await();
if(!bwt.isRunning()) {
throw new ServerException("Check if Bitcoin Core is running, and the authentication details are correct.");
}
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
return null;
} finally {
bwtStartLock.unlock();
}
}
}
if(firstCall) { if(firstCall) {
electrumServer.connect(); electrumServer.connect();
@ -839,16 +889,26 @@ public class ElectrumServer {
public void resetConnection() { public void resetConnection() {
try { try {
closeActiveConnection(); closeActiveConnection();
shutdown();
firstCall = true; firstCall = true;
} catch (ServerException e) { } catch (ServerException e) {
log.error("Error closing connection during connection reset", e); log.error("Error closing connection during connection reset", e);
} }
} }
public boolean isConnecting() {
return isRunning() && Config.get().getServerType() == ServerType.BITCOIN_CORE && bwt.isRunning() && !bwt.isReady();
}
public boolean isConnected() {
return isRunning() && (Config.get().getServerType() != ServerType.BITCOIN_CORE || (bwt.isRunning() && bwt.isReady()));
}
@Override @Override
public boolean cancel() { public boolean cancel() {
try { try {
closeActiveConnection(); closeActiveConnection();
shutdown();
} catch (ServerException e) { } catch (ServerException e) {
log.error("Error closing connection", e); log.error("Error closing connection", e);
} }
@ -856,6 +916,21 @@ public class ElectrumServer {
return super.cancel(); return super.cancel();
} }
private void shutdown() {
if(Config.get().getServerType() == ServerType.BITCOIN_CORE && bwt.isRunning()) {
Bwt.DisconnectionService disconnectionService = bwt.getDisconnectionService();
disconnectionService.setOnSucceeded(workerStateEvent -> {
ElectrumServer.bwtElectrumServer = null;
});
disconnectionService.setOnFailed(workerStateEvent -> {
log.error("Failed to stop BWT", workerStateEvent.getSource().getException());
});
Platform.runLater(disconnectionService::start);
} else {
Platform.runLater(() -> EventManager.get().post(new DisconnectionEvent()));
}
}
@Override @Override
public void reset() { public void reset() {
super.reset(); super.reset();
@ -872,6 +947,35 @@ public class ElectrumServer {
statusProperty.set(event.getStatus()); statusProperty.set(event.getStatus());
} }
@Subscribe
public void bwtElectrumReadyStatus(BwtElectrumReadyStatusEvent event) {
if(this.isRunning()) {
ElectrumServer.bwtElectrumServer = Protocol.TCP.toUrlString(HostAndPort.fromString(event.getElectrumAddr()));
}
}
@Subscribe
public void bwtReadyStatus(BwtReadyStatusEvent event) {
if(this.isRunning()) {
try {
bwtStartLock.lock();
bwtStartCondition.signal();
} finally {
bwtStartLock.unlock();
}
}
}
@Subscribe
public void bwtShutdown(BwtShutdownEvent event) {
try {
bwtStartLock.lock();
bwtStartCondition.signal();
} finally {
bwtStartLock.unlock();
}
}
public StringProperty statusProperty() { public StringProperty statusProperty() {
return statusProperty; return statusProperty;
} }

View file

@ -18,7 +18,7 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
public enum FeeRatesSource { public enum FeeRatesSource {
ELECTRUM_SERVER("Electrum Server") { ELECTRUM_SERVER("Server") {
@Override @Override
public Map<Integer, Double> getBlockTargetFeeRates(Map<Integer, Double> defaultblockTargetFeeRates) { public Map<Integer, Double> getBlockTargetFeeRates(Map<Integer, Double> defaultblockTargetFeeRates) {
return Collections.emptyMap(); return Collections.emptyMap();

View file

@ -0,0 +1,119 @@
package com.sparrowwallet.sparrow.net;
import java.io.*;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.ProviderNotFoundException;
import java.nio.file.StandardCopyOption;
/**
* A simple library class which helps with loading dynamic libraries stored in the
* JAR archive. These libraries usually contain implementation of some methods in
* native code (using JNI - Java Native Interface).
*
* @see <a href="http://adamheinrich.com/blog/2012/how-to-load-native-jni-library-from-jar">http://adamheinrich.com/blog/2012/how-to-load-native-jni-library-from-jar</a>
* @see <a href="https://github.com/adamheinrich/native-utils">https://github.com/adamheinrich/native-utils</a>
*
*/
public class NativeUtils {
/**
* The minimum length a prefix for a file has to have according to {@link File#createTempFile(String, String)}}.
*/
private static final int MIN_PREFIX_LENGTH = 3;
public static final String NATIVE_FOLDER_PATH_PREFIX = "nativeutils";
/**
* Temporary directory which will contain the DLLs.
*/
private static File temporaryDir;
/**
* Private constructor - this class will never be instanced
*/
private NativeUtils() {
}
/**
* Loads library from current JAR archive
*
* The file from JAR is copied into system temporary directory and then loaded. The temporary file is deleted after
* exiting.
* Method uses String as filename because the pathname is "abstract", not system-dependent.
*
* @param path The path of file inside JAR as absolute path (beginning with '/'), e.g. /package/File.ext
* @throws IOException If temporary file creation or read/write operation fails
* @throws IllegalArgumentException If source file (param path) does not exist
* @throws IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters
* (restriction of {@link File#createTempFile(java.lang.String, java.lang.String)}).
* @throws FileNotFoundException If the file could not be found inside the JAR.
*/
public static void loadLibraryFromJar(String path) throws IOException {
if (null == path || !path.startsWith("/")) {
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
}
// Obtain filename from path
String[] parts = path.split("/");
String filename = (parts.length > 1) ? parts[parts.length - 1] : null;
// Check if the filename is okay
if (filename == null || filename.length() < MIN_PREFIX_LENGTH) {
throw new IllegalArgumentException("The filename has to be at least 3 characters long.");
}
// Prepare temporary file
if (temporaryDir == null) {
temporaryDir = createTempDirectory(NATIVE_FOLDER_PATH_PREFIX);
temporaryDir.deleteOnExit();
}
File temp = new File(temporaryDir, filename);
try (InputStream is = NativeUtils.class.getResourceAsStream(path)) {
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
temp.delete();
throw e;
} catch (NullPointerException e) {
temp.delete();
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
}
try {
System.load(temp.getAbsolutePath());
} finally {
if (isPosixCompliant()) {
// Assume POSIX compliant file system, can be deleted after loading
temp.delete();
} else {
// Assume non-POSIX, and don't delete until last file descriptor closed
temp.deleteOnExit();
}
}
}
private static boolean isPosixCompliant() {
try {
return FileSystems.getDefault()
.supportedFileAttributeViews()
.contains("posix");
} catch (FileSystemNotFoundException
| ProviderNotFoundException
| SecurityException e) {
return false;
}
}
private static File createTempDirectory(String prefix) throws IOException {
String tempDir = System.getProperty("java.io.tmpdir");
File generatedDir = new File(tempDir, prefix + System.nanoTime());
if (!generatedDir.mkdir())
throw new IOException("Failed to create temp directory " + generatedDir.getName());
return generatedDir;
}
}

View file

@ -64,6 +64,27 @@ public enum Protocol {
public Transport getTransport(HostAndPort server, File serverCert, HostAndPort proxy) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { public Transport getTransport(HostAndPort server, File serverCert, HostAndPort proxy) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
return new ProxyTcpOverTlsTransport(server, serverCert, proxy); return new ProxyTcpOverTlsTransport(server, serverCert, proxy);
} }
},
HTTP {
@Override
public Transport getTransport(HostAndPort server) throws KeyManagementException, NoSuchAlgorithmException {
throw new UnsupportedOperationException("No transport supported for HTTP");
}
@Override
public Transport getTransport(HostAndPort server, File serverCert) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
throw new UnsupportedOperationException("No transport supported for HTTP");
}
@Override
public Transport getTransport(HostAndPort server, HostAndPort proxy) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
throw new UnsupportedOperationException("No transport supported for HTTP");
}
@Override
public Transport getTransport(HostAndPort server, File serverCert, HostAndPort proxy) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
throw new UnsupportedOperationException("No transport supported for HTTP");
}
}; };
public abstract Transport getTransport(HostAndPort server) throws KeyManagementException, NoSuchAlgorithmException; public abstract Transport getTransport(HostAndPort server) throws KeyManagementException, NoSuchAlgorithmException;
@ -105,6 +126,9 @@ public enum Protocol {
if(url.startsWith("ssl://")) { if(url.startsWith("ssl://")) {
return SSL; return SSL;
} }
if(url.startsWith("http://")) {
return HTTP;
}
return null; return null;
} }

View file

@ -0,0 +1,18 @@
package com.sparrowwallet.sparrow.net;
public class ServerConfigException extends ServerException {
public ServerConfigException() {
}
public ServerConfigException(String message) {
super(message);
}
public ServerConfigException(Throwable cause) {
super(cause);
}
public ServerConfigException(String message, Throwable cause) {
super(message, cause);
}
}

View file

@ -0,0 +1,15 @@
package com.sparrowwallet.sparrow.net;
public enum ServerType {
BITCOIN_CORE("Bitcoin Core"), ELECTRUM_SERVER("Electrum Server");
private final String name;
ServerType(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View file

@ -75,7 +75,7 @@ public class SimpleElectrumServerRpc implements ElectrumServerRpc {
Map<String, ScriptHashTx[]> result = new LinkedHashMap<>(); Map<String, ScriptHashTx[]> result = new LinkedHashMap<>();
for(String path : pathScriptHashes.keySet()) { for(String path : pathScriptHashes.keySet()) {
EventManager.get().post(new WalletHistoryStatusEvent(wallet, false, "Loading transactions for " + path)); EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Loading transactions for " + path));
try { try {
ScriptHashTx[] scriptHashTxes = new RetryLogic<ScriptHashTx[]>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> ScriptHashTx[] scriptHashTxes = new RetryLogic<ScriptHashTx[]>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() ->
client.createRequest().returnAs(ScriptHashTx[].class).method("blockchain.scripthash.get_history").id(path + "-" + idCounter.incrementAndGet()).params(pathScriptHashes.get(path)).execute()); client.createRequest().returnAs(ScriptHashTx[].class).method("blockchain.scripthash.get_history").id(path + "-" + idCounter.incrementAndGet()).params(pathScriptHashes.get(path)).execute());
@ -120,7 +120,7 @@ public class SimpleElectrumServerRpc implements ElectrumServerRpc {
Map<String, String> result = new LinkedHashMap<>(); Map<String, String> result = new LinkedHashMap<>();
for(String path : pathScriptHashes.keySet()) { for(String path : pathScriptHashes.keySet()) {
EventManager.get().post(new WalletHistoryStatusEvent(wallet, false, "Finding transactions for " + path)); EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Finding transactions for " + path));
try { try {
String scriptHash = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> String scriptHash = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() ->
client.createRequest().returnAs(String.class).method("blockchain.scripthash.subscribe").id(path + "-" + idCounter.incrementAndGet()).params(pathScriptHashes.get(path)).executeNullable()); client.createRequest().returnAs(String.class).method("blockchain.scripthash.subscribe").id(path + "-" + idCounter.incrementAndGet()).params(pathScriptHashes.get(path)).executeNullable());
@ -140,7 +140,7 @@ public class SimpleElectrumServerRpc implements ElectrumServerRpc {
Map<Integer, String> result = new LinkedHashMap<>(); Map<Integer, String> result = new LinkedHashMap<>();
for(Integer blockHeight : blockHeights) { for(Integer blockHeight : blockHeights) {
EventManager.get().post(new WalletHistoryStatusEvent(wallet, false, "Retrieving block at height " + blockHeight)); EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Retrieving block at height " + blockHeight));
try { try {
String blockHeader = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> String blockHeader = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() ->
client.createRequest().returnAs(String.class).method("blockchain.block.header").id(idCounter.incrementAndGet()).params(blockHeight).execute()); client.createRequest().returnAs(String.class).method("blockchain.block.header").id(idCounter.incrementAndGet()).params(blockHeight).execute());
@ -161,7 +161,7 @@ public class SimpleElectrumServerRpc implements ElectrumServerRpc {
Map<String, String> result = new LinkedHashMap<>(); Map<String, String> result = new LinkedHashMap<>();
for(String txid : txids) { for(String txid : txids) {
EventManager.get().post(new WalletHistoryStatusEvent(wallet, false, "Retrieving transaction [" + txid.substring(0, 6) + "]")); EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Retrieving transaction [" + txid.substring(0, 6) + "]"));
try { try {
String rawTxHex = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> String rawTxHex = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() ->
client.createRequest().returnAs(String.class).method("blockchain.transaction.get").id(idCounter.incrementAndGet()).params(txid).execute()); client.createRequest().returnAs(String.class).method("blockchain.transaction.get").id(idCounter.incrementAndGet()).params(txid).execute());
@ -181,7 +181,8 @@ public class SimpleElectrumServerRpc implements ElectrumServerRpc {
Map<String, VerboseTransaction> result = new LinkedHashMap<>(); Map<String, VerboseTransaction> result = new LinkedHashMap<>();
for(String txid : txids) { for(String txid : txids) {
try { try {
VerboseTransaction verboseTransaction = new RetryLogic<VerboseTransaction>(MAX_RETRIES, RETRY_DELAY, IllegalStateException.class).getResult(() -> //The server may return an error if the transaction has not yet been broadcasted - this is a valid state so only try once
VerboseTransaction verboseTransaction = new RetryLogic<VerboseTransaction>(1, RETRY_DELAY, IllegalStateException.class).getResult(() ->
client.createRequest().returnAs(VerboseTransaction.class).method("blockchain.transaction.get").id(idCounter.incrementAndGet()).params(txid, true).execute()); client.createRequest().returnAs(VerboseTransaction.class).method("blockchain.transaction.get").id(idCounter.incrementAndGet()).params(txid, true).execute());
result.put(txid, verboseTransaction); result.put(txid, verboseTransaction);
} catch(Exception e) { } catch(Exception e) {
@ -213,7 +214,7 @@ public class SimpleElectrumServerRpc implements ElectrumServerRpc {
verboseTransaction.blockhash = Sha256Hash.ZERO_HASH.toString(); verboseTransaction.blockhash = Sha256Hash.ZERO_HASH.toString();
result.put(txid, verboseTransaction); result.put(txid, verboseTransaction);
} catch(Exception ex) { } catch(Exception ex) {
throw new ElectrumServerRpcException("Error retrieving transaction: ", ex); //ignore
} }
} }
} }

View file

@ -149,7 +149,7 @@ public class TcpTransport implements Transport, Closeable {
//Restore interrupt status and continue //Restore interrupt status and continue
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch(Exception e) { } catch(Exception e) {
log.debug("Connection error while reading", e); log.trace("Connection error while reading", e);
if(running) { if(running) {
lastException = e; lastException = e;
reading = false; reading = false;
@ -177,7 +177,7 @@ public class TcpTransport implements Transport, Closeable {
String response = in.readLine(); String response = in.readLine();
if(response == null) { if(response == null) {
throw new IOException("Could not connect to server at " + Config.get().getElectrumServer()); throw new IOException("Could not connect to server at " + Config.get().getServerAddress());
} }
return response; return response;

View file

@ -3,6 +3,8 @@ package com.sparrowwallet.sparrow.preferences;
import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.io.Config;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
@ -24,6 +26,10 @@ public class PreferencesController implements Initializable {
@FXML @FXML
private StackPane preferencesPane; private StackPane preferencesPane;
private final BooleanProperty closing = new SimpleBooleanProperty(false);
private final BooleanProperty reconnectOnClosing = new SimpleBooleanProperty(false);
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
@ -56,6 +62,18 @@ public class PreferencesController implements Initializable {
} }
} }
BooleanProperty closingProperty() {
return closing;
}
public boolean isReconnectOnClosing() {
return reconnectOnClosing.get();
}
public BooleanProperty reconnectOnClosingProperty() {
return reconnectOnClosing;
}
FXMLLoader setPreferencePane(String fxmlName) { FXMLLoader setPreferencePane(String fxmlName) {
preferencesPane.getChildren().removeAll(preferencesPane.getChildren()); preferencesPane.getChildren().removeAll(preferencesPane.getChildren());

View file

@ -15,8 +15,6 @@ import org.controlsfx.tools.Borders;
import java.io.IOException; import java.io.IOException;
public class PreferencesDialog extends Dialog<Boolean> { public class PreferencesDialog extends Dialog<Boolean> {
private final boolean existingConnection;
public PreferencesDialog() { public PreferencesDialog() {
this(null); this(null);
} }
@ -49,11 +47,12 @@ public class PreferencesDialog extends Dialog<Boolean> {
} }
dialogPane.setPrefWidth(650); dialogPane.setPrefWidth(650);
dialogPane.setPrefHeight(550); dialogPane.setPrefHeight(600);
existingConnection = ElectrumServer.isConnected(); preferencesController.reconnectOnClosingProperty().set(AppServices.isConnecting() || AppServices.isConnected());
setOnCloseRequest(event -> { setOnCloseRequest(event -> {
if(existingConnection && !ElectrumServer.isConnected()) { preferencesController.closingProperty().set(true);
if(preferencesController.isReconnectOnClosing() && !(AppServices.isConnecting() || AppServices.isConnected())) {
EventManager.get().post(new RequestConnectEvent()); EventManager.get().post(new RequestConnectEvent());
} }
}); });

View file

@ -1,24 +1,24 @@
package com.sparrowwallet.sparrow.preferences; package com.sparrowwallet.sparrow.preferences;
import com.google.common.eventbus.Subscribe;
import com.google.common.net.HostAndPort; 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.EventManager;
import com.sparrowwallet.sparrow.Mode;
import com.sparrowwallet.sparrow.control.TextFieldValidator; import com.sparrowwallet.sparrow.control.TextFieldValidator;
import com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch; import com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch;
import com.sparrowwallet.sparrow.event.ConnectionEvent; import com.sparrowwallet.sparrow.event.*;
import com.sparrowwallet.sparrow.event.RequestDisconnectEvent;
import com.sparrowwallet.sparrow.glyphfont.FontAwesome5; import com.sparrowwallet.sparrow.glyphfont.FontAwesome5;
import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.io.Config;
import com.sparrowwallet.sparrow.net.ElectrumServer; import com.sparrowwallet.sparrow.net.*;
import com.sparrowwallet.sparrow.net.Protocol;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.concurrent.WorkerStateEvent; import javafx.concurrent.WorkerStateEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.*;
import javafx.scene.control.Control; import javafx.scene.text.Font;
import javafx.scene.control.TextArea; import javafx.stage.DirectoryChooser;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Duration; import javafx.util.Duration;
@ -30,30 +30,76 @@ import org.controlsfx.validation.decoration.StyleClassValidationDecoration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import tornadofx.control.Field;
import tornadofx.control.Form;
import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLHandshakeException;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List; import java.util.List;
public class ServerPreferencesController extends PreferencesDetailController { public class ServerPreferencesController extends PreferencesDetailController {
private static final Logger log = LoggerFactory.getLogger(ServerPreferencesController.class); private static final Logger log = LoggerFactory.getLogger(ServerPreferencesController.class);
@FXML @FXML
private TextField host; private ToggleGroup serverTypeToggleGroup;
@FXML @FXML
private TextField port; private Form coreForm;
@FXML @FXML
private UnlabeledToggleSwitch useSsl; private TextField coreHost;
@FXML @FXML
private TextField certificate; private TextField corePort;
@FXML @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 @FXML
private UnlabeledToggleSwitch useProxy; private UnlabeledToggleSwitch useProxy;
@ -75,35 +121,108 @@ public class ServerPreferencesController extends PreferencesDetailController {
private final ValidationSupport validationSupport = new ValidationSupport(); private final ValidationSupport validationSupport = new ValidationSupport();
private ElectrumServer.ConnectionService connectionService;
@Override @Override
public void initializeView(Config config) { public void initializeView(Config config) {
EventManager.get().register(this);
getMasterController().closingProperty().addListener((observable, oldValue, newValue) -> {
EventManager.get().unregister(this);
});
Platform.runLater(this::setupValidation); 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()); proxyPort.setTextFormatter(new TextFieldValidator(TextFieldValidator.ValidationModus.MAX_INTEGERS, 5).getFormatter());
host.textProperty().addListener(getElectrumServerListener(config)); coreHost.textProperty().addListener(getBitcoinCoreListener(config));
port.textProperty().addListener(getElectrumServerListener(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)); proxyHost.textProperty().addListener(getProxyListener(config));
proxyPort.textProperty().addListener(getProxyListener(config)); proxyPort.textProperty().addListener(getProxyListener(config));
useSsl.selectedProperty().addListener((observable, oldValue, newValue) -> { coreDataDirField.managedProperty().bind(coreDataDirField.visibleProperty());
setElectrumServerInConfig(config); coreUserPassField.managedProperty().bind(coreUserPassField.visibleProperty());
certificate.setDisable(!newValue); coreUserPassField.visibleProperty().bind(coreDataDirField.visibleProperty().not());
certificateSelect.setDisable(!newValue); 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);
}); });
certificate.textProperty().addListener((observable, oldValue, newValue) -> { coreDataDirSelect.setOnAction(event -> {
File crtFile = getCertificate(newValue); Stage window = new Stage();
if(crtFile != null) {
config.setElectrumServerCert(crtFile); DirectoryChooser directorChooser = new DirectoryChooser();
} else { directorChooser.setTitle("Select Bitcoin Core Data Directory");
config.setElectrumServerCert(null); 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());
} }
}); });
certificateSelect.setOnAction(event -> { 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);
electrumCertificate.setDisable(!newValue);
electrumCertificateSelect.setDisable(!newValue);
});
electrumCertificate.textProperty().addListener((observable, oldValue, newValue) -> {
File crtFile = getCertificate(newValue);
config.setElectrumServerCert(crtFile);
});
electrumCertificateSelect.setOnAction(event -> {
Stage window = new Stage(); Stage window = new Stage();
FileChooser fileChooser = new FileChooser(); FileChooser fileChooser = new FileChooser();
@ -115,7 +234,7 @@ public class ServerPreferencesController extends PreferencesDetailController {
File file = fileChooser.showOpenDialog(window); File file = fileChooser.showOpenDialog(window);
if(file != null) { if(file != null) {
certificate.setText(file.getAbsolutePath()); electrumCertificate.setText(file.getAbsolutePath());
} }
}); });
@ -127,45 +246,32 @@ public class ServerPreferencesController extends PreferencesDetailController {
proxyPort.setDisable(!newValue); proxyPort.setDisable(!newValue);
if(newValue) { if(newValue) {
useSsl.setSelected(true); electrumUseSsl.setSelected(true);
useSsl.setDisable(true); electrumUseSsl.setDisable(true);
} else { } else {
useSsl.setDisable(false); electrumUseSsl.setDisable(false);
} }
}); });
boolean isConnected = ElectrumServer.isConnected(); boolean isConnected = AppServices.isConnecting() || AppServices.isConnected();
setFieldsEditable(!isConnected); setFieldsEditable(!isConnected);
if(AppServices.isConnecting()) {
testResults.appendText("Connecting to server, please wait...");
}
testConnection.managedProperty().bind(testConnection.visibleProperty()); testConnection.managedProperty().bind(testConnection.visibleProperty());
testConnection.setVisible(!isConnected); testConnection.setVisible(!isConnected);
setTestResultsFont();
testConnection.setOnAction(event -> { testConnection.setOnAction(event -> {
testResults.setText("Connecting to " + config.getElectrumServer() + "...");
testConnection.setGraphic(getGlyph(FontAwesome5.Glyph.ELLIPSIS_H, null)); testConnection.setGraphic(getGlyph(FontAwesome5.Glyph.ELLIPSIS_H, null));
testResults.setText("Connecting to " + config.getServerAddress() + "...");
ElectrumServer.ConnectionService connectionService = new ElectrumServer.ConnectionService(false); startElectrumConnection();
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();
}); });
editConnection.managedProperty().bind(editConnection.visibleProperty()); editConnection.managedProperty().bind(editConnection.visibleProperty());
editConnection.setVisible(isConnected); editConnection.setVisible(isConnected);
editConnection.setDisable(AppServices.isConnecting());
editConnection.setOnAction(event -> { editConnection.setOnAction(event -> {
EventManager.get().post(new RequestDisconnectEvent()); EventManager.get().post(new RequestDisconnectEvent());
setFieldsEditable(true); setFieldsEditable(true);
@ -173,27 +279,61 @@ public class ServerPreferencesController extends PreferencesDetailController {
testConnection.setVisible(true); 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(); String electrumServer = config.getElectrumServer();
if(electrumServer != null) { if(electrumServer != null) {
Protocol protocol = Protocol.getProtocol(electrumServer); Protocol protocol = Protocol.getProtocol(electrumServer);
if(protocol != null) { if(protocol != null) {
boolean ssl = protocol.equals(Protocol.SSL); boolean ssl = protocol.equals(Protocol.SSL);
useSsl.setSelected(ssl); electrumUseSsl.setSelected(ssl);
certificate.setDisable(!ssl); electrumCertificate.setDisable(!ssl);
certificateSelect.setDisable(!ssl); electrumCertificateSelect.setDisable(!ssl);
HostAndPort server = protocol.getServerHostAndPort(electrumServer); HostAndPort server = protocol.getServerHostAndPort(electrumServer);
host.setText(server.getHost()); electrumHost.setText(server.getHost());
if(server.hasPort()) { if(server.hasPort()) {
port.setText(Integer.toString(server.getPort())); electrumPort.setText(Integer.toString(server.getPort()));
} }
} }
} }
File certificateFile = config.getElectrumServerCert(); File certificateFile = config.getElectrumServerCert();
if(certificateFile != null) { if(certificateFile != null) {
certificate.setText(certificateFile.getAbsolutePath()); electrumCertificate.setText(certificateFile.getAbsolutePath());
} }
useProxy.setSelected(config.isUseProxy()); useProxy.setSelected(config.isUseProxy());
@ -201,8 +341,8 @@ public class ServerPreferencesController extends PreferencesDetailController {
proxyPort.setDisable(!config.isUseProxy()); proxyPort.setDisable(!config.isUseProxy());
if(config.isUseProxy()) { if(config.isUseProxy()) {
useSsl.setSelected(true); electrumUseSsl.setSelected(true);
useSsl.setDisable(true); electrumUseSsl.setDisable(true);
} }
String proxyServer = config.getProxyServer(); 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) { private void setFieldsEditable(boolean editable) {
host.setEditable(editable); serverTypeToggleGroup.getToggles().forEach(toggle -> ((ToggleButton)toggle).setDisable(!editable));
port.setEditable(editable);
useSsl.setDisable(!editable); coreHost.setDisable(!editable);
certificate.setEditable(editable); corePort.setDisable(!editable);
certificateSelect.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); useProxy.setDisable(!editable);
proxyHost.setEditable(editable); proxyHost.setDisable(!editable);
proxyPort.setEditable(editable); proxyPort.setDisable(!editable);
} }
private void showConnectionSuccess(List<String> serverVersion, String serverBanner) { private void showConnectionSuccess(List<String> serverVersion, String serverBanner) {
@ -252,12 +432,36 @@ public class ServerPreferencesController extends PreferencesDetailController {
} }
private void setupValidation() { private void setupValidation() {
validationSupport.registerValidator(host, Validator.combine( validationSupport.registerValidator(coreHost, Validator.combine(
(Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Invalid host name", getHost(newValue) == null) (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Invalid Core host", getHost(newValue) == null)
)); ));
validationSupport.registerValidator(port, Validator.combine( validationSupport.registerValidator(corePort, Validator.combine(
(Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Invalid port", !newValue.isEmpty() && !isValidPort(Integer.parseInt(newValue))) (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(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( 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))) (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Invalid proxy port", !newValue.isEmpty() && !isValidPort(Integer.parseInt(newValue)))
)); ));
validationSupport.registerValidator(certificate, Validator.combine( validationSupport.registerValidator(electrumCertificate, Validator.combine(
(Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Invalid certificate file", newValue != null && !newValue.isEmpty() && getCertificate(newValue) == null) (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Invalid certificate file", newValue != null && !newValue.isEmpty() && getCertificate(newValue) == null)
)); ));
validationSupport.setValidationDecorator(new StyleClassValidationDecoration()); 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 @NotNull
private ChangeListener<String> getElectrumServerListener(Config config) { private ChangeListener<String> getElectrumServerListener(Config config) {
return (observable, oldValue, newValue) -> { return (observable, oldValue, newValue) -> {
@ -284,8 +519,8 @@ public class ServerPreferencesController extends PreferencesDetailController {
} }
private void setElectrumServerInConfig(Config config) { private void setElectrumServerInConfig(Config config) {
String hostAsString = getHost(host.getText()); String hostAsString = getHost(electrumHost.getText());
Integer portAsInteger = getPort(port.getText()); Integer portAsInteger = getPort(electrumPort.getText());
if(hostAsString != null && portAsInteger != null && isValidPort(portAsInteger)) { if(hostAsString != null && portAsInteger != null && isValidPort(portAsInteger)) {
config.setElectrumServer(getProtocol().toUrlString(hostAsString, portAsInteger)); config.setElectrumServer(getProtocol().toUrlString(hostAsString, portAsInteger));
} else if(hostAsString != null) { } else if(hostAsString != null) {
@ -311,7 +546,7 @@ public class ServerPreferencesController extends PreferencesDetailController {
} }
private Protocol getProtocol() { private Protocol getProtocol() {
return (useSsl.isSelected() ? Protocol.SSL : Protocol.TCP); return (electrumUseSsl.isSelected() ? Protocol.SSL : Protocol.TCP);
} }
private String getHost(String text) { 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) { private File getCertificate(String crtFileLocation) {
try { try {
File crtFile = new File(crtFileLocation); File crtFile = new File(crtFileLocation);
@ -357,4 +605,48 @@ public class ServerPreferencesController extends PreferencesDetailController {
private static boolean isValidPort(int port) { private static boolean isValidPort(int port) {
return port >= 0 && port <= 65535; 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();
}
}
} }

View file

@ -25,6 +25,7 @@ import com.sparrowwallet.sparrow.wallet.HashIndexEntry;
import com.sparrowwallet.sparrow.wallet.TransactionEntry; import com.sparrowwallet.sparrow.wallet.TransactionEntry;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
@ -415,6 +416,9 @@ public class HeadersController extends TransactionFormController implements Init
} }
}); });
Platform.runLater(this::requestOpenWallets);
}
headersForm.signingWalletProperty().addListener((observable, oldValue, signingWallet) -> { headersForm.signingWalletProperty().addListener((observable, oldValue, signingWallet) -> {
initializeSignButton(signingWallet); initializeSignButton(signingWallet);
updateSignedKeystores(signingWallet); updateSignedKeystores(signingWallet);
@ -423,9 +427,6 @@ public class HeadersController extends TransactionFormController implements Init
signaturesProgressBar.initialize(headersForm.getSignatureKeystoreMap(), threshold); signaturesProgressBar.initialize(headersForm.getSignatureKeystoreMap(), threshold);
}); });
Platform.runLater(this::requestOpenWallets);
}
blockchainForm.setDynamicUpdate(this); blockchainForm.setDynamicUpdate(this);
} }
@ -756,7 +757,7 @@ public class HeadersController extends TransactionFormController implements Init
} }
private void updateSignedKeystores(Wallet signingWallet) { private void updateSignedKeystores(Wallet signingWallet) {
Map<PSBTInput, Map<TransactionSignature, Keystore>> signedKeystoresMap = signingWallet.getSignedKeystores(headersForm.getPsbt()); Map<?, Map<TransactionSignature, Keystore>> signedKeystoresMap = headersForm.getPsbt() == null ? signingWallet.getSignedKeystores(headersForm.getTransaction()) : signingWallet.getSignedKeystores(headersForm.getPsbt());
Optional<Map<TransactionSignature, Keystore>> optSignedKeystores = signedKeystoresMap.values().stream().filter(map -> !map.isEmpty()).min(Comparator.comparingInt(Map::size)); Optional<Map<TransactionSignature, Keystore>> optSignedKeystores = signedKeystoresMap.values().stream().filter(map -> !map.isEmpty()).min(Comparator.comparingInt(Map::size));
optSignedKeystores.ifPresent(signedKeystores -> { optSignedKeystores.ifPresent(signedKeystores -> {
headersForm.getSignatureKeystoreMap().keySet().retainAll(signedKeystores.keySet()); headersForm.getSignatureKeystoreMap().keySet().retainAll(signedKeystores.keySet());
@ -781,7 +782,9 @@ public class HeadersController extends TransactionFormController implements Init
public void broadcastTransaction(ActionEvent event) { public void broadcastTransaction(ActionEvent event) {
broadcastButton.setDisable(true); broadcastButton.setDisable(true);
if(headersForm.getPsbt() != null) {
extractTransaction(event); extractTransaction(event);
}
if(headersForm.getSigningWallet() instanceof FinalizingPSBTWallet) { if(headersForm.getSigningWallet() instanceof FinalizingPSBTWallet) {
//Ensure the script hashes of the UTXOs in FinalizingPSBTWallet are subscribed to //Ensure the script hashes of the UTXOs in FinalizingPSBTWallet are subscribed to
@ -818,6 +821,21 @@ public class HeadersController extends TransactionFormController implements Init
} }
}); });
transactionMempoolService.start(); transactionMempoolService.start();
} else {
Sha256Hash txid = headersForm.getTransaction().getTxId();
ElectrumServer.TransactionReferenceService transactionReferenceService = new ElectrumServer.TransactionReferenceService(Set.of(txid));
transactionReferenceService.setOnSucceeded(successEvent -> {
Map<Sha256Hash, BlockTransaction> transactionMap = transactionReferenceService.getValue();
BlockTransaction blockTransaction = transactionMap.get(txid);
if(blockTransaction != null) {
headersForm.setBlockTransaction(blockTransaction);
updateBlockchainForm(blockTransaction, AppServices.getCurrentBlockHeight());
}
});
transactionReferenceService.setOnFailed(failedEvent -> {
log.error("Error fetching broadcasted transaction", failedEvent.getSource().getException());
});
transactionReferenceService.start();
} }
}); });
broadcastTransactionService.setOnFailed(workerStateEvent -> { broadcastTransactionService.setOnFailed(workerStateEvent -> {
@ -903,6 +921,43 @@ public class HeadersController extends TransactionFormController implements Init
if(event.getTxId().equals(headersForm.getTransaction().getTxId())) { if(event.getTxId().equals(headersForm.getTransaction().getTxId())) {
if(event.getBlockTransaction() != null && (!Sha256Hash.ZERO_HASH.equals(event.getBlockTransaction().getBlockHash()) || headersForm.getBlockTransaction() == null)) { if(event.getBlockTransaction() != null && (!Sha256Hash.ZERO_HASH.equals(event.getBlockTransaction().getBlockHash()) || headersForm.getBlockTransaction() == null)) {
updateBlockchainForm(event.getBlockTransaction(), AppServices.getCurrentBlockHeight()); updateBlockchainForm(event.getBlockTransaction(), AppServices.getCurrentBlockHeight());
} else if(headersForm.getPsbt() == null) {
boolean isSigned = true;
ObservableMap<TransactionSignature, Keystore> signatureKeystoreMap = FXCollections.observableMap(new LinkedHashMap<>());
for(TransactionInput txInput : headersForm.getTransaction().getInputs()) {
List<TransactionSignature> signatures = txInput.hasWitness() ? txInput.getWitness().getSignatures() : txInput.getScriptSig().getSignatures();
if(signatures.isEmpty()) {
isSigned = false;
break;
}
if(signatureKeystoreMap.isEmpty()) {
for(int i = 0; i < signatures.size(); i++) {
signatureKeystoreMap.put(signatures.get(i), new Keystore("Keystore " + (i+1)));
}
}
}
if(isSigned) {
blockchainForm.setVisible(false);
signaturesForm.setVisible(true);
broadcastButtonBox.setVisible(true);
viewFinalButton.setDisable(true);
if(headersForm.getSigningWallet() == null) {
for(Wallet wallet : AppServices.get().getOpenWallets().keySet()) {
if(wallet.canSign(headersForm.getTransaction())) {
headersForm.setSigningWallet(wallet);
break;
}
}
}
if(headersForm.getSigningWallet() == null) {
signaturesProgressBar.initialize(signatureKeystoreMap, signatureKeystoreMap.size());
}
}
} }
Long feeAmt = calculateFee(event.getInputTransactions()); Long feeAmt = calculateFee(event.getInputTransactions());

View file

@ -326,7 +326,7 @@ public class TransactionController implements Initializable {
} }
private void fetchThisAndInputBlockTransactions(int indexStart, int indexEnd) { private void fetchThisAndInputBlockTransactions(int indexStart, int indexEnd) {
if(AppServices.isOnline() && indexStart < getTransaction().getInputs().size()) { if(AppServices.isConnected() && indexStart < getTransaction().getInputs().size()) {
Set<Sha256Hash> references = new HashSet<>(); Set<Sha256Hash> references = new HashSet<>();
if(getPSBT() == null) { if(getPSBT() == null) {
references.add(getTransaction().getTxId()); references.add(getTransaction().getTxId());
@ -378,7 +378,7 @@ public class TransactionController implements Initializable {
} }
private void fetchOutputBlockTransactions(int indexStart, int indexEnd) { private void fetchOutputBlockTransactions(int indexStart, int indexEnd) {
if(AppServices.isOnline() && getPSBT() == null && indexStart < getTransaction().getOutputs().size()) { if(AppServices.isConnected() && getPSBT() == null && indexStart < getTransaction().getOutputs().size()) {
int maxIndex = Math.min(getTransaction().getOutputs().size(), indexEnd); int maxIndex = Math.min(getTransaction().getOutputs().size(), indexEnd);
ElectrumServer.TransactionOutputsReferenceService transactionOutputsReferenceService = new ElectrumServer.TransactionOutputsReferenceService(getTransaction(), indexStart, maxIndex); ElectrumServer.TransactionOutputsReferenceService transactionOutputsReferenceService = new ElectrumServer.TransactionOutputsReferenceService(getTransaction(), indexStart, maxIndex);
transactionOutputsReferenceService.setOnSucceeded(successEvent -> { transactionOutputsReferenceService.setOnSucceeded(successEvent -> {

View file

@ -2,16 +2,23 @@ package com.sparrowwallet.sparrow.wallet;
import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.control.DateStringConverter;
import com.sparrowwallet.sparrow.event.SettingsChangedEvent; import com.sparrowwallet.sparrow.event.SettingsChangedEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Spinner; import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory; import javafx.scene.control.SpinnerValueFactory;
import java.net.URL; import java.net.URL;
import java.time.ZoneId;
import java.util.Date;
import java.util.ResourceBundle; import java.util.ResourceBundle;
public class AdvancedController implements Initializable { public class AdvancedController implements Initializable {
@FXML
private DatePicker birthDate;
@FXML @FXML
private Spinner<Integer> gapLimit; private Spinner<Integer> gapLimit;
@ -21,6 +28,17 @@ public class AdvancedController implements Initializable {
} }
public void initializeView(Wallet wallet) { public void initializeView(Wallet wallet) {
birthDate.setConverter(new DateStringConverter());
if(wallet.getBirthDate() != null) {
birthDate.setValue(wallet.getBirthDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
}
birthDate.valueProperty().addListener((observable, oldValue, newValue) -> {
if(newValue != null) {
wallet.setBirthDate(Date.from(newValue.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
EventManager.get().post(new SettingsChangedEvent(wallet, SettingsChangedEvent.Type.BIRTH_DATE));
}
});
gapLimit.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(Wallet.DEFAULT_LOOKAHEAD, 10000, wallet.getGapLimit())); gapLimit.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(Wallet.DEFAULT_LOOKAHEAD, 10000, wallet.getGapLimit()));
gapLimit.valueProperty().addListener((observable, oldValue, newValue) -> { gapLimit.valueProperty().addListener((observable, oldValue, newValue) -> {
wallet.setGapLimit(newValue); wallet.setGapLimit(newValue);

View file

@ -138,7 +138,7 @@ public class ReceiveController extends WalletFormController implements Initializ
private void updateLastUsed() { private void updateLastUsed() {
Set<BlockTransactionHashIndex> currentOutputs = currentEntry.getNode().getTransactionOutputs(); Set<BlockTransactionHashIndex> currentOutputs = currentEntry.getNode().getTransactionOutputs();
if(AppServices.isOnline() && currentOutputs.isEmpty()) { if(AppServices.isConnected() && currentOutputs.isEmpty()) {
lastUsed.setText("Never"); lastUsed.setText("Never");
lastUsed.setGraphic(getUnusedGlyph()); lastUsed.setGraphic(getUnusedGlyph());
} else if(!currentOutputs.isEmpty()) { } else if(!currentOutputs.isEmpty()) {

View file

@ -14,10 +14,7 @@ import com.sparrowwallet.drongo.wallet.WalletModel;
import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.control.*; import com.sparrowwallet.sparrow.control.*;
import com.sparrowwallet.sparrow.event.RequestOpenWalletsEvent; import com.sparrowwallet.sparrow.event.*;
import com.sparrowwallet.sparrow.event.SettingsChangedEvent;
import com.sparrowwallet.sparrow.event.StorageEvent;
import com.sparrowwallet.sparrow.event.TimedEvent;
import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.io.Storage;
import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
@ -28,17 +25,14 @@ import javafx.fxml.Initializable;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import org.controlsfx.control.RangeSlider; import org.controlsfx.control.RangeSlider;
import org.controlsfx.tools.Borders;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import tornadofx.control.Fieldset; import tornadofx.control.Fieldset;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class SettingsController extends WalletFormController implements Initializable { public class SettingsController extends WalletFormController implements Initializable {
@ -329,6 +323,22 @@ public class SettingsController extends WalletFormController implements Initiali
} }
} }
@Subscribe
public void walletSettingsChanged(WalletSettingsChangedEvent event) {
updateBirthDate(event.getWalletFile(), event.getWallet());
}
@Subscribe
public void walletHistoryChanged(WalletHistoryChangedEvent event) {
updateBirthDate(event.getWalletFile(), event.getWallet());
}
private void updateBirthDate(File walletFile, Wallet wallet) {
if(walletFile.equals(walletForm.getWalletFile()) && !Objects.equals(wallet.getBirthDate(), walletForm.getWallet().getBirthDate())) {
walletForm.getWallet().setBirthDate(wallet.getBirthDate());
}
}
private void saveWallet(boolean changePassword) { private void saveWallet(boolean changePassword) {
ECKey existingPubKey = walletForm.getStorage().getEncryptionPubKey(); ECKey existingPubKey = walletForm.getStorage().getEncryptionPubKey();
@ -345,6 +355,15 @@ public class SettingsController extends WalletFormController implements Initiali
requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_SET; requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_SET;
} }
if(!changePassword && ((SettingsWalletForm)walletForm).isAddressChange() && !walletForm.getWallet().getTransactions().isEmpty()) {
Optional<ButtonType> optResponse = AppServices.showWarningDialog("Change Wallet Addresses?", "This wallet has existing transactions which will be replaced as the wallet addresses will change. Ok to proceed?", ButtonType.CANCEL, ButtonType.OK);
if(optResponse.isPresent() && optResponse.get().equals(ButtonType.CANCEL)) {
revert.setDisable(false);
apply.setDisable(false);
return;
}
}
WalletPasswordDialog dlg = new WalletPasswordDialog(requirement); WalletPasswordDialog dlg = new WalletPasswordDialog(requirement);
Optional<SecureString> password = dlg.showAndWait(); Optional<SecureString> password = dlg.showAndWait();
if(password.isPresent()) { if(password.isPresent()) {

View file

@ -7,6 +7,7 @@ import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent;
import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.io.Storage;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
/** /**
* This class extends WalletForm to allow rollback of wallet changes. It is used exclusively by SettingsController for this purpose. * This class extends WalletForm to allow rollback of wallet changes. It is used exclusively by SettingsController for this purpose.
@ -37,7 +38,7 @@ public class SettingsWalletForm extends WalletForm {
@Override @Override
public void saveAndRefresh() throws IOException { public void saveAndRefresh() throws IOException {
boolean refreshAll = changesScriptHashes(wallet, walletCopy); boolean refreshAll = isRefreshNecessary(wallet, walletCopy);
if(refreshAll) { if(refreshAll) {
walletCopy.clearNodes(); walletCopy.clearNodes();
} }
@ -50,11 +51,31 @@ public class SettingsWalletForm extends WalletForm {
} }
} }
private boolean changesScriptHashes(Wallet original, Wallet changed) { private boolean isRefreshNecessary(Wallet original, Wallet changed) {
if(!original.isValid() || !changed.isValid()) { if(!original.isValid() || !changed.isValid()) {
return true; return true;
} }
if(isAddressChange(original, changed)) {
return true;
}
if(original.getGapLimit() != changed.getGapLimit()) {
return true;
}
if(!Objects.equals(original.getBirthDate(), changed.getBirthDate())) {
return true;
}
return false;
}
protected boolean isAddressChange() {
return isAddressChange(wallet, walletCopy);
}
private boolean isAddressChange(Wallet original, Wallet changed) {
if(original.getPolicyType() != changed.getPolicyType()) { if(original.getPolicyType() != changed.getPolicyType()) {
return true; return true;
} }
@ -73,19 +94,15 @@ public class SettingsWalletForm extends WalletForm {
Keystore originalKeystore = original.getKeystores().get(i); Keystore originalKeystore = original.getKeystores().get(i);
Keystore changedKeystore = changed.getKeystores().get(i); Keystore changedKeystore = changed.getKeystores().get(i);
if(!originalKeystore.getKeyDerivation().equals(changedKeystore.getKeyDerivation())) { if(!Objects.equals(originalKeystore.getKeyDerivation(), changedKeystore.getKeyDerivation())) {
return true; return true;
} }
if(!originalKeystore.getExtendedPublicKey().equals(changedKeystore.getExtendedPublicKey())) { if(!Objects.equals(originalKeystore.getExtendedPublicKey(), changedKeystore.getExtendedPublicKey())) {
return true; return true;
} }
} }
if(original.getGapLimit() != changed.getGapLimit()) {
return true;
}
return false; return false;
} }
} }

View file

@ -14,11 +14,15 @@ import javafx.beans.property.IntegerProperty;
import javafx.beans.property.IntegerPropertyBase; import javafx.beans.property.IntegerPropertyBase;
import javafx.beans.property.LongProperty; import javafx.beans.property.LongProperty;
import javafx.beans.property.LongPropertyBase; import javafx.beans.property.LongPropertyBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class TransactionEntry extends Entry implements Comparable<TransactionEntry> { public class TransactionEntry extends Entry implements Comparable<TransactionEntry> {
private static final Logger log = LoggerFactory.getLogger(TransactionEntry.class);
private final BlockTransaction blockTransaction; private final BlockTransaction blockTransaction;
public TransactionEntry(Wallet wallet, BlockTransaction blockTransaction, Map<BlockTransactionHashIndex, KeyPurpose> inputs, Map<BlockTransactionHashIndex, KeyPurpose> outputs) { public TransactionEntry(Wallet wallet, BlockTransaction blockTransaction, Map<BlockTransactionHashIndex, KeyPurpose> inputs, Map<BlockTransactionHashIndex, KeyPurpose> outputs) {
@ -86,6 +90,7 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
if(optRef.isPresent()) { if(optRef.isPresent()) {
validEntries++; validEntries++;
if(getChildren().stream().noneMatch(entry -> ((HashIndexEntry)entry).getHashIndex().equals(optRef.get().getSpentBy()) && ((HashIndexEntry)entry).getType().equals(HashIndexEntry.Type.INPUT))) { if(getChildren().stream().noneMatch(entry -> ((HashIndexEntry)entry).getHashIndex().equals(optRef.get().getSpentBy()) && ((HashIndexEntry)entry).getType().equals(HashIndexEntry.Type.INPUT))) {
log.warn("TransactionEntry " + blockTransaction.getHash() + " for wallet " + getWallet().getName() + " missing child for input " + optRef.get().getSpentBy() + " on output " + optRef.get());
return false; return false;
} }
} }
@ -95,12 +100,14 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
if(optRef.isPresent()) { if(optRef.isPresent()) {
validEntries++; validEntries++;
if(getChildren().stream().noneMatch(entry -> ((HashIndexEntry)entry).getHashIndex().equals(optRef.get()) && ((HashIndexEntry)entry).getType().equals(HashIndexEntry.Type.OUTPUT))) { if(getChildren().stream().noneMatch(entry -> ((HashIndexEntry)entry).getHashIndex().equals(optRef.get()) && ((HashIndexEntry)entry).getType().equals(HashIndexEntry.Type.OUTPUT))) {
log.warn("TransactionEntry " + blockTransaction.getHash() + " for wallet " + getWallet().getName() + " missing child for output " + optRef.get());
return false; return false;
} }
} }
} }
if(getChildren().size() != validEntries) { if(getChildren().size() != validEntries) {
log.warn("TransactionEntry " + blockTransaction.getHash() + " for wallet " + getWallet().getName() + " has incorrect number of children " + getChildren().size() + " (should be " + validEntries + ")");
return false; return false;
} }

View file

@ -4,10 +4,7 @@ import com.google.common.eventbus.Subscribe;
import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.CurrencyRate; import com.sparrowwallet.sparrow.CurrencyRate;
import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.control.BalanceChart; import com.sparrowwallet.sparrow.control.*;
import com.sparrowwallet.sparrow.control.CoinLabel;
import com.sparrowwallet.sparrow.control.FiatLabel;
import com.sparrowwallet.sparrow.control.TransactionsTreeTable;
import com.sparrowwallet.sparrow.event.*; import com.sparrowwallet.sparrow.event.*;
import com.sparrowwallet.sparrow.net.ExchangeSource; import com.sparrowwallet.sparrow.net.ExchangeSource;
import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener;
@ -29,6 +26,12 @@ public class TransactionsController extends WalletFormController implements Init
@FXML @FXML
private CoinLabel mempoolBalance; private CoinLabel mempoolBalance;
@FXML
private FiatLabel fiatMempoolBalance;
@FXML
private CopyableLabel transactionCount;
@FXML @FXML
private TransactionsTreeTable transactionsTable; private TransactionsTreeTable transactionsTable;
@ -47,10 +50,14 @@ public class TransactionsController extends WalletFormController implements Init
transactionsTable.initialize(walletTransactionsEntry); transactionsTable.initialize(walletTransactionsEntry);
balance.valueProperty().addListener((observable, oldValue, newValue) -> { balance.valueProperty().addListener((observable, oldValue, newValue) -> {
setFiatBalance(AppServices.getFiatCurrencyExchangeRate(), newValue.longValue()); setFiatBalance(fiatBalance, AppServices.getFiatCurrencyExchangeRate(), newValue.longValue());
}); });
balance.setValue(walletTransactionsEntry.getBalance()); balance.setValue(walletTransactionsEntry.getBalance());
mempoolBalance.valueProperty().addListener((observable, oldValue, newValue) -> {
setFiatBalance(fiatMempoolBalance, AppServices.getFiatCurrencyExchangeRate(), newValue.longValue());
});
mempoolBalance.setValue(walletTransactionsEntry.getMempoolBalance()); mempoolBalance.setValue(walletTransactionsEntry.getMempoolBalance());
setTransactionCount(walletTransactionsEntry);
balanceChart.initialize(walletTransactionsEntry); balanceChart.initialize(walletTransactionsEntry);
transactionsTable.getSelectionModel().getSelectedIndices().addListener((ListChangeListener<Integer>) c -> { transactionsTable.getSelectionModel().getSelectedIndices().addListener((ListChangeListener<Integer>) c -> {
@ -61,12 +68,19 @@ public class TransactionsController extends WalletFormController implements Init
}); });
} }
private void setFiatBalance(CurrencyRate currencyRate, long balance) { private void setFiatBalance(FiatLabel fiatLabel, CurrencyRate currencyRate, long balance) {
if(currencyRate != null && currencyRate.isAvailable()) { if(currencyRate != null && currencyRate.isAvailable() && balance > 0) {
fiatBalance.set(currencyRate, balance); fiatLabel.set(currencyRate, balance);
} else {
fiatLabel.setCurrency(null);
fiatLabel.setBtcRate(0.0);
} }
} }
private void setTransactionCount(WalletTransactionsEntry walletTransactionsEntry) {
transactionCount.setText(walletTransactionsEntry.getChildren() != null ? Integer.toString(walletTransactionsEntry.getChildren().size()) : "0");
}
@Subscribe @Subscribe
public void walletNodesChanged(WalletNodesChangedEvent event) { public void walletNodesChanged(WalletNodesChangedEvent event) {
if(event.getWallet().equals(walletForm.getWallet())) { if(event.getWallet().equals(walletForm.getWallet())) {
@ -76,6 +90,7 @@ public class TransactionsController extends WalletFormController implements Init
balance.setValue(walletTransactionsEntry.getBalance()); balance.setValue(walletTransactionsEntry.getBalance());
mempoolBalance.setValue(walletTransactionsEntry.getMempoolBalance()); mempoolBalance.setValue(walletTransactionsEntry.getMempoolBalance());
balanceChart.update(walletTransactionsEntry); balanceChart.update(walletTransactionsEntry);
setTransactionCount(walletTransactionsEntry);
} }
} }
@ -91,6 +106,7 @@ public class TransactionsController extends WalletFormController implements Init
balance.setValue(walletTransactionsEntry.getBalance()); balance.setValue(walletTransactionsEntry.getBalance());
mempoolBalance.setValue(walletTransactionsEntry.getMempoolBalance()); mempoolBalance.setValue(walletTransactionsEntry.getMempoolBalance());
balanceChart.update(walletTransactionsEntry); balanceChart.update(walletTransactionsEntry);
setTransactionCount(walletTransactionsEntry);
} }
} }
@ -115,12 +131,15 @@ public class TransactionsController extends WalletFormController implements Init
if(event.getExchangeSource() == ExchangeSource.NONE) { if(event.getExchangeSource() == ExchangeSource.NONE) {
fiatBalance.setCurrency(null); fiatBalance.setCurrency(null);
fiatBalance.setBtcRate(0.0); fiatBalance.setBtcRate(0.0);
fiatMempoolBalance.setCurrency(null);
fiatMempoolBalance.setBtcRate(0.0);
} }
} }
@Subscribe @Subscribe
public void exchangeRatesUpdated(ExchangeRatesUpdatedEvent event) { public void exchangeRatesUpdated(ExchangeRatesUpdatedEvent event) {
setFiatBalance(event.getCurrencyRate(), getWalletForm().getWalletTransactionsEntry().getBalance()); setFiatBalance(fiatBalance, event.getCurrencyRate(), getWalletForm().getWalletTransactionsEntry().getBalance());
setFiatBalance(fiatMempoolBalance, event.getCurrencyRate(), getWalletForm().getWalletTransactionsEntry().getMempoolBalance());
} }
@Subscribe @Subscribe
@ -128,6 +147,21 @@ public class TransactionsController extends WalletFormController implements Init
transactionsTable.updateHistoryStatus(event); transactionsTable.updateHistoryStatus(event);
} }
@Subscribe
public void bwtSyncStatus(BwtSyncStatusEvent event) {
walletHistoryStatus(new WalletHistoryStatusEvent(walletForm.getWallet(), true, event.getStatus()));
}
@Subscribe
public void bwtScanStatus(BwtScanStatusEvent event) {
walletHistoryStatus(new WalletHistoryStatusEvent(walletForm.getWallet(), true, event.getStatus()));
}
@Subscribe
public void bwtShutdown(BwtShutdownEvent event) {
walletHistoryStatus(new WalletHistoryStatusEvent(walletForm.getWallet(), false));
}
@Subscribe @Subscribe
public void walletUtxoStatusChanged(WalletUtxoStatusChangedEvent event) { public void walletUtxoStatusChanged(WalletUtxoStatusChangedEvent event) {
if(event.getWallet().equals(getWalletForm().getWallet())) { if(event.getWallet().equals(getWalletForm().getWallet())) {

View file

@ -133,6 +133,22 @@ public class UtxosController extends WalletFormController implements Initializab
utxosTable.updateHistoryStatus(event); utxosTable.updateHistoryStatus(event);
} }
@Subscribe
public void bwtSyncStatus(BwtSyncStatusEvent event) {
walletHistoryStatus(new WalletHistoryStatusEvent(walletForm.getWallet(), true, event.getStatus()));
}
@Subscribe
public void bwtScanStatus(BwtScanStatusEvent event) {
walletHistoryStatus(new WalletHistoryStatusEvent(walletForm.getWallet(), true, event.getStatus()));
}
@Subscribe
public void bwtShutdown(BwtShutdownEvent event) {
walletHistoryStatus(new WalletHistoryStatusEvent(walletForm.getWallet(), false));
}
@Subscribe @Subscribe
public void walletUtxoStatusChanged(WalletUtxoStatusChangedEvent event) { public void walletUtxoStatusChanged(WalletUtxoStatusChangedEvent event) {
if(event.getWallet().equals(getWalletForm().getWallet())) { if(event.getWallet().equals(getWalletForm().getWallet())) {

View file

@ -75,18 +75,18 @@ public class WalletForm {
public void refreshHistory(Integer blockHeight, WalletNode node) { public void refreshHistory(Integer blockHeight, WalletNode node) {
Wallet previousWallet = wallet.copy(); Wallet previousWallet = wallet.copy();
if(wallet.isValid() && AppServices.isOnline()) { if(wallet.isValid() && AppServices.isConnected()) {
log.debug(node == null ? wallet.getName() + " refreshing full wallet history" : wallet.getName() + " requesting node wallet history for " + node.getDerivationPath()); log.debug(node == null ? wallet.getName() + " refreshing full wallet history" : wallet.getName() + " requesting node wallet history for " + node.getDerivationPath());
ElectrumServer.TransactionHistoryService historyService = new ElectrumServer.TransactionHistoryService(wallet, getWalletTransactionNodes(node)); ElectrumServer.TransactionHistoryService historyService = new ElectrumServer.TransactionHistoryService(wallet, getWalletTransactionNodes(node));
historyService.setOnSucceeded(workerStateEvent -> { historyService.setOnSucceeded(workerStateEvent -> {
EventManager.get().post(new WalletHistoryStatusEvent(wallet, true)); EventManager.get().post(new WalletHistoryStatusEvent(wallet, false));
updateWallet(previousWallet, blockHeight); updateWallet(previousWallet, blockHeight);
}); });
historyService.setOnFailed(workerStateEvent -> { historyService.setOnFailed(workerStateEvent -> {
log.error("Error retrieving wallet history", workerStateEvent.getSource().getException()); log.error("Error retrieving wallet history", workerStateEvent.getSource().getException());
EventManager.get().post(new WalletHistoryStatusEvent(wallet, workerStateEvent.getSource().getException().getMessage())); EventManager.get().post(new WalletHistoryStatusEvent(wallet, workerStateEvent.getSource().getException().getMessage()));
}); });
EventManager.get().post(new WalletHistoryStatusEvent(wallet, false)); EventManager.get().post(new WalletHistoryStatusEvent(wallet, true));
historyService.start(); historyService.start();
} }
} }
@ -106,7 +106,7 @@ public class WalletForm {
boolean changed = false; boolean changed = false;
if(!historyChangedNodes.isEmpty()) { if(!historyChangedNodes.isEmpty()) {
Platform.runLater(() -> EventManager.get().post(new WalletHistoryChangedEvent(wallet, historyChangedNodes))); Platform.runLater(() -> EventManager.get().post(new WalletHistoryChangedEvent(wallet, storage, historyChangedNodes)));
changed = true; changed = true;
} }

View file

@ -25,4 +25,5 @@ open module com.sparrowwallet.sparrow {
requires centerdevice.nsmenufx; requires centerdevice.nsmenufx;
requires jcommander; requires jcommander;
requires slf4j.api; requires slf4j.api;
requires bwt.jni;
} }

View file

@ -8,7 +8,7 @@
<?import javafx.scene.image.ImageView?> <?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?> <?import javafx.scene.image.Image?>
<StackPane prefHeight="420.0" prefWidth="600.0" stylesheets="@about.css" fx:controller="com.sparrowwallet.sparrow.AboutController" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"> <StackPane prefHeight="420.0" prefWidth="600.0" stylesheets="@about.css, @general.css" fx:controller="com.sparrowwallet.sparrow.AboutController" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<VBox spacing="20"> <VBox spacing="20">
<HBox styleClass="title-area"> <HBox styleClass="title-area">
<HBox alignment="CENTER_LEFT"> <HBox alignment="CENTER_LEFT">
@ -21,8 +21,9 @@
</HBox> </HBox>
<VBox spacing="10" styleClass="content-area"> <VBox spacing="10" styleClass="content-area">
<Label text="Sparrow is a Bitcoin wallet with the goal of providing greater transparency and usability on the path to full financial self sovereignty. It attempts to provide all of the detail about your wallet setup, transactions and UTXOs so that you can transact will a full understanding of your money." wrapText="true" /> <Label text="Sparrow is a Bitcoin wallet with the goal of providing greater transparency and usability on the path to full financial self sovereignty. It attempts to provide all of the detail about your wallet setup, transactions and UTXOs so that you can transact will a full understanding of your money." wrapText="true" />
<Label text="Sparrow can operate in both an online and offline mode. In the online mode it connects to your Electrum server to display transaction history. In the offline mode it is useful as a transaction editor and as an airgapped multisig coordinator." wrapText="true" /> <Label text="Sparrow can operate in both an online and offline mode. In the online mode it connects to your Bitcoin Core node or Electrum server to display transaction history. In the offline mode it is useful as a transaction editor and as an airgapped multisig coordinator." wrapText="true" />
<Label text="For privacy and security reasons it is not recommended to use a public Electrum server. Install an Electrum server that connects to your full node to index the blockchain and provide full privacy." wrapText="true" /> <Label text="Connecting Sparrow to your Bitcoin Core node ensures your privacy, while connecting Sparrow to your own Electrum server ensures wallets load quicker, you have access to a full blockchain explorer, and your public keys are always encrypted on disk. " wrapText="true" />
<HBox><Label text="If you find Sparrow useful, consider donating at "/><Hyperlink text="https://sparrowwallet.com/donate" onAction="#openDonate"/></HBox>
</VBox> </VBox>
<HBox styleClass="button-area" alignment="BOTTOM_RIGHT" VBox.vgrow="SOMETIMES"> <HBox styleClass="button-area" alignment="BOTTOM_RIGHT" VBox.vgrow="SOMETIMES">
<Button text="Done" onAction="#close" /> <Button text="Done" onAction="#close" />

View file

@ -29,3 +29,8 @@
-fx-padding: 0 0 0 8; -fx-padding: 0 0 0 8;
-fx-spacing: 10; -fx-spacing: 10;
} }
.core-server.toggle-switch:selected .thumb-area {
-fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -20%), derive(-fx-text-box-border, -30%)), linear-gradient(to bottom, derive(#50a14f, 30%), #50a14f);
-fx-background-insets: 0, 1;
}

View file

@ -168,3 +168,7 @@
-fx-border-width: 1px 0px 1px 0px; -fx-border-width: 1px 0px 1px 0px;
-fx-border-color: derive(-fx-background, -10%); -fx-border-color: derive(-fx-background, -10%);
} }
.root .placeholder .hyperlink {
-fx-text-fill: derive(#1e88cf, 20%);
}

View file

@ -40,6 +40,10 @@
-fx-padding: -20 0 0 0; -fx-padding: -20 0 0 0;
} }
.form .field .toggle-switch {
-fx-padding: 5 0 2 0;
}
.tab-error > .tab-container { .tab-error > .tab-container {
-fx-effect: dropshadow(three-pass-box, rgba(202, 18, 67, .6), 7, 0, 0, 0); -fx-effect: dropshadow(three-pass-box, rgba(202, 18, 67, .6), 7, 0, 0, 0);
} }

View file

@ -12,6 +12,10 @@
<?import tornadofx.control.Field?> <?import tornadofx.control.Field?>
<?import com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch?> <?import com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch?>
<?import org.controlsfx.glyphfont.Glyph?> <?import org.controlsfx.glyphfont.Glyph?>
<?import org.controlsfx.control.SegmentedButton?>
<?import com.sparrowwallet.sparrow.net.ServerType?>
<?import com.sparrowwallet.sparrow.net.CoreAuthType?>
<?import com.sparrowwallet.sparrow.control.HelpLabel?>
<GridPane hgap="10.0" vgap="10.0" stylesheets="@preferences.css, @../general.css" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sparrowwallet.sparrow.preferences.ServerPreferencesController"> <GridPane hgap="10.0" vgap="10.0" stylesheets="@preferences.css, @../general.css" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sparrowwallet.sparrow.preferences.ServerPreferencesController">
<padding> <padding>
<Insets left="25.0" right="25.0" top="25.0" /> <Insets left="25.0" right="25.0" top="25.0" />
@ -24,25 +28,92 @@
</rowConstraints> </rowConstraints>
<Form GridPane.columnIndex="0" GridPane.rowIndex="0"> <Form GridPane.columnIndex="0" GridPane.rowIndex="0">
<Fieldset inputGrow="SOMETIMES" text="Electrum Server"> <Fieldset inputGrow="SOMETIMES" text="Server">
<Field text="Type:">
<SegmentedButton>
<toggleGroup>
<ToggleGroup fx:id="serverTypeToggleGroup" />
</toggleGroup>
<buttons>
<ToggleButton text="Bitcoin Core" toggleGroup="$serverTypeToggleGroup">
<userData>
<ServerType fx:constant="BITCOIN_CORE"/>
</userData>
</ToggleButton>
<ToggleButton text="Electrum Server" toggleGroup="$serverTypeToggleGroup">
<userData>
<ServerType fx:constant="ELECTRUM_SERVER"/>
</userData>
</ToggleButton>
</buttons>
</SegmentedButton>
</Field>
</Fieldset>
</Form>
<Form fx:id="coreForm" GridPane.columnIndex="0" GridPane.rowIndex="1">
<Fieldset inputGrow="SOMETIMES" text="Bitcoin Core RPC">
<Field text="URL:"> <Field text="URL:">
<TextField fx:id="host" promptText="e.g. 127.0.0.1"/> <TextField fx:id="coreHost" promptText="e.g. 127.0.0.1"/>
<TextField fx:id="port" promptText="e.g. 50002" prefWidth="80" /> <TextField fx:id="corePort" promptText="e.g. 8332" prefWidth="80" />
</Field> </Field>
<Field text="Use SSL:"> <Field text="Authentication:">
<UnlabeledToggleSwitch fx:id="useSsl"/> <SegmentedButton>
<toggleGroup>
<ToggleGroup fx:id="coreAuthToggleGroup" />
</toggleGroup>
<buttons>
<ToggleButton text="Default" toggleGroup="$coreAuthToggleGroup">
<userData>
<CoreAuthType fx:constant="COOKIE"/>
</userData>
</ToggleButton>
<ToggleButton text="User / Pass" toggleGroup="$coreAuthToggleGroup">
<userData>
<CoreAuthType fx:constant="USERPASS"/>
</userData>
</ToggleButton>
</buttons>
</SegmentedButton>
</Field> </Field>
<Field text="Certificate:" styleClass="label-button"> <Field fx:id="coreDataDirField" text="Data Folder:" styleClass="label-button">
<TextField fx:id="certificate" promptText="Optional server certificate (.crt)"/> <TextField fx:id="coreDataDir"/>
<Button fx:id="certificateSelect" maxWidth="25" minWidth="-Infinity" prefWidth="30" text="Ed"> <Button fx:id="coreDataDirSelect" maxWidth="35" minWidth="-Infinity" prefWidth="35">
<graphic> <graphic>
<Glyph fontFamily="FontAwesome" icon="EDIT" prefWidth="15" /> <Glyph fontFamily="FontAwesome" icon="EDIT" fontSize="13" />
</graphic> </graphic>
</Button> </Button>
</Field> </Field>
<Field fx:id="coreUserPassField" text="User / Pass:" styleClass="label-button">
<TextField fx:id="coreUser"/>
<PasswordField fx:id="corePass"/>
</Field>
<Field text="Multi-Wallet:">
<UnlabeledToggleSwitch fx:id="coreMultiWallet"/> <HelpLabel helpText="Enable this if using multiple Bitcoin Core wallets" />
</Field>
<Field text="Wallet Name:" styleClass="label-button">
<TextField fx:id="coreWallet"/>
</Field>
</Fieldset> </Fieldset>
</Form>
<Fieldset inputGrow="SOMETIMES" text="Proxy"> <Form fx:id="electrumForm" GridPane.columnIndex="0" GridPane.rowIndex="1">
<Fieldset inputGrow="SOMETIMES" text="Electrum Server">
<Field text="URL:">
<TextField fx:id="electrumHost" promptText="e.g. 127.0.0.1"/>
<TextField fx:id="electrumPort" promptText="e.g. 50002" prefWidth="80" />
</Field>
<Field text="Use SSL:">
<UnlabeledToggleSwitch fx:id="electrumUseSsl"/>
</Field>
<Field text="Certificate:" styleClass="label-button">
<TextField fx:id="electrumCertificate" promptText="Optional server certificate (.crt)"/>
<Button fx:id="electrumCertificateSelect" maxWidth="35" minWidth="-Infinity" prefWidth="35">
<graphic>
<Glyph fontFamily="FontAwesome" icon="EDIT" fontSize="13" />
</graphic>
</Button>
</Field>
<Field text="Use Proxy:"> <Field text="Use Proxy:">
<UnlabeledToggleSwitch fx:id="useProxy"/> <UnlabeledToggleSwitch fx:id="useProxy"/>
</Field> </Field>
@ -53,7 +124,7 @@
</Fieldset> </Fieldset>
</Form> </Form>
<StackPane GridPane.columnIndex="0" GridPane.rowIndex="1"> <StackPane GridPane.columnIndex="0" GridPane.rowIndex="2">
<Button fx:id="testConnection" graphicTextGap="5" text="Test Connection"> <Button fx:id="testConnection" graphicTextGap="5" text="Test Connection">
<graphic> <graphic>
<Glyph fontFamily="FontAwesome" icon="QUESTION_CIRCLE" prefWidth="13" /> <Glyph fontFamily="FontAwesome" icon="QUESTION_CIRCLE" prefWidth="13" />
@ -66,7 +137,7 @@
</Button> </Button>
</StackPane> </StackPane>
<StackPane GridPane.columnIndex="0" GridPane.rowIndex="2"> <StackPane GridPane.columnIndex="0" GridPane.rowIndex="3">
<padding> <padding>
<Insets top="10.0" bottom="20.0"/> <Insets top="10.0" bottom="20.0"/>
</padding> </padding>

View file

@ -25,7 +25,11 @@
</rowConstraints> </rowConstraints>
<Form GridPane.columnIndex="0" GridPane.rowIndex="0"> <Form GridPane.columnIndex="0" GridPane.rowIndex="0">
<Fieldset inputGrow="SOMETIMES" text="Advanced Settings" styleClass="wideLabelFieldSet"> <Fieldset inputGrow="SOMETIMES" text="Advanced Settings">
<Field text="Birth date:">
<DatePicker editable="false" fx:id="birthDate" prefWidth="140" />
<HelpLabel helpText="The date of the earliest transaction (used to avoid scanning the entire blockchain)."/>
</Field>
<Field text="Gap limit:"> <Field text="Gap limit:">
<Spinner fx:id="gapLimit" editable="true" prefWidth="90" /> <Spinner fx:id="gapLimit" editable="true" prefWidth="90" />
<HelpLabel helpText="Change how far ahead to look for additional transactions beyond the highest derivation with previous transaction outputs."/> <HelpLabel helpText="Change how far ahead to look for additional transactions beyond the highest derivation with previous transaction outputs."/>

View file

@ -14,6 +14,7 @@
<?import tornadofx.control.Field?> <?import tornadofx.control.Field?>
<?import com.sparrowwallet.sparrow.control.CoinLabel?> <?import com.sparrowwallet.sparrow.control.CoinLabel?>
<?import com.sparrowwallet.sparrow.control.FiatLabel?> <?import com.sparrowwallet.sparrow.control.FiatLabel?>
<?import com.sparrowwallet.sparrow.control.CopyableLabel?>
<BorderPane stylesheets="@transactions.css, @wallet.css, @../general.css" styleClass="wallet-pane" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sparrowwallet.sparrow.wallet.TransactionsController"> <BorderPane stylesheets="@transactions.css, @wallet.css, @../general.css" styleClass="wallet-pane" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sparrowwallet.sparrow.wallet.TransactionsController">
<center> <center>
@ -32,13 +33,13 @@
<Form GridPane.columnIndex="0" GridPane.rowIndex="0"> <Form GridPane.columnIndex="0" GridPane.rowIndex="0">
<Fieldset inputGrow="SOMETIMES" text="Transactions" styleClass="header"> <Fieldset inputGrow="SOMETIMES" text="Transactions" styleClass="header">
<Field text="Balance:"> <Field text="Balance:">
<CoinLabel fx:id="balance"/> <CoinLabel fx:id="balance"/><Region HBox.hgrow="ALWAYS"/><FiatLabel fx:id="fiatBalance" minWidth="110" />
</Field>
<Field text="Fiat balance:">
<FiatLabel fx:id="fiatBalance" />
</Field> </Field>
<Field text="Mempool:"> <Field text="Mempool:">
<CoinLabel fx:id="mempoolBalance" /> <CoinLabel fx:id="mempoolBalance" /><Region HBox.hgrow="ALWAYS"/><FiatLabel fx:id="fiatMempoolBalance" minWidth="110" />
</Field>
<Field text="Transactions:">
<CopyableLabel fx:id="transactionCount" />
</Field> </Field>
</Fieldset> </Fieldset>
</Form> </Form>

View file

@ -22,7 +22,7 @@
</encoder> </encoder>
</appender> </appender>
<root level="info"> <root level="debug">
<appender-ref ref="FILE" /> <appender-ref ref="FILE" />
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT" />
</root> </root>

Binary file not shown.

Binary file not shown.

Binary file not shown.