diff --git a/build.gradle b/build.gradle index 2e6ebd99..91d1c710 100644 --- a/build.gradle +++ b/build.gradle @@ -124,8 +124,7 @@ dependencies { exclude group: 'org.slf4j' } implementation('com.sparrowwallet.bokmakierie:bokmakierie:1.0') - implementation('io.samourai.code.whirlpool:whirlpool-client:1.0.6') - implementation('io.samourai.code.wallet:java-http-client:2.0.2') + implementation('org.eclipse.jetty:jetty-client:9.4.54.v20240208') implementation('io.reactivex.rxjava2:rxjava:2.2.15') implementation('io.reactivex.rxjava2:rxjavafx:2.2.2') implementation('org.apache.commons:commons-lang3:3.7') diff --git a/drongo b/drongo index 3a2344f1..143d2816 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit 3a2344f1297e25a9c691ddad66264e00c391af44 +Subproject commit 143d28166a9a0b28469d1c57c460718e71803029 diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 71dae58b..83e86d64 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -27,10 +27,7 @@ import com.sparrowwallet.sparrow.net.ElectrumServer; import com.sparrowwallet.sparrow.net.ServerType; import com.sparrowwallet.sparrow.preferences.PreferenceGroup; import com.sparrowwallet.sparrow.preferences.PreferencesDialog; -import com.sparrowwallet.sparrow.soroban.CounterpartyDialog; import com.sparrowwallet.sparrow.paynym.PayNymDialog; -import com.sparrowwallet.sparrow.soroban.Soroban; -import com.sparrowwallet.sparrow.soroban.SorobanServices; import com.sparrowwallet.sparrow.transaction.TransactionController; import com.sparrowwallet.sparrow.transaction.TransactionData; import com.sparrowwallet.sparrow.transaction.TransactionView; @@ -187,9 +184,6 @@ public class AppController implements Initializable { @FXML private MenuItem sweepPrivateKey; - @FXML - private MenuItem findMixingPartner; - @FXML private MenuItem showPayNym; @@ -423,10 +417,6 @@ public class AppController implements Initializable { sendToMany.disableProperty().bind(exportWallet.disableProperty()); sweepPrivateKey.disableProperty().bind(Bindings.or(serverToggle.disableProperty(), AppServices.onlineProperty().not())); showPayNym.setDisable(true); - findMixingPartner.setDisable(true); - AppServices.onlineProperty().addListener((observable, oldValue, newValue) -> { - findMixingPartner.setDisable(exportWallet.isDisable() || getSelectedWalletForm() == null || !SorobanServices.canWalletMix(getSelectedWalletForm().getWallet()) || !newValue); - }); configureSwitchServer(); setServerType(Config.get().getServerType()); @@ -1459,75 +1449,6 @@ public class AppController implements Initializable { optTransaction.ifPresent(transaction -> addTransactionTab(null, null, transaction)); } - public void findMixingPartner(ActionEvent event) { - WalletForm selectedWalletForm = getSelectedWalletForm(); - if(selectedWalletForm != null) { - Wallet wallet = selectedWalletForm.getWallet(); - Soroban soroban = AppServices.getSorobanServices().getSoroban(selectedWalletForm.getWalletId()); - if(soroban.getHdWallet() == null) { - if(wallet.isEncrypted()) { - Wallet copy = wallet.copy(); - WalletPasswordDialog dlg = new WalletPasswordDialog(copy.getMasterName(), WalletPasswordDialog.PasswordRequirement.LOAD); - dlg.initOwner(rootStack.getScene().getWindow()); - Optional password = dlg.showAndWait(); - if(password.isPresent()) { - Storage storage = selectedWalletForm.getStorage(); - keyDerivationService = new Storage.KeyDerivationService(storage, password.get(), true); - keyDerivationService.setOnSucceeded(workerStateEvent -> { - EventManager.get().post(new StorageEvent(selectedWalletForm.getWalletId(), TimedEvent.Action.END, "Done")); - ECKey encryptionFullKey = keyDerivationService.getValue(); - Key key = new Key(encryptionFullKey.getPrivKeyBytes(), storage.getKeyDeriver().getSalt(), EncryptionType.Deriver.ARGON2); - copy.decrypt(key); - - try { - soroban.setHDWallet(copy); - CounterpartyDialog counterpartyDialog = new CounterpartyDialog(selectedWalletForm.getWalletId(), selectedWalletForm.getWallet()); - counterpartyDialog.initOwner(rootStack.getScene().getWindow()); - if(Config.get().isSameAppMixing()) { - counterpartyDialog.initModality(Modality.NONE); - } - counterpartyDialog.showAndWait(); - } finally { - key.clear(); - encryptionFullKey.clear(); - keyDerivationService = null; - } - }); - keyDerivationService.setOnFailed(workerStateEvent -> { - EventManager.get().post(new StorageEvent(selectedWalletForm.getWalletId(), TimedEvent.Action.END, "Failed")); - if(keyDerivationService.getException() instanceof InvalidPasswordException) { - Optional optResponse = showErrorDialog("Invalid Password", "The wallet password was invalid. Try again?", ButtonType.CANCEL, ButtonType.OK); - if(optResponse.isPresent() && optResponse.get().equals(ButtonType.OK)) { - Platform.runLater(() -> findMixingPartner(null)); - } - } else { - log.error("Error deriving wallet key", keyDerivationService.getException()); - } - keyDerivationService = null; - }); - EventManager.get().post(new StorageEvent(selectedWalletForm.getWalletId(), TimedEvent.Action.START, "Decrypting wallet...")); - keyDerivationService.start(); - } - } else { - soroban.setHDWallet(wallet); - CounterpartyDialog counterpartyDialog = new CounterpartyDialog(selectedWalletForm.getWalletId(), selectedWalletForm.getWallet()); - counterpartyDialog.initOwner(rootStack.getScene().getWindow()); - if(Config.get().isSameAppMixing()) { - counterpartyDialog.initModality(Modality.NONE); - } - counterpartyDialog.showAndWait(); - } - } else { - CounterpartyDialog counterpartyDialog = new CounterpartyDialog(selectedWalletForm.getWalletId(), selectedWalletForm.getWallet()); - counterpartyDialog.initOwner(rootStack.getScene().getWindow()); - if(Config.get().isSameAppMixing()) { - counterpartyDialog.initModality(Modality.NONE); - } - counterpartyDialog.showAndWait(); - } - } - } - public void showPayNym(ActionEvent event) { WalletForm selectedWalletForm = getSelectedWalletForm(); if(selectedWalletForm != null) { @@ -1727,14 +1648,6 @@ public class AppController implements Initializable { tabLabel.setGraphicTextGap(5.0); tab.setGraphic(tabLabel); tab.setClosable(true); - tab.setOnCloseRequest(event -> { - if(AppServices.getWhirlpoolServices().getWhirlpoolForMixToWallet(((WalletTabData)tab.getUserData()).getWalletForm().getWalletId()) != null) { - Optional optType = AppServices.showWarningDialog("Close mix to wallet?", "This wallet has been configured as the final destination for mixes, and needs to be open for this to occur.\n\nAre you sure you want to close?", ButtonType.NO, ButtonType.YES); - if(optType.isPresent() && optType.get() == ButtonType.NO) { - event.consume(); - } - } - }); TabPane subTabs = new TabPane(); subTabs.setSide(Side.LEFT); @@ -2554,7 +2467,6 @@ public class AppController implements Initializable { showLoadingLog.setDisable(true); showTxHex.setDisable(false); showPayNym.setDisable(true); - findMixingPartner.setDisable(true); } else if(event instanceof WalletTabSelectedEvent) { WalletTabSelectedEvent walletTabEvent = (WalletTabSelectedEvent)event; WalletTabData walletTabData = walletTabEvent.getWalletTabData(); @@ -2566,7 +2478,6 @@ public class AppController implements Initializable { showLoadingLog.setDisable(false); showTxHex.setDisable(true); showPayNym.setDisable(exportWallet.isDisable() || !walletTabData.getWallet().hasPaymentCode()); - findMixingPartner.setDisable(exportWallet.isDisable() || !SorobanServices.canWalletMix(walletTabData.getWallet()) || !AppServices.onlineProperty().get()); } } } @@ -2592,7 +2503,6 @@ public class AppController implements Initializable { if(selectedWalletForm.getWalletId().equals(event.getWalletId())) { exportWallet.setDisable(!event.getWallet().isValid() || selectedWalletForm.isLocked()); showPayNym.setDisable(exportWallet.isDisable() || !event.getWallet().hasPaymentCode()); - findMixingPartner.setDisable(exportWallet.isDisable() || !SorobanServices.canWalletMix(event.getWallet()) || !AppServices.onlineProperty().get()); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/AppServices.java b/src/main/java/com/sparrowwallet/sparrow/AppServices.java index 02decc4b..cd4418e3 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppServices.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppServices.java @@ -24,8 +24,6 @@ import com.sparrowwallet.sparrow.control.TrayManager; import com.sparrowwallet.sparrow.event.*; import com.sparrowwallet.sparrow.io.*; import com.sparrowwallet.sparrow.net.*; -import com.sparrowwallet.sparrow.soroban.SorobanServices; -import com.sparrowwallet.sparrow.whirlpool.WhirlpoolServices; import javafx.application.Application; import javafx.application.Platform; import javafx.beans.property.BooleanProperty; @@ -95,11 +93,7 @@ public class AppServices { private static AppServices INSTANCE; - private final WhirlpoolServices whirlpoolServices = new WhirlpoolServices(); - - private final SorobanServices sorobanServices = new SorobanServices(); - - private InteractionServices interactionServices; + private final InteractionServices interactionServices; private static HttpClientService httpClientService; @@ -188,8 +182,6 @@ public class AppServices { this.application = application; this.interactionServices = interactionServices; EventManager.get().register(this); - EventManager.get().register(whirlpoolServices); - EventManager.get().register(sorobanServices); } public void start() { @@ -534,14 +526,6 @@ public class AppServices { return INSTANCE; } - public static WhirlpoolServices getWhirlpoolServices() { - return get().whirlpoolServices; - } - - public static SorobanServices getSorobanServices() { - return get().sorobanServices; - } - public static InteractionServices getInteractionServices() { return get().interactionServices; } @@ -1095,6 +1079,37 @@ public class AppServices { return wallet; } + public static final List WHIRLPOOL_NETWORKS = List.of(Network.MAINNET, Network.TESTNET); + + public static boolean isWhirlpoolCompatible(Wallet wallet) { + return WHIRLPOOL_NETWORKS.contains(Network.get()) + && wallet.getScriptType() != ScriptType.P2TR //Taproot not yet supported + && wallet.getKeystores().size() == 1 + && wallet.getKeystores().get(0).hasSeed() + && wallet.getKeystores().get(0).getSeed().getType() == DeterministicSeed.Type.BIP39 + && wallet.getStandardAccountType() != null + && StandardAccount.isMixableAccount(wallet.getStandardAccountType()); + } + + public static boolean isWhirlpoolPostmixCompatible(Wallet wallet) { + return WHIRLPOOL_NETWORKS.contains(Network.get()) + && wallet.getScriptType() != ScriptType.P2TR //Taproot not yet supported + && wallet.getKeystores().size() == 1; + } + + public static List addWhirlpoolWallets(Wallet decryptedWallet, String walletId, Storage storage) { + List childWallets = new ArrayList<>(); + for(StandardAccount whirlpoolAccount : StandardAccount.WHIRLPOOL_ACCOUNTS) { + if(decryptedWallet.getChildWallet(whirlpoolAccount) == null) { + Wallet childWallet = decryptedWallet.addChildWallet(whirlpoolAccount); + childWallets.add(childWallet); + EventManager.get().post(new ChildWalletsAddedEvent(storage, decryptedWallet, childWallet)); + } + } + + return childWallets; + } + public static Font getMonospaceFont() { return Font.font("Roboto Mono", 13); } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/AddAccountDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/AddAccountDialog.java index a7ccf229..fab81a57 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/AddAccountDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/AddAccountDialog.java @@ -5,9 +5,6 @@ import com.sparrowwallet.drongo.wallet.StandardAccount; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.glyphfont.FontAwesome5; -import com.sparrowwallet.sparrow.io.Config; -import com.sparrowwallet.sparrow.net.ServerType; -import com.sparrowwallet.sparrow.whirlpool.WhirlpoolServices; import javafx.collections.FXCollections; import javafx.scene.control.*; import javafx.scene.layout.VBox; @@ -63,9 +60,9 @@ public class AddAccountDialog extends Dialog> { } } - if(WhirlpoolServices.canWalletMix(masterWallet) && !masterWallet.isWhirlpoolMasterWallet()) { + if(AppServices.isWhirlpoolCompatible(masterWallet) && !masterWallet.isWhirlpoolMasterWallet()) { availableAccounts.add(WHIRLPOOL_PREMIX); - } else if(WhirlpoolServices.canWatchPostmix(masterWallet) && !existingIndexes.contains(WHIRLPOOL_POSTMIX.getAccountNumber())) { + } else if(AppServices.isWhirlpoolPostmixCompatible(masterWallet) && !existingIndexes.contains(WHIRLPOOL_POSTMIX.getAccountNumber())) { availableAccounts.add(WHIRLPOOL_POSTMIX); } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/MixStatusCell.java b/src/main/java/com/sparrowwallet/sparrow/control/MixStatusCell.java index 14d3952e..10eb9ee8 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/MixStatusCell.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/MixStatusCell.java @@ -1,27 +1,11 @@ package com.sparrowwallet.sparrow.control; -import com.samourai.whirlpool.client.mix.listener.MixFailReason; -import com.samourai.whirlpool.client.mix.listener.MixStep; -import com.samourai.whirlpool.client.wallet.beans.MixProgress; -import com.samourai.whirlpool.protocol.beans.Utxo; -import com.sparrowwallet.sparrow.AppServices; -import com.sparrowwallet.sparrow.glyphfont.FontAwesome5; import com.sparrowwallet.sparrow.wallet.Entry; import com.sparrowwallet.sparrow.wallet.UtxoEntry; -import com.sparrowwallet.sparrow.whirlpool.Whirlpool; -import com.sparrowwallet.sparrow.whirlpool.WhirlpoolException; -import javafx.animation.Timeline; import javafx.geometry.Pos; import javafx.scene.control.*; -import javafx.util.Duration; -import org.controlsfx.glyphfont.Glyph; -import org.controlsfx.tools.Platform; - -import java.util.Locale; public class MixStatusCell extends TreeTableCell { - private static final int ERROR_DISPLAY_MILLIS = 5 * 60 * 1000; - public MixStatusCell() { super(); setAlignment(Pos.CENTER_RIGHT); @@ -41,167 +25,9 @@ public class MixStatusCell extends TreeTableCell { setGraphic(null); } else { setText(Integer.toString(mixStatus.getMixesDone())); - if(mixStatus.getNextMixUtxo() == null) { - setContextMenu(new MixStatusContextMenu(mixStatus.getUtxoEntry(), mixStatus.getMixProgress() != null && mixStatus.getMixProgress().getMixStep() != MixStep.FAIL)); - } else { - setContextMenu(null); - } - - if(mixStatus.getNextMixUtxo() != null) { - setMixSuccess(mixStatus.getNextMixUtxo()); - } else if(mixStatus.getMixFailReason() != null) { - setMixFail(mixStatus.getMixFailReason(), mixStatus.getMixError(), mixStatus.getMixErrorTimestamp()); - } else if(mixStatus.getMixProgress() != null) { - setMixProgress(mixStatus.getUtxoEntry(), mixStatus.getMixProgress()); - } else { - setGraphic(null); - setTooltip(null); - } - } - } - - private void setMixSuccess(Utxo nextMixUtxo) { - ProgressIndicator progressIndicator = getProgressIndicator(); - progressIndicator.setProgress(-1); - setGraphic(progressIndicator); - Tooltip tt = new Tooltip(); - tt.setText("Waiting for broadcast of " + nextMixUtxo.getHash().substring(0, 8) + "..." + ":" + nextMixUtxo.getIndex() ); - setTooltip(tt); - } - - private void setMixFail(MixFailReason mixFailReason, String mixError, Long mixErrorTimestamp) { - if(mixFailReason.isError()) { - long elapsed = mixErrorTimestamp == null ? 0L : System.currentTimeMillis() - mixErrorTimestamp; - if(elapsed >= ERROR_DISPLAY_MILLIS) { - //Old error, don't set again. - return; - } - - Glyph failGlyph = getFailGlyph(); - setGraphic(failGlyph); - Tooltip tt = new Tooltip(); - tt.setText(mixFailReason.getMessage() + (mixError == null ? "" : ": " + mixError) + - "\nMix failures are generally caused by peers disconnecting during a mix." + - "\nMake sure your internet connection is stable and the computer is configured to prevent sleeping." + - "\nTo prevent sleeping, use the " + getPlatformSleepConfig() + " or enable the function in the Tools menu."); - setTooltip(tt); - - Duration fadeDuration = Duration.millis(ERROR_DISPLAY_MILLIS - elapsed); - double fadeFromValue = 1.0 - ((double)elapsed / ERROR_DISPLAY_MILLIS); - Timeline timeline = AnimationUtil.getSlowFadeOut(failGlyph, fadeDuration, fadeFromValue, 10); - timeline.setOnFinished(event -> { - setTooltip(null); - }); - timeline.play(); - } else { + setContextMenu(null); setGraphic(null); setTooltip(null); } } - - private String getPlatformSleepConfig() { - Platform platform = Platform.getCurrent(); - if(platform == Platform.OSX) { - return "OSX System Preferences"; - } else if(platform == Platform.WINDOWS) { - return "Windows Control Panel"; - } - - return "system power settings"; - } - - private void setMixProgress(UtxoEntry utxoEntry, MixProgress mixProgress) { - if(mixProgress.getMixStep() != MixStep.FAIL) { - ProgressIndicator progressIndicator = getProgressIndicator(); - progressIndicator.setProgress(mixProgress.getMixStep().getProgressPercent() == 100 ? -1 : mixProgress.getMixStep().getProgressPercent() / 100.0); - setGraphic(progressIndicator); - Tooltip tt = new Tooltip(); - String status = mixProgress.getMixStep().getMessage().replaceAll("_", " "); - status = status.substring(0, 1).toUpperCase(Locale.ROOT) + status.substring(1).toLowerCase(Locale.ROOT); - if(mixProgress.getMixStep() == MixStep.REGISTER_INPUT) { - status += "\n\nThis progress is normal for one mixing UTXO per pool while waiting to be randomly selected for a mix.\n" + - "This may take hours or days, and time in the pool is generally more important than individual number of mixes.\n" + - "Each UTXO's anonymity set is dependent not only on its own mix count, but that of its peers as well."; - } - tt.setText(status); - setTooltip(tt); - } else { - setGraphic(null); - setTooltip(null); - } - } - - private ProgressIndicator getProgressIndicator() { - ProgressIndicator progressIndicator; - if(getGraphic() instanceof ProgressIndicator) { - progressIndicator = (ProgressIndicator)getGraphic(); - } else { - progressIndicator = new ProgressBar(); - } - - return progressIndicator; - } - - private static Glyph getMixGlyph() { - Glyph copyGlyph = new Glyph(FontAwesome5.FONT_NAME, FontAwesome5.Glyph.RANDOM); - copyGlyph.setFontSize(12); - return copyGlyph; - } - - private static Glyph getStopGlyph() { - Glyph copyGlyph = new Glyph(FontAwesome5.FONT_NAME, FontAwesome5.Glyph.STOP_CIRCLE); - copyGlyph.setFontSize(12); - return copyGlyph; - } - - public static Glyph getFailGlyph() { - Glyph failGlyph = new Glyph(FontAwesome5.FONT_NAME, FontAwesome5.Glyph.EXCLAMATION_CIRCLE); - failGlyph.getStyleClass().add("fail-warning"); - failGlyph.setFontSize(12); - return failGlyph; - } - - private static class MixStatusContextMenu extends ContextMenu { - public MixStatusContextMenu(UtxoEntry utxoEntry, boolean isMixing) { - Whirlpool pool = AppServices.getWhirlpoolServices().getWhirlpool(utxoEntry.getWallet()); - if(isMixing) { - MenuItem mixStop = new MenuItem("Stop Mixing"); - if(pool != null) { - mixStop.disableProperty().bind(pool.mixingProperty().not()); - } - mixStop.setGraphic(getStopGlyph()); - mixStop.setOnAction(event -> { - hide(); - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(utxoEntry.getWallet()); - if(whirlpool != null) { - try { - whirlpool.mixStop(utxoEntry.getHashIndex()); - } catch(WhirlpoolException e) { - AppServices.showErrorDialog("Error stopping mixing UTXO", e.getMessage()); - } - } - }); - getItems().add(mixStop); - } else { - MenuItem mixNow = new MenuItem("Mix Now"); - if(pool != null) { - mixNow.disableProperty().bind(pool.mixingProperty().not()); - } - - mixNow.setGraphic(getMixGlyph()); - mixNow.setOnAction(event -> { - hide(); - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(utxoEntry.getWallet()); - if(whirlpool != null) { - try { - whirlpool.mix(utxoEntry.getHashIndex()); - } catch(WhirlpoolException e) { - AppServices.showErrorDialog("Error mixing UTXO", e.getMessage()); - } - } - }); - getItems().add(mixNow); - } - } - } } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/PayNymAvatar.java b/src/main/java/com/sparrowwallet/sparrow/control/PayNymAvatar.java index 97c09bc5..70edca93 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/PayNymAvatar.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/PayNymAvatar.java @@ -79,10 +79,6 @@ public class PayNymAvatar extends StackPane { this.paymentCodeProperty.set(paymentCode); } - public void setPaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode paymentCode) { - setPaymentCode(PaymentCode.fromString(paymentCode.toString())); - } - public void clearPaymentCode() { this.paymentCodeProperty.set(null); } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/PayNymCell.java b/src/main/java/com/sparrowwallet/sparrow/control/PayNymCell.java index 7a81daab..b841414c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/PayNymCell.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/PayNymCell.java @@ -81,10 +81,7 @@ public class PayNymCell extends ListCell { linkButton.setDisable(true); payNymController.linkPayNym(payNym); }); - - if(payNymController.isSelectLinkedOnly()) { - getStyleClass().add("unlinked"); - } + getStyleClass().add("unlinked"); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/PaymentCodeTextField.java b/src/main/java/com/sparrowwallet/sparrow/control/PaymentCodeTextField.java index 2758e453..c22ab714 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/PaymentCodeTextField.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/PaymentCodeTextField.java @@ -10,11 +10,6 @@ public class PaymentCodeTextField extends CopyableTextField { setPaymentCodeString(); } - public void setPaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode paymentCode) { - this.paymentCodeStr = paymentCode.toString(); - setPaymentCodeString(); - } - private void setPaymentCodeString() { String abbrevPcode = paymentCodeStr.substring(0, 12) + "..." + paymentCodeStr.substring(paymentCodeStr.length() - 5); setText(abbrevPcode); diff --git a/src/main/java/com/sparrowwallet/sparrow/control/TransactionDiagram.java b/src/main/java/com/sparrowwallet/sparrow/control/TransactionDiagram.java index 6e3772f8..72c5b239 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/TransactionDiagram.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/TransactionDiagram.java @@ -12,11 +12,9 @@ import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.Theme; import com.sparrowwallet.sparrow.event.ExcludeUtxoEvent; import com.sparrowwallet.sparrow.event.ReplaceChangeAddressEvent; -import com.sparrowwallet.sparrow.event.SorobanInitiatedEvent; import com.sparrowwallet.sparrow.glyphfont.FontAwesome5; import com.sparrowwallet.sparrow.glyphfont.GlyphUtils; import com.sparrowwallet.sparrow.io.Config; -import com.sparrowwallet.sparrow.soroban.SorobanServices; import com.sparrowwallet.sparrow.wallet.OptimizationStrategy; import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; @@ -246,19 +244,9 @@ public class TransactionDiagram extends GridPane { } private List> getDisplayedUtxoSets() { - boolean addUserSet = getOptimizationStrategy() == OptimizationStrategy.PRIVACY && SorobanServices.canWalletMix(walletTx.getWallet()) - && walletTx.getPayments().size() == 1 - && (walletTx.getPayments().get(0).getAddress().getScriptType() == walletTx.getWallet().getFreshNode(KeyPurpose.RECEIVE).getAddress().getScriptType()); - List> displayedUtxoSets = new ArrayList<>(); for(Map selectedUtxoSet : walletTx.getSelectedUtxoSets()) { - displayedUtxoSets.add(getDisplayedUtxos(selectedUtxoSet, addUserSet ? 2 : walletTx.getSelectedUtxoSets().size())); - } - - if(addUserSet && displayedUtxoSets.size() == 1) { - Map addUserUtxoSet = new HashMap<>(); - addUserUtxoSet.put(new AddUserBlockTransactionHashIndex(!walletTx.isTwoPersonCoinjoin()), null); - displayedUtxoSets.add(addUserUtxoSet); + displayedUtxoSets.add(getDisplayedUtxos(selectedUtxoSet, walletTx.getSelectedUtxoSets().size())); } List> paddedUtxoSets = new ArrayList<>(); @@ -339,11 +327,9 @@ public class TransactionDiagram extends GridPane { double setHeight = (height / numSets) - 5; for(int set = 0; set < numSets; set++) { boolean externalUserSet = displayedUtxoSets.get(set).values().stream().anyMatch(Objects::nonNull); - boolean addUserSet = displayedUtxoSets.get(set).keySet().stream().anyMatch(ref -> ref instanceof AddUserBlockTransactionHashIndex); - if(externalUserSet || addUserSet) { - boolean replace = !isFinal() && set > 0 && SorobanServices.canWalletMix(walletTx.getWallet()); - Glyph bracketGlyph = !replace && walletTx.isCoinControlUsed() ? getLockGlyph() : (addUserSet ? getUserAddGlyph() : getCoinsGlyph(replace)); - String tooltipText = addUserSet ? "Click to add a mix partner" : (walletTx.getWallet().getFullDisplayName() + (replace ? "\nClick to replace with a mix partner" : "")); + if(externalUserSet) { + Glyph bracketGlyph = walletTx.isCoinControlUsed() ? getLockGlyph() : getCoinsGlyph(); + String tooltipText = walletTx.getWallet().getFullDisplayName(); StackPane stackPane = getBracket(width, setHeight, bracketGlyph, tooltipText); allBrackets.getChildren().add(stackPane); } else { @@ -474,14 +460,6 @@ public class TransactionDiagram extends GridPane { tooltip.setText(joiner.toString()); } else if(input instanceof InvisibleBlockTransactionHashIndex) { tooltip.setText(""); - } else if(input instanceof AddUserBlockTransactionHashIndex) { - tooltip.setText(""); - label.setGraphic(walletTx.isTwoPersonCoinjoin() ? getQuestionGlyph() : getFeeWarningGlyph()); - label.setOnMouseClicked(event -> { - EventManager.get().post(new SorobanInitiatedEvent(walletTx.getWallet())); - closeExpanded(); - event.consume(); - }); } else { if(walletTx.getInputTransactions() != null && walletTx.getInputTransactions().get(input.getHash()) != null) { BlockTransaction blockTransaction = walletTx.getInputTransactions().get(input.getHash()); @@ -570,7 +548,7 @@ public class TransactionDiagram extends GridPane { CubicCurve curve = new CubicCurve(); curve.getStyleClass().add("input-line"); - if(inputs.get(numUtxos-i) instanceof PayjoinBlockTransactionHashIndex || inputs.get(numUtxos-i) instanceof AddUserBlockTransactionHashIndex) { + if(inputs.get(numUtxos-i) instanceof PayjoinBlockTransactionHashIndex) { curve.getStyleClass().add("input-dashed-line"); } else if(inputs.get(numUtxos-i) instanceof InvisibleBlockTransactionHashIndex) { continue; @@ -952,46 +930,10 @@ public class TransactionDiagram extends GridPane { return null; } - private Glyph getUserAddGlyph() { - Glyph userAddGlyph = new Glyph(FontAwesome5.FONT_NAME, FontAwesome5.Glyph.USER_PLUS); - userAddGlyph.getStyleClass().add("useradd-icon"); - userAddGlyph.setFontSize(12); - userAddGlyph.setOnMouseEntered(event -> { - userAddGlyph.setFontSize(18); - }); - userAddGlyph.setOnMouseExited(event -> { - userAddGlyph.setFontSize(12); - }); - userAddGlyph.setOnMouseClicked(event -> { - EventManager.get().post(new SorobanInitiatedEvent(walletTx.getWallet())); - closeExpanded(); - event.consume(); - }); - return userAddGlyph; - } - - private Glyph getCoinsGlyph(boolean allowReplacement) { + private Glyph getCoinsGlyph() { Glyph coinsGlyph = new Glyph(FontAwesome5.FONT_NAME, FontAwesome5.Glyph.COINS); coinsGlyph.setFontSize(12); - if(allowReplacement) { - coinsGlyph.getStyleClass().add("coins-replace-icon"); - coinsGlyph.setOnMouseEntered(event -> { - coinsGlyph.setIcon(FontAwesome5.Glyph.USER_PLUS); - coinsGlyph.setFontSize(18); - }); - coinsGlyph.setOnMouseExited(event -> { - coinsGlyph.setIcon(FontAwesome5.Glyph.COINS); - coinsGlyph.setFontSize(12); - }); - coinsGlyph.setOnMouseClicked(event -> { - EventManager.get().post(new SorobanInitiatedEvent(walletTx.getWallet())); - closeExpanded(); - event.consume(); - }); - } else { - coinsGlyph.getStyleClass().add("coins-icon"); - } - + coinsGlyph.getStyleClass().add("coins-icon"); return coinsGlyph; } @@ -1094,20 +1036,6 @@ public class TransactionDiagram extends GridPane { } } - private static class AddUserBlockTransactionHashIndex extends BlockTransactionHashIndex { - private final boolean required; - - public AddUserBlockTransactionHashIndex(boolean required) { - super(Sha256Hash.ZERO_HASH, 0, new Date(), 0L, 0, 0); - this.required = required; - } - - @Override - public String getLabel() { - return "Add Mix Partner" + (required ? "" : "?"); - } - } - public static class AdditionalPayment extends Payment { private final List additionalPayments; diff --git a/src/main/java/com/sparrowwallet/sparrow/event/SorobanInitiatedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/SorobanInitiatedEvent.java deleted file mode 100644 index f4abe67a..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/event/SorobanInitiatedEvent.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.sparrowwallet.sparrow.event; - -import com.sparrowwallet.drongo.wallet.Wallet; - -public class SorobanInitiatedEvent { - private Wallet wallet; - - public SorobanInitiatedEvent(Wallet wallet) { - this.wallet = wallet; - } - - public Wallet getWallet() { - return wallet; - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/event/SpendUtxoEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/SpendUtxoEvent.java index 8d73103b..4816820e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/SpendUtxoEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/SpendUtxoEvent.java @@ -1,6 +1,5 @@ package com.sparrowwallet.sparrow.event; -import com.samourai.whirlpool.client.whirlpool.beans.Pool; import com.sparrowwallet.drongo.bip47.PaymentCode; import com.sparrowwallet.drongo.wallet.BlockTransaction; import com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex; @@ -17,7 +16,6 @@ public class SpendUtxoEvent { private final Long fee; private final boolean requireAllUtxos; private final BlockTransaction replacedTransaction; - private final Pool pool; private final PaymentCode paymentCode; public SpendUtxoEvent(Wallet wallet, List utxos) { @@ -32,19 +30,6 @@ public class SpendUtxoEvent { this.fee = fee; this.requireAllUtxos = requireAllUtxos; this.replacedTransaction = replacedTransaction; - this.pool = null; - this.paymentCode = null; - } - - public SpendUtxoEvent(Wallet wallet, List utxos, List payments, List opReturns, Long fee, Pool pool) { - this.wallet = wallet; - this.utxos = utxos; - this.payments = payments; - this.opReturns = opReturns; - this.fee = fee; - this.requireAllUtxos = false; - this.replacedTransaction = null; - this.pool = pool; this.paymentCode = null; } @@ -56,7 +41,6 @@ public class SpendUtxoEvent { this.fee = null; this.requireAllUtxos = false; this.replacedTransaction = null; - this.pool = null; this.paymentCode = paymentCode; } @@ -88,10 +72,6 @@ public class SpendUtxoEvent { return replacedTransaction; } - public Pool getPool() { - return pool; - } - public PaymentCode getPaymentCode() { return paymentCode; } diff --git a/src/main/java/com/sparrowwallet/sparrow/event/WhirlpoolIndexHighFrequencyEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/WhirlpoolIndexHighFrequencyEvent.java deleted file mode 100644 index a91065ca..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/event/WhirlpoolIndexHighFrequencyEvent.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.sparrowwallet.sparrow.event; - -import com.sparrowwallet.drongo.wallet.Wallet; - -public class WhirlpoolIndexHighFrequencyEvent { - private final Wallet wallet; - - public WhirlpoolIndexHighFrequencyEvent(Wallet wallet) { - this.wallet = wallet; - } - - public Wallet getWallet() { - return wallet; - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/event/WhirlpoolMixEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/WhirlpoolMixEvent.java deleted file mode 100644 index b7e6844a..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/event/WhirlpoolMixEvent.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.sparrowwallet.sparrow.event; - -import com.samourai.whirlpool.client.mix.listener.MixFailReason; -import com.samourai.whirlpool.client.wallet.beans.MixProgress; -import com.samourai.whirlpool.protocol.beans.Utxo; -import com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex; -import com.sparrowwallet.drongo.wallet.Wallet; - -public class WhirlpoolMixEvent { - private final Wallet wallet; - private final BlockTransactionHashIndex utxo; - private final MixProgress mixProgress; - private final Utxo nextUtxo; - private final MixFailReason mixFailReason; - private final String mixError; - - public WhirlpoolMixEvent(Wallet wallet, BlockTransactionHashIndex utxo, MixProgress mixProgress) { - this.wallet = wallet; - this.utxo = utxo; - this.mixProgress = mixProgress; - this.nextUtxo = null; - this.mixFailReason = null; - this.mixError = null; - } - - public WhirlpoolMixEvent(Wallet wallet, BlockTransactionHashIndex utxo, Utxo nextUtxo) { - this.wallet = wallet; - this.utxo = utxo; - this.mixProgress = null; - this.nextUtxo = nextUtxo; - this.mixFailReason = null; - this.mixError = null; - } - - public WhirlpoolMixEvent(Wallet wallet, BlockTransactionHashIndex utxo, MixFailReason mixFailReason, String mixError) { - this.wallet = wallet; - this.utxo = utxo; - this.mixProgress = null; - this.nextUtxo = null; - this.mixFailReason = mixFailReason; - this.mixError = mixError; - } - - public Wallet getWallet() { - return wallet; - } - - public BlockTransactionHashIndex getUtxo() { - return utxo; - } - - public MixProgress getMixProgress() { - return mixProgress; - } - - public Utxo getNextUtxo() { - return nextUtxo; - } - - public MixFailReason getMixFailReason() { - return mixFailReason; - } - - public String getMixError() { - return mixError; - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/event/WhirlpoolMixSuccessEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/WhirlpoolMixSuccessEvent.java deleted file mode 100644 index 0eb1170e..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/event/WhirlpoolMixSuccessEvent.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.sparrowwallet.sparrow.event; - -import com.samourai.whirlpool.protocol.beans.Utxo; -import com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex; -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.drongo.wallet.WalletNode; - -public class WhirlpoolMixSuccessEvent extends WhirlpoolMixEvent { - private final WalletNode walletNode; - - public WhirlpoolMixSuccessEvent(Wallet wallet, BlockTransactionHashIndex utxo, Utxo nextUtxo, WalletNode walletNode) { - super(wallet, utxo, nextUtxo); - this.walletNode = walletNode; - } - - public WalletNode getWalletNode() { - return walletNode; - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Config.java b/src/main/java/com/sparrowwallet/sparrow/io/Config.java index 5d26a0a7..ce515122 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Config.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Config.java @@ -77,7 +77,6 @@ public class Config { private int maxServerTimeout = DEFAULT_MAX_TIMEOUT; private int maxPageSize = DEFAULT_PAGE_SIZE; private boolean usePayNym; - private boolean sameAppMixing; private boolean mempoolFullRbf; private Double appWidth; private Double appHeight; @@ -662,15 +661,6 @@ public class Config { flush(); } - public boolean isSameAppMixing() { - return sameAppMixing; - } - - public void setSameAppMixing(boolean sameAppMixing) { - this.sameAppMixing = sameAppMixing; - flush(); - } - public boolean isMempoolFullRbf() { return mempoolFullRbf; } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Samourai.java b/src/main/java/com/sparrowwallet/sparrow/io/Samourai.java index 3fe0eb7f..6c1ad73e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Samourai.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Samourai.java @@ -4,9 +4,8 @@ import com.google.common.io.CharStreams; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; -import com.samourai.wallet.crypto.AESUtil; -import com.samourai.wallet.util.CharSequenceX; import com.sparrowwallet.drongo.Utils; +import com.sparrowwallet.drongo.crypto.SamouraiUtil; import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.wallet.*; @@ -46,9 +45,9 @@ public class Samourai implements KeystoreFileImport { String decrypted; if(version == 1) { - decrypted = AESUtil.decrypt(payload, new CharSequenceX(password), AESUtil.DefaultPBKDF2Iterations); + decrypted = SamouraiUtil.decrypt(payload, password, SamouraiUtil.DefaultPBKDF2Iterations); } else if(version == 2) { - decrypted = AESUtil.decryptSHA256(payload, new CharSequenceX(password)); + decrypted = SamouraiUtil.decryptSHA256(payload, password); } else { throw new ImportException("Unsupported backup version: " + version); } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index 1af64d79..57a57b39 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -8,8 +8,6 @@ import com.sparrowwallet.drongo.wallet.StandardAccount; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.SparrowWallet; -import com.sparrowwallet.sparrow.soroban.Soroban; -import com.sparrowwallet.sparrow.whirlpool.Whirlpool; import javafx.concurrent.ScheduledService; import javafx.concurrent.Service; import javafx.concurrent.Task; @@ -174,15 +172,6 @@ public class Storage { } } - if(wallet.isWhirlpoolMasterWallet()) { - String walletId = getWalletId(wallet); - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(walletId); - whirlpool.setScode(wallet.getMasterMixConfig().getScode()); - whirlpool.setHDWallet(getWalletId(wallet), copy); - Soroban soroban = AppServices.getSorobanServices().getSoroban(walletId); - soroban.setHDWallet(copy); - } - StandardAccount standardAccount = wallet.getStandardAccountType(); if(standardAccount != null && standardAccount.getMinimumGapLimit() != null && wallet.gapLimit() == null) { wallet.setGapLimit(standardAccount.getMinimumGapLimit()); diff --git a/src/main/java/com/sparrowwallet/sparrow/net/BroadcastSource.java b/src/main/java/com/sparrowwallet/sparrow/net/BroadcastSource.java index d338736f..2b8dbd08 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/BroadcastSource.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/BroadcastSource.java @@ -1,12 +1,12 @@ package com.sparrowwallet.sparrow.net; import com.google.common.net.HostAndPort; -import com.samourai.wallet.httpClient.HttpResponseException; import com.sparrowwallet.drongo.Network; import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.protocol.Sha256Hash; import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.sparrow.AppServices; +import com.sparrowwallet.sparrow.net.http.client.HttpResponseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/sparrowwallet/sparrow/net/ExchangeSource.java b/src/main/java/com/sparrowwallet/sparrow/net/ExchangeSource.java index 8a62dd2f..04078fa5 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/ExchangeSource.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/ExchangeSource.java @@ -1,8 +1,8 @@ package com.sparrowwallet.sparrow.net; -import com.samourai.wallet.httpClient.HttpResponseException; import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.event.ExchangeRatesUpdatedEvent; +import com.sparrowwallet.sparrow.net.http.client.HttpResponseException; import javafx.concurrent.ScheduledService; import javafx.concurrent.Service; import javafx.concurrent.Task; diff --git a/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java b/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java index 1eed8425..4171d319 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java @@ -1,12 +1,10 @@ package com.sparrowwallet.sparrow.net; import com.google.common.net.HostAndPort; -import com.samourai.http.client.JettyHttpClientService; -import com.samourai.wallet.httpClient.HttpUsage; -import com.samourai.wallet.httpClient.IHttpClient; -import com.samourai.wallet.util.AsyncUtil; -import com.samourai.wallet.util.ThreadUtil; -import com.samourai.whirlpool.client.utils.ClientUtils; +import com.sparrowwallet.sparrow.net.http.client.AsyncUtil; +import com.sparrowwallet.sparrow.net.http.client.HttpUsage; +import com.sparrowwallet.sparrow.net.http.client.IHttpClient; +import com.sparrowwallet.sparrow.net.http.client.JettyHttpClientService; import io.reactivex.Observable; import javafx.concurrent.Service; import javafx.concurrent.Task; @@ -22,15 +20,15 @@ public class HttpClientService extends JettyHttpClientService { } public T requestJson(String url, Class responseType, Map headers) throws Exception { - return getHttpClient(HttpUsage.BACKEND).getJson(url, responseType, headers); + return getHttpClient(HttpUsage.DEFAULT).getJson(url, responseType, headers); } public Observable> postJson(String url, Class responseType, Map headers, Object body) { - return getHttpClient(HttpUsage.BACKEND).postJson(url, responseType, headers, body).toObservable(); + return getHttpClient(HttpUsage.DEFAULT).postJson(url, responseType, headers, body).toObservable(); } public String postString(String url, Map headers, String contentType, String content) throws Exception { - IHttpClient httpClient = getHttpClient(HttpUsage.BACKEND); + IHttpClient httpClient = getHttpClient(HttpUsage.DEFAULT); return AsyncUtil.getInstance().blockingGet(httpClient.postString(url, headers, contentType, content)).get(); } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/HttpProxySupplier.java b/src/main/java/com/sparrowwallet/sparrow/net/HttpProxySupplier.java index 5a832652..41509f52 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/HttpProxySupplier.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/HttpProxySupplier.java @@ -1,10 +1,10 @@ package com.sparrowwallet.sparrow.net; import com.google.common.net.HostAndPort; -import com.samourai.http.client.IHttpProxySupplier; -import com.samourai.wallet.httpClient.HttpProxy; -import com.samourai.wallet.httpClient.HttpProxyProtocol; -import com.samourai.wallet.httpClient.HttpUsage; +import com.sparrowwallet.sparrow.net.http.client.HttpProxy; +import com.sparrowwallet.sparrow.net.http.client.HttpProxyProtocol; +import com.sparrowwallet.sparrow.net.http.client.HttpUsage; +import com.sparrowwallet.sparrow.net.http.client.IHttpProxySupplier; import java.util.Optional; diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/AsyncUtil.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/AsyncUtil.java new file mode 100644 index 00000000..8449a1ee --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/AsyncUtil.java @@ -0,0 +1,150 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import io.reactivex.Completable; +import io.reactivex.Observable; +import io.reactivex.Single; +import io.reactivex.functions.Action; +import io.reactivex.schedulers.Schedulers; +import org.slf4j.MDC; + +import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +public class AsyncUtil { + private static final ThreadUtil threadUtil = ThreadUtil.getInstance(); + private static AsyncUtil instance; + + public static AsyncUtil getInstance() { + if(instance == null) { + instance = new AsyncUtil(); + } + return instance; + } + + public T unwrapException(Callable c) throws Exception { + try { + return c.call(); + } catch(RuntimeException e) { + // blockingXXX wraps errors with RuntimeException, unwrap it + throw unwrapException(e); + } + } + + public Exception unwrapException(Exception e) throws Exception { + if(e.getCause() != null && e.getCause() instanceof Exception) { + throw (Exception) e.getCause(); + } + throw e; + } + + public T blockingGet(Single o) throws Exception { + try { + return unwrapException(o::blockingGet); + } catch(ExecutionException e) { + // blockingGet(threadUtil.runWithTimeoutAndRetry()) wraps InterruptedException("exit (done)") + // with ExecutionException, unwrap it + throw unwrapException(e); + } + } + + public T blockingGet(Single o, long timeoutMs) throws Exception { + Callable callable = () -> blockingGet(o); + return blockingGet(runAsync(callable, timeoutMs)); + } + + public T blockingGet(Future o, long timeoutMs) throws Exception { + return o.get(timeoutMs, TimeUnit.MILLISECONDS); + } + + public T blockingLast(Observable o) throws Exception { + return unwrapException(o::blockingLast); + } + + public void blockingAwait(Completable o) throws Exception { + Callable callable = () -> { + o.blockingAwait(); + return Optional.empty(); + }; + unwrapException(callable); + } + + public void blockingAwait(Completable o, long timeoutMs) throws Exception { + Callable callable = () -> { + o.blockingAwait(); + return Optional.empty(); + }; + blockingGet(runAsync(callable, timeoutMs)); + } + + public Single timeout(Single o, long timeoutMs) { + try { + return Single.just(blockingGet(o, timeoutMs)); + } catch(Exception e) { + return Single.error(e); + } + }/* + + public Completable timeout(Completable o, long timeoutMs) { + try { + return Completable.fromCallable(() -> { + blockingAwait(o, timeoutMs); + return Optional.empty(); + }); + } catch (Exception e) { + return Completable.error(e); + } + }*/ + + public Single runIOAsync(final Callable callable) { + return Single.fromCallable(callable).subscribeOn(Schedulers.io()); + } + + public Completable runIOAsyncCompletable(final Action action) { + return Completable.fromAction(action).subscribeOn(Schedulers.io()); + } + + public T runIO(final Callable callable) throws Exception { + return blockingGet(runIOAsync(callable)); + } + + public void runIO(final Action action) throws Exception { + blockingAwait(runIOAsyncCompletable(action)); + } + + public Completable runAsync(Runnable runnable, long timeoutMs) { + Future future = runAsync(() -> { + runnable.run(); + return Optional.empty(); // must return an object for using Completable.fromSingle() + }); + return Completable.fromSingle(Single.fromFuture(future, timeoutMs, TimeUnit.MILLISECONDS)); + } + + public Future runAsync(Callable callable) { + // preserve logging context + String mdc = mdcAppend("runAsync=" + System.currentTimeMillis()); + return threadUtil.runAsync(() -> { + MDC.put("mdc", mdc); + return callable.call(); + }); + } + + public Single runAsync(Callable callable, long timeoutMs) { + Future future = runAsync(callable); + return Single.fromFuture(future, timeoutMs, TimeUnit.MILLISECONDS); + } + + private static String mdcAppend(String info) { + String mdc = MDC.get("mdc"); + if(mdc == null) { + mdc = ""; + } else { + mdc += ","; + } + mdc += info; + return mdc; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpException.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpException.java new file mode 100644 index 00000000..5fb0d057 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpException.java @@ -0,0 +1,12 @@ +package com.sparrowwallet.sparrow.net.http.client; + +public abstract class HttpException extends Exception { + + public HttpException(Exception cause) { + super(cause); + } + + public HttpException(String message) { + super(message); + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpNetworkException.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpNetworkException.java new file mode 100644 index 00000000..ca7be862 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpNetworkException.java @@ -0,0 +1,11 @@ +package com.sparrowwallet.sparrow.net.http.client; + +public class HttpNetworkException extends HttpException { + public HttpNetworkException(String message) { + super(message); + } + + public HttpNetworkException(Exception cause) { + super(cause); + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpProxy.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpProxy.java new file mode 100644 index 00000000..7d1f3793 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpProxy.java @@ -0,0 +1,41 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import org.apache.commons.lang3.StringUtils; + +import java.util.Arrays; + +public class HttpProxy { + private final HttpProxyProtocol protocol; + private final String host; + private final int port; + + public HttpProxy(HttpProxyProtocol protocol, String host, int port) { + this.protocol = protocol; + this.host = host; + this.port = port; + } + + public static boolean validate(String proxy) { + // check protocol + String[] protocols = Arrays.stream(HttpProxyProtocol.values()).map(p -> p.name()).toArray(String[]::new); + String regex = "^(" + StringUtils.join(protocols, "|").toLowerCase() + ")://(.+?):([0-9]+)"; + return proxy.trim().toLowerCase().matches(regex); + } + + public HttpProxyProtocol getProtocol() { + return protocol; + } + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } + + @Override + public String toString() { + return protocol + "://" + host + ":" + port; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpProxyProtocol.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpProxyProtocol.java new file mode 100644 index 00000000..7e8f131a --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpProxyProtocol.java @@ -0,0 +1,17 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import java.util.Optional; + +public enum HttpProxyProtocol { + HTTP, + SOCKS, + SOCKS5; + + public static Optional find(String value) { + try { + return Optional.of(valueOf(value)); + } catch(Exception e) { + return Optional.empty(); + } + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpResponseException.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpResponseException.java new file mode 100644 index 00000000..990fdc1b --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpResponseException.java @@ -0,0 +1,38 @@ +package com.sparrowwallet.sparrow.net.http.client; + +public class HttpResponseException extends HttpException { + private final String responseBody; + private final int statusCode; + + public HttpResponseException(Exception cause, String responseBody, int statusCode) { + super(cause); + this.responseBody = responseBody; + this.statusCode = statusCode; + } + + public HttpResponseException(String message, String responseBody, int statusCode) { + super(message); + this.responseBody = responseBody; + this.statusCode = statusCode; + } + + public HttpResponseException(String responseBody, int statusCode) { + this("response statusCode=" + statusCode, responseBody, statusCode); + } + + public String getResponseBody() { + return responseBody; + } + + public int getStatusCode() { + return statusCode; + } + + @Override + public String toString() { + return "HttpResponseException{" + + "message=" + getMessage() + ", " + + "responseBody='" + responseBody + '\'' + + '}'; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpSystemException.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpSystemException.java new file mode 100644 index 00000000..1ac3adf8 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpSystemException.java @@ -0,0 +1,11 @@ +package com.sparrowwallet.sparrow.net.http.client; + +public class HttpSystemException extends HttpException { + public HttpSystemException(String message) { + super(message); + } + + public HttpSystemException(Exception cause) { + super(cause); + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpUsage.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpUsage.java new file mode 100644 index 00000000..3e5dbb97 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/HttpUsage.java @@ -0,0 +1,39 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import java.util.Objects; + +public class HttpUsage { + public static final HttpUsage DEFAULT = new HttpUsage("Default"); + + private final String name; + + public HttpUsage(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return name; + } + + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + if(o == null || getClass() != o.getClass()) { + return false; + } + HttpUsage httpUsage = (HttpUsage) o; + return Objects.equals(name, httpUsage.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/IBackendClient.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/IBackendClient.java new file mode 100644 index 00000000..0ec438bc --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/IBackendClient.java @@ -0,0 +1,11 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import java.util.Map; + +public interface IBackendClient { + T getJson(String url, Class responseType, Map headers) throws HttpException; + + T getJson(String url, Class responseType, Map headers, boolean async) throws HttpException; + + T postUrlEncoded(String url, Class responseType, Map headers, Map body) throws Exception; +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/IHttpClient.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/IHttpClient.java new file mode 100644 index 00000000..88e2925d --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/IHttpClient.java @@ -0,0 +1,14 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import io.reactivex.Single; + +import java.util.Map; +import java.util.Optional; + +public interface IHttpClient extends IBackendClient { + void connect() throws Exception; + + Single> postJson(String url, Class responseType, Map headers, Object body); + + Single> postString(String urlStr, Map headers, String contentType, String content); +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/IHttpClientService.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/IHttpClientService.java new file mode 100644 index 00000000..ed01db5c --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/IHttpClientService.java @@ -0,0 +1,9 @@ +package com.sparrowwallet.sparrow.net.http.client; + +public interface IHttpClientService { + IHttpClient getHttpClient(HttpUsage httpUsage); + + void changeIdentity(); // change Tor identity if any + + void stop(); +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/IHttpProxySupplier.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/IHttpProxySupplier.java new file mode 100644 index 00000000..0839699f --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/IHttpProxySupplier.java @@ -0,0 +1,9 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import java.util.Optional; + +public interface IHttpProxySupplier { + Optional getHttpProxy(HttpUsage httpUsage); + + void changeIdentity(); +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/JSONUtils.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/JSONUtils.java new file mode 100644 index 00000000..0d3b1679 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/JSONUtils.java @@ -0,0 +1,28 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +public class JSONUtils { + private static JSONUtils instance; + + private ObjectMapper objectMapper; + + public JSONUtils() { + objectMapper = new ObjectMapper(); + objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); + } + + public static final JSONUtils getInstance() { + if(instance == null) { + instance = new JSONUtils(); + } + return instance; + } + + public ObjectMapper getObjectMapper() { + return objectMapper; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/JacksonHttpClient.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/JacksonHttpClient.java new file mode 100644 index 00000000..55ee2cd9 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/JacksonHttpClient.java @@ -0,0 +1,168 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.reactivex.Single; +import io.reactivex.schedulers.Schedulers; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.function.Consumer; + +@SuppressWarnings("unchecked") +public abstract class JacksonHttpClient implements IHttpClient { + private static final Logger log = LoggerFactory.getLogger(JacksonHttpClient.class); + + private final Consumer onNetworkError; + + public JacksonHttpClient(Consumer onNetworkError) { + this.onNetworkError = onNetworkError; + } + + protected abstract String requestJsonGet(String urlStr, Map headers, boolean async) throws HttpException; + + protected abstract String requestJsonPost(String urlStr, Map headers, String jsonBody) throws HttpException; + + protected abstract String requestStringPost(String urlStr, Map headers, String contentType, String content) throws HttpException; + + protected abstract String requestJsonPostUrlEncoded(String urlStr, Map headers, Map body) throws HttpException; + + @Override + public T getJson(String urlStr, Class responseType, Map headers) throws HttpException { + return getJson(urlStr, responseType, headers, false); + } + + @Override + public T getJson(String urlStr, Class responseType, Map headers, boolean async) throws HttpException { + return httpObservableBlockingSingle(() -> { // run on ioThread + try { + String responseContent = handleNetworkError("getJson " + urlStr, () -> requestJsonGet(urlStr, headers, async)); + return parseJson(responseContent, responseType, 200); + } catch(Exception e) { + if(log.isDebugEnabled()) { + log.error("getJson failed: " + urlStr + ": " + e.toString()); + } + throw e; + } + }); + } + + @Override + public Single> postJson(final String urlStr, final Class responseType, final Map headers, final Object bodyObj) { + return httpObservable( + () -> { + try { + String jsonBody = getObjectMapper().writeValueAsString(bodyObj); + String responseContent = handleNetworkError("postJson " + urlStr, () -> requestJsonPost(urlStr, headers, jsonBody)); + return parseJson(responseContent, responseType, 200); + } catch(HttpException e) { + if(log.isDebugEnabled()) { + log.error("postJson failed: " + urlStr + ": " + e); + } + throw e; + } + }); + } + + @Override + public Single> postString(String urlStr, Map headers, String contentType, String content) { + return httpObservable( + () -> { + try { + return handleNetworkError("postString " + urlStr, () -> requestStringPost(urlStr, headers, contentType, content)); + } catch(HttpException e) { + if(log.isDebugEnabled()) { + log.error("postJson failed: " + urlStr + ": " + e.toString()); + } + throw e; + } + }); + } + + @Override + public T postUrlEncoded(String urlStr, Class responseType, Map headers, Map body) throws HttpException { + return httpObservableBlockingSingle(() -> { // run on ioThread + try { + String responseContent = handleNetworkError("postUrlEncoded " + urlStr, () -> requestJsonPostUrlEncoded(urlStr, headers, body)); + return parseJson(responseContent, responseType, 200); + } catch(Exception e) { + if(log.isDebugEnabled()) { + log.error("postUrlEncoded failed: " + urlStr + ": " + e); + } + throw e; + } + }); + } + + private T parseJson(String responseContent, Class responseType, int statusCode) throws HttpException { + T result; + if(log.isTraceEnabled()) { + String responseStr = (responseContent != null ? responseContent : "null"); + if(responseStr.length() > 500) { + responseStr = responseStr.substring(0, 500) + "..."; + } + log.trace("response[" + (responseType != null ? responseType.getCanonicalName() : "null") + "]: " + responseStr); + } + if(String.class.equals(responseType)) { + result = (T) responseContent; + } else { + try { + result = getObjectMapper().readValue(responseContent, responseType); + } catch(Exception e) { + throw new HttpResponseException(e, responseContent, statusCode); + } + } + return result; + } + + protected String handleNetworkError(String logInfo, Callable doHttpRequest) throws HttpException { + try { + try { + // first attempt + return doHttpRequest.call(); + } catch(HttpNetworkException e) { + if(log.isDebugEnabled()) { + log.warn("HTTP_ERROR_NETWORK " + logInfo + ", retrying: " + e.getMessage()); + } + // change tor proxy + onNetworkError(e); + + // retry second attempt + return doHttpRequest.call(); + } + } catch(HttpException e) { // forward + throw e; + } catch(Exception e) { // should never happen + throw new HttpSystemException(e); + } + } + + protected void onNetworkError(HttpNetworkException e) { + if(onNetworkError != null) { + synchronized(JacksonHttpClient.class) { // avoid overlapping Tor restarts between httpClients + onNetworkError.accept(e); + } + } + } + + protected Single> httpObservable(final Callable supplier) { + return Single.fromCallable(() -> Optional.ofNullable(supplier.call())).subscribeOn(Schedulers.io()); + } + + protected T httpObservableBlockingSingle(final Callable supplier) throws HttpException { + try { + Optional opt = AsyncUtil.getInstance().blockingGet(httpObservable(supplier)); + return opt.orElse(null); + } catch(HttpException e) { // forward + throw e; + } catch(Exception e) { // should never happen + throw new HttpNetworkException(e); + } + } + + protected ObjectMapper getObjectMapper() { + return JSONUtils.getInstance().getObjectMapper(); + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/JettyHttpClient.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/JettyHttpClient.java new file mode 100644 index 00000000..bfe8da17 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/JettyHttpClient.java @@ -0,0 +1,188 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.client.api.Response; +import org.eclipse.jetty.client.util.FormContentProvider; +import org.eclipse.jetty.client.util.InputStreamResponseListener; +import org.eclipse.jetty.client.util.StringContentProvider; +import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.util.Fields; +import org.eclipse.jetty.util.component.LifeCycle; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.Scanner; +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +public class JettyHttpClient extends JacksonHttpClient { + protected static Logger log = LoggerFactory.getLogger(JettyHttpClient.class); + public static final String CONTENTTYPE_APPLICATION_JSON = "application/json"; + + private final HttpClient httpClient; + private final long requestTimeout; + private final HttpUsage httpUsage; + + public JettyHttpClient(Consumer onNetworkError, HttpClient httpClient, long requestTimeout, HttpUsage httpUsage) { + super(onNetworkError); + this.httpClient = httpClient; + this.requestTimeout = requestTimeout; + this.httpUsage = httpUsage; + } + + @Override + public void connect() throws HttpException { + try { + if(!httpClient.isRunning()) { + httpClient.start(); + } + } catch(Exception e) { + throw new HttpNetworkException(e); + } + } + + public void restart() { + try { + if(log.isDebugEnabled()) { + log.debug("restart"); + } + if(httpClient.isRunning()) { + httpClient.stop(); + } + httpClient.start(); + } catch(Exception e) { + log.error("", e); + } + } + + public void stop() { + try { + if(httpClient.isRunning()) { + httpClient.stop(); + Executor executor = httpClient.getExecutor(); + if(executor instanceof LifeCycle) { + ((LifeCycle) executor).stop(); + } + } + } catch(Exception e) { + log.error("Error stopping client", e); + } + } + + @Override + protected String requestJsonGet(String urlStr, Map headers, boolean async) throws HttpException { + Request req = computeHttpRequest(urlStr, HttpMethod.GET, headers); + return makeRequest(req, async); + } + + @Override + protected String requestJsonPost(String urlStr, Map headers, String jsonBody) throws HttpException { + Request req = computeHttpRequest(urlStr, HttpMethod.POST, headers); + req.content(new StringContentProvider(CONTENTTYPE_APPLICATION_JSON, jsonBody, StandardCharsets.UTF_8)); + return makeRequest(req, false); + } + + @Override + protected String requestStringPost(String urlStr, Map headers, String contentType, String content) throws HttpException { + log.debug("POST " + urlStr); + Request req = computeHttpRequest(urlStr, HttpMethod.POST, headers); + req.content(new StringContentProvider(content), contentType); + return makeRequest(req, false); + } + + @Override + protected String requestJsonPostUrlEncoded(String urlStr, Map headers, Map body) throws HttpException { + Request req = computeHttpRequest(urlStr, HttpMethod.POST, headers); + req.content(new FormContentProvider(computeBodyFields(body))); + return makeRequest(req, false); + } + + private Fields computeBodyFields(Map body) { + Fields fields = new Fields(); + for(Map.Entry entry : body.entrySet()) { + fields.put(entry.getKey(), entry.getValue()); + } + return fields; + } + + protected String makeRequest(Request req, boolean async) throws HttpException { + String responseContent; + if(async) { + InputStreamResponseListener listener = new InputStreamResponseListener(); + req.send(listener); + + // Call to the listener's get() blocks until the headers arrived + Response response; + try { + response = listener.get(requestTimeout, TimeUnit.MILLISECONDS); + } catch(Exception e) { + throw new HttpNetworkException(e); + } + + // Read content + InputStream is = listener.getInputStream(); + Scanner s = new Scanner(is).useDelimiter("\\A"); + responseContent = s.hasNext() ? s.next() : null; + + // check status + checkResponseStatus(response.getStatus(), responseContent); + } else { + ContentResponse response; + try { + response = req.send(); + } catch(Exception e) { + throw new HttpNetworkException(e); + } + checkResponseStatus(response.getStatus(), response.getContentAsString()); + responseContent = response.getContentAsString(); + } + return responseContent; + } + + private void checkResponseStatus(int status, String responseBody) throws HttpResponseException { + if(!HttpStatus.isSuccess(status)) { + log.error("Http query failed: status=" + status + ", responseBody=" + responseBody); + throw new HttpResponseException(responseBody, status); + } + } + + public HttpClient getJettyHttpClient() throws HttpException { + connect(); + return httpClient; + } + + private Request computeHttpRequest(String url, HttpMethod method, Map headers) throws HttpException { + if(url.endsWith("/rpc")) { + // log RPC as TRACE + if(log.isTraceEnabled()) { + String headersStr = headers != null ? " (" + headers.keySet() + ")" : ""; + log.trace("+" + method + ": " + url + headersStr); + } + } else { + if(log.isDebugEnabled()) { + String headersStr = headers != null ? " (" + headers.keySet() + ")" : ""; + log.debug("+" + method + ": " + url + headersStr); + } + } + Request req = getJettyHttpClient().newRequest(url); + req.method(method); + if(headers != null) { + for(Map.Entry entry : headers.entrySet()) { + req.header(entry.getKey(), entry.getValue()); + } + } + req.timeout(requestTimeout, TimeUnit.MILLISECONDS); + return req; + } + + public HttpUsage getHttpUsage() { + return httpUsage; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/JettyHttpClientService.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/JettyHttpClientService.java new file mode 100644 index 00000000..43036a79 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/JettyHttpClientService.java @@ -0,0 +1,167 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import com.google.common.util.concurrent.RateLimiter; +import com.sparrowwallet.sparrow.net.http.client.socks5.Socks5Proxy; +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.ProxyConfiguration; +import org.eclipse.jetty.client.Socks4Proxy; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.eclipse.jetty.util.thread.QueuedThreadPool; +import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.invoke.MethodHandles; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; + +@SuppressWarnings("deprecation") +public class JettyHttpClientService implements IHttpClientService { + private static final Logger log = LoggerFactory.getLogger(JettyHttpClientService.class); + private static final String NAME = "HttpClient"; + public static final long DEFAULT_TIMEOUT = 30000; + + // limit changing Tor identity on network error every 4 minutes + private static final double RATE_CHANGE_IDENTITY_ON_NETWORK_ERROR = 1.0 / 240; + + protected Map httpClients; // used by Sparrow + private final IHttpProxySupplier httpProxySupplier; + private final long requestTimeout; + + public JettyHttpClientService(long requestTimeout, IHttpProxySupplier httpProxySupplier) { + this.httpProxySupplier = httpProxySupplier != null ? httpProxySupplier : computeHttpProxySupplierDefault(); + this.requestTimeout = requestTimeout; + this.httpClients = new ConcurrentHashMap<>(); + } + + public JettyHttpClientService(long requestTimeout) { + this(requestTimeout, null); + } + + public JettyHttpClientService() { + this(DEFAULT_TIMEOUT); + } + + protected static IHttpProxySupplier computeHttpProxySupplierDefault() { + return new IHttpProxySupplier() { + @Override + public Optional getHttpProxy(HttpUsage httpUsage) { + return Optional.empty(); + } + + @Override + public void changeIdentity() { + } + }; + } + + @Override + public JettyHttpClient getHttpClient(HttpUsage httpUsage) { + JettyHttpClient httpClient = httpClients.get(httpUsage); + if(httpClient == null) { + if(log.isDebugEnabled()) { + log.debug("+httpClient[" + httpUsage + "]"); + } + httpClient = computeHttpClient(httpUsage); + httpClients.put(httpUsage, httpClient); + } + return httpClient; + } + + protected JettyHttpClient computeHttpClient(HttpUsage httpUsage) { + Consumer onNetworkError = computeOnNetworkError(); + HttpClient httpClient = computeJettyClient(httpUsage); + return new JettyHttpClient(onNetworkError, httpClient, requestTimeout, httpUsage); + } + + protected HttpClient computeJettyClient(HttpUsage httpUsage) { + // we use jetty for proxy SOCKS support + HttpClient jettyHttpClient = new HttpClient(new SslContextFactory()); + // jettyHttpClient.setSocketAddressResolver(new MySocketAddressResolver()); + + // prevent user-agent tracking + jettyHttpClient.setUserAgentField(null); + + // configure + configureProxy(jettyHttpClient, httpUsage); + configureThread(jettyHttpClient, httpUsage); + + return jettyHttpClient; + } + + protected Consumer computeOnNetworkError() { + RateLimiter rateLimiter = RateLimiter.create(RATE_CHANGE_IDENTITY_ON_NETWORK_ERROR); + return e -> { + if(!rateLimiter.tryAcquire()) { + if(log.isDebugEnabled()) { + log.debug("onNetworkError: not changing Tor identity (too many recent attempts)"); + } + return; + } + // change Tor identity on network error + httpProxySupplier.changeIdentity(); + }; + } + + protected void configureProxy(HttpClient jettyHttpClient, HttpUsage httpUsage) { + Optional httpProxyOptional = httpProxySupplier.getHttpProxy(httpUsage); + if(httpProxyOptional != null && httpProxyOptional.isPresent()) { + HttpProxy httpProxy = httpProxyOptional.get(); + if(log.isDebugEnabled()) { + log.debug("+httpClient: proxy=" + httpProxy); + } + ProxyConfiguration.Proxy jettyProxy = computeJettyProxy(httpProxy); + jettyHttpClient.getProxyConfiguration().getProxies().add(jettyProxy); + } else { + if(log.isDebugEnabled()) { + log.debug("+httpClient: no proxy"); + } + } + } + + protected void configureThread(HttpClient jettyHttpClient, HttpUsage httpUsage) { + String name = NAME + "-" + httpUsage.toString(); + + QueuedThreadPool threadPool = new QueuedThreadPool(); + threadPool.setName(name); + threadPool.setDaemon(true); + jettyHttpClient.setExecutor(threadPool); + jettyHttpClient.setScheduler(new ScheduledExecutorScheduler(name + "-scheduler", true)); + } + + protected ProxyConfiguration.Proxy computeJettyProxy(HttpProxy httpProxy) { + ProxyConfiguration.Proxy jettyProxy = null; + switch(httpProxy.getProtocol()) { + case SOCKS: + jettyProxy = new Socks4Proxy(httpProxy.getHost(), httpProxy.getPort()); + break; + case SOCKS5: + jettyProxy = new Socks5Proxy(httpProxy.getHost(), httpProxy.getPort()); + break; + case HTTP: + jettyProxy = new org.eclipse.jetty.client.HttpProxy(httpProxy.getHost(), httpProxy.getPort()); + break; + } + return jettyProxy; + } + + @Override + public synchronized void stop() { + for(JettyHttpClient httpClient : httpClients.values()) { + httpClient.stop(); + } + httpClients.clear(); + } + + @Override + public void changeIdentity() { + stop(); + httpProxySupplier.changeIdentity(); + } + + public IHttpProxySupplier getHttpProxySupplier() { + return httpProxySupplier; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/ThreadUtil.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/ThreadUtil.java new file mode 100644 index 00000000..88ab9f5c --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/ThreadUtil.java @@ -0,0 +1,42 @@ +package com.sparrowwallet.sparrow.net.http.client; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.*; + +public class ThreadUtil { + private static final Logger log = LoggerFactory.getLogger(ThreadUtil.class); + + private static ThreadUtil instance; + + private ExecutorService executorService; + + protected ThreadUtil() { + this.executorService = computeExecutorService(); + } + + public static ThreadUtil getInstance() { + if(instance == null) { + instance = new ThreadUtil(); + } + return instance; + } + + protected ExecutorService computeExecutorService() { + return Executors.newFixedThreadPool(5, + r -> { + Thread t = Executors.defaultThreadFactory().newThread(r); + t.setDaemon(true); + return t; + }); + } + + public void setExecutorService(ScheduledExecutorService executorService) { + this.executorService = executorService; + } + + public Future runAsync(Callable callable) { + return executorService.submit(callable); + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/socks5/Socks5.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/socks5/Socks5.java new file mode 100644 index 00000000..3e91181b --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/socks5/Socks5.java @@ -0,0 +1,222 @@ +/** + * Socks5 backported from Jetty12 - we still use Jetty9 for JDK8 compatibility. + */ + +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package com.sparrowwallet.sparrow.net.http.client.socks5; + +import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.util.BufferUtil; +import org.eclipse.jetty.util.Callback; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.ClosedChannelException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Objects; + +/** + * Helper class for SOCKS5 proxying. + * + * @see Socks5Proxy + */ +public class Socks5 { + /** The SOCKS protocol version: {@value}. */ + public static final byte VERSION = 0x05; + + /** The SOCKS5 {@code CONNECT} command used in SOCKS5 connect requests. */ + public static final byte COMMAND_CONNECT = 0x01; + + /** The reserved byte value: {@value}. */ + public static final byte RESERVED = 0x00; + + /** The address type for IPv4 used in SOCKS5 connect requests and responses. */ + public static final byte ADDRESS_TYPE_IPV4 = 0x01; + + /** The address type for domain names used in SOCKS5 connect requests and responses. */ + public static final byte ADDRESS_TYPE_DOMAIN = 0x03; + + /** The address type for IPv6 used in SOCKS5 connect requests and responses. */ + public static final byte ADDRESS_TYPE_IPV6 = 0x04; + + private Socks5() { + } + + /** + * A SOCKS5 authentication method. + * + *

Implementations should send and receive the bytes that are specific to the particular + * authentication method. + */ + public interface Authentication { + /** + * Performs the authentication send and receive bytes exchanges specific for this {@link + * Authentication}. + * + * @param endPoint the {@link EndPoint} to send to and receive from the SOCKS5 server + * @param callback the callback to complete when the authentication is complete + */ + void authenticate(EndPoint endPoint, Callback callback); + + /** A factory for {@link Authentication}s. */ + interface Factory { + /** + * @return the authentication method defined by RFC 1928 + */ + byte getMethod(); + + /** + * @return a new {@link Authentication} + */ + Authentication newAuthentication(); + } + } + + /** + * The implementation of the {@code NO AUTH} authentication method defined in RFC 1928. + */ + public static class NoAuthenticationFactory implements Authentication.Factory { + public static final byte METHOD = 0x00; + + @Override + public byte getMethod() { + return METHOD; + } + + @Override + public Authentication newAuthentication() { + return (endPoint, callback) -> callback.succeeded(); + } + } + + /** + * The implementation of the {@code USERNAME/PASSWORD} authentication method defined in RFC 1929. + */ + public static class UsernamePasswordAuthenticationFactory implements Authentication.Factory { + public static final byte METHOD = 0x02; + public static final byte VERSION = 0x01; + private static final Logger LOG = + LoggerFactory.getLogger(UsernamePasswordAuthenticationFactory.class); + + private final String userName; + private final String password; + private final Charset charset; + + public UsernamePasswordAuthenticationFactory(String userName, String password) { + this(userName, password, StandardCharsets.US_ASCII); + } + + public UsernamePasswordAuthenticationFactory( + String userName, String password, Charset charset) { + this.userName = Objects.requireNonNull(userName); + this.password = Objects.requireNonNull(password); + this.charset = Objects.requireNonNull(charset); + } + + @Override + public byte getMethod() { + return METHOD; + } + + @Override + public Authentication newAuthentication() { + return new UsernamePasswordAuthentication(this); + } + + private static class UsernamePasswordAuthentication implements Authentication, Callback { + private final ByteBuffer byteBuffer = BufferUtil.allocate(2); + private final UsernamePasswordAuthenticationFactory factory; + private EndPoint endPoint; + private Callback callback; + + private UsernamePasswordAuthentication(UsernamePasswordAuthenticationFactory factory) { + this.factory = factory; + } + + @Override + public void authenticate(EndPoint endPoint, Callback callback) { + this.endPoint = endPoint; + this.callback = callback; + + byte[] userNameBytes = factory.userName.getBytes(factory.charset); + byte[] passwordBytes = factory.password.getBytes(factory.charset); + ByteBuffer byteBuffer = + (ByteBuffer) + ByteBuffer.allocate(3 + userNameBytes.length + passwordBytes.length) + .put(VERSION) + .put((byte) userNameBytes.length) + .put(userNameBytes) + .put((byte) passwordBytes.length) + .put(passwordBytes) + .flip(); + endPoint.write(Callback.from(this::authenticationSent, this::failed), byteBuffer); + } + + private void authenticationSent() { + if(LOG.isDebugEnabled()) { + LOG.debug("Written SOCKS5 username/password authentication request"); + } + endPoint.fillInterested(this); + } + + @Override + public void succeeded() { + try { + int filled = endPoint.fill(byteBuffer); + if(filled < 0) { + throw new ClosedChannelException(); + } + if(byteBuffer.remaining() < 2) { + endPoint.fillInterested(this); + return; + } + if(LOG.isDebugEnabled()) { + LOG.debug("Received SOCKS5 username/password authentication response"); + } + byte version = byteBuffer.get(); + if(version != VERSION) { + throw new IOException( + "Unsupported username/password authentication version: " + version); + } + byte status = byteBuffer.get(); + if(status != 0) { + throw new IOException("SOCK5 username/password authentication failure"); + } + if(LOG.isDebugEnabled()) { + LOG.debug("SOCKS5 username/password authentication succeeded"); + } + callback.succeeded(); + } catch(Throwable x) { + failed(x); + } + } + + @Override + public void failed(Throwable x) { + callback.failed(x); + } + + @Override + public InvocationType getInvocationType() { + return InvocationType.NON_BLOCKING; + } + } + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/http/client/socks5/Socks5Proxy.java b/src/main/java/com/sparrowwallet/sparrow/net/http/client/socks5/Socks5Proxy.java new file mode 100644 index 00000000..0c43903a --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/http/client/socks5/Socks5Proxy.java @@ -0,0 +1,419 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package com.sparrowwallet.sparrow.net.http.client.socks5; + +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.HttpClientTransport; +import org.eclipse.jetty.client.HttpDestination; +import org.eclipse.jetty.client.Origin; +import org.eclipse.jetty.client.ProxyConfiguration.Proxy; +import org.eclipse.jetty.io.AbstractConnection; +import org.eclipse.jetty.io.ClientConnectionFactory; +import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.ssl.SslClientConnectionFactory; +import org.eclipse.jetty.util.BufferUtil; +import org.eclipse.jetty.util.Callback; +import org.eclipse.jetty.util.Promise; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.InetAddress; +import java.nio.ByteBuffer; +import java.nio.channels.ClosedChannelException; +import java.nio.charset.StandardCharsets; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.Executor; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Client-side proxy configuration for SOCKS5, defined by RFC 1928. + * + *

Multiple authentication methods are supported via {@link + * #putAuthenticationFactory(Socks5.Authentication.Factory)}. By default only the {@link + * Socks5.NoAuthenticationFactory NO AUTH} authentication method is configured. The {@link + * Socks5.UsernamePasswordAuthenticationFactory USERNAME/PASSWORD} is available to applications but + * must be explicitly configured and added. + */ +public class Socks5Proxy extends Proxy { + private static final Logger LOG = LoggerFactory.getLogger(Socks5Proxy.class); + + private final Map authentications = new LinkedHashMap<>(); + + /** + * Creates a new instance with the given SOCKS5 proxy host and port. + * + * @param host the SOCKS5 proxy host name + * @param port the SOCKS5 proxy port + */ + public Socks5Proxy(String host, int port) { + this(new Origin.Address(host, port), false); + } + + /** + * Creates a new instance with the given SOCKS5 proxy address. + * + *

When {@code secure=true} the communication between the client and the proxy will be + * encrypted (using this proxy {@link #getSslContextFactory()} which typically defaults to that of + * {@link HttpClient}. + * + * @param address the SOCKS5 proxy address (host and port) + * @param secure whether the communication between the client and the SOCKS5 proxy should be + * secure + */ + public Socks5Proxy(Origin.Address address, boolean secure) { + super(address, secure); + putAuthenticationFactory(new Socks5.NoAuthenticationFactory()); + } + + protected static ClientConnectionFactory newSslClientConnectionFactory(HttpClient httpClient, SslContextFactory sslContextFactory, ClientConnectionFactory connectionFactory) { + if(sslContextFactory == null) { + sslContextFactory = httpClient.getSslContextFactory(); + } + return new SslClientConnectionFactory(sslContextFactory, httpClient.getByteBufferPool(), httpClient.getExecutor(), connectionFactory); + } + + /** + * Provides this class with the given SOCKS5 authentication method. + * + * @param authenticationFactory the SOCKS5 authentication factory + * @return the previous authentication method of the same type, or {@code null} if there was none + * of that type already present + */ + public Socks5.Authentication.Factory putAuthenticationFactory(Socks5.Authentication.Factory authenticationFactory) { + return authentications.put(authenticationFactory.getMethod(), authenticationFactory); + } + + /** + * Removes the authentication of the given {@code method}. + * + * @param method the authentication method to remove + */ + public Socks5.Authentication.Factory removeAuthenticationFactory(byte method) { + return authentications.remove(method); + } + + @Override + public ClientConnectionFactory newClientConnectionFactory(ClientConnectionFactory connectionFactory) { + return new Socks5ProxyClientConnectionFactory(connectionFactory); + } + + private static class Socks5ProxyConnection extends AbstractConnection implements Connection.UpgradeFrom { + private static final Pattern IPv4_PATTERN = Pattern.compile("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})"); + + // SOCKS5 response max length is 262 bytes. + private final ByteBuffer byteBuffer = BufferUtil.allocate(512); + private final ClientConnectionFactory connectionFactory; + private final Map context; + private final Map authentications; + private State state = State.HANDSHAKE; + + private Socks5ProxyConnection(EndPoint endPoint, Executor executor, ClientConnectionFactory connectionFactory, Map context, Map authentications) { + super(endPoint, executor); + this.connectionFactory = connectionFactory; + this.context = context; + this.authentications = new LinkedHashMap<>(authentications); + } + + @Override + public ByteBuffer onUpgradeFrom() { + return BufferUtil.copy(byteBuffer); + } + + @Override + public void onOpen() { + super.onOpen(); + sendHandshake(); + } + + private void sendHandshake() { + try { + // +-------------+--------------------+------------------+ + // | version (1) | num of methods (1) | methods (1..255) | + // +-------------+--------------------+------------------+ + int size = authentications.size(); + ByteBuffer byteBuffer = + ByteBuffer.allocate(1 + 1 + size).put(Socks5.VERSION).put((byte) size); + authentications.keySet().forEach(byteBuffer::put); + byteBuffer.flip(); + getEndPoint().write(Callback.from(this::handshakeSent, this::fail), byteBuffer); + } catch(Throwable x) { + fail(x); + } + } + + private void handshakeSent() { + if(LOG.isDebugEnabled()) { + LOG.debug("Written SOCKS5 handshake request"); + } + state = State.HANDSHAKE; + fillInterested(); + } + + private void fail(Throwable x) { + if(LOG.isDebugEnabled()) { + LOG.debug("SOCKS5 failure", x); + } + getEndPoint().close(); + @SuppressWarnings("unchecked") + Promise promise = + (Promise) + this.context.get(HttpClientTransport.HTTP_CONNECTION_PROMISE_CONTEXT_KEY); + promise.failed(x); + } + + @Override + public void onFillable() { + try { + switch(state) { + case HANDSHAKE: + receiveHandshake(); + break; + case CONNECT: + receiveConnect(); + break; + default: + throw new IllegalStateException(); + } + } catch(Throwable x) { + fail(x); + } + } + + private void receiveHandshake() throws IOException { + // +-------------+------------+ + // | version (1) | method (1) | + // +-------------+------------+ + int filled = getEndPoint().fill(byteBuffer); + if(filled < 0) { + throw new ClosedChannelException(); + } + if(byteBuffer.remaining() < 2) { + fillInterested(); + return; + } + + if(LOG.isDebugEnabled()) { + LOG.debug("Received SOCKS5 handshake response {}", BufferUtil.toDetailString(byteBuffer)); + } + + byte version = byteBuffer.get(); + if(version != Socks5.VERSION) { + throw new IOException("Unsupported SOCKS5 version: " + version); + } + + byte method = byteBuffer.get(); + if(method == -1) { + throw new IOException("Unacceptable SOCKS5 authentication methods"); + } + + Socks5.Authentication.Factory factory = authentications.get(method); + if(factory == null) { + throw new IOException("Unknown SOCKS5 authentication method: " + method); + } + + factory + .newAuthentication() + .authenticate(getEndPoint(), Callback.from(this::sendConnect, this::fail)); + } + + private void sendConnect() { + try { + // +-------------+-------------+--------------+------------------+------------------------+----------+ + // | version (1) | command (1) | reserved (1) | address type (1) | address bytes (4..255) | + // port (2) | + // +-------------+-------------+--------------+------------------+------------------------+----------+ + HttpDestination destination = (HttpDestination) context.get(HttpClientTransport.HTTP_DESTINATION_CONTEXT_KEY); + Origin.Address address = destination.getOrigin().getAddress(); + String host = address.getHost(); + short port = (short) address.getPort(); + + ByteBuffer byteBuffer; + Matcher matcher = IPv4_PATTERN.matcher(host); + if(matcher.matches()) { + byteBuffer = + ByteBuffer.allocate(10) + .put(Socks5.VERSION) + .put(Socks5.COMMAND_CONNECT) + .put(Socks5.RESERVED) + .put(Socks5.ADDRESS_TYPE_IPV4); + for(int i = 1; i <= 4; ++i) { + byteBuffer.put(Byte.parseByte(matcher.group(i))); + } + byteBuffer.putShort(port).flip(); + } else if(true /*URIUtil.isValidHostRegisteredName(host)*/) { + byte[] bytes = host.getBytes(StandardCharsets.US_ASCII); + if(bytes.length > 255) { + throw new IOException("Invalid host name: " + host); + } + byteBuffer = + (ByteBuffer) + ByteBuffer.allocate(7 + bytes.length) + .put(Socks5.VERSION) + .put(Socks5.COMMAND_CONNECT) + .put(Socks5.RESERVED) + .put(Socks5.ADDRESS_TYPE_DOMAIN) + .put((byte) bytes.length) + .put(bytes) + .putShort(port) + .flip(); + } else { + // Assume IPv6. + byte[] bytes = InetAddress.getByName(host).getAddress(); + byteBuffer = + (ByteBuffer) + ByteBuffer.allocate(22) + .put(Socks5.VERSION) + .put(Socks5.COMMAND_CONNECT) + .put(Socks5.RESERVED) + .put(Socks5.ADDRESS_TYPE_IPV6) + .put(bytes) + .putShort(port) + .flip(); + } + + getEndPoint().write(Callback.from(this::connectSent, this::fail), byteBuffer); + } catch(Throwable x) { + fail(x); + } + } + + private void connectSent() { + if(LOG.isDebugEnabled()) { + LOG.debug("Written SOCKS5 connect request"); + } + state = State.CONNECT; + fillInterested(); + } + + private void receiveConnect() throws IOException { + // +-------------+-----------+--------------+------------------+------------------------+----------+ + // | version (1) | reply (1) | reserved (1) | address type (1) | address bytes (4..255) | port + // (2) | + // +-------------+-----------+--------------+------------------+------------------------+----------+ + int filled = getEndPoint().fill(byteBuffer); + if(filled < 0) { + throw new ClosedChannelException(); + } + if(byteBuffer.remaining() < 5) { + fillInterested(); + return; + } + byte addressType = byteBuffer.get(3); + int length = 6; + if(addressType == Socks5.ADDRESS_TYPE_IPV4) { + length += 4; + } else if(addressType == Socks5.ADDRESS_TYPE_DOMAIN) { + length += 1 + (byteBuffer.get(4) & 0xFF); + } else if(addressType == Socks5.ADDRESS_TYPE_IPV6) { + length += 16; + } else { + throw new IOException("Invalid SOCKS5 address type: " + addressType); + } + if(byteBuffer.remaining() < length) { + fillInterested(); + return; + } + + if(LOG.isDebugEnabled()) { + LOG.debug("Received SOCKS5 connect response {}", BufferUtil.toDetailString(byteBuffer)); + } + + // We have all the SOCKS5 bytes. + byte version = byteBuffer.get(); + if(version != Socks5.VERSION) { + throw new IOException("Unsupported SOCKS5 version: " + version); + } + + byte status = byteBuffer.get(); + switch(status) { + case 0: { + // Consume the buffer before upgrading to the tunnel. + byteBuffer.position(length); + tunnel(); + break; + } + case 1: + throw new IOException("SOCKS5 general failure"); + case 2: + throw new IOException("SOCKS5 connection not allowed"); + case 3: + throw new IOException("SOCKS5 network unreachable"); + case 4: + throw new IOException("SOCKS5 host unreachable"); + case 5: + throw new IOException("SOCKS5 connection refused"); + case 6: + throw new IOException("SOCKS5 timeout expired"); + case 7: + throw new IOException("SOCKS5 unsupported command"); + case 8: + throw new IOException("SOCKS5 unsupported address"); + default: + throw new IOException("SOCKS5 unknown status: " + status); + } + } + + private void tunnel() { + try { + HttpDestination destination = + (HttpDestination) context.get(HttpClientTransport.HTTP_DESTINATION_CONTEXT_KEY); + this.context.put("ssl.peer.host", destination.getHost()); + this.context.put("ssl.peer.port", destination.getPort()); + // Origin.Address address = destination.getOrigin().getAddress(); + // Don't want to do DNS resolution here. + // InetSocketAddress inet = InetSocketAddress.createUnresolved(address.getHost(), + // address.getPort()); + // context.put(ClientConnector.REMOTE_SOCKET_ADDRESS_CONTEXT_KEY, inet); + ClientConnectionFactory connectionFactory = this.connectionFactory; + if(destination.isSecure()) { + connectionFactory = + newSslClientConnectionFactory(destination.getHttpClient(), null, connectionFactory); + } + Connection newConnection = connectionFactory.newConnection(getEndPoint(), context); + getEndPoint().upgrade(newConnection); + if(LOG.isDebugEnabled()) { + LOG.debug("SOCKS5 tunnel established: {} over {}", this, newConnection); + } + } catch(Throwable x) { + fail(x); + } + } + + private enum State { + HANDSHAKE, + CONNECT + } + } + + private class Socks5ProxyClientConnectionFactory implements ClientConnectionFactory { + private final ClientConnectionFactory connectionFactory; + + private Socks5ProxyClientConnectionFactory(ClientConnectionFactory connectionFactory) { + this.connectionFactory = connectionFactory; + } + + public Connection newConnection(EndPoint endPoint, Map context) { + HttpDestination destination = (HttpDestination) context.get(HttpClientTransport.HTTP_DESTINATION_CONTEXT_KEY); + Executor executor = destination.getHttpClient().getExecutor(); + Socks5ProxyConnection connection = new Socks5ProxyConnection(endPoint, executor, connectionFactory, context, authentications); + return customize(connection, context); + } + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/payjoin/Payjoin.java b/src/main/java/com/sparrowwallet/sparrow/payjoin/Payjoin.java index 613af32e..0c673d28 100644 --- a/src/main/java/com/sparrowwallet/sparrow/payjoin/Payjoin.java +++ b/src/main/java/com/sparrowwallet/sparrow/payjoin/Payjoin.java @@ -2,7 +2,6 @@ package com.sparrowwallet.sparrow.payjoin; import com.google.common.collect.ImmutableMap; import com.google.gson.Gson; -import com.samourai.wallet.httpClient.HttpResponseException; import com.sparrowwallet.drongo.protocol.Script; import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.protocol.TransactionInput; @@ -17,6 +16,7 @@ import com.sparrowwallet.drongo.wallet.WalletNode; import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.net.HttpClientService; import com.sparrowwallet.sparrow.net.Protocol; +import com.sparrowwallet.sparrow.net.http.client.HttpResponseException; import javafx.concurrent.Service; import javafx.concurrent.Task; import org.slf4j.Logger; diff --git a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNym.java b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNym.java index 04808cd1..a4bf1652 100644 --- a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNym.java +++ b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNym.java @@ -58,14 +58,6 @@ public class PayNym { return followers; } - public boolean isCollaborativeSend() { - return collaborativeSend; - } - - public void setCollaborativeSend(boolean collaborativeSend) { - this.collaborativeSend = collaborativeSend; - } - public List getScriptTypes() { return segwit ? getSegwitScriptTypes() : getV1ScriptTypes(); } diff --git a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymAddress.java b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymAddress.java deleted file mode 100644 index 06607741..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymAddress.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.sparrowwallet.sparrow.paynym; - -import com.sparrowwallet.drongo.address.P2WPKHAddress; - -public final class PayNymAddress extends P2WPKHAddress { - private final PayNym payNym; - - public PayNymAddress(PayNym payNym) { - super(new byte[20]); - this.payNym = payNym; - } - - public PayNym getPayNym() { - return payNym; - } - - public String toString() { - return payNym.nymName(); - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymController.java b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymController.java index 86b38b73..b5c35e91 100644 --- a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymController.java +++ b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymController.java @@ -46,7 +46,6 @@ public class PayNymController { public static final String INVALID_PAYMENT_CODE_LABEL = "Invalid Payment Code"; private String walletId; - private boolean selectLinkedOnly; private PayNym walletPayNym; private boolean requestingPassword; @@ -88,9 +87,8 @@ public class PayNymController { private final BooleanProperty closeProperty = new SimpleBooleanProperty(false); - public void initializeView(String walletId, boolean selectLinkedOnly) { + public void initializeView(String walletId) { this.walletId = walletId; - this.selectLinkedOnly = selectLinkedOnly; payNymName.managedProperty().bind(payNymName.visibleProperty()); payNymRetrieve.managedProperty().bind(payNymRetrieve.visibleProperty()); @@ -668,10 +666,6 @@ public class PayNymController { } } - public boolean isSelectLinkedOnly() { - return selectLinkedOnly; - } - public PayNym getPayNym() { return payNymProperty.get(); } diff --git a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymDialog.java b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymDialog.java index cb1196b2..b8b916eb 100644 --- a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymDialog.java @@ -9,10 +9,10 @@ import java.io.IOException; public class PayNymDialog extends Dialog { public PayNymDialog(String walletId) { - this(walletId, Operation.SHOW, false); + this(walletId, Operation.SHOW); } - public PayNymDialog(String walletId, Operation operation, boolean selectLinkedOnly) { + public PayNymDialog(String walletId, Operation operation) { final DialogPane dialogPane = getDialogPane(); AppServices.setStageIcon(dialogPane.getScene().getWindow()); AppServices.onEscapePressed(dialogPane.getScene(), this::close); @@ -21,7 +21,7 @@ public class PayNymDialog extends Dialog { FXMLLoader payNymLoader = new FXMLLoader(AppServices.class.getResource("paynym/paynym.fxml")); dialogPane.setContent(payNymLoader.load()); PayNymController payNymController = payNymLoader.getController(); - payNymController.initializeView(walletId, selectLinkedOnly); + payNymController.initializeView(walletId); EventManager.get().register(payNymController); @@ -33,26 +33,13 @@ public class PayNymDialog extends Dialog { dialogPane.getStylesheets().add(AppServices.class.getResource("app.css").toExternalForm()); dialogPane.getStylesheets().add(AppServices.class.getResource("paynym/paynym.css").toExternalForm()); - final ButtonType sendDirectlyButtonType = new javafx.scene.control.ButtonType("Send Directly", ButtonBar.ButtonData.APPLY); - final ButtonType sendCollaborativelyButtonType = new javafx.scene.control.ButtonType("Send Collaboratively", ButtonBar.ButtonData.OK_DONE); + final ButtonType sendDirectlyButtonType = new javafx.scene.control.ButtonType("Send To Contact", ButtonBar.ButtonData.APPLY); final ButtonType selectButtonType = new javafx.scene.control.ButtonType("Select Contact", ButtonBar.ButtonData.APPLY); final ButtonType cancelButtonType = new javafx.scene.control.ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE); final ButtonType doneButtonType = new javafx.scene.control.ButtonType("Done", ButtonBar.ButtonData.OK_DONE); if(operation == Operation.SEND) { - if(selectLinkedOnly) { - dialogPane.getButtonTypes().addAll(sendDirectlyButtonType, cancelButtonType); - } else { - dialogPane.getButtonTypes().addAll(sendDirectlyButtonType, sendCollaborativelyButtonType, cancelButtonType); - Button sendCollaborativelyButton = (Button)dialogPane.lookupButton(sendCollaborativelyButtonType); - sendCollaborativelyButton.setDisable(true); - sendCollaborativelyButton.setDefaultButton(false); - payNymController.payNymProperty().addListener((observable, oldValue, payNym) -> { - sendCollaborativelyButton.setDisable(payNym == null); - sendCollaborativelyButton.setDefaultButton(payNym != null && !payNymController.isLinked(payNym)); - }); - } - + dialogPane.getButtonTypes().addAll(sendDirectlyButtonType, cancelButtonType); Button sendDirectlyButton = (Button)dialogPane.lookupButton(sendDirectlyButtonType); sendDirectlyButton.setDisable(true); sendDirectlyButton.setDefaultButton(true); @@ -66,7 +53,7 @@ public class PayNymDialog extends Dialog { selectButton.setDisable(true); selectButton.setDefaultButton(true); payNymController.payNymProperty().addListener((observable, oldValue, payNym) -> { - selectButton.setDisable(payNym == null || (selectLinkedOnly && !payNymController.isLinked(payNym))); + selectButton.setDisable(payNym == null || !payNymController.isLinked(payNym)); }); } else { dialogPane.getButtonTypes().add(doneButtonType); @@ -83,14 +70,8 @@ public class PayNymDialog extends Dialog { }); setResultConverter(dialogButton -> { - if(dialogButton == sendCollaborativelyButtonType) { - PayNym payNym = payNymController.getPayNym(); - payNym.setCollaborativeSend(true); - return payNym; - } else if(dialogButton == sendDirectlyButtonType || dialogButton == selectButtonType) { - PayNym payNym = payNymController.getPayNym(); - payNym.setCollaborativeSend(false); - return payNym; + if(dialogButton == sendDirectlyButtonType || dialogButton == selectButtonType) { + return payNymController.getPayNym(); } return null; diff --git a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymService.java b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymService.java index d7a8f697..01014e61 100644 --- a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymService.java +++ b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymService.java @@ -155,10 +155,6 @@ public class PayNymService { .map(o -> o.get()); } - public static Observable> followPaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode paymentCode, String authToken, String signature) { - return followPaymentCode(PaymentCode.fromString(paymentCode.toString()), authToken, signature); - } - public static Observable> followPaymentCode(PaymentCode paymentCode, String authToken, String signature) { Map headers = new HashMap<>(); headers.put("content-type", "application/json"); diff --git a/src/main/java/com/sparrowwallet/sparrow/soroban/CounterpartyController.java b/src/main/java/com/sparrowwallet/sparrow/soroban/CounterpartyController.java deleted file mode 100644 index fa80ae7f..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/soroban/CounterpartyController.java +++ /dev/null @@ -1,465 +0,0 @@ -package com.sparrowwallet.sparrow.soroban; - -import com.google.common.base.Throwables; -import com.samourai.soroban.client.cahoots.OnlineCahootsMessage; -import com.samourai.soroban.client.meeting.SorobanRequestMessage; -import com.samourai.soroban.client.wallet.SorobanWalletService; -import com.samourai.soroban.client.wallet.counterparty.SorobanWalletCounterparty; -import com.samourai.wallet.bip47.rpc.PaymentCode; -import com.samourai.wallet.cahoots.Cahoots; -import com.samourai.wallet.cahoots.CahootsContext; -import com.samourai.wallet.cahoots.CahootsType; -import com.sparrowwallet.drongo.protocol.Transaction; -import com.sparrowwallet.drongo.psbt.PSBTParseException; -import com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex; -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.drongo.wallet.WalletNode; -import com.sparrowwallet.sparrow.AppServices; -import com.sparrowwallet.sparrow.control.*; -import com.sparrowwallet.sparrow.paynym.PayNymDialog; -import com.sparrowwallet.sparrow.paynym.PayNymService; -import io.reactivex.functions.Consumer; -import io.reactivex.rxjavafx.schedulers.JavaFxScheduler; -import io.reactivex.schedulers.Schedulers; -import javafx.application.Platform; -import javafx.beans.property.ObjectProperty; -import javafx.beans.property.SimpleObjectProperty; -import javafx.collections.FXCollections; -import javafx.concurrent.Service; -import javafx.concurrent.Task; -import javafx.event.ActionEvent; -import javafx.fxml.FXML; -import javafx.scene.control.*; -import javafx.scene.layout.VBox; -import javafx.util.StringConverter; -import org.controlsfx.glyphfont.Glyph; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.TimeoutException; - -import static com.sparrowwallet.sparrow.AppServices.showErrorDialog; -import static com.sparrowwallet.sparrow.soroban.Soroban.TIMEOUT_MS; - -public class CounterpartyController extends SorobanController { - private static final Logger log = LoggerFactory.getLogger(CounterpartyController.class); - - private String walletId; - private Wallet wallet; - - @FXML - private VBox step1; - - @FXML - private VBox step2; - - @FXML - private VBox step3; - - @FXML - private VBox step4; - - @FXML - private CopyableTextField payNym; - - @FXML - private Button showPayNym; - - @FXML - private PayNymAvatar payNymAvatar; - - @FXML - private Button payNymButton; - - @FXML - private PaymentCodeTextField paymentCode; - - @FXML - private Button paymentCodeQR; - - @FXML - private ComboBox mixWallet; - - @FXML - private ProgressTimer step2Timer; - - @FXML - private Label step2Desc; - - @FXML - private Label mixingPartner; - - @FXML - private PayNymAvatar mixPartnerAvatar; - - @FXML - private Hyperlink meetingFail; - - @FXML - private VBox mixDetails; - - @FXML - private Label mixType; - - @FXML - private Label mixFee; - - @FXML - private ProgressTimer step3Timer; - - @FXML - private Label step3Desc; - - @FXML - private ProgressBar sorobanProgressBar; - - @FXML - private Label sorobanProgressLabel; - - @FXML - private Glyph mixDeclined; - - @FXML - private TransactionDiagram transactionDiagram; - - private final ObjectProperty meetingReceived = new SimpleObjectProperty<>(null); - - private final ObjectProperty meetingAccepted = new SimpleObjectProperty<>(null); - - private final ObjectProperty transactionProperty = new SimpleObjectProperty<>(null); - - public void initializeView(String walletId, Wallet wallet) { - this.walletId = walletId; - this.wallet = wallet; - - step1.managedProperty().bind(step1.visibleProperty()); - step2.managedProperty().bind(step2.visibleProperty()); - step3.managedProperty().bind(step3.visibleProperty()); - step4.managedProperty().bind(step4.visibleProperty()); - - mixWallet.setConverter(new StringConverter<>() { - @Override - public String toString(Wallet wallet) { - return wallet == null ? "" : wallet.getFullDisplayName(); - } - - @Override - public Wallet fromString(String string) { - return null; - } - }); - mixWallet.setItems(FXCollections.observableList(wallet.getAllWallets())); - mixWallet.setValue(wallet); - mixWallet.valueProperty().addListener((observable, oldValue, selectedWallet) -> setWallet(selectedWallet)); - - sorobanProgressBar.managedProperty().bind(sorobanProgressBar.visibleProperty()); - sorobanProgressLabel.managedProperty().bind(sorobanProgressLabel.visibleProperty()); - mixDeclined.managedProperty().bind(mixDeclined.visibleProperty()); - sorobanProgressBar.visibleProperty().bind(sorobanProgressLabel.visibleProperty()); - mixDeclined.visibleProperty().bind(sorobanProgressLabel.visibleProperty().not()); - step2Timer.visibleProperty().bind(mixingPartner.visibleProperty()); - step3Timer.visibleProperty().bind(sorobanProgressLabel.visibleProperty()); - - step2.setVisible(false); - step3.setVisible(false); - step4.setVisible(false); - - Soroban soroban = AppServices.getSorobanServices().getSoroban(walletId); - if(soroban.getHdWallet() == null) { - throw new IllegalStateException("Soroban HD wallet must be set"); - } - - payNym.managedProperty().bind(payNym.visibleProperty()); - showPayNym.managedProperty().bind(showPayNym.visibleProperty()); - showPayNym.visibleProperty().bind(payNym.visibleProperty()); - payNymAvatar.managedProperty().bind(payNymAvatar.visibleProperty()); - payNymAvatar.visibleProperty().bind(payNym.visibleProperty()); - payNymButton.managedProperty().bind(payNymButton.visibleProperty()); - payNymButton.visibleProperty().bind(payNym.visibleProperty().not()); - if(isUsePayNym(wallet)) { - retrievePayNym(null); - } else { - payNym.setVisible(false); - } - - Wallet masterWallet = wallet.isMasterWallet() ? wallet : wallet.getMasterWallet(); - paymentCode.setPaymentCode(masterWallet.getPaymentCode()); - paymentCodeQR.prefHeightProperty().bind(paymentCode.heightProperty()); - paymentCodeQR.prefWidthProperty().bind(showPayNym.widthProperty()); - - mixingPartner.managedProperty().bind(mixingPartner.visibleProperty()); - meetingFail.managedProperty().bind(meetingFail.visibleProperty()); - meetingFail.visibleProperty().bind(mixingPartner.visibleProperty().not()); - meetingFail.setOnAction(event -> { - step2Desc.setText("Ask your mix partner to initiate the Soroban communication."); - mixingPartner.setVisible(true); - startCounterpartyMeetingReceive(); - step2Timer.start(e -> { - step2Desc.setText("Mix declined due to timeout."); - meetingReceived.set(Boolean.FALSE); - }); - }); - - mixDetails.managedProperty().bind(mixDetails.visibleProperty()); - mixDetails.setVisible(false); - - meetingAccepted.addListener((observable, oldValue, accepted) -> { - Platform.exitNestedEventLoop(meetingAccepted, accepted); - meetingReceived.set(null); - }); - - step2.visibleProperty().addListener((observable, oldValue, visible) -> { - if(visible) { - startCounterpartyMeetingReceive(); - step2Timer.start(e -> { - step2Desc.setText("Mix declined due to timeout."); - meetingReceived.set(Boolean.FALSE); - }); - } - }); - - step3.visibleProperty().addListener((observable, oldValue, visible) -> { - if(visible) { - meetingAccepted.set(Boolean.TRUE); - } - }); - } - - private void setWallet(Wallet wallet) { - this.walletId = AppServices.get().getOpenWallets().get(wallet).getWalletId(wallet); - this.wallet = wallet; - } - - private void startCounterpartyMeetingReceive() { - Soroban soroban = AppServices.getSorobanServices().getSoroban(walletId); - SorobanWalletService sorobanWalletService = soroban.getSorobanWalletService(); - - SparrowCahootsWallet cahootsWallet = soroban.getCahootsWallet(wallet); - SorobanWalletCounterparty sorobanWalletCounterparty = sorobanWalletService.getSorobanWalletCounterparty(cahootsWallet); - sorobanWalletCounterparty.setTimeoutMeetingMs(TIMEOUT_MS); - - Service receiveMeetingService = new Service<>() { - @Override - protected Task createTask() { - return new Task<>() { - @Override - protected SorobanRequestMessage call() throws Exception { - return sorobanWalletCounterparty.receiveMeetingRequest(); - } - }; - } - }; - receiveMeetingService.setOnSucceeded(event -> { - SorobanRequestMessage requestMessage = receiveMeetingService.getValue(); - PaymentCode paymentCodeInitiator = requestMessage.getSender(); - CahootsType cahootsType = requestMessage.getType(); - updateMixPartner(paymentCodeInitiator, cahootsType); - Boolean accepted = (Boolean)Platform.enterNestedEventLoop(meetingAccepted); - - sorobanWalletCounterparty.sendMeetingResponse(requestMessage, accepted) - .subscribeOn(Schedulers.io()) - .observeOn(JavaFxScheduler.platform()) - .subscribe(responseMessage -> { - requestUserAttention(); - if(accepted) { - startCounterpartyCollaboration(sorobanWalletCounterparty, paymentCodeInitiator, cahootsType, cahootsWallet.getAccount()); - followPaymentCode(paymentCodeInitiator); - } - }, error -> { - log.error("Error sending meeting response", error); - mixingPartner.setVisible(false); - requestUserAttention(); - }); - }); - receiveMeetingService.setOnFailed(event -> { - Throwable e = event.getSource().getException(); - log.error("Failed to receive meeting request", e); - mixingPartner.setVisible(false); - requestUserAttention(); - }); - receiveMeetingService.start(); - } - - private void updateMixPartner(PaymentCode paymentCodeInitiator, CahootsType cahootsType) { - String code = paymentCodeInitiator.toString(); - mixingPartner.setText(code.substring(0, 12) + "..." + code.substring(code.length() - 5)); - if(isUsePayNym(wallet)) { - mixPartnerAvatar.setPaymentCode(paymentCodeInitiator); - PayNymService.getPayNym(paymentCodeInitiator.toString()).subscribe(payNym -> { - mixingPartner.setText(payNym.nymName()); - }, error -> { - //ignore, may not be a PayNym - }); - } - - if(cahootsType == CahootsType.STONEWALLX2) { - mixType.setText("Two person coinjoin (" + cahootsType.getLabel() + ")"); - mixFee.setText("You pay half the miner fee"); - } else if(cahootsType == CahootsType.STOWAWAY) { - mixType.setText("Payjoin (" + cahootsType.getLabel() + ")"); - mixFee.setText("None"); - } else { - mixType.setText(cahootsType.getLabel()); - mixFee.setText("None"); - } - - mixDetails.setVisible(true); - meetingReceived.set(Boolean.TRUE); - } - - private void startCounterpartyCollaboration(SorobanWalletCounterparty sorobanWalletCounterparty, PaymentCode initiatorPaymentCode, CahootsType cahootsType, int account) { - sorobanProgressLabel.setText("Creating mix transaction..."); - SparrowCahootsWallet cahootsWallet = (SparrowCahootsWallet)sorobanWalletCounterparty.getCahootsWallet(); - - Map walletUtxos = wallet.getSpendableUtxos(); - for(Map.Entry entry : walletUtxos.entrySet()) { - cahootsWallet.addUtxo(entry.getValue(), wallet.getWalletTransaction(entry.getKey().getHash()), (int)entry.getKey().getIndex()); - } - - try { - CahootsContext cahootsContext = CahootsContext.newCounterparty(cahootsWallet, cahootsType, account); - Consumer onProgress = cahootsMessage -> { - if(cahootsMessage != null) { - Platform.runLater(() -> { - Cahoots cahoots = cahootsMessage.getCahoots(); - sorobanProgressBar.setProgress((double)(cahoots.getStep() + 1) / 5); - - if(cahoots.getStep() == 3) { - sorobanProgressLabel.setText("Your mix partner is reviewing the transaction..."); - step3Timer.start(); - } else if(cahoots.getStep() >= 4) { - try { - Transaction transaction = getTransaction(cahoots); - if(transaction != null) { - transactionProperty.set(transaction); - updateTransactionDiagram(transactionDiagram, wallet, null, transaction); - next(); - } - } catch(PSBTParseException e) { - log.error("Invalid collaborative PSBT created", e); - step3Desc.setText("Invalid transaction created."); - sorobanProgressLabel.setVisible(false); - } - } - }); - } - }; - - Service cahootsService = new Service<>() { - @Override - protected Task createTask() { - return new Task<>() { - @Override - protected Cahoots call() throws Exception { - return sorobanWalletCounterparty.counterparty(cahootsContext, initiatorPaymentCode, onProgress); - } - }; - } - }; - cahootsService.setOnFailed(event -> { - Throwable error = Throwables.getRootCause(event.getSource().getException()); - log.error("Error creating mix transaction", error); - String cutFrom = "Exception: "; - String message = error.getMessage() == null ? (error instanceof TimeoutException || step3Timer.getProgress() == 0d ? "Timed out receiving response" : "Error receiving response") : error.getMessage(); - int index = message.lastIndexOf(cutFrom); - String msg = index < 0 ? message : message.substring(index + cutFrom.length()); - msg = msg.replace("#Cahoots", "mix transaction"); - step3Desc.setText(msg); - sorobanProgressLabel.setVisible(false); - }); - cahootsService.start(); - } catch(Exception e) { - log.error("Error creating mix transaction", e); - sorobanProgressLabel.setText(e.getMessage()); - } - } - - private void followPaymentCode(PaymentCode paymentCodeInitiator) { - if(isUsePayNym(wallet)) { - PayNymService.getAuthToken(wallet, new HashMap<>()).subscribe(authToken -> { - String signature = PayNymService.getSignature(wallet, authToken); - PayNymService.followPaymentCode(paymentCodeInitiator, authToken, signature).subscribe(followMap -> { - log.debug("Followed payment code " + followMap.get("following")); - }, error -> { - log.warn("Could not follow payment code", error); - }); - }, error -> { - log.warn("Could not follow payment code", error); - }); - } - } - - public boolean next() { - if(step1.isVisible()) { - step1.setVisible(false); - step2.setVisible(true); - return true; - } - - if(step2.isVisible()) { - step2.setVisible(false); - step3.setVisible(true); - return true; - } - - if(step3.isVisible()) { - step3.setVisible(false); - step4.setVisible(true); - return true; - } - - return false; - } - - public void cancel() { - meetingAccepted.set(Boolean.FALSE); - } - - public void retrievePayNym(ActionEvent event) { - setUsePayNym(wallet, true); - - PayNymService.createPayNym(wallet).subscribe(createMap -> { - payNym.setText((String)createMap.get("nymName")); - payNymAvatar.setPaymentCode(wallet.isMasterWallet() ? wallet.getPaymentCode() : wallet.getMasterWallet().getPaymentCode()); - payNym.setVisible(true); - - PayNymService.claimPayNym(wallet, createMap, true); - }, error -> { - log.error("Error retrieving PayNym", error); - Optional optResponse = showErrorDialog("Error retrieving PayNym", "Could not retrieve PayNym. Try again?", ButtonType.CANCEL, ButtonType.OK); - if(optResponse.isPresent() && optResponse.get().equals(ButtonType.OK)) { - retrievePayNym(null); - } else { - payNym.setVisible(false); - } - }); - } - - public void showPayNym(ActionEvent event) { - PayNymDialog payNymDialog = new PayNymDialog(walletId); - payNymDialog.initOwner(payNym.getScene().getWindow()); - payNymDialog.showAndWait(); - } - - public void showPayNymQR(ActionEvent event) { - Wallet masterWallet = wallet.isMasterWallet() ? wallet : wallet.getMasterWallet(); - QRDisplayDialog qrDisplayDialog = new QRDisplayDialog(masterWallet.getPaymentCode().toString()); - qrDisplayDialog.initOwner(payNym.getScene().getWindow()); - qrDisplayDialog.showAndWait(); - } - - public ObjectProperty meetingReceivedProperty() { - return meetingReceived; - } - - public ObjectProperty meetingAcceptedProperty() { - return meetingAccepted; - } - - public ObjectProperty transactionProperty() { - return transactionProperty; - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/soroban/CounterpartyDialog.java b/src/main/java/com/sparrowwallet/sparrow/soroban/CounterpartyDialog.java deleted file mode 100644 index 2f63dec6..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/soroban/CounterpartyDialog.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.sparrowwallet.sparrow.soroban; - -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.sparrow.AppServices; -import javafx.event.ActionEvent; -import javafx.fxml.FXMLLoader; -import javafx.scene.control.*; - -import java.io.IOException; - -public class CounterpartyDialog extends Dialog { - public CounterpartyDialog(String walletId, Wallet wallet) { - final DialogPane dialogPane = getDialogPane(); - AppServices.setStageIcon(dialogPane.getScene().getWindow()); - AppServices.onEscapePressed(dialogPane.getScene(), this::close); - - try { - FXMLLoader counterpartyLoader = new FXMLLoader(AppServices.class.getResource("soroban/counterparty.fxml")); - dialogPane.setContent(counterpartyLoader.load()); - CounterpartyController counterpartyController = counterpartyLoader.getController(); - counterpartyController.initializeView(walletId, wallet); - - dialogPane.setPrefWidth(730); - dialogPane.setPrefHeight(520); - dialogPane.setMinHeight(dialogPane.getPrefHeight()); - AppServices.moveToActiveWindowScreen(this); - - dialogPane.getStylesheets().add(AppServices.class.getResource("app.css").toExternalForm()); - dialogPane.getStylesheets().add(AppServices.class.getResource("soroban/counterparty.css").toExternalForm()); - - final ButtonType nextButtonType = new javafx.scene.control.ButtonType("Next", ButtonBar.ButtonData.OK_DONE); - final ButtonType cancelButtonType = new javafx.scene.control.ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE); - final ButtonType doneButtonType = new javafx.scene.control.ButtonType("Done", ButtonBar.ButtonData.APPLY); - dialogPane.getButtonTypes().addAll(nextButtonType, cancelButtonType, doneButtonType); - - Button nextButton = (Button)dialogPane.lookupButton(nextButtonType); - Button cancelButton = (Button)dialogPane.lookupButton(cancelButtonType); - Button doneButton = (Button)dialogPane.lookupButton(doneButtonType); - doneButton.setDisable(true); - counterpartyController.meetingReceivedProperty().addListener((observable, oldValue, newValue) -> { - nextButton.setDisable(newValue != Boolean.TRUE); - }); - counterpartyController.transactionProperty().addListener((observable, oldValue, newValue) -> { - nextButton.setVisible(false); - doneButton.setDisable(newValue == null); - cancelButton.setDisable(newValue != null); - }); - - nextButton.managedProperty().bind(nextButton.visibleProperty()); - doneButton.managedProperty().bind(doneButton.visibleProperty()); - - doneButton.visibleProperty().bind(nextButton.visibleProperty().not()); - - nextButton.addEventFilter(ActionEvent.ACTION, event -> { - if(!counterpartyController.next()) { - nextButton.setVisible(false); - doneButton.setDefaultButton(true); - } - nextButton.setDisable(counterpartyController.meetingReceivedProperty().get() != Boolean.TRUE); - event.consume(); - }); - - cancelButton.addEventFilter(ActionEvent.ACTION, event -> { - if(counterpartyController.meetingReceivedProperty().get() == Boolean.TRUE) { - counterpartyController.cancel(); - } - }); - - setResultConverter(dialogButton -> dialogButton.getButtonData().equals(ButtonBar.ButtonData.APPLY)); - } catch(IOException e) { - throw new RuntimeException(e); - } - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/soroban/InitiatorController.java b/src/main/java/com/sparrowwallet/sparrow/soroban/InitiatorController.java deleted file mode 100644 index 87d9329c..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/soroban/InitiatorController.java +++ /dev/null @@ -1,722 +0,0 @@ -package com.sparrowwallet.sparrow.soroban; - -import com.google.common.eventbus.Subscribe; -import com.samourai.soroban.client.OnlineSorobanInteraction; -import com.samourai.soroban.client.meeting.SorobanResponseMessage; -import com.samourai.soroban.client.wallet.SorobanWalletService; -import com.samourai.soroban.client.wallet.sender.CahootsSorobanInitiatorListener; -import com.samourai.wallet.cahoots.CahootsContext; -import com.samourai.soroban.client.cahoots.OnlineCahootsMessage; -import com.samourai.wallet.bip47.rpc.PaymentCode; -import com.samourai.wallet.cahoots.Cahoots; -import com.samourai.wallet.cahoots.CahootsType; -import com.samourai.wallet.cahoots.TxBroadcastInteraction; -import com.samourai.wallet.sorobanClient.SorobanInteraction; -import com.sparrowwallet.drongo.SecureString; -import com.sparrowwallet.drongo.crypto.ECKey; -import com.sparrowwallet.drongo.crypto.EncryptionType; -import com.sparrowwallet.drongo.crypto.InvalidPasswordException; -import com.sparrowwallet.drongo.crypto.Key; -import com.sparrowwallet.drongo.protocol.Transaction; -import com.sparrowwallet.drongo.protocol.TransactionInput; -import com.sparrowwallet.drongo.psbt.PSBTParseException; -import com.sparrowwallet.drongo.wallet.*; -import com.sparrowwallet.sparrow.AppServices; -import com.sparrowwallet.sparrow.EventManager; -import com.sparrowwallet.sparrow.control.PayNymAvatar; -import com.sparrowwallet.sparrow.control.ProgressTimer; -import com.sparrowwallet.sparrow.control.TransactionDiagram; -import com.sparrowwallet.sparrow.control.WalletPasswordDialog; -import com.sparrowwallet.sparrow.event.StorageEvent; -import com.sparrowwallet.sparrow.event.TimedEvent; -import com.sparrowwallet.sparrow.event.WalletNodeHistoryChangedEvent; -import com.sparrowwallet.sparrow.io.Config; -import com.sparrowwallet.sparrow.io.Storage; -import com.sparrowwallet.sparrow.net.ElectrumServer; -import com.sparrowwallet.sparrow.paynym.PayNym; -import com.sparrowwallet.sparrow.paynym.PayNymAddress; -import com.sparrowwallet.sparrow.paynym.PayNymDialog; -import com.sparrowwallet.sparrow.paynym.PayNymService; -import io.reactivex.Observable; -import javafx.application.Platform; -import javafx.beans.property.ObjectProperty; -import javafx.beans.property.SimpleObjectProperty; -import javafx.beans.property.SimpleStringProperty; -import javafx.beans.property.StringProperty; -import javafx.beans.value.ChangeListener; -import javafx.collections.FXCollections; -import javafx.concurrent.Service; -import javafx.concurrent.Task; -import javafx.event.ActionEvent; -import javafx.fxml.FXML; -import javafx.scene.control.*; -import javafx.scene.input.KeyCode; -import javafx.scene.input.KeyEvent; -import javafx.scene.layout.VBox; -import javafx.util.Duration; -import javafx.util.StringConverter; -import org.controlsfx.glyphfont.Glyph; -import org.controlsfx.validation.ValidationResult; -import org.controlsfx.validation.ValidationSupport; -import org.controlsfx.validation.decoration.StyleClassValidationDecoration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.*; -import java.util.concurrent.TimeoutException; -import java.util.function.UnaryOperator; - -import static com.sparrowwallet.sparrow.AppServices.showErrorDialog; -import static com.sparrowwallet.sparrow.paynym.PayNymController.PAYNYM_REGEX; - -public class InitiatorController extends SorobanController { - private static final Logger log = LoggerFactory.getLogger(InitiatorController.class); - - private static final PayNym FIND_FOLLOWERS = new PayNym(null, null, "Retrieve Contacts...", false, Collections.emptyList(), Collections.emptyList()); - - private String walletId; - private Wallet wallet; - private WalletTransaction walletTransaction; - - @FXML - private VBox step1; - - @FXML - private VBox step2; - - @FXML - private VBox step3; - - @FXML - private VBox step4; - - @FXML - private ComboBox payNymFollowers; - - @FXML - private TextField counterparty; - - @FXML - private ProgressIndicator payNymLoading; - - @FXML - private Button findPayNym; - - @FXML - private PayNymAvatar payNymAvatar; - - @FXML - private ProgressTimer step2Timer; - - @FXML - private Label step2Desc; - - @FXML - private Hyperlink meetingFail; - - @FXML - private ProgressBar sorobanProgressBar; - - @FXML - private Label sorobanProgressLabel; - - @FXML - private Glyph mixDeclined; - - @FXML - private ProgressTimer step3Timer; - - @FXML - private Label step3Desc; - - @FXML - private TransactionDiagram transactionDiagram; - - @FXML - private Label step4Desc; - - @FXML - private ProgressBar broadcastProgressBar; - - @FXML - private Label broadcastProgressLabel; - - @FXML - private Glyph broadcastSuccessful; - - private final StringProperty counterpartyPayNymName = new SimpleStringProperty(); - - private final ObjectProperty counterpartyPaymentCode = new SimpleObjectProperty<>(null); - - private final ObjectProperty stepProperty = new SimpleObjectProperty<>(Step.SETUP); - - private final ObjectProperty transactionAccepted = new SimpleObjectProperty<>(null); - - private final ObjectProperty transactionProperty = new SimpleObjectProperty<>(null); - - private CahootsType cahootsType = CahootsType.STONEWALLX2; - - private ElectrumServer.TransactionMempoolService transactionMempoolService; - - private boolean closed; - - private final ChangeListener counterpartyListener = (observable, oldValue, newValue) -> { - if(newValue != null) { - if(newValue.startsWith("P") && newValue.contains("...") && newValue.length() == 20 && counterpartyPaymentCode.get() != null) { - //Assumed valid payment code - } else if(isUsePayNym(wallet) && PAYNYM_REGEX.matcher(newValue).matches()) { - if(!newValue.equals(counterpartyPayNymName.get())) { - searchPayNyms(newValue); - } - } else if(!newValue.equals(counterpartyPayNymName.get())) { - counterpartyPayNymName.set(null); - counterpartyPaymentCode.set(null); - payNymAvatar.clearPaymentCode(); - } - } - }; - - public void initializeView(String walletId, Wallet wallet, WalletTransaction walletTransaction) { - this.walletId = walletId; - this.wallet = wallet; - this.walletTransaction = walletTransaction; - - step1.managedProperty().bind(step1.visibleProperty()); - step2.managedProperty().bind(step2.visibleProperty()); - step3.managedProperty().bind(step3.visibleProperty()); - step4.managedProperty().bind(step4.visibleProperty()); - - sorobanProgressBar.managedProperty().bind(sorobanProgressBar.visibleProperty()); - sorobanProgressLabel.managedProperty().bind(sorobanProgressLabel.visibleProperty()); - mixDeclined.managedProperty().bind(mixDeclined.visibleProperty()); - sorobanProgressBar.visibleProperty().bind(sorobanProgressLabel.visibleProperty()); - mixDeclined.visibleProperty().bind(sorobanProgressLabel.visibleProperty().not()); - step2Timer.visibleProperty().bind(sorobanProgressLabel.visibleProperty()); - broadcastProgressBar.managedProperty().bind(broadcastProgressBar.visibleProperty()); - broadcastProgressLabel.managedProperty().bind(broadcastProgressLabel.visibleProperty()); - broadcastSuccessful.managedProperty().bind(broadcastSuccessful.visibleProperty()); - broadcastSuccessful.setVisible(false); - - meetingFail.managedProperty().bind(meetingFail.visibleProperty()); - meetingFail.setVisited(true); - meetingFail.setVisible(false); - meetingFail.setOnAction(event -> { - meetingFail.setVisible(false); - step2Desc.setText("Retrying..."); - sorobanProgressLabel.setVisible(true); - startInitiatorMeetAndInitiate(AppServices.getSorobanServices().getSoroban(walletId), wallet); - step2Timer.start(); - }); - - step2.setVisible(false); - step3.setVisible(false); - step4.setVisible(false); - - transactionAccepted.addListener((observable, oldValue, accepted) -> { - if(transactionProperty.get() != null && stepProperty.get() != Step.REBROADCAST) { - Platform.exitNestedEventLoop(transactionAccepted, accepted); - } - }); - - transactionProperty.addListener((observable, oldValue, transaction) -> { - if(transaction != null) { - updateTransactionDiagram(transactionDiagram, wallet, walletTransaction, transaction); - } - }); - - step2.visibleProperty().addListener((observable, oldValue, visible) -> { - if(visible) { - startInitiatorMeetAndInitiate(); - step2Timer.start(); - } - }); - - payNymLoading.managedProperty().bind(payNymLoading.visibleProperty()); - payNymLoading.maxHeightProperty().bind(counterparty.heightProperty()); - payNymLoading.setVisible(false); - - payNymAvatar.managedProperty().bind(payNymAvatar.visibleProperty()); - payNymFollowers.prefWidthProperty().bind(counterparty.widthProperty()); - payNymFollowers.valueProperty().addListener((observable, oldValue, payNym) -> { - if(payNym == FIND_FOLLOWERS) { - setUsePayNym(wallet, true); - setPayNymFollowers(); - } else if(payNym != null) { - counterpartyPayNymName.set(payNym.nymName()); - counterpartyPaymentCode.set(new PaymentCode(payNym.paymentCode().toString())); - payNymAvatar.setPaymentCode(payNym.paymentCode()); - counterparty.setText(payNym.nymName()); - step1.requestFocus(); - } - }); - payNymFollowers.setConverter(new StringConverter<>() { - @Override - public String toString(PayNym payNym) { - return payNym == null ? "" : payNym.nymName(); - } - - @Override - public PayNym fromString(String string) { - return null; - } - }); - - UnaryOperator paymentCodeFilter = change -> { - String input = change.getControlNewText(); - if(input.startsWith("P") && !input.contains("...")) { - try { - PaymentCode paymentCode = new PaymentCode(input); - if(paymentCode.isValid()) { - counterpartyPaymentCode.set(paymentCode); - if(payNymAvatar.getPaymentCode() == null || !input.equals(payNymAvatar.getPaymentCode().toString())) { - payNymAvatar.setPaymentCode(paymentCode); - } - - TextInputControl control = (TextInputControl)change.getControl(); - change.setText(input.substring(0, 12) + "..." + input.substring(input.length() - 5)); - change.setRange(0, control.getLength()); - change.setAnchor(change.getText().length()); - change.setCaretPosition(change.getText().length()); - } - } catch(Exception e) { - //ignore - } - } - - return change; - }; - counterparty.setTextFormatter(new TextFormatter<>(paymentCodeFilter)); - counterparty.textProperty().addListener(counterpartyListener); - counterparty.addEventFilter(KeyEvent.ANY, event -> { - if(counterparty.isEditable() && event.getCode() == KeyCode.ENTER) { - searchPayNyms(counterparty.getText()); - event.consume(); - } - }); - - stepProperty.addListener((observable, oldValue, step) -> { - if(step == Step.BROADCAST) { - step4Desc.setText("Broadcasting the mix transaction..."); - broadcastProgressLabel.setVisible(true); - } else if(step == Step.REBROADCAST) { - step4Desc.setText("Rebroadcast the mix transaction."); - broadcastProgressLabel.setVisible(false); - } - }); - - Payment payment = walletTransaction.getPayments().get(0); - if(payment.getAddress() instanceof PayNymAddress payNymAddress) { - PayNym payNym = payNymAddress.getPayNym(); - counterpartyPayNymName.set(payNym.nymName()); - counterpartyPaymentCode.set(new PaymentCode(payNym.paymentCode().toString())); - payNymAvatar.setPaymentCode(payNym.paymentCode()); - counterparty.setText(payNym.nymName()); - counterparty.setEditable(false); - findPayNym.setVisible(false); - cahootsType = CahootsType.STOWAWAY; - } else if(Config.get().isUsePayNym()) { - setPayNymFollowers(); - } else { - List defaultList = new ArrayList<>(); - defaultList.add(FIND_FOLLOWERS); - payNymFollowers.setItems(FXCollections.observableList(defaultList)); - } - - ValidationSupport validationSupport = new ValidationSupport(); - Platform.runLater(() -> { - validationSupport.setValidationDecorator(new StyleClassValidationDecoration()); - validationSupport.registerValidator(counterparty, (Control c, String newValue) -> ValidationResult.fromErrorIf(c, "Invalid counterparty", !isValidCounterparty())); - }); - } - - private void searchPayNyms(String identifier) { - payNymLoading.setVisible(true); - PayNymService.getPayNym(identifier).subscribe(payNym -> { - payNymLoading.setVisible(false); - counterpartyPayNymName.set(payNym.nymName()); - counterpartyPaymentCode.set(new PaymentCode(payNym.paymentCode().toString())); - payNymAvatar.setPaymentCode(payNym.paymentCode()); - counterparty.textProperty().removeListener(counterpartyListener); - int caret = counterparty.getCaretPosition(); - counterparty.setText(""); - counterparty.setText(payNym.nymName()); - counterparty.positionCaret(caret); - counterparty.textProperty().addListener(counterpartyListener); - }, error -> { - payNymLoading.setVisible(false); - //ignore, probably doesn't exist but will try again on meeting request - }); - } - - private void setPayNymFollowers() { - Wallet masterWallet = wallet.isMasterWallet() ? wallet : wallet.getMasterWallet(); - PayNymService.getPayNym(masterWallet.getPaymentCode().toString()).map(PayNym::following).subscribe(followerPayNyms -> { - findPayNym.setVisible(true); - payNymFollowers.setItems(FXCollections.observableList(followerPayNyms)); - }, error -> { - if(error.getMessage().endsWith("404")) { - setUsePayNym(masterWallet, false); - AppServices.showErrorDialog("Could not retrieve PayNym", "This wallet does not have an associated PayNym or any followers yet. You can retrieve the PayNym using the Find PayNym button."); - } else { - log.warn("Could not retrieve followers: ", error); - } - }); - } - - private void startInitiatorMeetAndInitiate() { - Soroban soroban = AppServices.getSorobanServices().getSoroban(walletId); - if(soroban.getHdWallet() == null) { - if(wallet.isEncrypted()) { - Wallet copy = wallet.copy(); - WalletPasswordDialog dlg = new WalletPasswordDialog(copy.getMasterName(), WalletPasswordDialog.PasswordRequirement.LOAD); - dlg.initOwner(payNymFollowers.getScene().getWindow()); - Optional password = dlg.showAndWait(); - if(password.isPresent()) { - Storage storage = AppServices.get().getOpenWallets().get(wallet); - Storage.KeyDerivationService keyDerivationService = new Storage.KeyDerivationService(storage, password.get(), true); - keyDerivationService.setOnSucceeded(workerStateEvent -> { - EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.END, "Done")); - ECKey encryptionFullKey = keyDerivationService.getValue(); - Key key = new Key(encryptionFullKey.getPrivKeyBytes(), storage.getKeyDeriver().getSalt(), EncryptionType.Deriver.ARGON2); - copy.decrypt(key); - - try { - soroban.setHDWallet(copy); - startInitiatorMeetAndInitiate(soroban, wallet); - } finally { - key.clear(); - encryptionFullKey.clear(); - password.get().clear(); - } - }); - keyDerivationService.setOnFailed(workerStateEvent -> { - EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.END, "Failed")); - if(keyDerivationService.getException() instanceof InvalidPasswordException) { - Optional optResponse = showErrorDialog("Invalid Password", "The wallet password was invalid. Try again?", ButtonType.CANCEL, ButtonType.OK); - if(optResponse.isPresent() && optResponse.get().equals(ButtonType.OK)) { - Platform.runLater(this::startInitiatorMeetAndInitiate); - } - } else { - log.error("Error deriving wallet key", keyDerivationService.getException()); - } - }); - EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.START, "Decrypting wallet...")); - keyDerivationService.start(); - } else { - step2.setVisible(false); - step1.setVisible(true); - } - } else { - soroban.setHDWallet(wallet); - startInitiatorMeetAndInitiate(soroban, wallet); - } - } else { - startInitiatorMeetAndInitiate(soroban, wallet); - } - } - - private void startInitiatorMeetAndInitiate(Soroban soroban, Wallet wallet) { - getPaymentCodeCounterparty().subscribe(paymentCodeCounterparty -> { - SparrowCahootsWallet cahootsWallet = soroban.getCahootsWallet(wallet); - Map firstSetUtxos = walletTransaction.isCoinControlUsed() ? walletTransaction.getSelectedUtxoSets().get(0) : wallet.getSpendableUtxos(); - for(Map.Entry entry : firstSetUtxos.entrySet()) { - cahootsWallet.addUtxo(entry.getValue(), wallet.getWalletTransaction(entry.getKey().getHash()), (int)entry.getKey().getIndex()); - } - - Payment payment = walletTransaction.getPayments().get(0); - long feePerB = (long)walletTransaction.getFeeRate(); - CahootsContext cahootsContext = CahootsContext.newInitiator(cahootsWallet, cahootsType, cahootsWallet.getAccount(), feePerB, payment.getAmount(), payment.getAddress().getAddress(), paymentCodeCounterparty.toString()); - - CahootsSorobanInitiatorListener listener = new CahootsSorobanInitiatorListener() { - @Override - public void onResponse(SorobanResponseMessage sorobanResponse) throws Exception { - super.onResponse(sorobanResponse); - - Platform.runLater(() -> { - requestUserAttention(); - if(sorobanResponse.isAccept()) { - sorobanProgressBar.setProgress(0.1); - sorobanProgressLabel.setText("Mix partner accepted!"); - } else { - step2Desc.setText("Mix partner declined."); - sorobanProgressLabel.setVisible(false); - } - }); - } - - @Override - public void onInteraction(OnlineSorobanInteraction interaction) throws Exception { - SorobanInteraction originInteraction = interaction.getInteraction(); - if(originInteraction instanceof TxBroadcastInteraction) { - Platform.runLater(() -> { - try { - Boolean accepted = (Boolean)Platform.enterNestedEventLoop(transactionAccepted); - if(accepted) { - interaction.sorobanAccept(); - } else { - interaction.sorobanReject("Mix partner declined to broadcast the transaction."); - } - } catch(Exception e) { - log.error("Error accepting Soroban interaction", e); - } - }); - } else { - throw new Exception("Unknown interaction: "+originInteraction.getTypeInteraction()); - } - } - - @Override - public void progress(OnlineCahootsMessage cahootsMessage) { - super.progress(cahootsMessage); - - if(cahootsMessage != null) { - Platform.runLater(() -> { - Cahoots cahoots = cahootsMessage.getCahoots(); - sorobanProgressBar.setProgress((double)(cahoots.getStep() + 1) / 5); - - if(cahoots.getStep() >= 3) { - try { - Transaction transaction = getTransaction(cahoots); - if(transaction != null) { - transactionProperty.set(transaction); - if(cahoots.getStep() == 3) { - next(); - step3Timer.start(e -> { - if(stepProperty.get() != Step.BROADCAST && stepProperty.get() != Step.REBROADCAST) { - step3Desc.setText("Transaction declined due to timeout."); - transactionAccepted.set(Boolean.FALSE); - } - }); - } else if(cahoots.getStep() == 4) { - next(); - broadcastTransaction(); - } - } - } catch(PSBTParseException e) { - log.error("Invalid collaborative PSBT created", e); - step2Desc.setText("Invalid transaction created."); - sorobanProgressLabel.setVisible(false); - } - } - }); - } - } - }; - SorobanWalletService sorobanWalletService = soroban.getSorobanWalletService(); - sorobanProgressLabel.setText("Waiting for mix partner..."); - - Service cahootsService = new Service<>() { - @Override - protected Task createTask() { - return new Task<>() { - @Override - protected Cahoots call() throws Exception { - return sorobanWalletService.getSorobanWalletInitiator(cahootsWallet).meetAndInitiate(cahootsContext, paymentCodeCounterparty, listener); - } - }; - } - }; - cahootsService.setOnFailed(event -> { - Throwable error = event.getSource().getException(); - log.error("Error receiving meeting response", error); - step2Desc.setText(getErrorMessage(error)); - sorobanProgressLabel.setVisible(false); - meetingFail.setVisible(true); - requestUserAttention(); - }); - cahootsService.start(); - }, error -> { - log.error("Could not retrieve payment code", error); - if(error.getMessage().endsWith("404")) { - step2Desc.setText("PayNym not found"); - } else if(error.getMessage().endsWith("400")) { - step2Desc.setText("Could not retrieve PayNym"); - } else { - step2Desc.setText(error.getMessage()); - } - sorobanProgressLabel.setVisible(false); - }); - } - - public void broadcastTransaction() { - stepProperty.set(Step.BROADCAST); - - ElectrumServer.BroadcastTransactionService broadcastTransactionService = new ElectrumServer.BroadcastTransactionService(getTransaction()); - broadcastTransactionService.setOnRunning(workerStateEvent -> { - broadcastProgressBar.setProgress(-1); - }); - broadcastTransactionService.setOnSucceeded(workerStateEvent -> { - Map selectedUtxos = new HashMap<>(); - Map walletTxos = wallet.getWalletTxos(); - for(TransactionInput txInput : getTransaction().getInputs()) { - Optional optSelectedUtxo = walletTxos.keySet().stream().filter(txo -> txInput.getOutpoint().getHash().equals(txo.getHash()) && txInput.getOutpoint().getIndex() == txo.getIndex()) - .findFirst(); - optSelectedUtxo.ifPresent(blockTransactionHashIndex -> selectedUtxos.put(blockTransactionHashIndex, walletTxos.get(blockTransactionHashIndex))); - } - - if(transactionMempoolService != null) { - transactionMempoolService.cancel(); - } - - transactionMempoolService = new ElectrumServer.TransactionMempoolService(wallet, getTransaction().getTxId(), new HashSet<>(selectedUtxos.values())); - transactionMempoolService.setDelay(Duration.seconds(3)); - transactionMempoolService.setPeriod(Duration.seconds(10)); - transactionMempoolService.setRestartOnFailure(false); - transactionMempoolService.setOnSucceeded(mempoolWorkerStateEvent -> { - Set scriptHashes = transactionMempoolService.getValue(); - if(!scriptHashes.isEmpty()) { - Platform.runLater(() -> EventManager.get().post(new WalletNodeHistoryChangedEvent(scriptHashes.iterator().next()))); - } - - if(transactionMempoolService.getIterationCount() > 3 && transactionMempoolService.isRunning()) { - transactionMempoolService.cancel(); - broadcastProgressBar.setProgress(0); - log.error("Timeout searching for broadcasted transaction"); - AppServices.showErrorDialog("Timeout searching for broadcasted transaction", "The transaction was broadcast but the server did not register it in the mempool. It is safe to try broadcasting again."); - stepProperty.set(Step.REBROADCAST); - } - }); - transactionMempoolService.setOnFailed(mempoolWorkerStateEvent -> { - transactionMempoolService.cancel(); - broadcastProgressBar.setProgress(0); - log.error("Timeout searching for broadcasted transaction"); - AppServices.showErrorDialog("Timeout searching for broadcasted transaction", "The transaction was broadcast but the server did not indicate it had entered the mempool. It is safe to try broadcasting again."); - stepProperty.set(Step.REBROADCAST); - }); - - if(!closed) { - transactionMempoolService.start(); - } - }); - broadcastTransactionService.setOnFailed(workerStateEvent -> { - broadcastProgressBar.setProgress(0); - Throwable exception = workerStateEvent.getSource().getException(); - while(exception.getCause() != null) { - exception = exception.getCause(); - } - - log.error("Error broadcasting transaction", exception); - AppServices.showErrorDialog("Error broadcasting transaction", exception.getMessage()); - stepProperty.set(Step.REBROADCAST); - }); - broadcastTransactionService.start(); - } - - public void next() { - if(step1.isVisible()) { - step1.setVisible(false); - step2.setVisible(true); - stepProperty.set(Step.COMMUNICATE); - return; - } - - if(step2.isVisible()) { - step2.setVisible(false); - step3.setVisible(true); - stepProperty.set(Step.REVIEW); - return; - } - - if(step3.isVisible()) { - step3.setVisible(false); - step4.setVisible(true); - stepProperty.set(Step.BROADCAST); - } - } - - private Observable getPaymentCodeCounterparty() { - if(counterpartyPaymentCode.get() != null) { - return Observable.just(counterpartyPaymentCode.get()); - } else { - return PayNymService.getPayNym(counterparty.getText()).map(payNym -> new PaymentCode(payNym.paymentCode().toString())); - } - } - - private boolean isValidCounterparty() { - if(counterpartyPaymentCode.get() != null) { - return true; - } - - if(counterparty.getText().startsWith("P") && counterparty.getText().contains("...") && counterparty.getText().length() == 20) { - return true; - } - - return PAYNYM_REGEX.matcher(counterparty.getText()).matches(); - } - - public void accept() { - transactionAccepted.set(Boolean.TRUE); - } - - public void cancel() { - transactionAccepted.set(Boolean.FALSE); - } - - public void findPayNym(ActionEvent event) { - PayNymDialog payNymDialog = new PayNymDialog(walletId, PayNymDialog.Operation.SELECT, false); - payNymDialog.initOwner(payNymFollowers.getScene().getWindow()); - Optional optPayNym = payNymDialog.showAndWait(); - optPayNym.ifPresent(payNym -> { - counterpartyPayNymName.set(payNym.nymName()); - counterpartyPaymentCode.set(new PaymentCode(payNym.paymentCode().toString())); - payNymAvatar.setPaymentCode(payNym.paymentCode()); - counterparty.setText(payNym.nymName()); - step1.requestFocus(); - }); - } - - public ObjectProperty counterpartyPaymentCodeProperty() { - return counterpartyPaymentCode; - } - - public ObjectProperty stepProperty() { - return stepProperty; - } - - public Transaction getTransaction() { - return transactionProperty.get(); - } - - public void close() { - closed = true; - if(transactionMempoolService != null) { - transactionMempoolService.cancel(); - } - } - - private static String getErrorMessage(Throwable error) { - String message = error.getMessage() == null ? (error instanceof TimeoutException ? "Timed out receiving meeting response" : "Error receiving meeting response") : error.getMessage(); - String cutFrom = "Exception: "; - int index = message.lastIndexOf(cutFrom); - String msg = index < 0 ? message : message.substring(index + cutFrom.length()); - msg = msg.replace("#Cahoots", "mix transaction"); - msg = msg.endsWith(".") ? msg : msg + "."; - return msg; - } - - public boolean isTransactionAccepted() { - return transactionAccepted.get() == Boolean.TRUE; - } - - public ObjectProperty transactionAcceptedProperty() { - return transactionAccepted; - } - - @Subscribe - public void walletNodeHistoryChanged(WalletNodeHistoryChangedEvent event) { - if(event.getWalletNode(wallet) != null) { - if(transactionMempoolService != null) { - transactionMempoolService.cancel(); - } - - broadcastProgressBar.setVisible(false); - broadcastProgressLabel.setVisible(false); - step4Desc.setText("Transaction broadcasted."); - broadcastSuccessful.setVisible(true); - } - } - - public enum Step { - SETUP, COMMUNICATE, REVIEW, BROADCAST, REBROADCAST - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/soroban/InitiatorDialog.java b/src/main/java/com/sparrowwallet/sparrow/soroban/InitiatorDialog.java deleted file mode 100644 index 7c69bf1b..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/soroban/InitiatorDialog.java +++ /dev/null @@ -1,169 +0,0 @@ -package com.sparrowwallet.sparrow.soroban; - -import com.sparrowwallet.drongo.SecureString; -import com.sparrowwallet.drongo.crypto.InvalidPasswordException; -import com.sparrowwallet.drongo.protocol.Transaction; -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.drongo.wallet.WalletTransaction; -import com.sparrowwallet.sparrow.AppServices; -import com.sparrowwallet.sparrow.EventManager; -import com.sparrowwallet.sparrow.control.WalletPasswordDialog; -import com.sparrowwallet.sparrow.event.StorageEvent; -import com.sparrowwallet.sparrow.event.TimedEvent; -import com.sparrowwallet.sparrow.io.Storage; -import javafx.application.Platform; -import javafx.event.ActionEvent; -import javafx.fxml.FXMLLoader; -import javafx.scene.control.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.Optional; - -import static com.sparrowwallet.sparrow.AppServices.showErrorDialog; - -public class InitiatorDialog extends Dialog { - private static final Logger log = LoggerFactory.getLogger(InitiatorDialog.class); - - private final boolean confirmationRequired; - - public InitiatorDialog(String walletId, Wallet wallet, WalletTransaction walletTransaction) { - this.confirmationRequired = AppServices.getSorobanServices().getSoroban(walletId).getHdWallet() != null; - - final DialogPane dialogPane = getDialogPane(); - AppServices.setStageIcon(dialogPane.getScene().getWindow()); - AppServices.onEscapePressed(dialogPane.getScene(), this::close); - - try { - FXMLLoader initiatorLoader = new FXMLLoader(AppServices.class.getResource("soroban/initiator.fxml")); - dialogPane.setContent(initiatorLoader.load()); - InitiatorController initiatorController = initiatorLoader.getController(); - initiatorController.initializeView(walletId, wallet, walletTransaction); - - EventManager.get().register(initiatorController); - - dialogPane.setPrefWidth(730); - dialogPane.setPrefHeight(530); - dialogPane.setMinHeight(dialogPane.getPrefHeight()); - AppServices.moveToActiveWindowScreen(this); - - dialogPane.getStylesheets().add(AppServices.class.getResource("general.css").toExternalForm()); - dialogPane.getStylesheets().add(AppServices.class.getResource("app.css").toExternalForm()); - dialogPane.getStylesheets().add(AppServices.class.getResource("soroban/initiator.css").toExternalForm()); - - final ButtonType nextButtonType = new javafx.scene.control.ButtonType("Next", ButtonBar.ButtonData.OK_DONE); - final ButtonType cancelButtonType = new javafx.scene.control.ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE); - final ButtonType broadcastButtonType = new javafx.scene.control.ButtonType("Sign & Broadcast", ButtonBar.ButtonData.APPLY); - final ButtonType doneButtonType = new javafx.scene.control.ButtonType("Done", ButtonBar.ButtonData.APPLY); - dialogPane.getButtonTypes().addAll(nextButtonType, cancelButtonType, broadcastButtonType, doneButtonType); - - Button nextButton = (Button)dialogPane.lookupButton(nextButtonType); - Button cancelButton = (Button)dialogPane.lookupButton(cancelButtonType); - Button broadcastButton = (Button)dialogPane.lookupButton(broadcastButtonType); - Button doneButton = (Button)dialogPane.lookupButton(doneButtonType); - nextButton.setDisable(initiatorController.counterpartyPaymentCodeProperty().get() == null); - broadcastButton.setDisable(true); - - nextButton.managedProperty().bind(nextButton.visibleProperty()); - cancelButton.managedProperty().bind(cancelButton.visibleProperty()); - broadcastButton.managedProperty().bind(broadcastButton.visibleProperty()); - doneButton.managedProperty().bind(doneButton.visibleProperty()); - - broadcastButton.setVisible(false); - doneButton.setVisible(false); - - initiatorController.counterpartyPaymentCodeProperty().addListener((observable, oldValue, paymentCode) -> { - nextButton.setDisable(paymentCode == null || !AppServices.isConnected()); - }); - - initiatorController.stepProperty().addListener((observable, oldValue, step) -> { - if(step == InitiatorController.Step.SETUP) { - nextButton.setDisable(false); - nextButton.setVisible(true); - } else if(step == InitiatorController.Step.COMMUNICATE) { - nextButton.setDisable(true); - nextButton.setVisible(true); - } else if(step == InitiatorController.Step.REVIEW) { - nextButton.setVisible(false); - broadcastButton.setVisible(true); - broadcastButton.setDefaultButton(true); - broadcastButton.setDisable(false); - } else if(step == InitiatorController.Step.BROADCAST) { - cancelButton.setVisible(false); - broadcastButton.setVisible(false); - doneButton.setVisible(true); - doneButton.setDefaultButton(true); - } else if(step == InitiatorController.Step.REBROADCAST) { - cancelButton.setVisible(true); - broadcastButton.setVisible(true); - broadcastButton.setDisable(false); - doneButton.setVisible(false); - } - }); - - initiatorController.transactionAcceptedProperty().addListener((observable, oldValue, accepted) -> { - broadcastButton.setDisable(accepted != Boolean.TRUE); - }); - - nextButton.addEventFilter(ActionEvent.ACTION, event -> { - initiatorController.next(); - event.consume(); - }); - - cancelButton.addEventFilter(ActionEvent.ACTION, event -> { - initiatorController.cancel(); - }); - - broadcastButton.addEventFilter(ActionEvent.ACTION, event -> { - if(initiatorController.isTransactionAccepted()) { - initiatorController.broadcastTransaction(); - } else { - acceptAndBroadcast(initiatorController, walletId, wallet); - } - event.consume(); - }); - - setOnCloseRequest(event -> { - initiatorController.close(); - EventManager.get().unregister(initiatorController); - }); - - setResultConverter(dialogButton -> dialogButton.getButtonData().equals(ButtonBar.ButtonData.APPLY) ? initiatorController.getTransaction() : null); - } catch(IOException e) { - throw new RuntimeException(e); - } - } - - private void acceptAndBroadcast(InitiatorController initiatorController, String walletId, Wallet wallet) { - if(confirmationRequired && wallet.isEncrypted()) { - WalletPasswordDialog dlg = new WalletPasswordDialog(wallet.getMasterName(), WalletPasswordDialog.PasswordRequirement.LOAD); - dlg.initOwner(getDialogPane().getScene().getWindow()); - Optional password = dlg.showAndWait(); - if(password.isPresent()) { - Storage storage = AppServices.get().getOpenWallets().get(wallet); - Storage.KeyDerivationService keyDerivationService = new Storage.KeyDerivationService(storage, password.get(), true); - keyDerivationService.setOnSucceeded(workerStateEvent -> { - EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.END, "Done")); - initiatorController.accept(); - password.get().clear(); - }); - keyDerivationService.setOnFailed(workerStateEvent -> { - EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.END, "Failed")); - if(keyDerivationService.getException() instanceof InvalidPasswordException) { - Optional optResponse = showErrorDialog("Invalid Password", "The wallet password was invalid. Try again?", ButtonType.CANCEL, ButtonType.OK); - if(optResponse.isPresent() && optResponse.get().equals(ButtonType.OK)) { - Platform.runLater(() -> acceptAndBroadcast(initiatorController, walletId, wallet)); - } - } else { - log.error("Error deriving wallet key", keyDerivationService.getException()); - } - }); - EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.START, "Decrypting wallet...")); - keyDerivationService.start(); - } - } else { - initiatorController.accept(); - } - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/soroban/Soroban.java b/src/main/java/com/sparrowwallet/sparrow/soroban/Soroban.java deleted file mode 100644 index b933b40e..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/soroban/Soroban.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.sparrowwallet.sparrow.soroban; - -import com.samourai.soroban.client.SorobanConfig; -import com.samourai.soroban.client.wallet.SorobanWalletService; -import com.samourai.wallet.hd.HD_Wallet; -import com.samourai.wallet.util.ExtLibJConfig; -import com.sparrowwallet.drongo.Network; -import com.sparrowwallet.drongo.protocol.ScriptType; -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.sparrow.AppServices; -import com.sparrowwallet.sparrow.whirlpool.Whirlpool; -import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowChainSupplier; -import org.bitcoinj.core.NetworkParameters; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -public class Soroban { - private static final Logger log = LoggerFactory.getLogger(Soroban.class); - - protected static final int TIMEOUT_MS = 60000; - public static final List SOROBAN_NETWORKS = List.of(Network.MAINNET, Network.TESTNET); - - private final SorobanWalletService sorobanWalletService; - - private HD_Wallet hdWallet; - private int bip47Account; - private SparrowChainSupplier chainSupplier; - - public Soroban() { - SorobanConfig sorobanConfig = AppServices.getWhirlpoolServices().getSorobanConfig(); - this.sorobanWalletService = sorobanConfig.getSorobanWalletService(); - } - - public HD_Wallet getHdWallet() { - return hdWallet; - } - - public void setHDWallet(Wallet wallet) { - ExtLibJConfig extLibJConfig = sorobanWalletService.getSorobanService().getSorobanConfig().getExtLibJConfig(); - NetworkParameters params = extLibJConfig.getSamouraiNetwork().getParams(); - hdWallet = Whirlpool.computeHdWallet(wallet, params); - bip47Account = wallet.isMasterWallet() ? wallet.getAccountIndex() : wallet.getMasterWallet().getAccountIndex(); - } - - public SparrowCahootsWallet getCahootsWallet(Wallet wallet) { - if(wallet.getScriptType() != ScriptType.P2WPKH) { - throw new IllegalArgumentException("Wallet must be P2WPKH"); - } - - if(hdWallet == null) { - for(Wallet associatedWallet : wallet.getAllWallets()) { - Soroban soroban = AppServices.getSorobanServices().getSoroban(associatedWallet); - if(soroban != null && soroban.getHdWallet() != null) { - hdWallet = soroban.hdWallet; - } - } - } - - if(hdWallet == null) { - throw new IllegalStateException("HD wallet is not set"); - } - - try { - if(chainSupplier == null) { - chainSupplier = new SparrowChainSupplier(wallet.getStoredBlockHeight()); - chainSupplier.open(); - } - return new SparrowCahootsWallet(chainSupplier, wallet, hdWallet, bip47Account); - } catch(Exception e) { - log.error("Could not create cahoots wallet", e); - } - - return null; - } - - public int getBip47Account() { - return bip47Account; - } - - public SorobanWalletService getSorobanWalletService() { - return sorobanWalletService; - } - - public void close() { - if(chainSupplier != null) { - chainSupplier.close(); - } - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/soroban/SorobanController.java b/src/main/java/com/sparrowwallet/sparrow/soroban/SorobanController.java deleted file mode 100644 index 648645a3..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/soroban/SorobanController.java +++ /dev/null @@ -1,157 +0,0 @@ -package com.sparrowwallet.sparrow.soroban; - -import com.samourai.wallet.cahoots.Cahoots; -import com.sparrowwallet.drongo.KeyPurpose; -import com.sparrowwallet.drongo.address.Address; -import com.sparrowwallet.drongo.protocol.Sha256Hash; -import com.sparrowwallet.drongo.protocol.Transaction; -import com.sparrowwallet.drongo.protocol.TransactionInput; -import com.sparrowwallet.drongo.protocol.TransactionOutput; -import com.sparrowwallet.drongo.psbt.PSBT; -import com.sparrowwallet.drongo.psbt.PSBTParseException; -import com.sparrowwallet.drongo.wallet.*; -import com.sparrowwallet.sparrow.EventManager; -import com.sparrowwallet.sparrow.control.TransactionDiagram; -import com.sparrowwallet.sparrow.event.WalletConfigChangedEvent; -import com.sparrowwallet.sparrow.io.Config; -import com.sparrowwallet.sparrow.net.ElectrumServer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.awt.Taskbar; -import java.util.*; -import java.util.stream.Collectors; - -public class SorobanController { - private static final Logger log = LoggerFactory.getLogger(SorobanController.class); - - protected Transaction getTransaction(Cahoots cahoots) throws PSBTParseException { - if(cahoots.getPSBT() != null) { - PSBT psbt = new PSBT(cahoots.getPSBT().toBytes()); - return psbt.getTransaction(); - } - - return null; - } - - protected void updateTransactionDiagram(TransactionDiagram transactionDiagram, Wallet wallet, WalletTransaction walletTransaction, Transaction transaction) { - WalletTransaction txWalletTransaction = getWalletTransaction(wallet, walletTransaction, transaction, null); - transactionDiagram.update(txWalletTransaction); - - if(txWalletTransaction.getSelectedUtxoSets().size() == 2) { - Set references = txWalletTransaction.getSelectedUtxoSets().get(1).keySet().stream().map(BlockTransactionHash::getHash).collect(Collectors.toSet()); - ElectrumServer.TransactionReferenceService transactionReferenceService = new ElectrumServer.TransactionReferenceService(references); - transactionReferenceService.setOnSucceeded(successEvent -> { - Map transactionMap = transactionReferenceService.getValue(); - transactionDiagram.update(getWalletTransaction(wallet, walletTransaction, transaction, transactionMap)); - }); - transactionReferenceService.setOnFailed(failedEvent -> { - log.error("Failed to retrieve referenced transactions", failedEvent.getSource().getException()); - }); - transactionReferenceService.start(); - } - } - - private WalletTransaction getWalletTransaction(Wallet wallet, WalletTransaction walletTransaction, Transaction transaction, Map inputTransactions) { - Map allWalletUtxos = wallet.getWalletTxos(); - Map walletUtxos = new LinkedHashMap<>(); - Map externalUtxos = new LinkedHashMap<>(); - - for(TransactionInput txInput : transaction.getInputs()) { - Optional optWalletUtxo = allWalletUtxos.keySet().stream().filter(txo -> txo.getHash().equals(txInput.getOutpoint().getHash()) && txo.getIndex() == txInput.getOutpoint().getIndex()).findFirst(); - if(optWalletUtxo.isPresent()) { - walletUtxos.put(optWalletUtxo.get(), allWalletUtxos.get(optWalletUtxo.get())); - } else { - BlockTransactionHashIndex externalUtxo; - if(inputTransactions != null && inputTransactions.containsKey(txInput.getOutpoint().getHash())) { - BlockTransaction blockTransaction = inputTransactions.get(txInput.getOutpoint().getHash()); - TransactionOutput txOutput = blockTransaction.getTransaction().getOutputs().get((int)txInput.getOutpoint().getIndex()); - externalUtxo = new BlockTransactionHashIndex(blockTransaction.getHash(), blockTransaction.getHeight(), blockTransaction.getDate(), blockTransaction.getFee(), txInput.getOutpoint().getIndex(), txOutput.getValue()); - } else { - externalUtxo = new BlockTransactionHashIndex(txInput.getOutpoint().getHash(), 0, null, null, txInput.getOutpoint().getIndex(), 0); - } - externalUtxos.put(externalUtxo, null); - } - } - - List> selectedUtxoSets = new ArrayList<>(); - selectedUtxoSets.add(walletUtxos); - selectedUtxoSets.add(externalUtxos); - - Map walletAddresses = wallet.getWalletAddresses(); - List payments = new ArrayList<>(); - Map changeMap = new LinkedHashMap<>(); - for(TransactionOutput txOutput : transaction.getOutputs()) { - Address address = txOutput.getScript().getToAddress(); - if(address != null) { - Optional optPayment = walletTransaction == null ? Optional.empty() : - walletTransaction.getPayments().stream().filter(payment -> payment.getAddress().equals(address) && payment.getAmount() == txOutput.getValue()).findFirst(); - if(optPayment.isPresent()) { - payments.add(optPayment.get()); - } else if(walletAddresses.containsKey(address) && walletAddresses.get(address).getKeyPurpose() == KeyPurpose.CHANGE) { - changeMap.put(walletAddresses.get(address), txOutput.getValue()); - } else { - Payment payment = new Payment(address, null, txOutput.getValue(), false); - if(transaction.getOutputs().stream().anyMatch(txo -> txo != txOutput && txo.getValue() == txOutput.getValue())) { - payment.setType(Payment.Type.MIX); - } - payments.add(payment); - } - } - } - - long fee = calculateFee(walletTransaction, selectedUtxoSets, transaction); - return new WalletTransaction(wallet, transaction, Collections.emptyList(), selectedUtxoSets, payments, changeMap, fee, inputTransactions); - } - - private long calculateFee(WalletTransaction walletTransaction, List> selectedUtxoSets, Transaction transaction) { - Map selectedUtxos = new LinkedHashMap<>(); - selectedUtxoSets.forEach(selectedUtxos::putAll); - - long feeAmt = 0L; - for(BlockTransactionHashIndex utxo : selectedUtxos.keySet()) { - if(utxo.getValue() == 0) { - return walletTransaction == null ? -1 : walletTransaction.getFee(); - } - - feeAmt += utxo.getValue(); - } - - for(TransactionOutput txOutput : transaction.getOutputs()) { - feeAmt -= txOutput.getValue(); - } - - return feeAmt; - } - - protected void requestUserAttention() { - if(Taskbar.isTaskbarSupported() && Taskbar.getTaskbar().isSupported(Taskbar.Feature.USER_ATTENTION)) { - Taskbar.getTaskbar().requestUserAttention(true, false); - } - } - - protected boolean isUsePayNym(Wallet wallet) { - //TODO: Remove config setting - boolean usePayNym = Config.get().isUsePayNym(); - if(usePayNym && wallet != null) { - setUsePayNym(wallet, true); - } - - return usePayNym; - } - - protected void setUsePayNym(Wallet wallet, boolean usePayNym) { - //TODO: Remove config setting - if(Config.get().isUsePayNym() != usePayNym) { - Config.get().setUsePayNym(usePayNym); - } - - if(wallet != null) { - WalletConfig walletConfig = wallet.getMasterWalletConfig(); - if(walletConfig.isUsePayNym() != usePayNym) { - walletConfig.setUsePayNym(usePayNym); - EventManager.get().post(new WalletConfigChangedEvent(wallet.isMasterWallet() ? wallet : wallet.getMasterWallet())); - } - } - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/soroban/SorobanServices.java b/src/main/java/com/sparrowwallet/sparrow/soroban/SorobanServices.java deleted file mode 100644 index ae8384b0..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/soroban/SorobanServices.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.sparrowwallet.sparrow.soroban; - -import com.google.common.eventbus.Subscribe; -import com.sparrowwallet.drongo.Network; -import com.sparrowwallet.drongo.protocol.ScriptType; -import com.sparrowwallet.drongo.wallet.DeterministicSeed; -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.sparrow.AppServices; -import com.sparrowwallet.sparrow.WalletTabData; -import com.sparrowwallet.sparrow.event.WalletTabsClosedEvent; -import com.sparrowwallet.sparrow.io.Storage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.HashMap; -import java.util.Map; - -public class SorobanServices { - private static final Logger log = LoggerFactory.getLogger(SorobanServices.class); - - private final Map sorobanMap = new HashMap<>(); - - public Soroban getSoroban(Wallet wallet) { - Wallet masterWallet = wallet.isMasterWallet() ? wallet : wallet.getMasterWallet(); - for(Map.Entry entry : AppServices.get().getOpenWallets().entrySet()) { - if(entry.getKey() == masterWallet) { - return sorobanMap.get(entry.getValue().getWalletId(entry.getKey())); - } - } - - return null; - } - - public Soroban getSoroban(String walletId) { - Soroban soroban = sorobanMap.get(walletId); - if(soroban == null) { - soroban = new Soroban(); - sorobanMap.put(walletId, soroban); - } - - return soroban; - } - - public static boolean canWalletMix(Wallet wallet) { - return Soroban.SOROBAN_NETWORKS.contains(Network.get()) - && wallet.getKeystores().size() == 1 - && wallet.getKeystores().get(0).hasSeed() - && wallet.getKeystores().get(0).getSeed().getType() == DeterministicSeed.Type.BIP39 - && wallet.getScriptType() == ScriptType.P2WPKH; - } - - @Subscribe - public void walletTabsClosed(WalletTabsClosedEvent event) { - for(WalletTabData walletTabData : event.getClosedWalletTabData()) { - String walletId = walletTabData.getStorage().getWalletId(walletTabData.getWallet()); - Soroban soroban = sorobanMap.remove(walletId); - if(soroban != null) { - soroban.close(); - } - } - } -} \ No newline at end of file diff --git a/src/main/java/com/sparrowwallet/sparrow/soroban/SparrowCahootsWallet.java b/src/main/java/com/sparrowwallet/sparrow/soroban/SparrowCahootsWallet.java deleted file mode 100644 index d15cd9c0..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/soroban/SparrowCahootsWallet.java +++ /dev/null @@ -1,120 +0,0 @@ -package com.sparrowwallet.sparrow.soroban; - -import com.samourai.wallet.api.backend.beans.UnspentOutput; -import com.samourai.wallet.bip47.rpc.BIP47Wallet; -import com.samourai.wallet.bip47.rpc.PaymentAddress; -import com.samourai.wallet.bip47.rpc.PaymentCode; -import com.samourai.wallet.bip47.rpc.java.Bip47UtilJava; -import com.samourai.wallet.bipFormat.BipFormat; -import com.samourai.wallet.cahoots.AbstractCahootsWallet; -import com.samourai.wallet.cahoots.CahootsUtxo; -import com.samourai.wallet.chain.ChainSupplier; -import com.samourai.wallet.hd.HD_Address; -import com.samourai.wallet.hd.HD_Wallet; -import com.samourai.wallet.send.MyTransactionOutPoint; -import com.sparrowwallet.drongo.KeyPurpose; -import com.sparrowwallet.drongo.protocol.ScriptType; -import com.sparrowwallet.drongo.wallet.BlockTransaction; -import com.sparrowwallet.drongo.wallet.StandardAccount; -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.drongo.wallet.WalletNode; -import com.sparrowwallet.sparrow.whirlpool.Whirlpool; -import org.bitcoinj.core.NetworkParameters; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public class SparrowCahootsWallet extends AbstractCahootsWallet { - private final Wallet wallet; - private final HD_Wallet bip84w; - private final int account; - private final List utxos; - private final Map lastWalletNodes = new HashMap<>(); - - public SparrowCahootsWallet(ChainSupplier chainSupplier, Wallet wallet, HD_Wallet bip84w, int bip47Account) { - super(chainSupplier, bip84w.getFingerprint(), new BIP47Wallet(bip84w).getAccount(bip47Account)); - this.wallet = wallet; - this.bip84w = bip84w; - this.account = wallet.getAccountIndex(); - this.utxos = new LinkedList<>(); - - bip84w.getAccount(account).getReceive().setAddrIdx(wallet.getFreshNode(KeyPurpose.RECEIVE).getIndex()); - bip84w.getAccount(account).getChange().setAddrIdx(wallet.getFreshNode(KeyPurpose.CHANGE).getIndex()); - - if(!wallet.isMasterWallet() && account != 0) { - Wallet masterWallet = wallet.getMasterWallet(); - bip84w.getAccount(0).getReceive().setAddrIdx(masterWallet.getFreshNode(KeyPurpose.RECEIVE).getIndex()); - bip84w.getAccount(0).getChange().setAddrIdx(masterWallet.getFreshNode(KeyPurpose.CHANGE).getIndex()); - } - } - - public Wallet getWallet() { - return wallet; - } - - public int getAccount() { - return account; - } - - @Override - protected String doFetchAddressReceive(int account, boolean increment, BipFormat bipFormat) throws Exception { - if(account == StandardAccount.WHIRLPOOL_POSTMIX.getAccountNumber()) { - // force change chain - return getAddress(account, increment, KeyPurpose.CHANGE); - } - - return getAddress(account, increment, KeyPurpose.RECEIVE); - } - - @Override - protected String doFetchAddressChange(int account, boolean increment, BipFormat bipFormat) throws Exception { - return getAddress(account, increment, KeyPurpose.CHANGE); - } - - @Override - public List getUtxosWpkhByAccount(int account) { - return utxos; - } - - private String getAddress(int account, boolean increment, KeyPurpose keyPurpose) { - WalletNode addressNode = getWallet(account).getFreshNode(keyPurpose, increment ? lastWalletNodes.get(keyPurpose) : null); - lastWalletNodes.put(keyPurpose, addressNode); - return addressNode.getAddress().getAddress(); - } - - private Wallet getWallet(int account) { - if(account != this.account && account == 0 && !wallet.isMasterWallet()) { - return wallet.getMasterWallet(); - } - - return wallet; - } - public void addUtxo(WalletNode node, BlockTransaction blockTransaction, int index) { - if(node.getWallet().getScriptType() != ScriptType.P2WPKH) { - return; - } - - NetworkParameters params = getBip47Account().getParams(); - UnspentOutput unspentOutput = Whirlpool.getUnspentOutput(node, blockTransaction, index); - MyTransactionOutPoint myTransactionOutPoint = unspentOutput.computeOutpoint(params); - - CahootsUtxo cahootsUtxo; - if(node.getWallet().isBip47()) { - try { - String strPaymentCode = node.getWallet().getKeystores().get(0).getExternalPaymentCode().toString(); - HD_Address hdAddress = getBip47Account().addressAt(node.getIndex()); - PaymentAddress paymentAddress = Bip47UtilJava.getInstance().getPaymentAddress(new PaymentCode(strPaymentCode), 0, hdAddress, params); - cahootsUtxo = new CahootsUtxo(myTransactionOutPoint, node.getDerivationPath(), null, paymentAddress.getReceiveECKey().getPrivKeyBytes()); - } catch(Exception e) { - throw new IllegalStateException("Cannot add BIP47 UTXO", e); - } - } else { - HD_Address hdAddress = bip84w.getAddressAt(account, unspentOutput); - cahootsUtxo = new CahootsUtxo(myTransactionOutPoint, node.getDerivationPath(), null, hdAddress.getECKey().getPrivKeyBytes()); - } - - utxos.add(cahootsUtxo); - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/AddAccountDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/AddAccountDialog.java index 24badd42..ee864a51 100644 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/AddAccountDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/AddAccountDialog.java @@ -5,7 +5,7 @@ import com.googlecode.lanterna.gui2.*; import com.googlecode.lanterna.gui2.dialogs.DialogWindow; import com.sparrowwallet.drongo.wallet.StandardAccount; import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.sparrow.whirlpool.WhirlpoolServices; +import com.sparrowwallet.sparrow.AppServices; import java.util.ArrayList; import java.util.List; @@ -47,7 +47,7 @@ final class AddAccountDialog extends DialogWindow { } } - if(WhirlpoolServices.canWalletMix(masterWallet) && !masterWallet.isWhirlpoolMasterWallet()) { + if(AppServices.isWhirlpoolCompatible(masterWallet) && !masterWallet.isWhirlpoolMasterWallet()) { availableAccounts.add(StandardAccount.WHIRLPOOL_PREMIX); } diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/MixDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/MixDialog.java deleted file mode 100644 index 3e2d4f9b..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/MixDialog.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.sparrowwallet.sparrow.terminal.wallet; - -import com.googlecode.lanterna.TerminalSize; -import com.googlecode.lanterna.gui2.*; -import com.samourai.whirlpool.client.wallet.beans.Tx0FeeTarget; -import com.samourai.whirlpool.client.whirlpool.beans.Pool; -import com.sparrowwallet.drongo.wallet.MixConfig; -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.sparrow.EventManager; -import com.sparrowwallet.sparrow.event.WalletMasterMixConfigChangedEvent; -import com.sparrowwallet.sparrow.terminal.SparrowTerminal; -import com.sparrowwallet.sparrow.wallet.UtxoEntry; -import com.sparrowwallet.sparrow.wallet.WalletForm; -import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowMinerFeeSupplier; - -import java.util.List; -import java.util.Locale; - -public class MixDialog extends WalletDialog { - private static final List FEE_PRIORITIES = List.of(new FeePriority("Low", Tx0FeeTarget.MIN), new FeePriority("Normal", Tx0FeeTarget.BLOCKS_4), new FeePriority("High", Tx0FeeTarget.BLOCKS_2)); - - private final String walletId; - private final List utxoEntries; - - private final TextBox scode; - private final ComboBox premixPriority; - private final Label premixFeeRate; - - private Pool mixPool; - - public MixDialog(String walletId, WalletForm walletForm, List utxoEntries) { - super(walletForm.getWallet().getFullDisplayName() + " Premix Config", walletForm); - - this.walletId = walletId; - this.utxoEntries = utxoEntries; - - setHints(List.of(Hint.CENTERED)); - - Wallet wallet = walletForm.getWallet(); - MixConfig mixConfig = wallet.getMasterMixConfig(); - - Panel mainPanel = new Panel(); - mainPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(5).setVerticalSpacing(1)); - - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - - mainPanel.addComponent(new Label("SCODE")); - scode = new TextBox(new TerminalSize(20, 1)); - mainPanel.addComponent(scode); - - mainPanel.addComponent(new Label("Premix priority")); - premixPriority = new ComboBox<>(); - FEE_PRIORITIES.forEach(premixPriority::addItem); - mainPanel.addComponent(premixPriority); - - mainPanel.addComponent(new Label("Premix fee rate")); - premixFeeRate = new Label(""); - mainPanel.addComponent(premixFeeRate); - - Panel buttonPanel = new Panel(); - buttonPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(1)); - buttonPanel.addComponent(new Button("Cancel", this::onCancel)); - Button next = new Button("Next", this::onNext).setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.CENTER, GridLayout.Alignment.CENTER, true, false)); - buttonPanel.addComponent(next); - - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - - buttonPanel.setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.END, GridLayout.Alignment.CENTER,false,false)).addTo(mainPanel); - setComponent(mainPanel); - - scode.setText(mixConfig.getScode() == null ? "" : mixConfig.getScode()); - - premixPriority.addListener((selectedIndex, previousSelection, changedByUserInteraction) -> { - FeePriority feePriority = premixPriority.getItem(selectedIndex); - premixFeeRate.setText(SparrowMinerFeeSupplier.getFee(Integer.parseInt(feePriority.getTx0FeeTarget().getFeeTarget().getValue())) + " sats/vB"); - }); - premixPriority.setSelectedIndex(1); - - scode.setTextChangeListener((newText, changedByUserInteraction) -> { - if(changedByUserInteraction) { - scode.setText(newText.toUpperCase(Locale.ROOT)); - } - - mixConfig.setScode(newText.toUpperCase(Locale.ROOT)); - EventManager.get().post(new WalletMasterMixConfigChangedEvent(wallet)); - }); - } - - private void onNext() { - MixPoolDialog mixPoolDialog = new MixPoolDialog(walletId, getWalletForm(), utxoEntries, premixPriority.getSelectedItem().getTx0FeeTarget()); - mixPool = mixPoolDialog.showDialog(SparrowTerminal.get().getGui()); - close(); - } - - private void onCancel() { - close(); - } - - @Override - public Pool showDialog(WindowBasedTextGUI textGUI) { - super.showDialog(textGUI); - return mixPool; - } - - private static class FeePriority { - private final String name; - private final Tx0FeeTarget tx0FeeTarget; - - public FeePriority(String name, Tx0FeeTarget tx0FeeTarget) { - this.name = name; - this.tx0FeeTarget = tx0FeeTarget; - } - - public Tx0FeeTarget getTx0FeeTarget() { - return tx0FeeTarget; - } - - @Override - public String toString() { - return name; - } - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/MixPoolDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/MixPoolDialog.java deleted file mode 100644 index 6142fafe..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/MixPoolDialog.java +++ /dev/null @@ -1,241 +0,0 @@ -package com.sparrowwallet.sparrow.terminal.wallet; - -import com.googlecode.lanterna.TerminalSize; -import com.googlecode.lanterna.gui2.*; -import com.samourai.whirlpool.client.tx0.Tx0Preview; -import com.samourai.whirlpool.client.tx0.Tx0Previews; -import com.samourai.whirlpool.client.wallet.beans.Tx0FeeTarget; -import com.samourai.whirlpool.client.whirlpool.beans.Pool; -import com.sparrowwallet.drongo.BitcoinUnit; -import com.sparrowwallet.drongo.wallet.MixConfig; -import com.sparrowwallet.sparrow.AppServices; -import com.sparrowwallet.sparrow.EventManager; -import com.sparrowwallet.sparrow.UnitFormat; -import com.sparrowwallet.sparrow.event.WalletMasterMixConfigChangedEvent; -import com.sparrowwallet.sparrow.io.Config; -import com.sparrowwallet.sparrow.terminal.SparrowTerminal; -import com.sparrowwallet.sparrow.wallet.Entry; -import com.sparrowwallet.sparrow.wallet.UtxoEntry; -import com.sparrowwallet.sparrow.wallet.WalletForm; -import com.sparrowwallet.sparrow.whirlpool.Whirlpool; -import javafx.application.Platform; -import javafx.beans.property.ObjectProperty; -import javafx.beans.property.SimpleObjectProperty; -import javafx.scene.control.ButtonBar; -import javafx.scene.control.ButtonType; - -import java.util.List; -import java.util.Optional; -import java.util.OptionalLong; - -public class MixPoolDialog extends WalletDialog { - private static final DisplayPool NULL_POOL = new DisplayPool(null); - - private final String walletId; - private final List utxoEntries; - private final Tx0FeeTarget tx0FeeTarget; - - private final ComboBox pool; - private final Label poolFeeLabel; - private final Label poolFee; - private final Label premixOutputs; - private final Button broadcast; - - private Tx0Previews tx0Previews; - private final ObjectProperty tx0PreviewProperty = new SimpleObjectProperty<>(null); - private Pool mixPool; - - public MixPoolDialog(String walletId, WalletForm walletForm, List utxoEntries, Tx0FeeTarget tx0FeeTarget) { - super(walletForm.getWallet().getFullDisplayName() + " Premix Pool", walletForm); - - this.walletId = walletId; - this.utxoEntries = utxoEntries; - this.tx0FeeTarget = tx0FeeTarget; - - setHints(List.of(Hint.CENTERED)); - - Panel mainPanel = new Panel(); - mainPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(5).setVerticalSpacing(1)); - - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - - mainPanel.addComponent(new Label("Pool")); - pool = new ComboBox<>(); - pool.addItem(NULL_POOL); - pool.setEnabled(false); - mainPanel.addComponent(pool); - - poolFeeLabel = new Label("Pool fee"); - poolFeeLabel.setPreferredSize(new TerminalSize(21, 1)); - mainPanel.addComponent(poolFeeLabel); - poolFee = new Label(""); - mainPanel.addComponent(poolFee); - - mainPanel.addComponent(new Label("Premix outputs")); - premixOutputs = new Label(""); - mainPanel.addComponent(premixOutputs); - - Panel buttonPanel = new Panel(); - buttonPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(1)); - buttonPanel.addComponent(new Button("Cancel", this::onCancel)); - broadcast = new Button("Broadcast", this::onBroadcast).setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.CENTER, GridLayout.Alignment.CENTER, true, false)); - buttonPanel.addComponent(broadcast); - broadcast.setEnabled(false); - - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - - buttonPanel.setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.END, GridLayout.Alignment.CENTER,false,false)).addTo(mainPanel); - setComponent(mainPanel); - - pool.addListener((selectedIndex, previousSelection, changedByUserInteraction) -> { - DisplayPool selectedPool = pool.getSelectedItem(); - if(selectedPool != NULL_POOL) { - UnitFormat format = Config.get().getUnitFormat() == null ? UnitFormat.DOT : Config.get().getUnitFormat(); - poolFee.setText(format.formatSatsValue(selectedPool.pool.getFeeValue()) + " sats"); - fetchTx0Preview(selectedPool.pool); - } - }); - - tx0PreviewProperty.addListener((observable, oldValue, tx0Preview) -> { - SparrowTerminal.get().getGuiThread().invokeLater(() -> { - if(tx0Preview == null) { - premixOutputs.setText("Calculating..."); - broadcast.setEnabled(false); - } else { - if(tx0Preview.getPool().getFeeValue() != tx0Preview.getTx0Data().getFeeValue()) { - poolFeeLabel.setText("Pool fee (discounted)"); - } else { - poolFeeLabel.setText("Pool fee"); - } - - UnitFormat format = Config.get().getUnitFormat() == null ? UnitFormat.DOT : Config.get().getUnitFormat(); - poolFee.setText(format.formatSatsValue(tx0Preview.getTx0Data().getFeeValue()) + " sats"); - premixOutputs.setText(tx0Preview.getNbPremix() + " UTXOs"); - broadcast.setEnabled(true); - } - }); - }); - - Platform.runLater(this::fetchPools); - } - - private void onBroadcast() { - mixPool = tx0PreviewProperty.get() == null ? null : tx0PreviewProperty.get().getPool(); - close(); - } - - private void onCancel() { - close(); - } - - @Override - public Pool showDialog(WindowBasedTextGUI textGUI) { - super.showDialog(textGUI); - return mixPool; - } - - private void fetchPools() { - long totalUtxoValue = utxoEntries.stream().mapToLong(Entry::getValue).sum(); - Whirlpool.PoolsService poolsService = new Whirlpool.PoolsService(AppServices.getWhirlpoolServices().getWhirlpool(walletId), totalUtxoValue); - poolsService.setOnSucceeded(workerStateEvent -> { - List availablePools = poolsService.getValue().stream().toList(); - if(availablePools.isEmpty()) { - SparrowTerminal.get().getGuiThread().invokeLater(() -> pool.setEnabled(false)); - - Whirlpool.PoolsService allPoolsService = new Whirlpool.PoolsService(AppServices.getWhirlpoolServices().getWhirlpool(walletId), null); - allPoolsService.setOnSucceeded(poolsStateEvent -> { - OptionalLong optMinValue = allPoolsService.getValue().stream().mapToLong(pool1 -> pool1.getPremixValueMin() + pool1.getFeeValue()).min(); - if(optMinValue.isPresent() && totalUtxoValue < optMinValue.getAsLong()) { - UnitFormat format = Config.get().getUnitFormat() == null ? UnitFormat.DOT : Config.get().getUnitFormat(); - String satsValue = format.formatSatsValue(optMinValue.getAsLong()) + " sats"; - String btcValue = format.formatBtcValue(optMinValue.getAsLong()) + " BTC"; - AppServices.showErrorDialog("Insufficient UTXO Value", "No available pools. Select a value over " + (Config.get().getBitcoinUnit() == BitcoinUnit.BTC ? btcValue : satsValue) + "."); - SparrowTerminal.get().getGuiThread().invokeLater(this::close); - } - }); - allPoolsService.start(); - } else { - SparrowTerminal.get().getGuiThread().invokeLater(() -> { - pool.setEnabled(true); - pool.clearItems(); - availablePools.stream().map(DisplayPool::new).forEach(pool::addItem); - pool.setSelectedIndex(0); - }); - } - }); - poolsService.setOnFailed(workerStateEvent -> { - Throwable exception = workerStateEvent.getSource().getException(); - while(exception.getCause() != null) { - exception = exception.getCause(); - } - - Optional optButton = AppServices.showErrorDialog("Error fetching pools", exception.getMessage(), ButtonType.CANCEL, new ButtonType("Retry", ButtonBar.ButtonData.APPLY)); - if(optButton.isPresent()) { - if(optButton.get().getButtonData().equals(ButtonBar.ButtonData.APPLY)) { - fetchPools(); - } else { - SparrowTerminal.get().getGuiThread().invokeLater(() -> pool.setEnabled(false)); - } - } - }); - poolsService.start(); - } - - private void fetchTx0Preview(Pool pool) { - MixConfig mixConfig = getWalletForm().getWallet().getMasterMixConfig(); - if(mixConfig.getScode() == null) { - mixConfig.setScode(""); - EventManager.get().post(new WalletMasterMixConfigChangedEvent(getWalletForm().getWallet())); - } - - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(walletId); - if(tx0Previews != null && mixConfig.getScode().equals(whirlpool.getScode()) && tx0FeeTarget == whirlpool.getTx0FeeTarget()) { - Tx0Preview tx0Preview = tx0Previews.getTx0Preview(pool.getPoolId()); - tx0PreviewProperty.set(tx0Preview); - } else { - tx0Previews = null; - whirlpool.setScode(mixConfig.getScode()); - whirlpool.setTx0FeeTarget(tx0FeeTarget); - whirlpool.setMixFeeTarget(tx0FeeTarget); - - Whirlpool.Tx0PreviewsService tx0PreviewsService = new Whirlpool.Tx0PreviewsService(whirlpool, utxoEntries); - tx0PreviewsService.setOnRunning(workerStateEvent -> { - premixOutputs.setText("Calculating..."); - tx0PreviewProperty.set(null); - }); - tx0PreviewsService.setOnSucceeded(workerStateEvent -> { - tx0Previews = tx0PreviewsService.getValue(); - Tx0Preview tx0Preview = tx0Previews.getTx0Preview(this.pool.getSelectedItem() == null ? pool.getPoolId() : this.pool.getSelectedItem().pool.getPoolId()); - tx0PreviewProperty.set(tx0Preview); - }); - tx0PreviewsService.setOnFailed(workerStateEvent -> { - Throwable exception = workerStateEvent.getSource().getException(); - while(exception.getCause() != null) { - exception = exception.getCause(); - } - - AppServices.showErrorDialog("Error fetching Tx0","Error fetching Tx0: " + exception.getMessage()); - }); - tx0PreviewsService.start(); - } - } - - private static final class DisplayPool { - private final Pool pool; - - public DisplayPool(Pool pool) { - this.pool = pool; - } - - @Override - public String toString() { - if(pool == null) { - return "Fetching pools..."; - } - - UnitFormat format = Config.get().getUnitFormat() == null ? UnitFormat.DOT : Config.get().getUnitFormat(); - return format.formatSatsValue(pool.getDenomination()) + " sats"; - } - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/MixToDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/MixToDialog.java deleted file mode 100644 index 142dc5ec..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/MixToDialog.java +++ /dev/null @@ -1,222 +0,0 @@ -package com.sparrowwallet.sparrow.terminal.wallet; - -import com.googlecode.lanterna.TerminalSize; -import com.googlecode.lanterna.gui2.*; -import com.samourai.whirlpool.client.wallet.beans.IndexRange; -import com.sparrowwallet.drongo.protocol.ScriptType; -import com.sparrowwallet.drongo.wallet.MixConfig; -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.sparrow.AppServices; -import com.sparrowwallet.sparrow.wallet.WalletForm; -import com.sparrowwallet.sparrow.whirlpool.Whirlpool; - -import java.util.*; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -import static com.sparrowwallet.drongo.wallet.StandardAccount.WHIRLPOOL_BADBANK; -import static com.sparrowwallet.drongo.wallet.StandardAccount.WHIRLPOOL_PREMIX; - -public class MixToDialog extends WalletDialog { - private static final DisplayWallet NONE_DISPLAY_WALLET = new DisplayWallet(null); - - private MixConfig mixConfig; - - private final ComboBox mixToWallet; - private final TextBox minimumMixes; - private final ComboBox indexRange; - private final Button apply; - - public MixToDialog(WalletForm walletForm) { - super(walletForm.getWallet().getFullDisplayName() + " Mix To", walletForm); - - setHints(List.of(Hint.CENTERED)); - - Wallet wallet = getWalletForm().getWallet(); - this.mixConfig = wallet.getMasterMixConfig().copy(); - - Panel mainPanel = new Panel(); - mainPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(5).setVerticalSpacing(1)); - - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - - mainPanel.addComponent(new Label("Mix to wallet")); - mixToWallet = new ComboBox<>(); - mainPanel.addComponent(mixToWallet); - - mainPanel.addComponent(new Label("Minimum mixes")); - minimumMixes = new TextBox().setValidationPattern(Pattern.compile("[0-9]*")); - mainPanel.addComponent(minimumMixes); - - mainPanel.addComponent(new Label("Postmix index range")); - indexRange = new ComboBox<>(); - mainPanel.addComponent(indexRange); - - Panel buttonPanel = new Panel(); - buttonPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(1)); - buttonPanel.addComponent(new Button("Cancel", this::onCancel)); - apply = new Button("Apply", this::onApply).setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.CENTER, GridLayout.Alignment.CENTER, true, false)); - apply.setEnabled(false); - buttonPanel.addComponent(apply); - - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - - buttonPanel.setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.END, GridLayout.Alignment.CENTER,false,false)).addTo(mainPanel); - setComponent(mainPanel); - - List allWallets = new ArrayList<>(); - allWallets.add(NONE_DISPLAY_WALLET); - List destinationWallets = AppServices.get().getOpenWallets().keySet().stream().filter(openWallet -> openWallet.isValid() - && (openWallet.getScriptType() == ScriptType.P2WPKH || openWallet.getScriptType() == ScriptType.P2WSH) - && openWallet != wallet && openWallet != wallet.getMasterWallet() - && (openWallet.getStandardAccountType() == null || !List.of(WHIRLPOOL_PREMIX, WHIRLPOOL_BADBANK).contains(openWallet.getStandardAccountType()))).collect(Collectors.toList()); - allWallets.addAll(destinationWallets.stream().map(DisplayWallet::new).collect(Collectors.toList())); - allWallets.forEach(mixToWallet::addItem); - - String mixToWalletId = null; - try { - mixToWalletId = AppServices.getWhirlpoolServices().getWhirlpoolMixToWalletId(mixConfig); - } catch(NoSuchElementException e) { - //ignore, mix to wallet is not open - } - - if(mixToWalletId != null) { - mixToWallet.setSelectedItem(new DisplayWallet(AppServices.get().getWallet(mixToWalletId))); - } else { - mixToWallet.setSelectedItem(NONE_DISPLAY_WALLET); - } - - int initialMinMixes = mixConfig.getMinMixes() == null ? Whirlpool.DEFAULT_MIXTO_MIN_MIXES : mixConfig.getMinMixes(); - minimumMixes.setText(Integer.toString(initialMinMixes)); - - List indexRanges = Arrays.stream(IndexRange.values()).map(DisplayIndexRange::new).collect(Collectors.toList()); - indexRanges.forEach(indexRange::addItem); - - indexRange.setSelectedItem(new DisplayIndexRange(IndexRange.FULL)); - if(mixConfig.getIndexRange() != null) { - try { - indexRange.setSelectedItem(new DisplayIndexRange(IndexRange.valueOf(mixConfig.getIndexRange()))); - } catch(Exception e) { - //ignore - } - } - - mixToWallet.addListener((selectedIndex, previousSelection, changedByUserInteraction) -> { - DisplayWallet newValue = mixToWallet.getSelectedItem(); - if(newValue == NONE_DISPLAY_WALLET) { - mixConfig.setMixToWalletName(null); - mixConfig.setMixToWalletFile(null); - } else { - mixConfig.setMixToWalletName(newValue.getWallet().getName()); - mixConfig.setMixToWalletFile(AppServices.get().getOpenWallets().get(newValue.getWallet()).getWalletFile()); - } - apply.setEnabled(apply.isEnabled() || selectedIndex != previousSelection); - }); - - minimumMixes.setTextChangeListener((newText, changedByUserInteraction) -> { - try { - int newValue = Integer.parseInt(newText); - if(newValue < 2 || newValue > 10000) { - return; - } - - mixConfig.setMinMixes(newValue); - apply.setEnabled(true); - } catch(NumberFormatException e) { - return; - } - }); - - indexRange.addListener((selectedIndex, previousSelection, changedByUserInteraction) -> { - DisplayIndexRange newValue = indexRange.getSelectedItem(); - mixConfig.setIndexRange(newValue.getIndexRange().toString()); - apply.setEnabled(apply.isEnabled() || selectedIndex != previousSelection); - }); - } - - private void onCancel() { - mixConfig = null; - close(); - } - - private void onApply() { - close(); - } - - @Override - public Object showDialog(WindowBasedTextGUI textGUI) { - super.showDialog(textGUI); - return mixConfig; - } - - private static class DisplayWallet { - private final Wallet wallet; - - public DisplayWallet(Wallet wallet) { - this.wallet = wallet; - } - - public Wallet getWallet() { - return wallet; - } - - @Override - public String toString() { - return wallet == null ? "None" : wallet.getFullDisplayName(); - } - - @Override - public boolean equals(Object o) { - if(this == o) { - return true; - } - if(o == null || getClass() != o.getClass()) { - return false; - } - - DisplayWallet that = (DisplayWallet) o; - return Objects.equals(wallet, that.wallet); - } - - @Override - public int hashCode() { - return wallet != null ? wallet.hashCode() : 0; - } - } - - private static class DisplayIndexRange { - private final IndexRange indexRange; - - public DisplayIndexRange(IndexRange indexRange) { - this.indexRange = indexRange; - } - - public IndexRange getIndexRange() { - return indexRange; - } - - @Override - public String toString() { - return indexRange.toString().charAt(0) + indexRange.toString().substring(1).toLowerCase(Locale.ROOT); - } - - @Override - public boolean equals(Object o) { - if(this == o) { - return true; - } - if(o == null || getClass() != o.getClass()) { - return false; - } - - DisplayIndexRange that = (DisplayIndexRange) o; - return indexRange == that.indexRange; - } - - @Override - public int hashCode() { - return indexRange.hashCode(); - } - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/UtxosDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/UtxosDialog.java index c6c4ac36..e4cdd245 100644 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/UtxosDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/UtxosDialog.java @@ -5,28 +5,18 @@ import com.googlecode.lanterna.TerminalSize; import com.googlecode.lanterna.gui2.*; import com.googlecode.lanterna.gui2.table.Table; import com.googlecode.lanterna.gui2.table.TableModel; -import com.samourai.whirlpool.client.wallet.beans.MixProgress; -import com.samourai.whirlpool.client.whirlpool.beans.Pool; -import com.sparrowwallet.drongo.protocol.Sha256Hash; import com.sparrowwallet.drongo.wallet.*; import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.event.*; import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.net.ExchangeSource; -import com.sparrowwallet.sparrow.terminal.ModalDialog; import com.sparrowwallet.sparrow.terminal.SparrowTerminal; import com.sparrowwallet.sparrow.terminal.wallet.table.*; import com.sparrowwallet.sparrow.wallet.*; -import com.sparrowwallet.sparrow.whirlpool.Whirlpool; -import com.sparrowwallet.sparrow.whirlpool.WhirlpoolServices; -import javafx.application.Platform; -import javafx.beans.value.ChangeListener; -import javafx.beans.value.WeakChangeListener; import java.util.ArrayList; import java.util.List; -import java.util.NoSuchElementException; import java.util.stream.Collectors; public class UtxosDialog extends WalletDialog { @@ -37,59 +27,6 @@ public class UtxosDialog extends WalletDialog { private final Label utxoCount; private final Table utxos; - private Button startMix; - private Button mixTo; - private Button mixSelected; - - private final ChangeListener mixingOnlineListener = (observable, oldValue, newValue) -> { - SparrowTerminal.get().getGuiThread().invokeLater(() -> startMix.setEnabled(newValue)); - }; - - private final ChangeListener mixingStartingListener = (observable, oldValue, newValue) -> { - try { - SparrowTerminal.get().getGuiThread().invokeAndWait(() -> { - startMix.setEnabled(!newValue && AppServices.onlineProperty().get()); - startMix.setLabel(newValue && AppServices.onlineProperty().get() ? "Starting Mixing..." : isMixing() ? "Stop Mixing" : "Start Mixing"); - mixTo.setEnabled(!newValue); - }); - } catch(InterruptedException e) { - //ignore - } - }; - - private final ChangeListener mixingStoppingListener = (observable, oldValue, newValue) -> { - try { - SparrowTerminal.get().getGuiThread().invokeAndWait(() -> { - startMix.setEnabled(!newValue && AppServices.onlineProperty().get()); - startMix.setLabel(newValue ? "Stopping Mixing..." : isMixing() ? "Stop Mixing" : "Start Mixing"); - mixTo.setEnabled(!newValue); - }); - } catch(InterruptedException e) { - //ignore - } - }; - - private final ChangeListener mixingListener = (observable, oldValue, newValue) -> { - if(!newValue) { - WalletUtxosEntry walletUtxosEntry = getWalletForm().getWalletUtxosEntry(); - for(Entry entry : walletUtxosEntry.getChildren()) { - UtxoEntry utxoEntry = (UtxoEntry)entry; - if(utxoEntry.getMixStatus() != null && utxoEntry.getMixStatus().getMixProgress() != null - && utxoEntry.getMixStatus().getMixProgress().getMixStep() != null - && utxoEntry.getMixStatus().getMixProgress().getMixStep().isInterruptable()) { - whirlpoolMix(new WhirlpoolMixEvent(getWalletForm().getWallet(), utxoEntry.getHashIndex(), (MixProgress)null)); - } - } - } - - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWalletForm().getWallet()); - if(newValue) { - startMix.setLabel("Stop Mixing"); - } else if(whirlpool == null || !whirlpool.stoppingProperty().get()) { - startMix.setLabel("Start Mixing"); - } - }; - public UtxosDialog(WalletForm walletForm) { super(walletForm.getWallet().getFullDisplayName() + " UTXOs", walletForm); @@ -116,7 +53,6 @@ public class UtxosDialog extends WalletDialog { if(utxos.getTableModel().getRowCount() > utxos.getSelectedRow()) { TableCell dateCell = utxos.getTableModel().getRow(utxos.getSelectedRow()).get(0); dateCell.setSelected(!dateCell.isSelected()); - updateMixSelectedButton(); } }); utxos.setInputFilter((interactable, keyStroke) -> { @@ -138,58 +74,12 @@ public class UtxosDialog extends WalletDialog { updateHistory(getWalletForm().getWalletUtxosEntry()); Panel buttonPanel = new Panel(new GridLayout(5).setHorizontalSpacing(2).setVerticalSpacing(0)); - if(getWalletForm().getWallet().isWhirlpoolMixWallet()) { - startMix = new Button("Start Mixing", this::toggleMixing).setSize(new TerminalSize(20, 1)).addTo(buttonPanel); - startMix.setEnabled(AppServices.onlineProperty().get()); - mixTo = new Button("Mix to...", this::showMixToDialog); - if(getWalletForm().getWallet().getStandardAccountType() == StandardAccount.WHIRLPOOL_POSTMIX) { - buttonPanel.addComponent(mixTo); - } else { - buttonPanel.addComponent(new EmptySpace(new TerminalSize(15, 1))); - } - - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWalletForm().getWallet()); - if(whirlpool != null) { - startMix.setLabel(whirlpool.isMixing() ? "Stop Mixing" : "Start Mixing"); - if(whirlpool.startingProperty().getValue()) { - mixingStartingListener.changed(whirlpool.startingProperty(), null, whirlpool.startingProperty().getValue()); - } - whirlpool.startingProperty().addListener(new WeakChangeListener<>(mixingStartingListener)); - if(whirlpool.stoppingProperty().getValue()) { - mixingStoppingListener.changed(whirlpool.stoppingProperty(), null, whirlpool.stoppingProperty().getValue()); - } - whirlpool.stoppingProperty().addListener(new WeakChangeListener<>(mixingStoppingListener)); - whirlpool.mixingProperty().addListener(new WeakChangeListener<>(mixingListener)); - updateMixToButton(); - } - - AppServices.onlineProperty().addListener(new WeakChangeListener<>(mixingOnlineListener)); - - if(WhirlpoolServices.canWalletMix(getWalletForm().getWallet())) { - mixSelected = new Button("Mix Selected", this::mixSelected); - mixSelected.setEnabled(false); - buttonPanel.addComponent(mixSelected); - } else { - buttonPanel.addComponent(new EmptySpace(new TerminalSize(15, 1))); - } - - buttonPanel.addComponent(new Button("Back", () -> onBack(Function.UTXOS))); - buttonPanel.addComponent(new Button("Refresh", this::onRefresh)); - } else { - if(WhirlpoolServices.canWalletMix(getWalletForm().getWallet())) { - mixSelected = new Button("Mix Selected", this::mixSelected); - mixSelected.setEnabled(false); - buttonPanel.addComponent(mixSelected); - } else { - buttonPanel.addComponent(new EmptySpace(new TerminalSize(15, 1))); - } - - buttonPanel.addComponent(new EmptySpace(new TerminalSize(15, 1))); - buttonPanel.addComponent(new EmptySpace(new TerminalSize(15, 1))); - buttonPanel.addComponent(new Button("Back", () -> onBack(Function.UTXOS))); - buttonPanel.addComponent(new Button("Refresh", this::onRefresh)); - } + buttonPanel.addComponent(new EmptySpace(new TerminalSize(15, 1))); + buttonPanel.addComponent(new EmptySpace(new TerminalSize(15, 1))); + buttonPanel.addComponent(new EmptySpace(new TerminalSize(15, 1))); + buttonPanel.addComponent(new Button("Back", () -> onBack(Function.UTXOS))); + buttonPanel.addComponent(new Button("Refresh", this::onRefresh)); Panel mainPanel = new Panel(); mainPanel.setLayoutManager(new LinearLayout(Direction.VERTICAL).setSpacing(1)); @@ -257,161 +147,10 @@ public class UtxosDialog extends WalletDialog { utxoCount.setText(walletUtxosEntry.getChildren() != null ? Integer.toString(walletUtxosEntry.getChildren().size()) : "0"); } - private boolean isMixing() { - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWalletForm().getWallet()); - return whirlpool != null && whirlpool.isMixing(); - } - - public void toggleMixing() { - if(isMixing()) { - stopMixing(); - } else { - startMixing(); - } - } - - public void startMixing() { - startMix.setEnabled(false); - - Platform.runLater(() -> { - getWalletForm().getWallet().getMasterMixConfig().setMixOnStartup(Boolean.TRUE); - EventManager.get().post(new WalletMasterMixConfigChangedEvent(getWalletForm().getWallet())); - - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWalletForm().getWallet()); - if(whirlpool != null && !whirlpool.isStarted() && AppServices.isConnected()) { - AppServices.getWhirlpoolServices().startWhirlpool(getWalletForm().getWallet(), whirlpool, true); - } - }); - } - - public void stopMixing() { - startMix.setEnabled(AppServices.onlineProperty().get()); - - Platform.runLater(() -> { - getWalletForm().getWallet().getMasterMixConfig().setMixOnStartup(Boolean.FALSE); - EventManager.get().post(new WalletMasterMixConfigChangedEvent(getWalletForm().getWallet())); - - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWalletForm().getWallet()); - if(whirlpool.isStarted()) { - AppServices.getWhirlpoolServices().stopWhirlpool(whirlpool, true); - } else { - //Ensure http clients are shutdown - whirlpool.shutdown(); - } - }); - } - - public void showMixToDialog() { - MixToDialog mixToDialog = new MixToDialog(getWalletForm()); - MixConfig changedMixConfig = (MixConfig)mixToDialog.showDialog(SparrowTerminal.get().getGui()); - - if(changedMixConfig != null) { - MixConfig mixConfig = getWalletForm().getWallet().getMasterMixConfig(); - - mixConfig.setMixToWalletName(changedMixConfig.getMixToWalletName()); - mixConfig.setMixToWalletFile(changedMixConfig.getMixToWalletFile()); - mixConfig.setMinMixes(changedMixConfig.getMinMixes()); - mixConfig.setIndexRange(changedMixConfig.getIndexRange()); - - Platform.runLater(() -> { - EventManager.get().post(new WalletMasterMixConfigChangedEvent(getWalletForm().getWallet())); - - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWalletForm().getWallet()); - whirlpool.setPostmixIndexRange(mixConfig.getIndexRange()); - try { - String mixToWalletId = AppServices.getWhirlpoolServices().getWhirlpoolMixToWalletId(mixConfig); - whirlpool.setMixToWallet(mixToWalletId, mixConfig.getMinMixes()); - } catch(NoSuchElementException e) { - mixConfig.setMixToWalletName(null); - mixConfig.setMixToWalletFile(null); - EventManager.get().post(new WalletMasterMixConfigChangedEvent(getWalletForm().getWallet())); - whirlpool.setMixToWallet(null, null); - } - - SparrowTerminal.get().getGuiThread().invokeLater(this::updateMixToButton); - if(whirlpool.isStarted()) { - //Will automatically restart - AppServices.getWhirlpoolServices().stopWhirlpool(whirlpool, false); - } - }); - } - } - - private void updateMixToButton() { - if(mixTo == null) { - return; - } - - MixConfig mixConfig = getWalletForm().getWallet().getMasterMixConfig(); - if(mixConfig != null && mixConfig.getMixToWalletName() != null) { - mixTo.setLabel("Mixing to " + mixConfig.getMixToWalletName()); - try { - String mixToWalletId = AppServices.getWhirlpoolServices().getWhirlpoolMixToWalletId(mixConfig); - String mixToName = AppServices.get().getWallet(mixToWalletId).getFullDisplayName(); - mixTo.setLabel("Mixing to " + mixToName); - } catch(NoSuchElementException e) { - mixTo.setLabel("! Not Open"); - } - } else { - mixTo.setLabel("Mix to..."); - } - } - - private void updateMixSelectedButton() { - if(mixSelected == null) { - return; - } - - mixSelected.setEnabled(!getSelectedEntries().isEmpty()); - } - private List getSelectedEntries() { return utxos.getTableModel().getRows().stream().map(row -> row.get(0)).filter(TableCell::isSelected).map(dateCell -> (UtxoEntry)dateCell.getEntry()).collect(Collectors.toList()); } - private void mixSelected() { - MixDialog mixDialog = new MixDialog(getWalletForm().getMasterWalletId(), getWalletForm(), getSelectedEntries()); - Pool pool = mixDialog.showDialog(SparrowTerminal.get().getGui()); - - if(pool != null) { - Wallet wallet = getWalletForm().getWallet(); - if(wallet.isMasterWallet() && !wallet.isWhirlpoolMasterWallet()) { - addAccount(wallet, StandardAccount.WHIRLPOOL_PREMIX, () -> broadcastPremix(pool)); - } else { - Platform.runLater(() -> broadcastPremix(pool)); - } - } - } - - public void broadcastPremix(Pool pool) { - ModalDialog broadcastingDialog = new ModalDialog(getWalletForm().getWallet().getFullDisplayName(), "Broadcasting premix..."); - SparrowTerminal.get().getGuiThread().invokeLater(() -> SparrowTerminal.get().getGui().addWindow(broadcastingDialog)); - - //The WhirlpoolWallet has already been configured - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWalletForm().getStorage().getWalletId(getWalletForm().getMasterWallet())); - List utxos = getSelectedEntries().stream().map(HashIndexEntry::getHashIndex).collect(Collectors.toList()); - Whirlpool.Tx0BroadcastService tx0BroadcastService = new Whirlpool.Tx0BroadcastService(whirlpool, pool, utxos); - tx0BroadcastService.setOnSucceeded(workerStateEvent -> { - Sha256Hash txid = tx0BroadcastService.getValue(); - SparrowTerminal.get().getGuiThread().invokeLater(() -> { - SparrowTerminal.get().getGui().removeWindow(broadcastingDialog); - AppServices.showSuccessDialog("Broadcast Successful", "Premix transaction id:\n" + txid.toString()); - }); - }); - tx0BroadcastService.setOnFailed(workerStateEvent -> { - Throwable exception = workerStateEvent.getSource().getException(); - while(exception.getCause() != null) { - exception = exception.getCause(); - } - String message = exception.getMessage(); - SparrowTerminal.get().getGuiThread().invokeLater(() -> { - SparrowTerminal.get().getGui().removeWindow(broadcastingDialog); - AppServices.showErrorDialog("Error broadcasting premix transaction", message); - }); - }); - tx0BroadcastService.start(); - } - @Subscribe public void walletNodesChanged(WalletNodesChangedEvent event) { if(event.getWallet().equals(getWalletForm().getWallet())) { @@ -431,60 +170,6 @@ public class UtxosDialog extends WalletDialog { } } - @Subscribe - public void whirlpoolMix(WhirlpoolMixEvent event) { - if(event.getWallet().equals(getWalletForm().getWallet())) { - WalletUtxosEntry walletUtxosEntry = getWalletForm().getWalletUtxosEntry(); - for(Entry entry : walletUtxosEntry.getChildren()) { - UtxoEntry utxoEntry = (UtxoEntry)entry; - if(utxoEntry.getHashIndex().equals(event.getUtxo())) { - if(event.getNextUtxo() != null) { - utxoEntry.setNextMixUtxo(event.getNextUtxo()); - } else if(event.getMixFailReason() != null) { - utxoEntry.setMixFailReason(event.getMixFailReason(), event.getMixError()); - } else { - utxoEntry.setMixProgress(event.getMixProgress()); - } - - TableModel tableModel = utxos.getTableModel(); - for(int row = 0; row < tableModel.getRowCount(); row++) { - UtxoEntry tableEntry = (UtxoEntry)tableModel.getRow(row).get(0).getEntry(); - if(tableEntry.getHashIndex().equals(event.getUtxo())) { - final int utxoRow = row; - SparrowTerminal.get().getGuiThread().invokeLater(() -> { - tableModel.setCell(2, utxoRow, new MixTableCell(utxoEntry)); - }); - } - } - } - } - } - } - - @Subscribe - public void newBlock(NewBlockEvent event) { - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWalletForm().getWallet()); - if(whirlpool != null) { - for(Entry entry : getWalletForm().getWalletUtxosEntry().getChildren()) { - UtxoEntry utxoEntry = (UtxoEntry)entry; - MixProgress mixProgress = whirlpool.getMixProgress(utxoEntry.getHashIndex()); - if(mixProgress != null || utxoEntry.getMixStatus() == null || (utxoEntry.getMixStatus().getMixFailReason() == null && utxoEntry.getMixStatus().getNextMixUtxo() == null)) { - whirlpoolMix(new WhirlpoolMixEvent(getWalletForm().getWallet(), utxoEntry.getHashIndex(), mixProgress)); - } - } - } - } - - @Subscribe - public void openWallets(OpenWalletsEvent event) { - SparrowTerminal.get().getGuiThread().invokeLater(this::updateMixToButton); - } - - @Subscribe - public void walletLabelChanged(WalletLabelChangedEvent event) { - SparrowTerminal.get().getGuiThread().invokeLater(this::updateMixToButton); - } - @Subscribe public void includeMempoolOutputsChangedEvent(IncludeMempoolOutputsChangedEvent event) { updateHistory(getWalletForm().getWalletUtxosEntry()); diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/WalletDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/WalletDialog.java index d3fe2a7b..517a4665 100644 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/WalletDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/WalletDialog.java @@ -22,7 +22,6 @@ import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.terminal.SparrowTerminal; import com.sparrowwallet.sparrow.wallet.Function; import com.sparrowwallet.sparrow.wallet.WalletForm; -import com.sparrowwallet.sparrow.whirlpool.WhirlpoolServices; import javafx.application.Platform; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -125,7 +124,7 @@ public class WalletDialog extends DialogWindow { private void addAndSaveAccount(Wallet masterWallet, StandardAccount standardAccount, Key key) { List childWallets; if(StandardAccount.isWhirlpoolAccount(standardAccount)) { - childWallets = WhirlpoolServices.prepareWhirlpoolWallet(masterWallet, getWalletForm().getWalletId(), getWalletForm().getStorage()); + childWallets = AppServices.addWhirlpoolWallets(masterWallet, getWalletForm().getWalletId(), getWalletForm().getStorage()); } else { Wallet childWallet = masterWallet.addChildWallet(standardAccount); EventManager.get().post(new ChildWalletsAddedEvent(getWalletForm().getStorage(), masterWallet, childWallet)); diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/table/MixTableCell.java b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/table/MixTableCell.java index 4bcc84e7..c9b1ad59 100644 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/table/MixTableCell.java +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/table/MixTableCell.java @@ -1,14 +1,10 @@ package com.sparrowwallet.sparrow.terminal.wallet.table; import com.google.common.base.Strings; -import com.googlecode.lanterna.Symbols; -import com.samourai.whirlpool.client.mix.listener.MixFailReason; -import com.samourai.whirlpool.client.wallet.beans.MixProgress; import com.sparrowwallet.sparrow.wallet.Entry; import com.sparrowwallet.sparrow.wallet.UtxoEntry; public class MixTableCell extends TableCell { - private static final int ERROR_DISPLAY_MILLIS = 5 * 60 * 1000; public static final int WIDTH = 18; public MixTableCell(Entry entry) { @@ -18,48 +14,12 @@ public class MixTableCell extends TableCell { @Override public String formatCell() { if(entry instanceof UtxoEntry utxoEntry) { - if(utxoEntry.getMixStatus() != null) { - UtxoEntry.MixStatus mixStatus = utxoEntry.getMixStatus(); - if(mixStatus.getNextMixUtxo() != null) { - return getMixSuccess(mixStatus); - } else if(mixStatus.getMixFailReason() != null) { - return getMixFail(mixStatus); - } else if(mixStatus.getMixProgress() != null) { - return getMixProgress(mixStatus, mixStatus.getMixProgress()); - } - } - - return getMixCountOnly(utxoEntry.getMixStatus()); + return getMixCountOnly(utxoEntry.mixStatusProperty().get()); } return ""; } - private String getMixSuccess(UtxoEntry.MixStatus mixStatus) { - String msg = "Success!"; - String mixesDone = Strings.padStart(Integer.toString(mixStatus.getMixesDone()), WIDTH - msg.length(), ' '); - return msg + mixesDone; - } - - private String getMixFail(UtxoEntry.MixStatus mixStatus) { - long elapsed = mixStatus.getMixErrorTimestamp() == null ? 0L : System.currentTimeMillis() - mixStatus.getMixErrorTimestamp(); - if(!mixStatus.getMixFailReason().isError() || elapsed >= ERROR_DISPLAY_MILLIS) { - return getMixCountOnly(mixStatus); - } - - String msg = mixStatus.getMixFailReason().getMessage(); - msg = msg.length() > 14 ? msg.substring(0, 14) : msg; - String mixesDone = Strings.padStart(Integer.toString(mixStatus.getMixesDone()), WIDTH - msg.length(), ' '); - return msg + mixesDone; - } - - private String getMixProgress(UtxoEntry.MixStatus mixStatus, MixProgress mixProgress) { - int progress = mixProgress.getMixStep().getProgressPercent(); - String progressBar = Strings.padEnd(Strings.repeat(Character.toString(Symbols.BLOCK_SOLID), progress / 10), 10, ' '); - String mixesDone = Strings.padStart(Integer.toString(mixStatus.getMixesDone()), WIDTH - 10, ' '); - return progressBar + mixesDone; - } - private String getMixCountOnly(UtxoEntry.MixStatus mixStatus) { return Strings.padStart(Integer.toString(mixStatus == null ? 0 : mixStatus.getMixesDone()), WIDTH, ' '); } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/MixToController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/MixToController.java deleted file mode 100644 index e74f1daa..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/MixToController.java +++ /dev/null @@ -1,142 +0,0 @@ -package com.sparrowwallet.sparrow.wallet; - -import com.samourai.whirlpool.client.wallet.beans.IndexRange; -import com.sparrowwallet.drongo.protocol.ScriptType; -import com.sparrowwallet.drongo.wallet.MixConfig; -import com.sparrowwallet.drongo.wallet.StandardAccount; -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.sparrow.AppServices; -import com.sparrowwallet.sparrow.EventManager; -import com.sparrowwallet.sparrow.control.IntegerSpinner; -import com.sparrowwallet.sparrow.event.MixToConfigChangedEvent; -import com.sparrowwallet.sparrow.whirlpool.Whirlpool; -import javafx.collections.FXCollections; -import javafx.fxml.FXML; -import javafx.fxml.Initializable; -import javafx.scene.control.ComboBox; -import javafx.util.StringConverter; - -import java.net.URL; -import java.util.*; -import java.util.stream.Collectors; - -import static com.sparrowwallet.drongo.wallet.StandardAccount.*; - -public class MixToController implements Initializable { - private static final Wallet NONE_WALLET = new Wallet("None"); - - @FXML - private ComboBox mixToWallets; - - @FXML - private IntegerSpinner minMixes; - - @FXML - private ComboBox indexRange; - - private MixConfig mixConfig; - - @Override - public void initialize(URL location, ResourceBundle resources) { - - } - - public void initializeView(Wallet wallet) { - mixConfig = wallet.getMasterMixConfig().copy(); - - List allWallets = new ArrayList<>(); - allWallets.add(NONE_WALLET); - - List destinationWallets = AppServices.get().getOpenWallets().keySet().stream().filter(openWallet -> openWallet.isValid() - && (openWallet.getScriptType() == ScriptType.P2WPKH || openWallet.getScriptType() == ScriptType.P2WSH) - && openWallet != wallet && openWallet != wallet.getMasterWallet() - && (openWallet.getStandardAccountType() == null || !List.of(WHIRLPOOL_PREMIX, WHIRLPOOL_BADBANK).contains(openWallet.getStandardAccountType()))).collect(Collectors.toList()); - allWallets.addAll(destinationWallets); - - mixToWallets.setItems(FXCollections.observableList(allWallets)); - mixToWallets.setConverter(new StringConverter<>() { - @Override - public String toString(Wallet wallet) { - return wallet == null ? "" : wallet.getFullDisplayName(); - } - - @Override - public Wallet fromString(String string) { - return null; - } - }); - - String mixToWalletId = null; - try { - mixToWalletId = AppServices.getWhirlpoolServices().getWhirlpoolMixToWalletId(mixConfig); - } catch(NoSuchElementException e) { - //ignore, mix to wallet is not open - } - - if(mixToWalletId != null) { - mixToWallets.setValue(AppServices.get().getWallet(mixToWalletId)); - } else { - mixToWallets.setValue(NONE_WALLET); - } - - mixToWallets.valueProperty().addListener((observable, oldValue, newValue) -> { - if(newValue == NONE_WALLET) { - mixConfig.setMixToWalletName(null); - mixConfig.setMixToWalletFile(null); - } else { - mixConfig.setMixToWalletName(newValue.getName()); - mixConfig.setMixToWalletFile(AppServices.get().getOpenWallets().get(newValue).getWalletFile()); - } - - EventManager.get().post(new MixToConfigChangedEvent(wallet)); - }); - - int initialMinMixes = mixConfig.getMinMixes() == null ? Whirlpool.DEFAULT_MIXTO_MIN_MIXES : mixConfig.getMinMixes(); - minMixes.setValueFactory(new IntegerSpinner.ValueFactory(2, 10000, initialMinMixes)); - minMixes.valueProperty().addListener((observable, oldValue, newValue) -> { - if(newValue == null || newValue < 2 || newValue > 10000) { - return; - } - - mixConfig.setMinMixes(newValue); - EventManager.get().post(new MixToConfigChangedEvent(wallet)); - }); - - indexRange.setConverter(new StringConverter<>() { - @Override - public String toString(IndexRange indexRange) { - if(indexRange == null) { - return ""; - } - - return indexRange.toString().charAt(0) + indexRange.toString().substring(1).toLowerCase(Locale.ROOT); - } - - @Override - public IndexRange fromString(String string) { - return null; - } - }); - - indexRange.setValue(IndexRange.FULL); - if(mixConfig.getIndexRange() != null) { - try { - indexRange.setValue(IndexRange.valueOf(mixConfig.getIndexRange())); - } catch(Exception e) { - //ignore - } - } - indexRange.valueProperty().addListener((observable, oldValue, newValue) -> { - mixConfig.setIndexRange(newValue.toString()); - EventManager.get().post(new MixToConfigChangedEvent(wallet)); - }); - } - - public void close() { - minMixes.commitValue(); - } - - public MixConfig getMixConfig() { - return mixConfig; - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/MixToDialog.java b/src/main/java/com/sparrowwallet/sparrow/wallet/MixToDialog.java deleted file mode 100644 index 47983321..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/MixToDialog.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.sparrowwallet.sparrow.wallet; - -import com.google.common.eventbus.Subscribe; -import com.sparrowwallet.drongo.wallet.MixConfig; -import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.sparrow.AppServices; -import com.sparrowwallet.sparrow.EventManager; -import com.sparrowwallet.sparrow.event.MixToConfigChangedEvent; -import com.sparrowwallet.sparrow.whirlpool.Whirlpool; -import javafx.fxml.FXMLLoader; -import javafx.scene.control.*; -import org.controlsfx.tools.Borders; - -import java.io.IOException; -import java.util.NoSuchElementException; - -public class MixToDialog extends Dialog { - private final Wallet wallet; - private final Button applyButton; - - public MixToDialog(Wallet wallet) { - final DialogPane dialogPane = getDialogPane(); - AppServices.setStageIcon(dialogPane.getScene().getWindow()); - this.wallet = wallet; - - try { - FXMLLoader mixToLoader = new FXMLLoader(AppServices.class.getResource("wallet/mixto.fxml")); - dialogPane.setContent(Borders.wrap(mixToLoader.load()).emptyBorder().buildAll()); - MixToController mixToController = mixToLoader.getController(); - mixToController.initializeView(wallet); - - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(wallet); - final ButtonType closeButtonType = new javafx.scene.control.ButtonType("Close", ButtonBar.ButtonData.CANCEL_CLOSE); - final ButtonType applyButtonType = new javafx.scene.control.ButtonType(whirlpool.isStarted() ? "Restart Whirlpool" : "Apply", ButtonBar.ButtonData.APPLY); - dialogPane.getButtonTypes().addAll(closeButtonType, applyButtonType); - - applyButton = (Button)dialogPane.lookupButton(applyButtonType); - applyButton.setDisable(true); - applyButton.setDefaultButton(true); - - try { - AppServices.getWhirlpoolServices().getWhirlpoolMixToWalletId(wallet.getMasterMixConfig()); - } catch(NoSuchElementException e) { - applyButton.setDisable(false); - } - - dialogPane.setPrefWidth(400); - dialogPane.setPrefHeight(300); - dialogPane.setMinHeight(dialogPane.getPrefHeight()); - AppServices.moveToActiveWindowScreen(this); - - setResultConverter(dialogButton -> dialogButton == applyButtonType ? mixToController.getMixConfig() : null); - - setOnCloseRequest(event -> { - mixToController.close(); - EventManager.get().unregister(this); - }); - EventManager.get().register(this); - } - catch(IOException e) { - throw new RuntimeException(e); - } - } - - @Subscribe - public void mixToConfigChanged(MixToConfigChangedEvent event) { - if(event.getWallet() == wallet) { - applyButton.setDisable(false); - } - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/PaymentController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/PaymentController.java index 549e2bda..21622a8d 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/PaymentController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/PaymentController.java @@ -26,9 +26,7 @@ import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.net.ExchangeSource; import com.sparrowwallet.sparrow.paynym.PayNym; -import com.sparrowwallet.sparrow.paynym.PayNymAddress; import com.sparrowwallet.sparrow.paynym.PayNymDialog; -import com.sparrowwallet.sparrow.soroban.*; import javafx.application.Platform; import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; @@ -52,7 +50,6 @@ import org.slf4j.LoggerFactory; import java.net.URL; import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; import java.util.*; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -128,8 +125,6 @@ public class PaymentController extends WalletFormController implements Initializ emptyAmountProperty.set(true); } - updateMixOnlyStatus(); - sendController.updateTransaction(); } }; @@ -166,8 +161,7 @@ public class PaymentController extends WalletFormController implements Initializ openWallets.prefWidthProperty().bind(address.widthProperty()); openWallets.valueProperty().addListener((observable, oldValue, newValue) -> { if(newValue == payNymWallet) { - boolean selectLinkedOnly = sendController.getPaymentTabs().getTabs().size() > 1 || !SorobanServices.canWalletMix(sendController.getWalletForm().getWallet()); - PayNymDialog payNymDialog = new PayNymDialog(sendController.getWalletForm().getWalletId(), PayNymDialog.Operation.SEND, selectLinkedOnly); + PayNymDialog payNymDialog = new PayNymDialog(sendController.getWalletForm().getWalletId(), PayNymDialog.Operation.SEND); payNymDialog.initOwner(scanQrButton.getScene().getWindow()); Optional optPayNym = payNymDialog.showAndWait(); optPayNym.ifPresent(this::setPayNym); @@ -208,7 +202,6 @@ public class PaymentController extends WalletFormController implements Initializ }); payNymProperty.addListener((observable, oldValue, payNym) -> { - updateMixOnlyStatus(payNym); revalidateAmount(); }); @@ -325,29 +318,11 @@ public class PaymentController extends WalletFormController implements Initializ address.setText(payNym.nymName()); address.leftProperty().set(getPayNymGlyph()); label.requestFocus(); - if(existingPayNym != null && payNym.nymName().equals(existingPayNym.nymName()) && payNym.isCollaborativeSend() != existingPayNym.isCollaborativeSend()) { + if(existingPayNym != null && payNym.nymName().equals(existingPayNym.nymName())) { sendController.updateTransaction(); } } - public void updateMixOnlyStatus() { - updateMixOnlyStatus(payNymProperty.get()); - } - - public void updateMixOnlyStatus(PayNym payNym) { - boolean mixOnly = false; - try { - mixOnly = payNym != null && getRecipientAddress() instanceof PayNymAddress; - } catch(InvalidAddressException e) { - log.error("Error creating payment code from PayNym", e); - } - - addPaymentButton.setDisable(mixOnly); - if(mixOnly) { - sendController.setPayNymMixOnlyPayment(); - } - } - private void updateOpenWallets() { updateOpenWallets(AppServices.get().getOpenWallets().keySet()); } @@ -424,27 +399,22 @@ public class PaymentController extends WalletFormController implements Initializ return Address.fromString(address.getText()); } - if(!payNym.isCollaborativeSend()) { - try { - Wallet recipientBip47Wallet = getWalletForPayNym(payNym); - if(recipientBip47Wallet != null) { - int index = sendController.getPayNymSendIndex(this); - WalletNode sendNode = recipientBip47Wallet.getFreshNode(KeyPurpose.SEND); - for(int i = 0; i < index; i++) { - sendNode = recipientBip47Wallet.getFreshNode(KeyPurpose.SEND, sendNode); - } - ECKey pubKey = sendNode.getPubKey(); - Address address = recipientBip47Wallet.getScriptType().getAddress(pubKey); - if(sendController.getPaymentTabs().getTabs().size() > 1 || (getRecipientValueSats() != null && getRecipientValueSats() > getRecipientDustThreshold(address)) || maxButton.isSelected()) { - return address; - } + try { + Wallet recipientBip47Wallet = getWalletForPayNym(payNym); + if(recipientBip47Wallet != null) { + int index = sendController.getPayNymSendIndex(this); + WalletNode sendNode = recipientBip47Wallet.getFreshNode(KeyPurpose.SEND); + for(int i = 0; i < index; i++) { + sendNode = recipientBip47Wallet.getFreshNode(KeyPurpose.SEND, sendNode); } - } catch(InvalidPaymentCodeException e) { - log.error("Error creating payment code from PayNym", e); + ECKey pubKey = sendNode.getPubKey(); + return recipientBip47Wallet.getScriptType().getAddress(pubKey); } + } catch(InvalidPaymentCodeException e) { + log.error("Error creating payment code from PayNym", e); } - return new PayNymAddress(payNym); + throw new InvalidAddressException(); } private Wallet getWalletForPayNym(PayNym payNym) throws InvalidPaymentCodeException { @@ -453,8 +423,7 @@ public class PaymentController extends WalletFormController implements Initializ } boolean isSentToSamePayNym(PaymentController paymentController) { - return (this != paymentController && payNymProperty.get() != null && !payNymProperty.get().isCollaborativeSend() - && payNymProperty.get().paymentCode().equals(paymentController.payNymProperty.get().paymentCode())); + return (this != paymentController && payNymProperty.get() != null && payNymProperty.get().paymentCode().equals(paymentController.payNymProperty.get().paymentCode())); } private Long getRecipientValueSats() { @@ -492,10 +461,6 @@ public class PaymentController extends WalletFormController implements Initializ address = new P2PKHAddress(new byte[20]); } - if(address instanceof PayNymAddress && SorobanServices.canWalletMix(sendController.getWalletForm().getWallet())) { - return 0; - } - return getRecipientDustThreshold(address); } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java index 09769f8a..1cba5000 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java @@ -1,7 +1,6 @@ package com.sparrowwallet.sparrow.wallet; import com.google.common.eventbus.Subscribe; -import com.samourai.whirlpool.client.whirlpool.beans.Pool; import com.sparrowwallet.drongo.BitcoinUnit; import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.Network; @@ -26,14 +25,9 @@ import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.net.*; import com.sparrowwallet.sparrow.paynym.PayNym; import com.sparrowwallet.sparrow.paynym.PayNymService; -import com.sparrowwallet.sparrow.soroban.InitiatorDialog; -import com.sparrowwallet.sparrow.paynym.PayNymAddress; -import com.sparrowwallet.sparrow.soroban.SorobanServices; -import com.sparrowwallet.sparrow.whirlpool.Whirlpool; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Platform; -import javafx.beans.binding.Bindings; import javafx.beans.property.*; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; @@ -49,7 +43,6 @@ import javafx.scene.Node; import javafx.scene.control.*; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; -import javafx.stage.Modality; import javafx.util.Duration; import javafx.util.StringConverter; import org.controlsfx.glyphfont.Glyph; @@ -148,9 +141,6 @@ public class SendController extends WalletFormController implements Initializabl @FXML private Button createButton; - @FXML - private Button premixButton; - @FXML private Button notificationButton; @@ -162,8 +152,6 @@ public class SendController extends WalletFormController implements Initializabl private final ObjectProperty txoFilterProperty = new SimpleObjectProperty<>(null); - private final ObjectProperty whirlpoolProperty = new SimpleObjectProperty<>(null); - private final ObjectProperty paymentCodeProperty = new SimpleObjectProperty<>(null); private final ObjectProperty walletTransactionProperty = new SimpleObjectProperty<>(null); @@ -234,7 +222,6 @@ public class SendController extends WalletFormController implements Initializabl }; private final ChangeListener broadcastButtonsOnlineListener = (observable, oldValue, newValue) -> { - premixButton.setDisable(!newValue); notificationButton.setDisable(walletTransactionProperty.get() == null || isInsufficientFeeRate() || !newValue); }; @@ -269,7 +256,6 @@ public class SendController extends WalletFormController implements Initializabl } paymentTabs.getTabs().forEach(tab -> { tab.setClosable(true); - ((PaymentController)tab.getUserData()).updateMixOnlyStatus(); }); } else { paymentTabs.getStyleClass().remove("multiple-tabs"); @@ -400,7 +386,7 @@ public class SendController extends WalletFormController implements Initializabl transactionDiagram.update(walletTransaction); updatePrivacyAnalysis(walletTransaction); - createButton.setDisable(walletTransaction == null || isInsufficientFeeRate() || isPayNymMixOnlyPayment(walletTransaction.getPayments())); + createButton.setDisable(walletTransaction == null || isInsufficientFeeRate()); notificationButton.setDisable(walletTransaction == null || isInsufficientFeeRate() || !AppServices.isConnected()); }); @@ -432,10 +418,8 @@ public class SendController extends WalletFormController implements Initializabl optimizationHelp.visibleProperty().bind(privacyAnalysis.visibleProperty().not()); createButton.managedProperty().bind(createButton.visibleProperty()); - premixButton.managedProperty().bind(premixButton.visibleProperty()); notificationButton.managedProperty().bind(notificationButton.visibleProperty()); - createButton.visibleProperty().bind(Bindings.and(premixButton.visibleProperty().not(), notificationButton.visibleProperty().not())); - premixButton.setVisible(false); + createButton.visibleProperty().bind(notificationButton.visibleProperty().not()); notificationButton.setVisible(false); AppServices.onlineProperty().addListener(new WeakChangeListener<>(broadcastButtonsOnlineListener)); } @@ -634,8 +618,7 @@ public class SendController extends WalletFormController implements Initializabl OptimizationStrategy optimizationStrategy = (OptimizationStrategy)optimizationToggleGroup.getSelectedToggle().getUserData(); if(optimizationStrategy == OptimizationStrategy.PRIVACY && payments.size() == 1 - && (payments.get(0).getAddress().getScriptType() == getWalletForm().getWallet().getFreshNode(KeyPurpose.RECEIVE).getAddress().getScriptType()) - && !(payments.get(0).getAddress() instanceof PayNymAddress)) { + && (payments.get(0).getAddress().getScriptType() == getWalletForm().getWallet().getFreshNode(KeyPurpose.RECEIVE).getAddress().getScriptType())) { selectors.add(new StonewallUtxoSelector(payments.get(0).getAddress().getScriptType(), noInputsFee)); } @@ -991,28 +974,14 @@ public class SendController extends WalletFormController implements Initializabl } } - private boolean isPayNymMixOnlyPayment(List payments) { - return payments.size() == 1 && payments.get(0).getAddress() instanceof PayNymAddress; - } - - public void setPayNymMixOnlyPayment() { - optimizationToggleGroup.selectToggle(privacyToggle); - transactionDiagram.setOptimizationStrategy(OptimizationStrategy.PRIVACY); - efficiencyToggle.setDisable(true); - privacyToggle.setDisable(false); - } - - private boolean isMixPossible(List payments) { - return (utxoSelectorProperty.get() == null || SorobanServices.canWalletMix(walletForm.getWallet())) - && payments.size() == 1 + private boolean isFakeMixPossible(List payments) { + return utxoSelectorProperty.get() == null && payments.size() == 1 && (payments.get(0).getAddress().getScriptType() == getWalletForm().getWallet().getFreshNode(KeyPurpose.RECEIVE).getAddress().getScriptType()) && AppServices.getPayjoinURI(payments.get(0).getAddress()) == null; } private void updateOptimizationButtons(List payments) { - if(isPayNymMixOnlyPayment(payments)) { - setPayNymMixOnlyPayment(); - } else if(isMixPossible(payments)) { + if(isFakeMixPossible(payments)) { setPreferredOptimizationStrategy(); efficiencyToggle.setDisable(false); privacyToggle.setDisable(false); @@ -1091,11 +1060,9 @@ public class SendController extends WalletFormController implements Initializabl efficiencyToggle.setDisable(false); privacyToggle.setDisable(false); - premixButton.setVisible(false); notificationButton.setVisible(false); createButton.setDefaultButton(true); - whirlpoolProperty.set(null); paymentCodeProperty.set(null); addressNodeMap.clear(); @@ -1183,51 +1150,6 @@ public class SendController extends WalletFormController implements Initializabl walletForm.addWalletTransactionNodes(nodes); } - public void broadcastPremix(ActionEvent event) { - //Ensure all child wallets have been saved - Wallet masterWallet = getWalletForm().getWallet().isMasterWallet() ? getWalletForm().getWallet() : getWalletForm().getWallet().getMasterWallet(); - for(Wallet childWallet : masterWallet.getChildWallets()) { - if(!childWallet.isNested()) { - Storage storage = AppServices.get().getOpenWallets().get(childWallet); - if(!storage.isPersisted(childWallet)) { - try { - storage.saveWallet(childWallet); - EventManager.get().post(new NewChildWalletSavedEvent(storage, masterWallet, childWallet)); - } catch(Exception e) { - AppServices.showErrorDialog("Error saving wallet " + childWallet.getName(), e.getMessage()); - } - } - } - } - - //The WhirlpoolWallet has already been configured for the tx0 preview - Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWalletForm().getStorage().getWalletId(masterWallet)); - Map utxos = walletTransactionProperty.get().getSelectedUtxos(); - Whirlpool.Tx0BroadcastService tx0BroadcastService = new Whirlpool.Tx0BroadcastService(whirlpool, whirlpoolProperty.get(), utxos.keySet()); - tx0BroadcastService.setOnRunning(workerStateEvent -> { - premixButton.setDisable(true); - addWalletTransactionNodes(); - }); - tx0BroadcastService.setOnSucceeded(workerStateEvent -> { - premixButton.setDisable(false); - Sha256Hash txid = tx0BroadcastService.getValue(); - clear(null); - }); - tx0BroadcastService.setOnFailed(workerStateEvent -> { - premixButton.setDisable(false); - Throwable exception = workerStateEvent.getSource().getException(); - while(exception.getCause() != null) { - exception = exception.getCause(); - } - - AppServices.showErrorDialog("Error broadcasting premix transaction", exception.getMessage()); - }); - ServiceProgressDialog progressDialog = new ServiceProgressDialog("Whirlpool", "Broadcast Premix Transaction", "/image/whirlpool.png", tx0BroadcastService); - progressDialog.initOwner(premixButton.getScene().getWindow()); - AppServices.moveToActiveWindowScreen(progressDialog); - tx0BroadcastService.start(); - } - public void broadcastNotification(ActionEvent event) { Wallet wallet = getWalletForm().getWallet(); Storage storage = AppServices.get().getOpenWallets().get(wallet); @@ -1487,7 +1409,7 @@ public class SendController extends WalletFormController implements Initializabl @Subscribe public void spendUtxos(SpendUtxoEvent event) { if((event.getUtxos() == null || !event.getUtxos().isEmpty()) && event.getWallet().equals(getWalletForm().getWallet())) { - if(whirlpoolProperty.get() != null || paymentCodeProperty.get() != null) { + if(paymentCodeProperty.get() != null) { clear(null); } @@ -1516,19 +1438,14 @@ public class SendController extends WalletFormController implements Initializabl } txoFilterProperty.set(null); - whirlpoolProperty.set(event.getPool()); paymentCodeProperty.set(event.getPaymentCode()); updateTransaction(event.getPayments() == null || event.getPayments().stream().anyMatch(Payment::isSendMax)); - boolean isWhirlpoolPremix = (event.getPool() != null); - premixButton.setVisible(isWhirlpoolPremix); - premixButton.setDefaultButton(isWhirlpoolPremix); - boolean isNotificationTransaction = (event.getPaymentCode() != null); notificationButton.setVisible(isNotificationTransaction); notificationButton.setDefaultButton(isNotificationTransaction); - setInputFieldsDisabled(isWhirlpoolPremix || isNotificationTransaction, isWhirlpoolPremix); + setInputFieldsDisabled(isNotificationTransaction, false); } } @@ -1646,37 +1563,6 @@ public class SendController extends WalletFormController implements Initializabl } } - @Subscribe - public void sorobanInitiated(SorobanInitiatedEvent event) { - if(event.getWallet().equals(getWalletForm().getWallet())) { - if(!AppServices.onlineProperty().get()) { - Optional optButtonType = AppServices.showErrorDialog("Cannot Mix Offline", "Sparrow needs to be connected to a server to perform collaborative mixes. Try to connect?", ButtonType.CANCEL, ButtonType.OK); - if(optButtonType.isPresent() && optButtonType.get() == ButtonType.OK) { - AppServices.onlineProperty().set(true); - } - return; - } - - Platform.runLater(() -> { - InitiatorDialog initiatorDialog = new InitiatorDialog(getWalletForm().getWalletId(), getWalletForm().getWallet(), walletTransactionProperty.get()); - initiatorDialog.initOwner(paymentTabs.getScene().getWindow()); - if(Config.get().isSameAppMixing()) { - initiatorDialog.initModality(Modality.NONE); - } - Optional optTransaction = initiatorDialog.showAndWait(); - if(optTransaction.isPresent()) { - BlockTransaction blockTransaction = walletForm.getWallet().getWalletTransaction(optTransaction.get().getTxId()); - if(blockTransaction != null && blockTransaction.getLabel() == null && walletTransactionProperty.get() != null) { - blockTransaction.setLabel(walletTransactionProperty.get().getPayments().stream().map(Payment::getLabel).findFirst().orElse(null)); - TransactionEntry transactionEntry = new TransactionEntry(walletForm.getWallet(), blockTransaction, Collections.emptyMap(), Collections.emptyMap()); - EventManager.get().post(new WalletEntryLabelsChangedEvent(walletForm.getWallet(), List.of(transactionEntry))); - } - clear(null); - } - }); - } - } - private class PrivacyAnalysisTooltip extends VBox { private final List

diff --git a/src/main/resources/com/sparrowwallet/sparrow/soroban/counterparty.css b/src/main/resources/com/sparrowwallet/sparrow/soroban/counterparty.css deleted file mode 100644 index 4e01c11f..00000000 --- a/src/main/resources/com/sparrowwallet/sparrow/soroban/counterparty.css +++ /dev/null @@ -1,50 +0,0 @@ -.counterparty-pane { - -fx-padding: 0; -} - -.title-area { - -fx-background-color: -fx-control-inner-background; - -fx-padding: 10 25 10 25; - -fx-border-width: 0px 0px 1px 0px; - -fx-border-color: #e5e5e6; -} - -#counterpartyBox, .button-bar { - -fx-padding: 10 25 25 25; -} - -.button-bar .container { - -fx-padding: 0 0 15px 0; -} - -.title-label { - -fx-font-size: 24px; -} - -.title-text { - -fx-font-size: 20px; - -fx-padding: 0 0 15px 0; - -fx-graphic-text-gap: 10px; -} - -.content-text { - -fx-font-size: 16px; - -fx-text-fill: derive(-fx-text-base-color, 15%); -} - -.field-box { - -fx-pref-height: 30px; - -fx-alignment: CENTER_LEFT; -} - -.wide-field-label { - -fx-pref-width: 180px; -} - -.field-label { - -fx-pref-width: 110px; -} - -.field-control { - -fx-pref-width: 200px; -} \ No newline at end of file diff --git a/src/main/resources/com/sparrowwallet/sparrow/soroban/counterparty.fxml b/src/main/resources/com/sparrowwallet/sparrow/soroban/counterparty.fxml deleted file mode 100644 index 20b1926c..00000000 --- a/src/main/resources/com/sparrowwallet/sparrow/soroban/counterparty.fxml +++ /dev/null @@ -1,171 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/com/sparrowwallet/sparrow/soroban/initiator.css b/src/main/resources/com/sparrowwallet/sparrow/soroban/initiator.css deleted file mode 100644 index 12d2d346..00000000 --- a/src/main/resources/com/sparrowwallet/sparrow/soroban/initiator.css +++ /dev/null @@ -1,46 +0,0 @@ -.initiator-pane { - -fx-padding: 0; -} - -.title-area { - -fx-background-color: -fx-control-inner-background; - -fx-padding: 10 25 10 25; - -fx-border-width: 0px 0px 1px 0px; - -fx-border-color: #e5e5e6; -} - -#initiatorBox, .button-bar { - -fx-padding: 10 25 25 25; -} - -.button-bar .container { - -fx-padding: 0 0 15px 0; -} - -.title-label { - -fx-font-size: 24px; -} - -.title-text { - -fx-font-size: 20px; - -fx-padding: 0 0 15px 0; - -fx-graphic-text-gap: 10px; -} - -.content-text { - -fx-font-size: 16px; - -fx-text-fill: derive(-fx-text-base-color, 15%); -} - -.field-box { - -fx-pref-height: 30px; - -fx-alignment: CENTER_LEFT; -} - -.field-label { - -fx-pref-width: 180px; -} - -.field-control { - -fx-pref-width: 200px; -} \ No newline at end of file diff --git a/src/main/resources/com/sparrowwallet/sparrow/soroban/initiator.fxml b/src/main/resources/com/sparrowwallet/sparrow/soroban/initiator.fxml deleted file mode 100644 index 5dea1bf3..00000000 --- a/src/main/resources/com/sparrowwallet/sparrow/soroban/initiator.fxml +++ /dev/null @@ -1,134 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/com/sparrowwallet/sparrow/wallet/mixto.fxml b/src/main/resources/com/sparrowwallet/sparrow/wallet/mixto.fxml deleted file mode 100644 index 86488c49..00000000 --- a/src/main/resources/com/sparrowwallet/sparrow/wallet/mixto.fxml +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
-
- - - - - - - - -
-
- -
-
- - - - - - - - - - - - -
-
-
-
-
- diff --git a/src/main/resources/com/sparrowwallet/sparrow/wallet/send.fxml b/src/main/resources/com/sparrowwallet/sparrow/wallet/send.fxml index d3cc1d8f..04f01998 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/wallet/send.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/wallet/send.fxml @@ -192,11 +192,6 @@ - - -