mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-25 05:06:45 +00:00
remove whirlpool and soroban features and dependencies
This commit is contained in:
parent
f7e603118f
commit
1676676e06
97 changed files with 1739 additions and 7535 deletions
|
@ -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')
|
||||
|
|
2
drongo
2
drongo
|
@ -1 +1 @@
|
|||
Subproject commit 3a2344f1297e25a9c691ddad66264e00c391af44
|
||||
Subproject commit 143d28166a9a0b28469d1c57c460718e71803029
|
|
@ -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<SecureString> 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<ButtonType> 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<ButtonType> 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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Network> 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<Wallet> addWhirlpoolWallets(Wallet decryptedWallet, String walletId, Storage storage) {
|
||||
List<Wallet> 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);
|
||||
}
|
||||
|
|
|
@ -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<List<StandardAccount>> {
|
|||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Entry, UtxoEntry.MixStatus> {
|
||||
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<Entry, UtxoEntry.MixStatus> {
|
|||
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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -81,12 +81,9 @@ public class PayNymCell extends ListCell<PayNym> {
|
|||
linkButton.setDisable(true);
|
||||
payNymController.linkPayNym(payNym);
|
||||
});
|
||||
|
||||
if(payNymController.isSelectLinkedOnly()) {
|
||||
getStyleClass().add("unlinked");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setText(null);
|
||||
setGraphic(pane);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<Map<BlockTransactionHashIndex, WalletNode>> 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<Map<BlockTransactionHashIndex, WalletNode>> displayedUtxoSets = new ArrayList<>();
|
||||
for(Map<BlockTransactionHashIndex, WalletNode> selectedUtxoSet : walletTx.getSelectedUtxoSets()) {
|
||||
displayedUtxoSets.add(getDisplayedUtxos(selectedUtxoSet, addUserSet ? 2 : walletTx.getSelectedUtxoSets().size()));
|
||||
}
|
||||
|
||||
if(addUserSet && displayedUtxoSets.size() == 1) {
|
||||
Map<BlockTransactionHashIndex, WalletNode> addUserUtxoSet = new HashMap<>();
|
||||
addUserUtxoSet.put(new AddUserBlockTransactionHashIndex(!walletTx.isTwoPersonCoinjoin()), null);
|
||||
displayedUtxoSets.add(addUserUtxoSet);
|
||||
displayedUtxoSets.add(getDisplayedUtxos(selectedUtxoSet, walletTx.getSelectedUtxoSets().size()));
|
||||
}
|
||||
|
||||
List<Map<BlockTransactionHashIndex, WalletNode>> 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");
|
||||
}
|
||||
|
||||
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<Payment> additionalPayments;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<BlockTransactionHashIndex> 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<BlockTransactionHashIndex> utxos, List<Payment> payments, List<byte[]> 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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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> T requestJson(String url, Class<T> responseType, Map<String, String> headers) throws Exception {
|
||||
return getHttpClient(HttpUsage.BACKEND).getJson(url, responseType, headers);
|
||||
return getHttpClient(HttpUsage.DEFAULT).getJson(url, responseType, headers);
|
||||
}
|
||||
|
||||
public <T> Observable<Optional<T>> postJson(String url, Class<T> responseType, Map<String, String> 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<String, String> 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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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> T unwrapException(Callable<T> 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> T blockingGet(Single<T> 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> T blockingGet(Single<T> o, long timeoutMs) throws Exception {
|
||||
Callable<T> callable = () -> blockingGet(o);
|
||||
return blockingGet(runAsync(callable, timeoutMs));
|
||||
}
|
||||
|
||||
public <T> T blockingGet(Future<T> o, long timeoutMs) throws Exception {
|
||||
return o.get(timeoutMs, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public <T> T blockingLast(Observable<T> o) throws Exception {
|
||||
return unwrapException(o::blockingLast);
|
||||
}
|
||||
|
||||
public void blockingAwait(Completable o) throws Exception {
|
||||
Callable<Optional> callable = () -> {
|
||||
o.blockingAwait();
|
||||
return Optional.empty();
|
||||
};
|
||||
unwrapException(callable);
|
||||
}
|
||||
|
||||
public void blockingAwait(Completable o, long timeoutMs) throws Exception {
|
||||
Callable<Optional> callable = () -> {
|
||||
o.blockingAwait();
|
||||
return Optional.empty();
|
||||
};
|
||||
blockingGet(runAsync(callable, timeoutMs));
|
||||
}
|
||||
|
||||
public <T> Single<T> timeout(Single<T> 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 <T> Single<T> runIOAsync(final Callable<T> callable) {
|
||||
return Single.fromCallable(callable).subscribeOn(Schedulers.io());
|
||||
}
|
||||
|
||||
public Completable runIOAsyncCompletable(final Action action) {
|
||||
return Completable.fromAction(action).subscribeOn(Schedulers.io());
|
||||
}
|
||||
|
||||
public <T> T runIO(final Callable<T> 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 <T> Future<T> runAsync(Callable<T> callable) {
|
||||
// preserve logging context
|
||||
String mdc = mdcAppend("runAsync=" + System.currentTimeMillis());
|
||||
return threadUtil.runAsync(() -> {
|
||||
MDC.put("mdc", mdc);
|
||||
return callable.call();
|
||||
});
|
||||
}
|
||||
|
||||
public <T> Single<T> runAsync(Callable<T> callable, long timeoutMs) {
|
||||
Future<T> 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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.sparrowwallet.sparrow.net.http.client;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public enum HttpProxyProtocol {
|
||||
HTTP,
|
||||
SOCKS,
|
||||
SOCKS5;
|
||||
|
||||
public static Optional<HttpProxyProtocol> find(String value) {
|
||||
try {
|
||||
return Optional.of(valueOf(value));
|
||||
} catch(Exception e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.sparrowwallet.sparrow.net.http.client;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface IBackendClient {
|
||||
<T> T getJson(String url, Class<T> responseType, Map<String, String> headers) throws HttpException;
|
||||
|
||||
<T> T getJson(String url, Class<T> responseType, Map<String, String> headers, boolean async) throws HttpException;
|
||||
|
||||
<T> T postUrlEncoded(String url, Class<T> responseType, Map<String, String> headers, Map<String, String> body) throws Exception;
|
||||
}
|
|
@ -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;
|
||||
|
||||
<T> Single<Optional<T>> postJson(String url, Class<T> responseType, Map<String, String> headers, Object body);
|
||||
|
||||
Single<Optional<String>> postString(String urlStr, Map<String, String> headers, String contentType, String content);
|
||||
}
|
|
@ -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();
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.sparrowwallet.sparrow.net.http.client;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface IHttpProxySupplier {
|
||||
Optional<HttpProxy> getHttpProxy(HttpUsage httpUsage);
|
||||
|
||||
void changeIdentity();
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<Exception> onNetworkError;
|
||||
|
||||
public JacksonHttpClient(Consumer<Exception> onNetworkError) {
|
||||
this.onNetworkError = onNetworkError;
|
||||
}
|
||||
|
||||
protected abstract String requestJsonGet(String urlStr, Map<String, String> headers, boolean async) throws HttpException;
|
||||
|
||||
protected abstract String requestJsonPost(String urlStr, Map<String, String> headers, String jsonBody) throws HttpException;
|
||||
|
||||
protected abstract String requestStringPost(String urlStr, Map<String, String> headers, String contentType, String content) throws HttpException;
|
||||
|
||||
protected abstract String requestJsonPostUrlEncoded(String urlStr, Map<String, String> headers, Map<String, String> body) throws HttpException;
|
||||
|
||||
@Override
|
||||
public <T> T getJson(String urlStr, Class<T> responseType, Map<String, String> headers) throws HttpException {
|
||||
return getJson(urlStr, responseType, headers, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getJson(String urlStr, Class<T> responseType, Map<String, String> 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 <T> Single<Optional<T>> postJson(final String urlStr, final Class<T> responseType, final Map<String, String> 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<Optional<String>> postString(String urlStr, Map<String, String> 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> T postUrlEncoded(String urlStr, Class<T> responseType, Map<String, String> headers, Map<String, String> 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> T parseJson(String responseContent, Class<T> 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<String> 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 <T> Single<Optional<T>> httpObservable(final Callable<T> supplier) {
|
||||
return Single.fromCallable(() -> Optional.ofNullable(supplier.call())).subscribeOn(Schedulers.io());
|
||||
}
|
||||
|
||||
protected <T> T httpObservableBlockingSingle(final Callable<T> supplier) throws HttpException {
|
||||
try {
|
||||
Optional<T> 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();
|
||||
}
|
||||
}
|
|
@ -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<Exception> 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<String, String> headers, boolean async) throws HttpException {
|
||||
Request req = computeHttpRequest(urlStr, HttpMethod.GET, headers);
|
||||
return makeRequest(req, async);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String requestJsonPost(String urlStr, Map<String, String> 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<String, String> 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<String, String> headers, Map<String, String> body) throws HttpException {
|
||||
Request req = computeHttpRequest(urlStr, HttpMethod.POST, headers);
|
||||
req.content(new FormContentProvider(computeBodyFields(body)));
|
||||
return makeRequest(req, false);
|
||||
}
|
||||
|
||||
private Fields computeBodyFields(Map<String, String> body) {
|
||||
Fields fields = new Fields();
|
||||
for(Map.Entry<String, String> 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<String, String> 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<String, String> entry : headers.entrySet()) {
|
||||
req.header(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
req.timeout(requestTimeout, TimeUnit.MILLISECONDS);
|
||||
return req;
|
||||
}
|
||||
|
||||
public HttpUsage getHttpUsage() {
|
||||
return httpUsage;
|
||||
}
|
||||
}
|
|
@ -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<HttpUsage, JettyHttpClient> 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<HttpProxy> 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<Exception> 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<Exception> 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<HttpProxy> 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;
|
||||
}
|
||||
}
|
|
@ -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 <T> Future<T> runAsync(Callable<T> callable) {
|
||||
return executorService.submit(callable);
|
||||
}
|
||||
}
|
|
@ -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.
|
||||
*
|
||||
* <p>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 <a
|
||||
* href="https://datatracker.ietf.org/doc/html/rfc1928">RFC 1928</a>.
|
||||
*/
|
||||
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 <a
|
||||
* href="https://datatracker.ietf.org/doc/html/rfc1929">RFC 1929</a>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 <a
|
||||
* href="https://datatracker.ietf.org/doc/html/rfc1928">RFC 1928</a>.
|
||||
*
|
||||
* <p>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<Byte, Socks5.Authentication.Factory> 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.
|
||||
*
|
||||
* <p>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<String, Object> context;
|
||||
private final Map<Byte, Socks5.Authentication.Factory> authentications;
|
||||
private State state = State.HANDSHAKE;
|
||||
|
||||
private Socks5ProxyConnection(EndPoint endPoint, Executor executor, ClientConnectionFactory connectionFactory, Map<String, Object> context, Map<Byte, Socks5.Authentication.Factory> 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<Connection> promise =
|
||||
(Promise<Connection>)
|
||||
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<String, Object> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -58,14 +58,6 @@ public class PayNym {
|
|||
return followers;
|
||||
}
|
||||
|
||||
public boolean isCollaborativeSend() {
|
||||
return collaborativeSend;
|
||||
}
|
||||
|
||||
public void setCollaborativeSend(boolean collaborativeSend) {
|
||||
this.collaborativeSend = collaborativeSend;
|
||||
}
|
||||
|
||||
public List<ScriptType> getScriptTypes() {
|
||||
return segwit ? getSegwitScriptTypes() : getV1ScriptTypes();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -9,10 +9,10 @@ import java.io.IOException;
|
|||
|
||||
public class PayNymDialog extends Dialog<PayNym> {
|
||||
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<PayNym> {
|
|||
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<PayNym> {
|
|||
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));
|
||||
});
|
||||
}
|
||||
|
||||
Button sendDirectlyButton = (Button)dialogPane.lookupButton(sendDirectlyButtonType);
|
||||
sendDirectlyButton.setDisable(true);
|
||||
sendDirectlyButton.setDefaultButton(true);
|
||||
|
@ -66,7 +53,7 @@ public class PayNymDialog extends Dialog<PayNym> {
|
|||
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<PayNym> {
|
|||
});
|
||||
|
||||
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;
|
||||
|
|
|
@ -155,10 +155,6 @@ public class PayNymService {
|
|||
.map(o -> o.get());
|
||||
}
|
||||
|
||||
public static Observable<Map<String, Object>> followPaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode paymentCode, String authToken, String signature) {
|
||||
return followPaymentCode(PaymentCode.fromString(paymentCode.toString()), authToken, signature);
|
||||
}
|
||||
|
||||
public static Observable<Map<String, Object>> followPaymentCode(PaymentCode paymentCode, String authToken, String signature) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("content-type", "application/json");
|
||||
|
|
|
@ -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<Wallet> 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<Boolean> meetingReceived = new SimpleObjectProperty<>(null);
|
||||
|
||||
private final ObjectProperty<Boolean> meetingAccepted = new SimpleObjectProperty<>(null);
|
||||
|
||||
private final ObjectProperty<Transaction> 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<SorobanRequestMessage> receiveMeetingService = new Service<>() {
|
||||
@Override
|
||||
protected Task<SorobanRequestMessage> 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<BlockTransactionHashIndex, WalletNode> walletUtxos = wallet.getSpendableUtxos();
|
||||
for(Map.Entry<BlockTransactionHashIndex, WalletNode> 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<OnlineCahootsMessage> 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<Cahoots> cahootsService = new Service<>() {
|
||||
@Override
|
||||
protected Task<Cahoots> 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<ButtonType> 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<Boolean> meetingReceivedProperty() {
|
||||
return meetingReceived;
|
||||
}
|
||||
|
||||
public ObjectProperty<Boolean> meetingAcceptedProperty() {
|
||||
return meetingAccepted;
|
||||
}
|
||||
|
||||
public ObjectProperty<Transaction> transactionProperty() {
|
||||
return transactionProperty;
|
||||
}
|
||||
}
|
|
@ -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<Boolean> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<PayNym> 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<PaymentCode> counterpartyPaymentCode = new SimpleObjectProperty<>(null);
|
||||
|
||||
private final ObjectProperty<Step> stepProperty = new SimpleObjectProperty<>(Step.SETUP);
|
||||
|
||||
private final ObjectProperty<Boolean> transactionAccepted = new SimpleObjectProperty<>(null);
|
||||
|
||||
private final ObjectProperty<Transaction> transactionProperty = new SimpleObjectProperty<>(null);
|
||||
|
||||
private CahootsType cahootsType = CahootsType.STONEWALLX2;
|
||||
|
||||
private ElectrumServer.TransactionMempoolService transactionMempoolService;
|
||||
|
||||
private boolean closed;
|
||||
|
||||
private final ChangeListener<String> 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<TextFormatter.Change> 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<PayNym> 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<SecureString> 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<ButtonType> 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<BlockTransactionHashIndex, WalletNode> firstSetUtxos = walletTransaction.isCoinControlUsed() ? walletTransaction.getSelectedUtxoSets().get(0) : wallet.getSpendableUtxos();
|
||||
for(Map.Entry<BlockTransactionHashIndex, WalletNode> 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<Cahoots> cahootsService = new Service<>() {
|
||||
@Override
|
||||
protected Task<Cahoots> 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<BlockTransactionHashIndex, WalletNode> selectedUtxos = new HashMap<>();
|
||||
Map<BlockTransactionHashIndex, WalletNode> walletTxos = wallet.getWalletTxos();
|
||||
for(TransactionInput txInput : getTransaction().getInputs()) {
|
||||
Optional<BlockTransactionHashIndex> 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<String> 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<PaymentCode> 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<PayNym> 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<PaymentCode> counterpartyPaymentCodeProperty() {
|
||||
return counterpartyPaymentCode;
|
||||
}
|
||||
|
||||
public ObjectProperty<Step> 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<Boolean> 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
|
||||
}
|
||||
}
|
|
@ -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<Transaction> {
|
||||
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<SecureString> 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<ButtonType> 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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<Network> 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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<Sha256Hash> references = txWalletTransaction.getSelectedUtxoSets().get(1).keySet().stream().map(BlockTransactionHash::getHash).collect(Collectors.toSet());
|
||||
ElectrumServer.TransactionReferenceService transactionReferenceService = new ElectrumServer.TransactionReferenceService(references);
|
||||
transactionReferenceService.setOnSucceeded(successEvent -> {
|
||||
Map<Sha256Hash, BlockTransaction> 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<Sha256Hash, BlockTransaction> inputTransactions) {
|
||||
Map<BlockTransactionHashIndex, WalletNode> allWalletUtxos = wallet.getWalletTxos();
|
||||
Map<BlockTransactionHashIndex, WalletNode> walletUtxos = new LinkedHashMap<>();
|
||||
Map<BlockTransactionHashIndex, WalletNode> externalUtxos = new LinkedHashMap<>();
|
||||
|
||||
for(TransactionInput txInput : transaction.getInputs()) {
|
||||
Optional<BlockTransactionHashIndex> 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<Map<BlockTransactionHashIndex, WalletNode>> selectedUtxoSets = new ArrayList<>();
|
||||
selectedUtxoSets.add(walletUtxos);
|
||||
selectedUtxoSets.add(externalUtxos);
|
||||
|
||||
Map<Address, WalletNode> walletAddresses = wallet.getWalletAddresses();
|
||||
List<Payment> payments = new ArrayList<>();
|
||||
Map<WalletNode, Long> changeMap = new LinkedHashMap<>();
|
||||
for(TransactionOutput txOutput : transaction.getOutputs()) {
|
||||
Address address = txOutput.getScript().getToAddress();
|
||||
if(address != null) {
|
||||
Optional<Payment> 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<Map<BlockTransactionHashIndex, WalletNode>> selectedUtxoSets, Transaction transaction) {
|
||||
Map<BlockTransactionHashIndex, WalletNode> 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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<String, Soroban> sorobanMap = new HashMap<>();
|
||||
|
||||
public Soroban getSoroban(Wallet wallet) {
|
||||
Wallet masterWallet = wallet.isMasterWallet() ? wallet : wallet.getMasterWallet();
|
||||
for(Map.Entry<Wallet, Storage> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<CahootsUtxo> utxos;
|
||||
private final Map<KeyPurpose, WalletNode> 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<CahootsUtxo> 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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<FeePriority> 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<UtxoEntry> utxoEntries;
|
||||
|
||||
private final TextBox scode;
|
||||
private final ComboBox<FeePriority> premixPriority;
|
||||
private final Label premixFeeRate;
|
||||
|
||||
private Pool mixPool;
|
||||
|
||||
public MixDialog(String walletId, WalletForm walletForm, List<UtxoEntry> 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<UtxoEntry> utxoEntries;
|
||||
private final Tx0FeeTarget tx0FeeTarget;
|
||||
|
||||
private final ComboBox<DisplayPool> pool;
|
||||
private final Label poolFeeLabel;
|
||||
private final Label poolFee;
|
||||
private final Label premixOutputs;
|
||||
private final Button broadcast;
|
||||
|
||||
private Tx0Previews tx0Previews;
|
||||
private final ObjectProperty<Tx0Preview> tx0PreviewProperty = new SimpleObjectProperty<>(null);
|
||||
private Pool mixPool;
|
||||
|
||||
public MixPoolDialog(String walletId, WalletForm walletForm, List<UtxoEntry> 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<Pool> 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<ButtonType> 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";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<DisplayWallet> mixToWallet;
|
||||
private final TextBox minimumMixes;
|
||||
private final ComboBox<DisplayIndexRange> 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<DisplayWallet> allWallets = new ArrayList<>();
|
||||
allWallets.add(NONE_DISPLAY_WALLET);
|
||||
List<Wallet> 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<DisplayIndexRange> 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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<TableCell> utxos;
|
||||
|
||||
private Button startMix;
|
||||
private Button mixTo;
|
||||
private Button mixSelected;
|
||||
|
||||
private final ChangeListener<Boolean> mixingOnlineListener = (observable, oldValue, newValue) -> {
|
||||
SparrowTerminal.get().getGuiThread().invokeLater(() -> startMix.setEnabled(newValue));
|
||||
};
|
||||
|
||||
private final ChangeListener<Boolean> 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<Boolean> 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<Boolean> 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));
|
||||
}
|
||||
|
||||
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<UtxoEntry> 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<BlockTransactionHashIndex> 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<TableCell> 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());
|
||||
|
|
|
@ -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<Wallet> 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));
|
||||
|
|
|
@ -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, ' ');
|
||||
}
|
||||
|
|
|
@ -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<Wallet> mixToWallets;
|
||||
|
||||
@FXML
|
||||
private IntegerSpinner minMixes;
|
||||
|
||||
@FXML
|
||||
private ComboBox<IndexRange> indexRange;
|
||||
|
||||
private MixConfig mixConfig;
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
|
||||
}
|
||||
|
||||
public void initializeView(Wallet wallet) {
|
||||
mixConfig = wallet.getMasterMixConfig().copy();
|
||||
|
||||
List<Wallet> allWallets = new ArrayList<>();
|
||||
allWallets.add(NONE_WALLET);
|
||||
|
||||
List<Wallet> 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;
|
||||
}
|
||||
}
|
|
@ -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<MixConfig> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<PayNym> 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,7 +399,6 @@ public class PaymentController extends WalletFormController implements Initializ
|
|||
return Address.fromString(address.getText());
|
||||
}
|
||||
|
||||
if(!payNym.isCollaborativeSend()) {
|
||||
try {
|
||||
Wallet recipientBip47Wallet = getWalletForPayNym(payNym);
|
||||
if(recipientBip47Wallet != null) {
|
||||
|
@ -434,17 +408,13 @@ public class PaymentController extends WalletFormController implements Initializ
|
|||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<TxoFilter> txoFilterProperty = new SimpleObjectProperty<>(null);
|
||||
|
||||
private final ObjectProperty<Pool> whirlpoolProperty = new SimpleObjectProperty<>(null);
|
||||
|
||||
private final ObjectProperty<PaymentCode> paymentCodeProperty = new SimpleObjectProperty<>(null);
|
||||
|
||||
private final ObjectProperty<WalletTransaction> walletTransactionProperty = new SimpleObjectProperty<>(null);
|
||||
|
@ -234,7 +222,6 @@ public class SendController extends WalletFormController implements Initializabl
|
|||
};
|
||||
|
||||
private final ChangeListener<Boolean> 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<Payment> 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<Payment> payments) {
|
||||
return (utxoSelectorProperty.get() == null || SorobanServices.canWalletMix(walletForm.getWallet()))
|
||||
&& payments.size() == 1
|
||||
private boolean isFakeMixPossible(List<Payment> 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<Payment> 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<BlockTransactionHashIndex, WalletNode> 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<ButtonType> 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<Transaction> 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<Label> analysisLabels = new ArrayList<>();
|
||||
|
||||
|
@ -1685,7 +1571,6 @@ public class SendController extends WalletFormController implements Initializabl
|
|||
List<Payment> userPayments = payments.stream().filter(payment -> payment.getType() != Payment.Type.FAKE_MIX).collect(Collectors.toList());
|
||||
Map<Address, WalletNode> walletAddresses = walletTransaction.getAddressNodeMap();
|
||||
OptimizationStrategy optimizationStrategy = getPreferredOptimizationStrategy();
|
||||
boolean payNymPresent = isPayNymMixOnlyPayment(payments);
|
||||
boolean fakeMixPresent = payments.stream().anyMatch(payment -> payment.getType() == Payment.Type.FAKE_MIX);
|
||||
boolean roundPaymentAmounts = userPayments.stream().anyMatch(payment -> payment.getAmount() % 100 == 0);
|
||||
boolean mixedAddressTypes = userPayments.stream().anyMatch(payment -> payment.getAddress().getScriptType() != getWalletForm().getWallet().getFreshNode(KeyPurpose.RECEIVE).getAddress().getScriptType());
|
||||
|
@ -1693,29 +1578,21 @@ public class SendController extends WalletFormController implements Initializabl
|
|||
boolean payjoinPresent = userPayments.stream().anyMatch(payment -> AppServices.getPayjoinURI(payment.getAddress()) != null);
|
||||
|
||||
if(optimizationStrategy == OptimizationStrategy.PRIVACY) {
|
||||
if(payNymPresent) {
|
||||
addLabel("Appears as a normal transaction, but actual value transferred is hidden", getPlusGlyph());
|
||||
} else if(fakeMixPresent) {
|
||||
if(fakeMixPresent) {
|
||||
addLabel("Appears as a two person coinjoin", getPlusGlyph());
|
||||
} else {
|
||||
if(mixedAddressTypes) {
|
||||
addLabel("Cannot coinjoin due to mixed address types", getInfoGlyph());
|
||||
addLabel("Cannot fake coinjoin due to mixed address types", getInfoGlyph());
|
||||
} else if(userPayments.size() > 1) {
|
||||
addLabel("Cannot coinjoin due to multiple payments", getInfoGlyph());
|
||||
addLabel("Cannot fake coinjoin due to multiple payments", getInfoGlyph());
|
||||
} else if(payjoinPresent) {
|
||||
addLabel("Cannot coinjoin due to payjoin", getInfoGlyph());
|
||||
addLabel("Cannot fake coinjoin due to payjoin", getInfoGlyph());
|
||||
} else {
|
||||
if(utxoSelectorProperty().get() != null) {
|
||||
addLabel("Cannot fake coinjoin due to coin control", getInfoGlyph());
|
||||
} else {
|
||||
addLabel("Cannot fake coinjoin due to insufficient funds", getInfoGlyph());
|
||||
}
|
||||
|
||||
if(!SorobanServices.canWalletMix(getWalletForm().getWallet())) {
|
||||
addLabel("Can only add mix partner to Native Segwit software wallets", getInfoGlyph());
|
||||
} else {
|
||||
addLabel("Add a mix partner to create a two person coinjoin", getInfoGlyph());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1724,7 +1601,7 @@ public class SendController extends WalletFormController implements Initializabl
|
|||
addLabel("Address types different to the wallet indicate external payments", getMinusGlyph());
|
||||
}
|
||||
|
||||
if(roundPaymentAmounts && !fakeMixPresent && !payNymPresent) {
|
||||
if(roundPaymentAmounts && !fakeMixPresent) {
|
||||
addLabel("Rounded payment amounts indicate external payments", getMinusGlyph());
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,6 @@ import com.sparrowwallet.sparrow.io.Storage;
|
|||
import com.sparrowwallet.sparrow.io.StorageException;
|
||||
import com.sparrowwallet.sparrow.net.ElectrumServer;
|
||||
import com.sparrowwallet.sparrow.net.ServerType;
|
||||
import com.sparrowwallet.sparrow.whirlpool.WhirlpoolServices;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.SimpleIntegerProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
|
@ -737,7 +736,7 @@ public class SettingsController extends WalletFormController implements Initiali
|
|||
private void addAndSaveAccount(Wallet masterWallet, StandardAccount standardAccount, Key key) {
|
||||
List<Wallet> childWallets;
|
||||
if(standardAccount == StandardAccount.WHIRLPOOL_PREMIX) {
|
||||
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));
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
package com.sparrowwallet.sparrow.wallet;
|
||||
|
||||
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.drongo.KeyPurpose;
|
||||
import com.sparrowwallet.drongo.address.Address;
|
||||
import com.sparrowwallet.drongo.protocol.TransactionInput;
|
||||
import com.sparrowwallet.drongo.wallet.*;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
import com.sparrowwallet.sparrow.whirlpool.Whirlpool;
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class UtxoEntry extends HashIndexEntry {
|
||||
private final WalletNode node;
|
||||
|
||||
|
@ -47,10 +47,6 @@ public class UtxoEntry extends HashIndexEntry {
|
|||
return Function.UTXOS;
|
||||
}
|
||||
|
||||
public boolean isMixing() {
|
||||
return mixStatusProperty != null && ((mixStatusProperty.get().getMixProgress() != null && mixStatusProperty.get().getMixProgress().getMixStep() != MixStep.FAIL) || mixStatusProperty.get().getNextMixUtxo() != null);
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return node.getAddress();
|
||||
}
|
||||
|
@ -121,51 +117,19 @@ public class UtxoEntry extends HashIndexEntry {
|
|||
*/
|
||||
private ObjectProperty<MixStatus> mixStatusProperty;
|
||||
|
||||
public void setMixProgress(MixProgress mixProgress) {
|
||||
mixStatusProperty().set(new MixStatus(mixProgress));
|
||||
}
|
||||
|
||||
public void setMixFailReason(MixFailReason mixFailReason, String mixError) {
|
||||
mixStatusProperty().set(new MixStatus(mixFailReason, mixError));
|
||||
}
|
||||
|
||||
public void setNextMixUtxo(Utxo nextMixUtxo) {
|
||||
mixStatusProperty().set(new MixStatus(nextMixUtxo));
|
||||
}
|
||||
|
||||
public final MixStatus getMixStatus() {
|
||||
return mixStatusProperty == null ? null : mixStatusProperty.get();
|
||||
}
|
||||
|
||||
public final ObjectProperty<MixStatus> mixStatusProperty() {
|
||||
if(mixStatusProperty == null) {
|
||||
mixStatusProperty = new SimpleObjectProperty<>(UtxoEntry.this, "mixStatus", null);
|
||||
mixStatusProperty = new SimpleObjectProperty<>(UtxoEntry.this, "mixStatus", new MixStatus());
|
||||
}
|
||||
|
||||
return mixStatusProperty;
|
||||
}
|
||||
|
||||
public class MixStatus {
|
||||
private MixProgress mixProgress;
|
||||
private Utxo nextMixUtxo;
|
||||
private MixFailReason mixFailReason;
|
||||
private String mixError;
|
||||
private Long mixErrorTimestamp;
|
||||
|
||||
public MixStatus(MixProgress mixProgress) {
|
||||
this.mixProgress = mixProgress;
|
||||
}
|
||||
|
||||
public MixStatus(Utxo nextMixUtxo) {
|
||||
this.nextMixUtxo = nextMixUtxo;
|
||||
}
|
||||
|
||||
public MixStatus(MixFailReason mixFailReason, String mixError) {
|
||||
this.mixFailReason = mixFailReason;
|
||||
this.mixError = mixError;
|
||||
this.mixErrorTimestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public UtxoEntry getUtxoEntry() {
|
||||
return UtxoEntry.this;
|
||||
}
|
||||
|
@ -177,38 +141,37 @@ public class UtxoEntry extends HashIndexEntry {
|
|||
}
|
||||
|
||||
//Mix data not available - recount (and store if WhirlpoolWallet is running)
|
||||
Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(wallet);
|
||||
if(whirlpool != null && getUtxoEntry().getWallet().getStandardAccountType() == StandardAccount.WHIRLPOOL_POSTMIX && node.getKeyPurpose() == KeyPurpose.RECEIVE) {
|
||||
int mixesDone = whirlpool.recountMixesDone(getUtxoEntry().getWallet(), getHashIndex());
|
||||
whirlpool.setMixesDone(getHashIndex(), mixesDone);
|
||||
if(getUtxoEntry().getWallet().getStandardAccountType() == StandardAccount.WHIRLPOOL_POSTMIX && node.getKeyPurpose() == KeyPurpose.RECEIVE) {
|
||||
int mixesDone = recountMixesDone(getUtxoEntry().getWallet(), getHashIndex());
|
||||
return new UtxoMixData(mixesDone, null);
|
||||
}
|
||||
|
||||
return new UtxoMixData(getUtxoEntry().getWallet().getStandardAccountType() == StandardAccount.WHIRLPOOL_POSTMIX ? 1 : 0, null);
|
||||
}
|
||||
|
||||
public int recountMixesDone(Wallet postmixWallet, BlockTransactionHashIndex postmixUtxo) {
|
||||
int mixesDone = 0;
|
||||
Set<BlockTransactionHashIndex> walletTxos = postmixWallet.getWalletTxos().entrySet().stream()
|
||||
.filter(entry -> entry.getValue().getKeyPurpose() == KeyPurpose.RECEIVE).map(Map.Entry::getKey).collect(Collectors.toSet());
|
||||
BlockTransaction blkTx = postmixWallet.getTransactions().get(postmixUtxo.getHash());
|
||||
|
||||
while(blkTx != null) {
|
||||
mixesDone++;
|
||||
List<TransactionInput> inputs = blkTx.getTransaction().getInputs();
|
||||
blkTx = null;
|
||||
for(TransactionInput txInput : inputs) {
|
||||
BlockTransaction inputTx = postmixWallet.getTransactions().get(txInput.getOutpoint().getHash());
|
||||
if(inputTx != null && walletTxos.stream().anyMatch(txo -> txo.getHash().equals(inputTx.getHash()) && txo.getIndex() == txInput.getOutpoint().getIndex()) && inputTx.getTransaction() != null) {
|
||||
blkTx = inputTx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mixesDone;
|
||||
}
|
||||
|
||||
public int getMixesDone() {
|
||||
return getUtxoMixData().getMixesDone();
|
||||
}
|
||||
|
||||
public MixProgress getMixProgress() {
|
||||
return mixProgress;
|
||||
}
|
||||
|
||||
public Utxo getNextMixUtxo() {
|
||||
return nextMixUtxo;
|
||||
}
|
||||
|
||||
public MixFailReason getMixFailReason() {
|
||||
return mixFailReason;
|
||||
}
|
||||
|
||||
public String getMixError() {
|
||||
return mixError;
|
||||
}
|
||||
|
||||
public Long getMixErrorTimestamp() {
|
||||
return mixErrorTimestamp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,13 +2,7 @@ package com.sparrowwallet.sparrow.wallet;
|
|||
|
||||
import com.csvreader.CsvWriter;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.samourai.whirlpool.client.tx0.Tx0Preview;
|
||||
import com.sparrowwallet.drongo.BitcoinUnit;
|
||||
import com.sparrowwallet.drongo.KeyPurpose;
|
||||
import com.sparrowwallet.drongo.SecureString;
|
||||
import com.sparrowwallet.drongo.address.Address;
|
||||
import com.sparrowwallet.drongo.address.InvalidAddressException;
|
||||
import com.sparrowwallet.drongo.crypto.*;
|
||||
import com.sparrowwallet.drongo.wallet.*;
|
||||
import com.sparrowwallet.sparrow.UnitFormat;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
|
@ -17,24 +11,16 @@ import com.sparrowwallet.sparrow.control.*;
|
|||
import com.sparrowwallet.sparrow.event.*;
|
||||
import com.sparrowwallet.sparrow.glyphfont.FontAwesome5;
|
||||
import com.sparrowwallet.sparrow.io.Config;
|
||||
import com.sparrowwallet.sparrow.io.Storage;
|
||||
import com.sparrowwallet.sparrow.io.WalletTransactions;
|
||||
import com.sparrowwallet.sparrow.net.ExchangeSource;
|
||||
import com.sparrowwallet.sparrow.whirlpool.Whirlpool;
|
||||
import com.sparrowwallet.sparrow.whirlpool.WhirlpoolDialog;
|
||||
import com.sparrowwallet.sparrow.whirlpool.WhirlpoolServices;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.WeakChangeListener;
|
||||
import javafx.collections.ListChangeListener;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ButtonType;
|
||||
import javafx.scene.control.Tooltip;
|
||||
import javafx.scene.control.TreeItem;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.stage.Stage;
|
||||
import org.controlsfx.glyphfont.Glyph;
|
||||
|
@ -49,8 +35,6 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.sparrowwallet.sparrow.AppServices.showErrorDialog;
|
||||
|
||||
public class UtxosController extends WalletFormController implements Initializable {
|
||||
private static final Logger log = LoggerFactory.getLogger(UtxosController.class);
|
||||
|
||||
|
@ -72,18 +56,6 @@ public class UtxosController extends WalletFormController implements Initializab
|
|||
@FXML
|
||||
private UtxosTreeTable utxosTable;
|
||||
|
||||
@FXML
|
||||
private HBox mixButtonsBox;
|
||||
|
||||
@FXML
|
||||
private Button startMix;
|
||||
|
||||
@FXML
|
||||
private Button stopMix;
|
||||
|
||||
@FXML
|
||||
private Button mixTo;
|
||||
|
||||
@FXML
|
||||
private Button selectAll;
|
||||
|
||||
|
@ -93,44 +65,9 @@ public class UtxosController extends WalletFormController implements Initializab
|
|||
@FXML
|
||||
private Button sendSelected;
|
||||
|
||||
@FXML
|
||||
private Button mixSelected;
|
||||
|
||||
@FXML
|
||||
private UtxosChart utxosChart;
|
||||
|
||||
private final ChangeListener<Boolean> mixingOnlineListener = (observable, oldValue, newValue) -> {
|
||||
mixSelected.setDisable(getSelectedEntries().isEmpty() || !newValue);
|
||||
startMix.setDisable(!newValue);
|
||||
stopMix.setDisable(!newValue);
|
||||
};
|
||||
|
||||
private final ChangeListener<Boolean> mixingStartingListener = (observable, oldValue, newValue) -> {
|
||||
startMix.setDisable(newValue || !AppServices.onlineProperty().get());
|
||||
Platform.runLater(() -> startMix.setText(newValue && AppServices.onlineProperty().get() ? "Starting Mixing..." : "Start Mixing"));
|
||||
mixTo.setDisable(newValue);
|
||||
};
|
||||
|
||||
private final ChangeListener<Boolean> mixingStoppingListener = (observable, oldValue, newValue) -> {
|
||||
startMix.setDisable(newValue || !AppServices.onlineProperty().get());
|
||||
Platform.runLater(() -> startMix.setText(newValue ? "Stopping Mixing..." : "Start Mixing"));
|
||||
mixTo.setDisable(newValue);
|
||||
};
|
||||
|
||||
private final ChangeListener<Boolean> 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()) {
|
||||
utxoEntry.setMixProgress(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
EventManager.get().register(this);
|
||||
|
@ -150,47 +87,9 @@ public class UtxosController extends WalletFormController implements Initializab
|
|||
utxosTable.initialize(walletUtxosEntry);
|
||||
utxosChart.initialize(walletUtxosEntry);
|
||||
|
||||
mixButtonsBox.managedProperty().bind(mixButtonsBox.visibleProperty());
|
||||
mixButtonsBox.setVisible(getWalletForm().getWallet().isWhirlpoolMixWallet());
|
||||
startMix.managedProperty().bind(startMix.visibleProperty());
|
||||
startMix.setDisable(!AppServices.isConnected());
|
||||
stopMix.managedProperty().bind(stopMix.visibleProperty());
|
||||
startMix.visibleProperty().bind(stopMix.visibleProperty().not());
|
||||
stopMix.visibleProperty().addListener((observable, oldValue, newValue) -> {
|
||||
stopMix.setDisable(!newValue);
|
||||
});
|
||||
mixTo.managedProperty().bind(mixTo.visibleProperty());
|
||||
mixTo.setVisible(getWalletForm().getWallet().getStandardAccountType() == StandardAccount.WHIRLPOOL_POSTMIX);
|
||||
|
||||
if(mixButtonsBox.isVisible()) {
|
||||
Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWalletForm().getWallet());
|
||||
if(whirlpool != null) {
|
||||
stopMix.visibleProperty().bind(whirlpool.mixingProperty());
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
selectAll.managedProperty().bind(selectAll.visibleProperty());
|
||||
selectAll.setVisible(getWalletForm().getWallet().getStandardAccountType() != StandardAccount.WHIRLPOOL_POSTMIX);
|
||||
clear.managedProperty().bind(clear.visibleProperty());
|
||||
clear.setVisible(getWalletForm().getWallet().getStandardAccountType() != StandardAccount.WHIRLPOOL_POSTMIX);
|
||||
|
||||
clear.setDisable(true);
|
||||
sendSelected.setDisable(true);
|
||||
sendSelected.setTooltip(new Tooltip("Send selected UTXOs. Use " + (org.controlsfx.tools.Platform.getCurrent() == org.controlsfx.tools.Platform.OSX ? "Cmd" : "Ctrl") + "+click to select multiple." ));
|
||||
mixSelected.managedProperty().bind(mixSelected.visibleProperty());
|
||||
mixSelected.setVisible(canWalletMix());
|
||||
mixSelected.setDisable(true);
|
||||
AppServices.onlineProperty().addListener(new WeakChangeListener<>(mixingOnlineListener));
|
||||
|
||||
utxosTable.getSelectionModel().getSelectedIndices().addListener((ListChangeListener<Integer>) c -> {
|
||||
List<Entry> selectedEntries = utxosTable.getSelectionModel().getSelectedCells().stream().filter(tp -> tp.getTreeItem() != null).map(tp -> tp.getTreeItem().getValue()).collect(Collectors.toList());
|
||||
|
@ -212,17 +111,12 @@ public class UtxosController extends WalletFormController implements Initializab
|
|||
utxoCount.setText((selectedCount > 0 ? selectedCount + "/" : "") + (walletUtxosEntry.getChildren() != null ? Integer.toString(walletUtxosEntry.getChildren().size()) : "0"));
|
||||
}
|
||||
|
||||
private boolean canWalletMix() {
|
||||
return WhirlpoolServices.canWalletMix(getWalletForm().getWallet());
|
||||
}
|
||||
|
||||
private void updateButtons(UnitFormat format, BitcoinUnit unit) {
|
||||
List<Entry> selectedEntries = getSelectedEntries();
|
||||
|
||||
selectAll.setDisable(utxosTable.getRoot().getChildren().size() == utxosTable.getSelectionModel().getSelectedCells().size());
|
||||
clear.setDisable(selectedEntries.isEmpty());
|
||||
sendSelected.setDisable(selectedEntries.isEmpty());
|
||||
mixSelected.setDisable(selectedEntries.isEmpty() || !AppServices.isConnected());
|
||||
|
||||
long selectedTotal = selectedEntries.stream().mapToLong(Entry::getValue).sum();
|
||||
if(selectedTotal > 0) {
|
||||
|
@ -236,35 +130,11 @@ public class UtxosController extends WalletFormController implements Initializab
|
|||
|
||||
if(unit.equals(BitcoinUnit.BTC)) {
|
||||
sendSelected.setText("Send Selected (" + format.formatBtcValue(selectedTotal) + " BTC)");
|
||||
mixSelected.setText("Mix Selected (" + format.formatBtcValue(selectedTotal) + " BTC)");
|
||||
} else {
|
||||
sendSelected.setText("Send Selected (" + format.formatSatsValue(selectedTotal) + " sats)");
|
||||
mixSelected.setText("Mix Selected (" + format.formatSatsValue(selectedTotal) + " sats)");
|
||||
}
|
||||
} else {
|
||||
sendSelected.setText("Send Selected");
|
||||
mixSelected.setText("Mix Selected");
|
||||
}
|
||||
}
|
||||
|
||||
private void updateMixToButton() {
|
||||
MixConfig mixConfig = getWalletForm().getWallet().getMasterMixConfig();
|
||||
if(mixConfig != null && mixConfig.getMixToWalletName() != null) {
|
||||
mixTo.setText("Mixing to " + mixConfig.getMixToWalletName());
|
||||
try {
|
||||
String mixToWalletId = AppServices.getWhirlpoolServices().getWhirlpoolMixToWalletId(mixConfig);
|
||||
String mixToName = AppServices.get().getWallet(mixToWalletId).getFullDisplayName();
|
||||
mixTo.setText("Mixing to " + mixToName);
|
||||
mixTo.setGraphic(getExternalGlyph());
|
||||
mixTo.setTooltip(new Tooltip("Mixing to " + mixToName + " after at least " + (mixConfig.getMinMixes() == null ? Whirlpool.DEFAULT_MIXTO_MIN_MIXES : mixConfig.getMinMixes()) + " mixes"));
|
||||
} catch(NoSuchElementException e) {
|
||||
mixTo.setGraphic(getErrorGlyph());
|
||||
mixTo.setTooltip(new Tooltip(mixConfig.getMixToWalletName() + " is not open - open this wallet to mix to it!"));
|
||||
}
|
||||
} else {
|
||||
mixTo.setText("Mix to...");
|
||||
mixTo.setGraphic(null);
|
||||
mixTo.setTooltip(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -283,116 +153,6 @@ public class UtxosController extends WalletFormController implements Initializab
|
|||
Platform.runLater(() -> EventManager.get().post(new SpendUtxoEvent(getWalletForm().getWallet(), spendingUtxos)));
|
||||
}
|
||||
|
||||
public void mixSelected(ActionEvent event) {
|
||||
if(!getWalletForm().getWallet().isMasterWallet() && getWalletForm().getWallet().getStandardAccountType() == StandardAccount.ACCOUNT_0) {
|
||||
showErrorDialog("Invalid Whirlpool wallet", "Create a new wallet with Account #0 as the first account.");
|
||||
return;
|
||||
}
|
||||
|
||||
List<UtxoEntry> selectedEntries = getSelectedUtxos();
|
||||
WhirlpoolDialog whirlpoolDialog = new WhirlpoolDialog(getWalletForm().getMasterWalletId(), getWalletForm().getWallet(), selectedEntries);
|
||||
whirlpoolDialog.initOwner(utxosTable.getScene().getWindow());
|
||||
Optional<Tx0Preview> optTx0Preview = whirlpoolDialog.showAndWait();
|
||||
optTx0Preview.ifPresent(tx0Preview -> previewPremix(tx0Preview, selectedEntries));
|
||||
}
|
||||
|
||||
public void previewPremix(Tx0Preview tx0Preview, List<UtxoEntry> utxoEntries) {
|
||||
Wallet wallet = getWalletForm().getWallet();
|
||||
String walletId = walletForm.getWalletId();
|
||||
|
||||
if(wallet.isMasterWallet() && !wallet.isWhirlpoolMasterWallet() && wallet.isEncrypted()) {
|
||||
WalletPasswordDialog dlg = new WalletPasswordDialog(wallet.getMasterName(), WalletPasswordDialog.PasswordRequirement.LOAD);
|
||||
dlg.initOwner(utxosTable.getScene().getWindow());
|
||||
Optional<SecureString> password = dlg.showAndWait();
|
||||
if(password.isPresent()) {
|
||||
Storage.KeyDerivationService keyDerivationService = new Storage.KeyDerivationService(walletForm.getStorage(), 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(), walletForm.getStorage().getKeyDeriver().getSalt(), EncryptionType.Deriver.ARGON2);
|
||||
wallet.decrypt(key);
|
||||
|
||||
try {
|
||||
prepareWhirlpoolWallet(wallet);
|
||||
} finally {
|
||||
wallet.encrypt(key);
|
||||
for(Wallet childWallet : wallet.getChildWallets()) {
|
||||
if(!childWallet.isNested() && !childWallet.isEncrypted()) {
|
||||
childWallet.encrypt(key);
|
||||
}
|
||||
}
|
||||
key.clear();
|
||||
encryptionFullKey.clear();
|
||||
password.get().clear();
|
||||
}
|
||||
|
||||
previewPremix(wallet, tx0Preview, utxoEntries);
|
||||
});
|
||||
keyDerivationService.setOnFailed(workerStateEvent -> {
|
||||
EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.END, "Failed"));
|
||||
if(keyDerivationService.getException() instanceof InvalidPasswordException) {
|
||||
Optional<ButtonType> 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(() -> previewPremix(tx0Preview, utxoEntries));
|
||||
}
|
||||
} else {
|
||||
log.error("Error deriving wallet key", keyDerivationService.getException());
|
||||
}
|
||||
});
|
||||
EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.START, "Decrypting wallet..."));
|
||||
keyDerivationService.start();
|
||||
}
|
||||
} else {
|
||||
if(wallet.isMasterWallet() && !wallet.isWhirlpoolMasterWallet()) {
|
||||
prepareWhirlpoolWallet(wallet);
|
||||
}
|
||||
|
||||
previewPremix(wallet, tx0Preview, utxoEntries);
|
||||
}
|
||||
}
|
||||
|
||||
private void prepareWhirlpoolWallet(Wallet decryptedWallet) {
|
||||
WhirlpoolServices.prepareWhirlpoolWallet(decryptedWallet, getWalletForm().getWalletId(), getWalletForm().getStorage());
|
||||
}
|
||||
|
||||
private void previewPremix(Wallet wallet, Tx0Preview tx0Preview, List<UtxoEntry> utxoEntries) {
|
||||
Wallet masterWallet = wallet.isMasterWallet() ? wallet : wallet.getMasterWallet();
|
||||
|
||||
Wallet premixWallet = masterWallet.getChildWallet(StandardAccount.WHIRLPOOL_PREMIX);
|
||||
Wallet badbankWallet = masterWallet.getChildWallet(StandardAccount.WHIRLPOOL_BADBANK);
|
||||
|
||||
List<Payment> payments = new ArrayList<>();
|
||||
if(tx0Preview.getTx0Data().getFeeAddress() != null) {
|
||||
try {
|
||||
Address whirlpoolFeeAddress = Address.fromString(tx0Preview.getTx0Data().getFeeAddress());
|
||||
Payment whirlpoolFeePayment = new Payment(whirlpoolFeeAddress, "Whirlpool Fee", tx0Preview.getFeeValue(), false);
|
||||
whirlpoolFeePayment.setType(Payment.Type.WHIRLPOOL_FEE);
|
||||
payments.add(whirlpoolFeePayment);
|
||||
} catch(InvalidAddressException e) {
|
||||
throw new IllegalStateException("Cannot parse whirlpool fee address " + tx0Preview.getTx0Data().getFeeAddress(), e);
|
||||
}
|
||||
}
|
||||
|
||||
WalletNode badbankNode = badbankWallet.getFreshNode(KeyPurpose.RECEIVE);
|
||||
Payment changePayment = new Payment(badbankNode.getAddress(), "Badbank Change", tx0Preview.getChangeValue(), false);
|
||||
payments.add(changePayment);
|
||||
|
||||
WalletNode premixNode = null;
|
||||
for(int i = 0; i < tx0Preview.getNbPremix(); i++) {
|
||||
premixNode = premixWallet.getFreshNode(KeyPurpose.RECEIVE, premixNode);
|
||||
Address premixAddress = premixNode.getAddress();
|
||||
payments.add(new Payment(premixAddress, "Premix #" + i, tx0Preview.getPremixValue(), false));
|
||||
}
|
||||
|
||||
List<byte[]> opReturns = List.of(new byte[64]);
|
||||
|
||||
final List<BlockTransactionHashIndex> utxos = utxoEntries.stream().map(HashIndexEntry::getHashIndex).collect(Collectors.toList());
|
||||
Platform.runLater(() -> {
|
||||
EventManager.get().post(new SendActionEvent(getWalletForm().getWallet(), utxos));
|
||||
Platform.runLater(() -> EventManager.get().post(new SpendUtxoEvent(getWalletForm().getWallet(), utxos, payments, opReturns, tx0Preview.getTx0MinerFee(), tx0Preview.getPool())));
|
||||
});
|
||||
}
|
||||
|
||||
private List<UtxoEntry> getSelectedUtxos() {
|
||||
return utxosTable.getSelectionModel().getSelectedCells().stream()
|
||||
.map(tp -> tp.getTreeItem().getValue())
|
||||
|
@ -410,69 +170,6 @@ public class UtxosController extends WalletFormController implements Initializab
|
|||
utxosTable.getSelectionModel().clearSelection();
|
||||
}
|
||||
|
||||
public void startMixing(ActionEvent event) {
|
||||
startMix.setDisable(true);
|
||||
stopMix.setDisable(false);
|
||||
|
||||
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(ActionEvent event) {
|
||||
stopMix.setDisable(true);
|
||||
startMix.setDisable(!AppServices.onlineProperty().get());
|
||||
|
||||
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(ActionEvent event) {
|
||||
MixToDialog mixToDialog = new MixToDialog(getWalletForm().getWallet());
|
||||
mixToDialog.initOwner(mixTo.getScene().getWindow());
|
||||
Optional<MixConfig> optMixConfig = mixToDialog.showAndWait();
|
||||
if(optMixConfig.isPresent()) {
|
||||
MixConfig changedMixConfig = optMixConfig.get();
|
||||
MixConfig mixConfig = getWalletForm().getWallet().getMasterMixConfig();
|
||||
|
||||
mixConfig.setMixToWalletName(changedMixConfig.getMixToWalletName());
|
||||
mixConfig.setMixToWalletFile(changedMixConfig.getMixToWalletFile());
|
||||
mixConfig.setMinMixes(changedMixConfig.getMinMixes());
|
||||
mixConfig.setIndexRange(changedMixConfig.getIndexRange());
|
||||
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);
|
||||
}
|
||||
|
||||
updateMixToButton();
|
||||
if(whirlpool.isStarted()) {
|
||||
//Will automatically restart
|
||||
AppServices.getWhirlpoolServices().stopWhirlpool(whirlpool, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void exportUtxos(ActionEvent event) {
|
||||
Stage window = new Stage();
|
||||
|
||||
|
@ -528,7 +225,6 @@ public class UtxosController extends WalletFormController implements Initializab
|
|||
updateFields(walletUtxosEntry);
|
||||
utxosTable.updateAll(walletUtxosEntry);
|
||||
utxosChart.update(walletUtxosEntry);
|
||||
mixSelected.setVisible(canWalletMix());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -578,11 +274,6 @@ public class UtxosController extends WalletFormController implements Initializab
|
|||
utxosTable.updateHistoryStatus(event);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void newBlock(NewBlockEvent event) {
|
||||
getWalletForm().getWalletUtxosEntry().updateMixProgress();
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void cormorantStatus(CormorantStatusEvent event) {
|
||||
if(event.isFor(walletForm.getWallet())) {
|
||||
|
@ -628,35 +319,6 @@ public class UtxosController extends WalletFormController implements Initializab
|
|||
utxosChart.setVisible(event.isVisible() && !getWalletForm().getWallet().isWhirlpoolMixWallet());
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void openWallets(OpenWalletsEvent event) {
|
||||
Platform.runLater(this::updateMixToButton);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void walletLabelChanged(WalletLabelChangedEvent event) {
|
||||
Platform.runLater(this::updateMixToButton);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void whirlpoolMix(WhirlpoolMixEvent event) {
|
||||
if(event.getWallet().equals(walletForm.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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void selectEntry(SelectEntryEvent event) {
|
||||
if(event.getWallet().equals(getWalletForm().getWallet()) && event.getEntry().getWalletFunction() == Function.UTXOS) {
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.sparrowwallet.sparrow.wallet;
|
|||
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.sparrowwallet.drongo.KeyPurpose;
|
||||
import com.sparrowwallet.drongo.protocol.Sha256Hash;
|
||||
import com.sparrowwallet.drongo.wallet.*;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
import com.sparrowwallet.sparrow.EventManager;
|
||||
|
@ -21,7 +20,6 @@ import javafx.beans.property.BooleanProperty;
|
|||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.beans.property.SimpleBooleanProperty;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.util.Duration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -626,31 +624,6 @@ public class WalletForm {
|
|||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void whirlpoolMixSuccess(WhirlpoolMixSuccessEvent event) {
|
||||
if(event.getWallet() == wallet && event.getWalletNode() != null) {
|
||||
if(transactionMempoolService != null) {
|
||||
transactionMempoolService.cancel();
|
||||
}
|
||||
|
||||
transactionMempoolService = new ElectrumServer.TransactionMempoolService(event.getWallet(), Sha256Hash.wrap(event.getNextUtxo().getHash()), Set.of(event.getWalletNode()));
|
||||
transactionMempoolService.setDelay(Duration.seconds(5));
|
||||
transactionMempoolService.setPeriod(Duration.seconds(5));
|
||||
transactionMempoolService.setRestartOnFailure(false);
|
||||
transactionMempoolService.setOnSucceeded(mempoolWorkerStateEvent -> {
|
||||
Set<String> scriptHashes = transactionMempoolService.getValue();
|
||||
if(!scriptHashes.isEmpty()) {
|
||||
Platform.runLater(() -> EventManager.get().post(new WalletNodeHistoryChangedEvent(scriptHashes.iterator().next())));
|
||||
}
|
||||
|
||||
if(transactionMempoolService.getIterationCount() > 10) {
|
||||
transactionMempoolService.cancel();
|
||||
}
|
||||
});
|
||||
transactionMempoolService.start();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void walletTabsClosed(WalletTabsClosedEvent event) {
|
||||
for(WalletTabData tabData : event.getClosedWalletTabData()) {
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
package com.sparrowwallet.sparrow.wallet;
|
||||
|
||||
import com.samourai.whirlpool.client.wallet.beans.MixProgress;
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.drongo.wallet.WalletNode;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
import com.sparrowwallet.sparrow.io.Config;
|
||||
import com.sparrowwallet.sparrow.whirlpool.Whirlpool;
|
||||
import javafx.application.Platform;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -18,9 +14,6 @@ public class WalletUtxosEntry extends Entry {
|
|||
super(wallet, wallet.getName(), wallet.getWalletUtxos().entrySet().stream().map(entry -> new UtxoEntry(entry.getValue().getWallet(), entry.getKey(), HashIndexEntry.Type.OUTPUT, entry.getValue())).collect(Collectors.toList()));
|
||||
calculateDuplicates();
|
||||
calculateDust();
|
||||
if(wallet.isWhirlpoolMixWallet()) {
|
||||
updateMixProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -68,19 +61,6 @@ public class WalletUtxosEntry extends Entry {
|
|||
}
|
||||
}
|
||||
|
||||
public void updateMixProgress() {
|
||||
Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(getWallet());
|
||||
if(whirlpool != null) {
|
||||
for(Entry entry : 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)) {
|
||||
utxoEntry.setMixProgress(mixProgress);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateUtxos() {
|
||||
List<Entry> current = getWallet().getWalletUtxos().entrySet().stream().map(entry -> new UtxoEntry(entry.getValue().getWallet(), entry.getKey(), HashIndexEntry.Type.OUTPUT, entry.getValue())).collect(Collectors.toList());
|
||||
List<Entry> previous = new ArrayList<>(getChildren());
|
||||
|
@ -95,8 +75,6 @@ public class WalletUtxosEntry extends Entry {
|
|||
|
||||
calculateDuplicates();
|
||||
calculateDust();
|
||||
//Update mix status after SparrowUtxoSupplier has refreshed
|
||||
Platform.runLater(this::updateMixProgress);
|
||||
}
|
||||
|
||||
public long getBalance() {
|
||||
|
|
|
@ -1,788 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool;
|
||||
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.samourai.soroban.client.SorobanConfig;
|
||||
import com.samourai.wallet.api.backend.beans.UnspentOutput;
|
||||
import com.samourai.wallet.bipFormat.BIP_FORMAT;
|
||||
import com.samourai.wallet.bipWallet.WalletSupplier;
|
||||
import com.samourai.wallet.constants.BIP_WALLETS;
|
||||
import com.samourai.wallet.constants.SamouraiAccount;
|
||||
import com.samourai.wallet.constants.SamouraiNetwork;
|
||||
import com.samourai.wallet.hd.HD_Wallet;
|
||||
import com.samourai.wallet.hd.HD_WalletFactoryGeneric;
|
||||
import com.samourai.wallet.util.AsyncUtil;
|
||||
import com.samourai.wallet.util.FormatsUtilGeneric;
|
||||
import com.samourai.whirlpool.client.event.*;
|
||||
import com.samourai.whirlpool.client.mix.handler.IPostmixHandler;
|
||||
import com.samourai.whirlpool.client.tx0.Tx0;
|
||||
import com.samourai.whirlpool.client.tx0.Tx0Config;
|
||||
import com.samourai.whirlpool.client.tx0.Tx0Info;
|
||||
import com.samourai.whirlpool.client.tx0.Tx0Previews;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolEventService;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolWallet;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolWalletConfig;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolWalletService;
|
||||
import com.samourai.whirlpool.client.wallet.beans.*;
|
||||
import com.samourai.whirlpool.client.wallet.data.WhirlpoolInfo;
|
||||
import com.samourai.whirlpool.client.wallet.data.coordinator.CoordinatorSupplier;
|
||||
import com.samourai.whirlpool.client.wallet.data.dataPersister.DataPersisterFactory;
|
||||
import com.samourai.whirlpool.client.wallet.data.dataSource.DataSourceConfig;
|
||||
import com.samourai.whirlpool.client.wallet.data.dataSource.DataSourceFactory;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxo.UtxoSupplier;
|
||||
import com.samourai.whirlpool.client.whirlpool.WhirlpoolClientConfig;
|
||||
import com.samourai.whirlpool.client.whirlpool.beans.Pool;
|
||||
import com.sparrowwallet.drongo.ExtendedKey;
|
||||
import com.sparrowwallet.drongo.KeyPurpose;
|
||||
import com.sparrowwallet.drongo.Network;
|
||||
import com.sparrowwallet.drongo.Utils;
|
||||
import com.sparrowwallet.drongo.protocol.*;
|
||||
import com.sparrowwallet.drongo.wallet.*;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
import com.sparrowwallet.sparrow.EventManager;
|
||||
import com.sparrowwallet.sparrow.event.WhirlpoolMixEvent;
|
||||
import com.sparrowwallet.sparrow.event.WhirlpoolMixSuccessEvent;
|
||||
import com.sparrowwallet.sparrow.wallet.UtxoEntry;
|
||||
import com.sparrowwallet.sparrow.whirlpool.dataPersister.SparrowDataPersister;
|
||||
import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowChainSupplier;
|
||||
import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowDataSource;
|
||||
import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowMinerFeeSupplier;
|
||||
import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowPostmixHandler;
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.rxjavafx.schedulers.JavaFxScheduler;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.BooleanProperty;
|
||||
import javafx.beans.property.SimpleBooleanProperty;
|
||||
import javafx.concurrent.ScheduledService;
|
||||
import javafx.concurrent.Service;
|
||||
import javafx.concurrent.Task;
|
||||
import javafx.util.Duration;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Whirlpool {
|
||||
private static final Logger log = LoggerFactory.getLogger(Whirlpool.class);
|
||||
|
||||
public static final List<Network> WHIRLPOOL_NETWORKS = List.of(Network.MAINNET, Network.TESTNET);
|
||||
public static final int DEFAULT_MIXTO_MIN_MIXES = 3;
|
||||
public static final int DEFAULT_MIXTO_RANDOM_FACTOR = 4;
|
||||
|
||||
private final WhirlpoolWalletService whirlpoolWalletService;
|
||||
private final WhirlpoolWalletConfig config;
|
||||
private WhirlpoolInfo whirlpoolInfo;
|
||||
private Tx0Info tx0Info;
|
||||
private Tx0FeeTarget tx0FeeTarget = Tx0FeeTarget.BLOCKS_4;
|
||||
private Tx0FeeTarget mixFeeTarget = Tx0FeeTarget.BLOCKS_4;
|
||||
private HD_Wallet hdWallet;
|
||||
private String walletId;
|
||||
private String mixToWalletId;
|
||||
private boolean resyncMixesDone;
|
||||
|
||||
private StartupService startupService;
|
||||
private Duration startupServiceDelay;
|
||||
|
||||
private final BooleanProperty startingProperty = new SimpleBooleanProperty(false);
|
||||
private final BooleanProperty stoppingProperty = new SimpleBooleanProperty(false);
|
||||
private final BooleanProperty mixingProperty = new SimpleBooleanProperty(false);
|
||||
|
||||
public Whirlpool(Integer storedBlockHeight) {
|
||||
this.whirlpoolWalletService = new WhirlpoolWalletService();
|
||||
this.config = computeWhirlpoolWalletConfig(storedBlockHeight);
|
||||
this.tx0Info = null; // instantiated by getTx0Info()
|
||||
this.whirlpoolInfo = null; // instantiated by getWhirlpoolInfo()
|
||||
|
||||
WhirlpoolEventService.getInstance().register(this);
|
||||
}
|
||||
|
||||
private WhirlpoolWalletConfig computeWhirlpoolWalletConfig(Integer storedBlockHeight) {
|
||||
SorobanConfig sorobanConfig = AppServices.getWhirlpoolServices().getSorobanConfig();
|
||||
DataSourceConfig dataSourceConfig = computeDataSourceConfig(storedBlockHeight);
|
||||
DataSourceFactory dataSourceFactory = (whirlpoolWallet, bip44w, passphrase, walletStateSupplier, utxoConfigSupplier) -> new SparrowDataSource(whirlpoolWallet, bip44w, walletStateSupplier, utxoConfigSupplier, dataSourceConfig);
|
||||
|
||||
WhirlpoolWalletConfig whirlpoolWalletConfig = new WhirlpoolWalletConfig(dataSourceFactory, sorobanConfig, false);
|
||||
DataPersisterFactory dataPersisterFactory = (whirlpoolWallet, bip44w) -> new SparrowDataPersister(whirlpoolWallet, whirlpoolWalletConfig.getPersistDelaySeconds());
|
||||
whirlpoolWalletConfig.setDataPersisterFactory(dataPersisterFactory);
|
||||
whirlpoolWalletConfig.setPartner("SPARROW");
|
||||
whirlpoolWalletConfig.setIndexRangePostmix(IndexRange.FULL);
|
||||
return whirlpoolWalletConfig;
|
||||
}
|
||||
|
||||
private DataSourceConfig computeDataSourceConfig(Integer storedBlockHeight) {
|
||||
return new DataSourceConfig(SparrowMinerFeeSupplier.getInstance(), new SparrowChainSupplier(storedBlockHeight), BIP_FORMAT.PROVIDER, BIP_WALLETS.WHIRLPOOL);
|
||||
}
|
||||
|
||||
private WhirlpoolInfo getWhirlpoolInfo() {
|
||||
if(whirlpoolInfo == null) {
|
||||
whirlpoolInfo = new WhirlpoolInfo(SparrowMinerFeeSupplier.getInstance(), config);
|
||||
}
|
||||
|
||||
return whirlpoolInfo;
|
||||
}
|
||||
|
||||
public Collection<Pool> getPools(Long totalUtxoValue) throws Exception {
|
||||
CoordinatorSupplier coordinatorSupplier = getWhirlpoolInfo().getCoordinatorSupplier();
|
||||
coordinatorSupplier.load();
|
||||
if(totalUtxoValue == null) {
|
||||
return coordinatorSupplier.getPools();
|
||||
}
|
||||
|
||||
return coordinatorSupplier.findPoolsForTx0(totalUtxoValue);
|
||||
}
|
||||
|
||||
public Tx0Previews getTx0Previews(Collection<UnspentOutput> utxos) throws Exception {
|
||||
Tx0Info tx0Info = getTx0Info();
|
||||
|
||||
// preview all pools
|
||||
Tx0Config tx0Config = computeTx0Config(tx0Info);
|
||||
return tx0Info.tx0Previews(tx0Config, utxos);
|
||||
}
|
||||
|
||||
public Tx0 broadcastTx0(Pool pool, Collection<BlockTransactionHashIndex> utxos) throws Exception {
|
||||
WhirlpoolWallet whirlpoolWallet = getWhirlpoolWallet();
|
||||
whirlpoolWallet.startAsync().subscribeOn(Schedulers.io()).observeOn(JavaFxScheduler.platform());
|
||||
UtxoSupplier utxoSupplier = whirlpoolWallet.getUtxoSupplier();
|
||||
List<WhirlpoolUtxo> whirlpoolUtxos = utxos.stream().map(ref -> utxoSupplier.findUtxo(ref.getHashAsString(), (int)ref.getIndex())).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
||||
if(whirlpoolUtxos.size() != utxos.size()) {
|
||||
throw new IllegalStateException("Failed to find UTXOs in Whirlpool wallet");
|
||||
}
|
||||
|
||||
Tx0Info tx0Info = getTx0Info();
|
||||
|
||||
WalletSupplier walletSupplier = whirlpoolWallet.getWalletSupplier();
|
||||
Tx0Config tx0Config = computeTx0Config(tx0Info);
|
||||
Tx0 tx0 = tx0Info.tx0(walletSupplier, utxoSupplier, whirlpoolUtxos, pool, tx0Config);
|
||||
|
||||
//Clear tx0 for new fee addresses
|
||||
clearTx0Info();
|
||||
return tx0;
|
||||
}
|
||||
|
||||
private Tx0Info getTx0Info() throws Exception {
|
||||
if(tx0Info == null) {
|
||||
tx0Info = fetchTx0Info();
|
||||
}
|
||||
|
||||
return tx0Info;
|
||||
}
|
||||
|
||||
private Tx0Info fetchTx0Info() throws Exception {
|
||||
return AsyncUtil.getInstance().blockingGet(
|
||||
Single.fromCallable(() -> getWhirlpoolInfo().fetchTx0Info(getScode()))
|
||||
.subscribeOn(Schedulers.io()).observeOn(JavaFxScheduler.platform()));
|
||||
}
|
||||
|
||||
private void clearTx0Info() {
|
||||
tx0Info = null;
|
||||
}
|
||||
|
||||
private Tx0Config computeTx0Config(Tx0Info tx0Info) {
|
||||
Tx0Config tx0Config = tx0Info.getTx0Config(tx0FeeTarget, mixFeeTarget);
|
||||
tx0Config.setChangeWallet(SamouraiAccount.BADBANK);
|
||||
return tx0Config;
|
||||
}
|
||||
|
||||
public void setHDWallet(String walletId, Wallet wallet) {
|
||||
NetworkParameters params = config.getSamouraiNetwork().getParams();
|
||||
this.hdWallet = computeHdWallet(wallet, params);
|
||||
this.walletId = walletId;
|
||||
}
|
||||
|
||||
public static HD_Wallet computeHdWallet(Wallet wallet, NetworkParameters params) {
|
||||
if(wallet.isEncrypted()) {
|
||||
throw new IllegalStateException("Wallet cannot be encrypted");
|
||||
}
|
||||
|
||||
try {
|
||||
Keystore keystore = wallet.getKeystores().get(0);
|
||||
ScriptType scriptType = wallet.getScriptType();
|
||||
int purpose = scriptType.getDefaultDerivation().get(0).num();
|
||||
List<String> words = keystore.getSeed().getMnemonicCode();
|
||||
String passphrase = keystore.getSeed().getPassphrase() == null ? "" : keystore.getSeed().getPassphrase().asString();
|
||||
HD_WalletFactoryGeneric hdWalletFactory = HD_WalletFactoryGeneric.getInstance();
|
||||
byte[] seed = hdWalletFactory.computeSeedFromWords(words);
|
||||
return hdWalletFactory.getHD(purpose, seed, passphrase, params);
|
||||
} catch(Exception e) {
|
||||
throw new IllegalStateException("Could not create Whirlpool HD wallet ", e);
|
||||
}
|
||||
}
|
||||
|
||||
public WhirlpoolWallet getWhirlpoolWallet() throws WhirlpoolException {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() != null) {
|
||||
return whirlpoolWalletService.whirlpoolWallet();
|
||||
}
|
||||
|
||||
if(hdWallet == null) {
|
||||
throw new IllegalStateException("Whirlpool HD wallet not added");
|
||||
}
|
||||
|
||||
try {
|
||||
WhirlpoolWallet whirlpoolWallet = new WhirlpoolWallet(config, Utils.hexToBytes(hdWallet.getSeedHex()), hdWallet.getPassphrase(), walletId);
|
||||
return whirlpoolWalletService.openWallet(whirlpoolWallet, hdWallet.getPassphrase());
|
||||
} catch(Exception e) {
|
||||
throw new WhirlpoolException("Could not create whirlpool wallet ", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() != null) {
|
||||
whirlpoolWalletService.whirlpoolWallet().stop();
|
||||
}
|
||||
}
|
||||
|
||||
public void mix(BlockTransactionHashIndex utxo) throws WhirlpoolException {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() == null) {
|
||||
throw new WhirlpoolException("Whirlpool wallet not yet created");
|
||||
}
|
||||
|
||||
try {
|
||||
WhirlpoolUtxo whirlpoolUtxo = whirlpoolWalletService.whirlpoolWallet().getUtxoSupplier().findUtxo(utxo.getHashAsString(), (int)utxo.getIndex());
|
||||
whirlpoolWalletService.whirlpoolWallet().mix(whirlpoolUtxo);
|
||||
} catch(Exception e) {
|
||||
throw new WhirlpoolException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void mixStop(BlockTransactionHashIndex utxo) throws WhirlpoolException {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() == null) {
|
||||
throw new WhirlpoolException("Whirlpool wallet not yet created");
|
||||
}
|
||||
|
||||
try {
|
||||
WhirlpoolUtxo whirlpoolUtxo = whirlpoolWalletService.whirlpoolWallet().getUtxoSupplier().findUtxo(utxo.getHashAsString(), (int)utxo.getIndex());
|
||||
whirlpoolWalletService.whirlpoolWallet().mixStop(whirlpoolUtxo);
|
||||
} catch(Exception e) {
|
||||
throw new WhirlpoolException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public MixProgress getMixProgress(BlockTransactionHashIndex utxo) {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() == null || utxo.getStatus() == Status.FROZEN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
WhirlpoolUtxo whirlpoolUtxo = whirlpoolWalletService.whirlpoolWallet().getUtxoSupplier().findUtxo(utxo.getHashAsString(), (int)utxo.getIndex());
|
||||
if(whirlpoolUtxo != null && whirlpoolUtxo.getUtxoState() != null) {
|
||||
MixProgress mixProgress = whirlpoolUtxo.getUtxoState().getMixProgress();
|
||||
if(mixProgress != null && !isMixing(utxo)) {
|
||||
log.debug("Utxo " + utxo + " mix state is " + whirlpoolUtxo.getUtxoState() + " but utxo is not mixing");
|
||||
return null;
|
||||
}
|
||||
|
||||
return mixProgress;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isMixing(BlockTransactionHashIndex utxo) {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() == null || !whirlpoolWalletService.whirlpoolWallet().isStarted()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return whirlpoolWalletService.whirlpoolWallet().getMixingState().getUtxosMixing().stream().map(WhirlpoolUtxo::getUtxo).anyMatch(uo -> uo.tx_hash.equals(utxo.getHashAsString()) && uo.tx_output_n == (int)utxo.getIndex());
|
||||
}
|
||||
|
||||
public void refreshUtxos() {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() != null) {
|
||||
whirlpoolWalletService.whirlpoolWallet().refreshUtxosAsync().subscribeOn(Schedulers.io()).observeOn(JavaFxScheduler.platform());
|
||||
}
|
||||
}
|
||||
|
||||
private void resyncMixesDone(Whirlpool whirlpool, Wallet postmixWallet) {
|
||||
Set<BlockTransactionHashIndex> receiveUtxos = postmixWallet.getWalletUtxos().entrySet().stream()
|
||||
.filter(entry -> entry.getValue().getKeyPurpose() == KeyPurpose.RECEIVE).map(Map.Entry::getKey).collect(Collectors.toSet());
|
||||
for(BlockTransactionHashIndex utxo : receiveUtxos) {
|
||||
int mixesDone = recountMixesDone(postmixWallet, utxo);
|
||||
whirlpool.setMixesDone(utxo, mixesDone);
|
||||
}
|
||||
}
|
||||
|
||||
public int recountMixesDone(Wallet postmixWallet, BlockTransactionHashIndex postmixUtxo) {
|
||||
int mixesDone = 0;
|
||||
Set<BlockTransactionHashIndex> walletTxos = postmixWallet.getWalletTxos().entrySet().stream()
|
||||
.filter(entry -> entry.getValue().getKeyPurpose() == KeyPurpose.RECEIVE).map(Map.Entry::getKey).collect(Collectors.toSet());
|
||||
BlockTransaction blkTx = postmixWallet.getTransactions().get(postmixUtxo.getHash());
|
||||
|
||||
while(blkTx != null) {
|
||||
mixesDone++;
|
||||
List<TransactionInput> inputs = blkTx.getTransaction().getInputs();
|
||||
blkTx = null;
|
||||
for(TransactionInput txInput : inputs) {
|
||||
BlockTransaction inputTx = postmixWallet.getTransactions().get(txInput.getOutpoint().getHash());
|
||||
if(inputTx != null && walletTxos.stream().anyMatch(txo -> txo.getHash().equals(inputTx.getHash()) && txo.getIndex() == txInput.getOutpoint().getIndex()) && inputTx.getTransaction() != null) {
|
||||
blkTx = inputTx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mixesDone;
|
||||
}
|
||||
|
||||
public void setMixesDone(BlockTransactionHashIndex utxo, int mixesDone) {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
WhirlpoolUtxo whirlpoolUtxo = whirlpoolWalletService.whirlpoolWallet().getUtxoSupplier().findUtxo(utxo.getHashAsString(), (int)utxo.getIndex());
|
||||
if(whirlpoolUtxo != null) {
|
||||
whirlpoolUtxo.setMixsDone(mixesDone);
|
||||
}
|
||||
}
|
||||
|
||||
public void checkIfMixing() {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(isMixing()) {
|
||||
if(!whirlpoolWalletService.whirlpoolWallet().isStarted()) {
|
||||
log.warn("Wallet is not started, but mixingProperty is true");
|
||||
WhirlpoolEventService.getInstance().post(new WalletStopEvent(whirlpoolWalletService.whirlpoolWallet()));
|
||||
} else if(whirlpoolWalletService.whirlpoolWallet().getMixingState().getUtxosMixing().isEmpty() &&
|
||||
!whirlpoolWalletService.whirlpoolWallet().getUtxoSupplier().findUtxos(SamouraiAccount.PREMIX, SamouraiAccount.POSTMIX).isEmpty()) {
|
||||
log.warn("No UTXOs mixing, but mixingProperty is true");
|
||||
//Will automatically restart
|
||||
AppServices.getWhirlpoolServices().stopWhirlpool(this, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void logDebug() {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() == null) {
|
||||
log.warn("Whirlpool wallet for " + walletId + " not started");
|
||||
return;
|
||||
}
|
||||
|
||||
log.warn("Whirlpool debug for " + walletId + "\n" + whirlpoolWalletService.whirlpoolWallet().getDebug());
|
||||
}
|
||||
|
||||
public boolean hasWallet() {
|
||||
return hdWallet != null;
|
||||
}
|
||||
|
||||
public boolean isStarted() {
|
||||
if(whirlpoolWalletService.whirlpoolWallet() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return whirlpoolWalletService.whirlpoolWallet().isStarted();
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
whirlpoolWalletService.closeWallet();
|
||||
}
|
||||
|
||||
public StartupService createStartupService() {
|
||||
if(startupService != null) {
|
||||
startupService.cancel();
|
||||
}
|
||||
|
||||
startupService = new StartupService(this);
|
||||
return startupService;
|
||||
}
|
||||
|
||||
public StartupService getStartupService() {
|
||||
return startupService;
|
||||
}
|
||||
|
||||
private WalletUtxo getUtxo(WhirlpoolUtxo whirlpoolUtxo) {
|
||||
Wallet wallet = AppServices.get().getWallet(walletId);
|
||||
if(wallet != null) {
|
||||
wallet = getStandardAccountWallet(whirlpoolUtxo.getAccount(), wallet);
|
||||
|
||||
if(wallet != null) {
|
||||
for(BlockTransactionHashIndex utxo : wallet.getWalletUtxos().keySet()) {
|
||||
if(utxo.getHashAsString().equals(whirlpoolUtxo.getUtxo().tx_hash) && utxo.getIndex() == whirlpoolUtxo.getUtxo().tx_output_n) {
|
||||
return new WalletUtxo(wallet, utxo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Wallet getWallet(String walletId) {
|
||||
return AppServices.get().getOpenWallets().entrySet().stream().filter(entry -> entry.getValue().getWalletId(entry.getKey()).equals(walletId)).map(Map.Entry::getKey).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
public static Wallet getStandardAccountWallet(SamouraiAccount whirlpoolAccount, Wallet wallet) {
|
||||
StandardAccount standardAccount = getStandardAccount(whirlpoolAccount);
|
||||
if(StandardAccount.isWhirlpoolAccount(standardAccount) || wallet.getStandardAccountType() != standardAccount) {
|
||||
Wallet standardWallet = wallet.getChildWallet(standardAccount);
|
||||
if(standardWallet == null) {
|
||||
throw new IllegalStateException("Cannot find " + standardAccount + " wallet");
|
||||
}
|
||||
|
||||
return standardWallet;
|
||||
}
|
||||
|
||||
return wallet;
|
||||
}
|
||||
|
||||
public static StandardAccount getStandardAccount(SamouraiAccount whirlpoolAccount) {
|
||||
if(whirlpoolAccount == SamouraiAccount.PREMIX) {
|
||||
return StandardAccount.WHIRLPOOL_PREMIX;
|
||||
} else if(whirlpoolAccount == SamouraiAccount.POSTMIX) {
|
||||
return StandardAccount.WHIRLPOOL_POSTMIX;
|
||||
} else if(whirlpoolAccount == SamouraiAccount.BADBANK) {
|
||||
return StandardAccount.WHIRLPOOL_BADBANK;
|
||||
}
|
||||
|
||||
return StandardAccount.ACCOUNT_0;
|
||||
}
|
||||
|
||||
public static UnspentOutput getUnspentOutput(WalletNode node, BlockTransaction blockTransaction, int index) {
|
||||
TransactionOutput txOutput = blockTransaction.getTransaction().getOutputs().get(index);
|
||||
|
||||
UnspentOutput out = new UnspentOutput();
|
||||
out.tx_hash = txOutput.getHash().toString();
|
||||
out.tx_output_n = txOutput.getIndex();
|
||||
out.value = txOutput.getValue();
|
||||
out.script = Utils.bytesToHex(txOutput.getScriptBytes());
|
||||
|
||||
try {
|
||||
out.addr = txOutput.getScript().getToAddresses()[0].toString();
|
||||
} catch(Exception e) {
|
||||
//ignore
|
||||
}
|
||||
|
||||
Transaction transaction = (Transaction)txOutput.getParent();
|
||||
out.tx_version = (int)transaction.getVersion();
|
||||
out.tx_locktime = transaction.getLocktime();
|
||||
if(AppServices.getCurrentBlockHeight() != null) {
|
||||
out.confirmations = blockTransaction.getConfirmations(AppServices.getCurrentBlockHeight());
|
||||
}
|
||||
|
||||
Wallet wallet = node.getWallet().isBip47() ? node.getWallet().getMasterWallet() : node.getWallet();
|
||||
if(wallet.getKeystores().size() != 1) {
|
||||
throw new IllegalStateException("Cannot mix outputs from a wallet with multiple keystores");
|
||||
}
|
||||
|
||||
SamouraiNetwork samouraiNetwork = AppServices.getWhirlpoolServices().getSamouraiNetwork();
|
||||
boolean testnet = FormatsUtilGeneric.getInstance().isTestNet(samouraiNetwork.getParams());
|
||||
|
||||
UnspentOutput.Xpub xpub = new UnspentOutput.Xpub();
|
||||
ExtendedKey.Header header = testnet ? ExtendedKey.Header.tpub : ExtendedKey.Header.xpub;
|
||||
xpub.m = wallet.getKeystores().get(0).getExtendedPublicKey().toString(header);
|
||||
xpub.path = node.getWallet().isBip47() ? null : node.getDerivationPath().toUpperCase(Locale.ROOT);
|
||||
|
||||
out.xpub = xpub;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
public void refreshTorCircuits() {
|
||||
AppServices.getHttpClientService().changeIdentity();
|
||||
}
|
||||
|
||||
public String getScode() {
|
||||
return config.getScode();
|
||||
}
|
||||
|
||||
public void setScode(String scode) {
|
||||
config.setScode(scode);
|
||||
}
|
||||
|
||||
public Tx0FeeTarget getTx0FeeTarget() {
|
||||
return tx0FeeTarget;
|
||||
}
|
||||
|
||||
public void setTx0FeeTarget(Tx0FeeTarget tx0FeeTarget) {
|
||||
this.tx0FeeTarget = tx0FeeTarget;
|
||||
}
|
||||
|
||||
public Tx0FeeTarget getMixFeeTarget() {
|
||||
return mixFeeTarget;
|
||||
}
|
||||
|
||||
public void setMixFeeTarget(Tx0FeeTarget mixFeeTarget) {
|
||||
this.mixFeeTarget = mixFeeTarget;
|
||||
}
|
||||
|
||||
public String getWalletId() {
|
||||
return walletId;
|
||||
}
|
||||
|
||||
public String getMixToWalletId() {
|
||||
return mixToWalletId;
|
||||
}
|
||||
|
||||
public void setResyncMixesDone(boolean resyncMixesDone) {
|
||||
this.resyncMixesDone = resyncMixesDone;
|
||||
}
|
||||
|
||||
public void setPostmixIndexRange(String indexRange) {
|
||||
if(indexRange != null) {
|
||||
try {
|
||||
config.setIndexRangePostmix(IndexRange.valueOf(indexRange));
|
||||
} catch(Exception e) {
|
||||
log.error("Invalid index range " + indexRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setMixToWallet(String mixToWalletId, Integer minMixes) {
|
||||
if(mixToWalletId == null) {
|
||||
config.setExternalDestination(null);
|
||||
} else {
|
||||
Wallet mixToWallet = getWallet(mixToWalletId);
|
||||
if(mixToWallet == null) {
|
||||
throw new IllegalStateException("Cannot find mix to wallet with id " + mixToWalletId);
|
||||
}
|
||||
|
||||
int mixes = minMixes == null ? DEFAULT_MIXTO_MIN_MIXES : minMixes;
|
||||
|
||||
IPostmixHandler postmixHandler = new SparrowPostmixHandler(whirlpoolWalletService, mixToWallet, KeyPurpose.RECEIVE);
|
||||
ExternalDestination externalDestination = new ExternalDestination(postmixHandler, 0, mixes, DEFAULT_MIXTO_RANDOM_FACTOR);
|
||||
config.setExternalDestination(externalDestination);
|
||||
}
|
||||
|
||||
this.mixToWalletId = mixToWalletId;
|
||||
}
|
||||
|
||||
public boolean isMixing() {
|
||||
return mixingProperty.get();
|
||||
}
|
||||
|
||||
public BooleanProperty mixingProperty() {
|
||||
return mixingProperty;
|
||||
}
|
||||
|
||||
public boolean isStarting() {
|
||||
return startingProperty.get();
|
||||
}
|
||||
|
||||
public BooleanProperty startingProperty() {
|
||||
return startingProperty;
|
||||
}
|
||||
|
||||
public boolean isStopping() {
|
||||
return stoppingProperty.get();
|
||||
}
|
||||
|
||||
public BooleanProperty stoppingProperty() {
|
||||
return stoppingProperty;
|
||||
}
|
||||
|
||||
public Duration getStartupServiceDelay() {
|
||||
return startupServiceDelay;
|
||||
}
|
||||
|
||||
public void setStartupServiceDelay(Duration startupServiceDelay) {
|
||||
this.startupServiceDelay = startupServiceDelay;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onMixSuccess(MixSuccessEvent e) {
|
||||
WhirlpoolUtxo whirlpoolUtxo = e.getMixParams().getWhirlpoolUtxo();
|
||||
WalletUtxo walletUtxo = getUtxo(whirlpoolUtxo);
|
||||
if(walletUtxo != null) {
|
||||
log.debug("Mix success, new utxo " + e.getReceiveUtxo().getHash() + ":" + e.getReceiveUtxo().getIndex());
|
||||
Platform.runLater(() -> EventManager.get().post(new WhirlpoolMixSuccessEvent(walletUtxo.wallet, walletUtxo.utxo, e.getReceiveUtxo(), getReceiveNode(e, walletUtxo))));
|
||||
}
|
||||
}
|
||||
|
||||
private WalletNode getReceiveNode(MixSuccessEvent e, WalletUtxo walletUtxo) {
|
||||
for(WalletNode walletNode : walletUtxo.wallet.getNode(KeyPurpose.RECEIVE).getChildren()) {
|
||||
if(walletNode.getAddress().toString().equals(e.getReceiveDestination().getAddress())) {
|
||||
return walletNode;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onMixFail(MixFailEvent e) {
|
||||
WhirlpoolUtxo whirlpoolUtxo = e.getMixParams().getWhirlpoolUtxo();
|
||||
WalletUtxo walletUtxo = getUtxo(whirlpoolUtxo);
|
||||
if(walletUtxo != null) {
|
||||
log.debug("Mix failed for utxo " + whirlpoolUtxo.getUtxo().tx_hash + ":" + whirlpoolUtxo.getUtxo().tx_output_n + " " + e.getMixFailReason());
|
||||
Platform.runLater(() -> EventManager.get().post(new WhirlpoolMixEvent(walletUtxo.wallet, walletUtxo.utxo, e.getMixFailReason(), e.getError())));
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onMixProgress(MixProgressEvent e) {
|
||||
WhirlpoolUtxo whirlpoolUtxo = e.getMixParams().getWhirlpoolUtxo();
|
||||
MixProgress mixProgress = whirlpoolUtxo.getUtxoState().getMixProgress();
|
||||
WalletUtxo walletUtxo = getUtxo(whirlpoolUtxo);
|
||||
if(walletUtxo != null && isMixing()) {
|
||||
log.debug("Mix progress for utxo " + whirlpoolUtxo.getUtxo().tx_hash + ":" + whirlpoolUtxo.getUtxo().tx_output_n + " " + whirlpoolUtxo.getMixsDone() + " " + mixProgress.getMixStep() + " " + whirlpoolUtxo.getUtxoState().getStatus());
|
||||
Platform.runLater(() -> EventManager.get().post(new WhirlpoolMixEvent(walletUtxo.wallet, walletUtxo.utxo, mixProgress)));
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onWalletStart(WalletStartEvent e) {
|
||||
if(e.getWhirlpoolWallet() == whirlpoolWalletService.whirlpoolWallet()) {
|
||||
log.info("Mixing to " + e.getWhirlpoolWallet().getConfig().getExternalDestination());
|
||||
mixingProperty.set(true);
|
||||
|
||||
if(resyncMixesDone) {
|
||||
Wallet wallet = AppServices.get().getWallet(walletId);
|
||||
if(wallet != null) {
|
||||
Wallet postmixWallet = getStandardAccountWallet(SamouraiAccount.POSTMIX, wallet);
|
||||
resyncMixesDone(this, postmixWallet);
|
||||
resyncMixesDone = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onWalletStop(WalletStopEvent e) {
|
||||
if(e.getWhirlpoolWallet() == whirlpoolWalletService.whirlpoolWallet()) {
|
||||
mixingProperty.set(false);
|
||||
|
||||
Wallet wallet = AppServices.get().getWallet(walletId);
|
||||
if(wallet != null) {
|
||||
Platform.runLater(() -> {
|
||||
if(AppServices.isConnected()) {
|
||||
AppServices.getWhirlpoolServices().startWhirlpool(wallet, this, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class PoolsService extends Service<Collection<Pool>> {
|
||||
private final Whirlpool whirlpool;
|
||||
private final Long totalUtxoValue;
|
||||
|
||||
public PoolsService(Whirlpool whirlpool, Long totalUtxoValue) {
|
||||
this.whirlpool = whirlpool;
|
||||
this.totalUtxoValue = totalUtxoValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Task<Collection<Pool>> createTask() {
|
||||
return new Task<>() {
|
||||
protected Collection<Pool> call() throws Exception {
|
||||
return whirlpool.getPools(totalUtxoValue);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static class Tx0PreviewsService extends Service<Tx0Previews> {
|
||||
private final Whirlpool whirlpool;
|
||||
private final List<UtxoEntry> utxoEntries;
|
||||
|
||||
public Tx0PreviewsService(Whirlpool whirlpool, List<UtxoEntry> utxoEntries) {
|
||||
this.whirlpool = whirlpool;
|
||||
this.utxoEntries = utxoEntries;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Task<Tx0Previews> createTask() {
|
||||
return new Task<>() {
|
||||
protected Tx0Previews call() throws Exception {
|
||||
updateProgress(-1, 1);
|
||||
updateMessage("Fetching premix preview...");
|
||||
|
||||
Collection<UnspentOutput> utxos = utxoEntries.stream().map(utxoEntry -> Whirlpool.getUnspentOutput(utxoEntry.getNode(), utxoEntry.getBlockTransaction(), (int)utxoEntry.getHashIndex().getIndex())).collect(Collectors.toList());
|
||||
return whirlpool.getTx0Previews(utxos);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static class Tx0BroadcastService extends Service<Sha256Hash> {
|
||||
private final Whirlpool whirlpool;
|
||||
private final Pool pool;
|
||||
private final Collection<BlockTransactionHashIndex> utxos;
|
||||
|
||||
public Tx0BroadcastService(Whirlpool whirlpool, Pool pool, Collection<BlockTransactionHashIndex> utxos) {
|
||||
this.whirlpool = whirlpool;
|
||||
this.pool = pool;
|
||||
this.utxos = utxos;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Task<Sha256Hash> createTask() {
|
||||
return new Task<>() {
|
||||
protected Sha256Hash call() throws Exception {
|
||||
updateProgress(-1, 1);
|
||||
updateMessage("Broadcasting premix transaction...");
|
||||
|
||||
Tx0 tx0 = whirlpool.broadcastTx0(pool, utxos);
|
||||
return Sha256Hash.wrap(tx0.getTx().getHashAsString());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static class StartupService extends ScheduledService<WhirlpoolWallet> {
|
||||
private final Whirlpool whirlpool;
|
||||
|
||||
public StartupService(Whirlpool whirlpool) {
|
||||
this.whirlpool = whirlpool;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Task<WhirlpoolWallet> createTask() {
|
||||
return new Task<>() {
|
||||
protected WhirlpoolWallet call() throws Exception {
|
||||
updateProgress(-1, 1);
|
||||
updateMessage("Starting Whirlpool...");
|
||||
|
||||
try {
|
||||
whirlpool.startingProperty.set(true);
|
||||
WhirlpoolWallet whirlpoolWallet = whirlpool.getWhirlpoolWallet();
|
||||
if(AppServices.onlineProperty().get()) {
|
||||
whirlpoolWallet.startAsync().subscribeOn(Schedulers.io()).observeOn(JavaFxScheduler.platform()).subscribe();
|
||||
}
|
||||
|
||||
return whirlpoolWallet;
|
||||
} finally {
|
||||
whirlpool.startingProperty.set(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static class ShutdownService extends Service<Boolean> {
|
||||
private final Whirlpool whirlpool;
|
||||
|
||||
public ShutdownService(Whirlpool whirlpool) {
|
||||
this.whirlpool = whirlpool;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Task<Boolean> createTask() {
|
||||
return new Task<>() {
|
||||
protected Boolean call() throws Exception {
|
||||
updateProgress(-1, 1);
|
||||
updateMessage("Disconnecting from Whirlpool...");
|
||||
|
||||
try {
|
||||
whirlpool.stoppingProperty.set(true);
|
||||
whirlpool.shutdown();
|
||||
return true;
|
||||
} finally {
|
||||
whirlpool.stoppingProperty.set(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static class WalletUtxo {
|
||||
public final Wallet wallet;
|
||||
public final BlockTransactionHashIndex utxo;
|
||||
|
||||
public WalletUtxo(Wallet wallet, BlockTransactionHashIndex utxo) {
|
||||
this.wallet = wallet;
|
||||
this.utxo = utxo;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,361 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool;
|
||||
|
||||
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.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.sparrow.UnitFormat;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
import com.sparrowwallet.sparrow.EventManager;
|
||||
import com.sparrowwallet.sparrow.control.CopyableCoinLabel;
|
||||
import com.sparrowwallet.sparrow.control.CopyableLabel;
|
||||
import com.sparrowwallet.sparrow.event.WalletMasterMixConfigChangedEvent;
|
||||
import com.sparrowwallet.sparrow.io.Config;
|
||||
import com.sparrowwallet.sparrow.wallet.Entry;
|
||||
import com.sparrowwallet.sparrow.wallet.UtxoEntry;
|
||||
import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowMinerFeeSupplier;
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class WhirlpoolController {
|
||||
private static final List<Tx0FeeTarget> FEE_TARGETS = List.of(Tx0FeeTarget.MIN, Tx0FeeTarget.BLOCKS_4, Tx0FeeTarget.BLOCKS_2);
|
||||
|
||||
@FXML
|
||||
private VBox whirlpoolBox;
|
||||
|
||||
@FXML
|
||||
private VBox step1;
|
||||
|
||||
@FXML
|
||||
private VBox step2;
|
||||
|
||||
@FXML
|
||||
private VBox step3;
|
||||
|
||||
@FXML
|
||||
private VBox step4;
|
||||
|
||||
@FXML
|
||||
private TextField scode;
|
||||
|
||||
@FXML
|
||||
private Slider premixPriority;
|
||||
|
||||
@FXML
|
||||
private CopyableLabel premixFeeRate;
|
||||
|
||||
@FXML
|
||||
private Label lowPremixFeeRate;
|
||||
|
||||
@FXML
|
||||
private ComboBox<Pool> pool;
|
||||
|
||||
@FXML
|
||||
private VBox selectedPool;
|
||||
|
||||
@FXML
|
||||
private CopyableCoinLabel poolFee;
|
||||
|
||||
@FXML
|
||||
private Label poolInsufficient;
|
||||
|
||||
@FXML
|
||||
private Label poolAnonset;
|
||||
|
||||
@FXML
|
||||
private HBox discountFeeBox;
|
||||
|
||||
@FXML
|
||||
private HBox nbOutputsBox;
|
||||
|
||||
@FXML
|
||||
private Label nbOutputsLoading;
|
||||
|
||||
@FXML
|
||||
private Label nbOutputs;
|
||||
|
||||
@FXML
|
||||
private CopyableCoinLabel discountFee;
|
||||
|
||||
private String walletId;
|
||||
private Wallet wallet;
|
||||
private MixConfig mixConfig;
|
||||
private List<UtxoEntry> utxoEntries;
|
||||
private Tx0Previews tx0Previews;
|
||||
private final ObjectProperty<Tx0Preview> tx0PreviewProperty = new SimpleObjectProperty<>(null);
|
||||
|
||||
public void initializeView(String walletId, Wallet wallet, List<UtxoEntry> utxoEntries) {
|
||||
this.walletId = walletId;
|
||||
this.wallet = wallet;
|
||||
this.utxoEntries = utxoEntries;
|
||||
this.mixConfig = wallet.getMasterMixConfig();
|
||||
|
||||
step1.managedProperty().bind(step1.visibleProperty());
|
||||
step2.managedProperty().bind(step2.visibleProperty());
|
||||
step3.managedProperty().bind(step3.visibleProperty());
|
||||
step4.managedProperty().bind(step4.visibleProperty());
|
||||
|
||||
step2.setVisible(false);
|
||||
step3.setVisible(false);
|
||||
step4.setVisible(false);
|
||||
|
||||
scode.setText(mixConfig.getScode() == null ? "" : mixConfig.getScode());
|
||||
scode.setTextFormatter(new TextFormatter<>((change) -> {
|
||||
change.setText(change.getText().toUpperCase(Locale.ROOT));
|
||||
return change;
|
||||
}));
|
||||
scode.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||
pool.setItems(FXCollections.emptyObservableList());
|
||||
tx0PreviewProperty.set(null);
|
||||
mixConfig.setScode(newValue);
|
||||
EventManager.get().post(new WalletMasterMixConfigChangedEvent(wallet));
|
||||
});
|
||||
|
||||
premixPriority.setMin(0);
|
||||
premixPriority.setMax(FEE_TARGETS.size() - 1);
|
||||
premixPriority.setMajorTickUnit(1);
|
||||
premixPriority.setMinorTickCount(0);
|
||||
premixPriority.setLabelFormatter(new StringConverter<>() {
|
||||
@Override
|
||||
public String toString(Double object) {
|
||||
return object.intValue() == 0 ? "Low" : (object.intValue() == 1 ? "Normal" : "High");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double fromString(String string) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
premixPriority.valueProperty().addListener((observable, oldValue, newValue) -> {
|
||||
pool.setItems(FXCollections.emptyObservableList());
|
||||
tx0Previews = null;
|
||||
tx0PreviewProperty.set(null);
|
||||
Tx0FeeTarget tx0FeeTarget = FEE_TARGETS.get(newValue.intValue());
|
||||
premixFeeRate.setText(getFeeRate(tx0FeeTarget) + " sats/vB");
|
||||
lowPremixFeeRate.setVisible(tx0FeeTarget == Tx0FeeTarget.MIN && getFeeRate(tx0FeeTarget) * 2 < getFeeRate(Tx0FeeTarget.BLOCKS_4));
|
||||
});
|
||||
premixPriority.setValue(1);
|
||||
lowPremixFeeRate.managedProperty().bind(lowPremixFeeRate.visibleProperty());
|
||||
lowPremixFeeRate.setVisible(false);
|
||||
|
||||
if(mixConfig.getScode() != null) {
|
||||
step1.setVisible(false);
|
||||
step3.setVisible(true);
|
||||
}
|
||||
|
||||
pool.setConverter(new StringConverter<Pool>() {
|
||||
@Override
|
||||
public String toString(Pool selectedPool) {
|
||||
if(selectedPool == null) {
|
||||
pool.setTooltip(null);
|
||||
return "Fetching pools...";
|
||||
}
|
||||
|
||||
UnitFormat format = Config.get().getUnitFormat() == null ? UnitFormat.DOT : Config.get().getUnitFormat();
|
||||
BitcoinUnit bitcoinUnit = wallet.getAutoUnit();
|
||||
String satsValue = format.formatSatsValue(selectedPool.getDenomination()) + " sats";
|
||||
String btcValue = format.formatBtcValue(selectedPool.getDenomination()) + " BTC";
|
||||
|
||||
pool.setTooltip(bitcoinUnit == BitcoinUnit.BTC ? new Tooltip(satsValue) : new Tooltip(btcValue));
|
||||
return bitcoinUnit == BitcoinUnit.BTC ? btcValue : satsValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pool fromString(String string) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
pool.valueProperty().addListener((observable, oldValue, newValue) -> {
|
||||
if(newValue == null) {
|
||||
selectedPool.setVisible(false);
|
||||
} else {
|
||||
poolFee.setValue(newValue.getFeeValue());
|
||||
poolAnonset.setText(newValue.getAnonymitySet() + " UTXOs");
|
||||
selectedPool.setVisible(true);
|
||||
fetchTx0Preview(newValue);
|
||||
}
|
||||
});
|
||||
|
||||
step4.visibleProperty().addListener((observable, oldValue, newValue) -> {
|
||||
if(newValue && pool.getItems().isEmpty()) {
|
||||
fetchPools();
|
||||
}
|
||||
});
|
||||
|
||||
selectedPool.managedProperty().bind(selectedPool.visibleProperty());
|
||||
selectedPool.setVisible(false);
|
||||
pool.managedProperty().bind(pool.visibleProperty());
|
||||
poolInsufficient.managedProperty().bind(poolInsufficient.visibleProperty());
|
||||
poolInsufficient.visibleProperty().bind(pool.visibleProperty().not());
|
||||
discountFeeBox.managedProperty().bind(discountFeeBox.visibleProperty());
|
||||
discountFeeBox.setVisible(false);
|
||||
nbOutputsBox.managedProperty().bind(nbOutputsBox.visibleProperty());
|
||||
nbOutputsBox.setVisible(false);
|
||||
nbOutputsLoading.managedProperty().bind(nbOutputsLoading.visibleProperty());
|
||||
nbOutputs.managedProperty().bind(nbOutputs.visibleProperty());
|
||||
nbOutputsLoading.visibleProperty().bind(nbOutputs.visibleProperty().not());
|
||||
nbOutputs.setVisible(false);
|
||||
|
||||
tx0PreviewProperty.addListener((observable, oldValue, tx0Preview) -> {
|
||||
if(tx0Preview == null) {
|
||||
nbOutputsBox.setVisible(true);
|
||||
nbOutputsLoading.setText("Calculating...");
|
||||
nbOutputs.setVisible(false);
|
||||
discountFeeBox.setVisible(false);
|
||||
} else {
|
||||
discountFeeBox.setVisible(tx0Preview.getPool().getFeeValue() != tx0Preview.getTx0Data().getFeeValue());
|
||||
discountFee.setValue(tx0Preview.getTx0Data().getFeeValue());
|
||||
nbOutputsBox.setVisible(true);
|
||||
nbOutputs.setText(tx0Preview.getNbPremix() + " UTXOs");
|
||||
nbOutputs.setVisible(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private int getFeeRate(Tx0FeeTarget tx0FeeTarget) {
|
||||
return SparrowMinerFeeSupplier.getFee(Integer.parseInt(tx0FeeTarget.getFeeTarget().getValue()));
|
||||
}
|
||||
|
||||
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 false;
|
||||
}
|
||||
|
||||
public boolean back() {
|
||||
if(step2.isVisible()) {
|
||||
step2.setVisible(false);
|
||||
step1.setVisible(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(step3.isVisible()) {
|
||||
step3.setVisible(false);
|
||||
step2.setVisible(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
if(step4.isVisible()) {
|
||||
step4.setVisible(false);
|
||||
step3.setVisible(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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<Pool> availablePools = poolsService.getValue().stream().toList();
|
||||
if(availablePools.isEmpty()) {
|
||||
pool.setVisible(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";
|
||||
poolInsufficient.setText("No available pools. Select a value over " + (Config.get().getBitcoinUnit() == BitcoinUnit.BTC ? btcValue : satsValue) + ".");
|
||||
}
|
||||
});
|
||||
allPoolsService.start();
|
||||
} else {
|
||||
pool.setDisable(false);
|
||||
pool.setItems(FXCollections.observableList(availablePools));
|
||||
pool.getSelectionModel().select(0);
|
||||
}
|
||||
});
|
||||
poolsService.setOnFailed(workerStateEvent -> {
|
||||
Throwable exception = workerStateEvent.getSource().getException();
|
||||
while(exception.getCause() != null) {
|
||||
exception = exception.getCause();
|
||||
}
|
||||
|
||||
Optional<ButtonType> 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 {
|
||||
pool.setDisable(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
poolsService.start();
|
||||
}
|
||||
|
||||
private void fetchTx0Preview(Pool pool) {
|
||||
if(mixConfig.getScode() == null) {
|
||||
mixConfig.setScode("");
|
||||
EventManager.get().post(new WalletMasterMixConfigChangedEvent(wallet));
|
||||
}
|
||||
|
||||
Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(walletId);
|
||||
if(tx0Previews != null && mixConfig.getScode().equals(whirlpool.getScode())) {
|
||||
Tx0Preview tx0Preview = tx0Previews.getTx0Preview(pool.getPoolId());
|
||||
tx0PreviewProperty.set(tx0Preview);
|
||||
} else {
|
||||
tx0Previews = null;
|
||||
whirlpool.setScode(mixConfig.getScode());
|
||||
whirlpool.setTx0FeeTarget(FEE_TARGETS.get(premixPriority.valueProperty().intValue()));
|
||||
whirlpool.setMixFeeTarget(FEE_TARGETS.get(premixPriority.valueProperty().intValue()));
|
||||
|
||||
Whirlpool.Tx0PreviewsService tx0PreviewsService = new Whirlpool.Tx0PreviewsService(whirlpool, utxoEntries);
|
||||
tx0PreviewsService.setOnRunning(workerStateEvent -> {
|
||||
nbOutputsBox.setVisible(true);
|
||||
nbOutputsLoading.setText("Calculating...");
|
||||
nbOutputs.setVisible(false);
|
||||
discountFeeBox.setVisible(false);
|
||||
tx0PreviewProperty.set(null);
|
||||
});
|
||||
tx0PreviewsService.setOnSucceeded(workerStateEvent -> {
|
||||
tx0Previews = tx0PreviewsService.getValue();
|
||||
Tx0Preview tx0Preview = tx0Previews.getTx0Preview(this.pool.getValue() == null ? pool.getPoolId() : this.pool.getValue().getPoolId());
|
||||
tx0PreviewProperty.set(tx0Preview);
|
||||
});
|
||||
tx0PreviewsService.setOnFailed(workerStateEvent -> {
|
||||
Throwable exception = workerStateEvent.getSource().getException();
|
||||
while(exception.getCause() != null) {
|
||||
exception = exception.getCause();
|
||||
}
|
||||
|
||||
nbOutputsLoading.setText("Error fetching Tx0: " + exception.getMessage());
|
||||
});
|
||||
tx0PreviewsService.start();
|
||||
}
|
||||
}
|
||||
|
||||
public ObjectProperty<Tx0Preview> getTx0PreviewProperty() {
|
||||
return tx0PreviewProperty;
|
||||
}
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool;
|
||||
|
||||
import com.samourai.whirlpool.client.tx0.Tx0Preview;
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
import com.sparrowwallet.sparrow.wallet.UtxoEntry;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.control.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class WhirlpoolDialog extends Dialog<Tx0Preview> {
|
||||
public WhirlpoolDialog(String walletId, Wallet wallet, List<UtxoEntry> utxoEntries) {
|
||||
final DialogPane dialogPane = getDialogPane();
|
||||
AppServices.setStageIcon(dialogPane.getScene().getWindow());
|
||||
AppServices.onEscapePressed(dialogPane.getScene(), this::close);
|
||||
|
||||
try {
|
||||
FXMLLoader whirlpoolLoader = new FXMLLoader(AppServices.class.getResource("whirlpool/whirlpool.fxml"));
|
||||
dialogPane.setContent(whirlpoolLoader.load());
|
||||
WhirlpoolController whirlpoolController = whirlpoolLoader.getController();
|
||||
whirlpoolController.initializeView(walletId, wallet, utxoEntries);
|
||||
|
||||
dialogPane.setPrefWidth(600);
|
||||
dialogPane.setPrefHeight(570);
|
||||
dialogPane.setMinHeight(dialogPane.getPrefHeight());
|
||||
AppServices.moveToActiveWindowScreen(this);
|
||||
|
||||
dialogPane.getStylesheets().add(AppServices.class.getResource("app.css").toExternalForm());
|
||||
dialogPane.getStylesheets().add(AppServices.class.getResource("whirlpool/whirlpool.css").toExternalForm());
|
||||
|
||||
final ButtonType nextButtonType = new javafx.scene.control.ButtonType("Next", ButtonBar.ButtonData.OK_DONE);
|
||||
final ButtonType backButtonType = new javafx.scene.control.ButtonType("Back", ButtonBar.ButtonData.LEFT);
|
||||
final ButtonType cancelButtonType = new javafx.scene.control.ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
|
||||
final ButtonType previewButtonType = new javafx.scene.control.ButtonType("Preview Premix", ButtonBar.ButtonData.APPLY);
|
||||
dialogPane.getButtonTypes().addAll(nextButtonType, backButtonType, cancelButtonType, previewButtonType);
|
||||
|
||||
Button nextButton = (Button)dialogPane.lookupButton(nextButtonType);
|
||||
Button backButton = (Button)dialogPane.lookupButton(backButtonType);
|
||||
Button previewButton = (Button)dialogPane.lookupButton(previewButtonType);
|
||||
previewButton.setDisable(true);
|
||||
whirlpoolController.getTx0PreviewProperty().addListener(new ChangeListener<Tx0Preview>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends Tx0Preview> observable, Tx0Preview oldValue, Tx0Preview newValue) {
|
||||
previewButton.setDisable(newValue == null);
|
||||
}
|
||||
});
|
||||
|
||||
nextButton.managedProperty().bind(nextButton.visibleProperty());
|
||||
backButton.managedProperty().bind(backButton.visibleProperty());
|
||||
previewButton.managedProperty().bind(previewButton.visibleProperty());
|
||||
|
||||
if(wallet.getMasterMixConfig().getScode() == null) {
|
||||
backButton.setDisable(true);
|
||||
}
|
||||
previewButton.visibleProperty().bind(nextButton.visibleProperty().not());
|
||||
|
||||
nextButton.addEventFilter(ActionEvent.ACTION, event -> {
|
||||
if(!whirlpoolController.next()) {
|
||||
nextButton.setVisible(false);
|
||||
previewButton.setDefaultButton(true);
|
||||
}
|
||||
backButton.setDisable(false);
|
||||
event.consume();
|
||||
});
|
||||
|
||||
backButton.addEventFilter(ActionEvent.ACTION, event -> {
|
||||
nextButton.setVisible(true);
|
||||
if(!whirlpoolController.back()) {
|
||||
backButton.setDisable(true);
|
||||
}
|
||||
event.consume();
|
||||
});
|
||||
|
||||
setResultConverter(dialogButton -> dialogButton.getButtonData().equals(ButtonBar.ButtonData.APPLY) ? whirlpoolController.getTx0PreviewProperty().get() : null);
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool;
|
||||
|
||||
public class WhirlpoolException extends Exception {
|
||||
public WhirlpoolException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public WhirlpoolException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
|
@ -1,334 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool;
|
||||
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.google.common.net.HostAndPort;
|
||||
import com.samourai.soroban.client.SorobanConfig;
|
||||
import com.samourai.soroban.client.rpc.RpcClientService;
|
||||
import com.samourai.wallet.constants.SamouraiNetwork;
|
||||
import com.samourai.wallet.util.ExtLibJConfig;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolEventService;
|
||||
import com.sparrowwallet.drongo.Drongo;
|
||||
import com.sparrowwallet.drongo.Network;
|
||||
import com.sparrowwallet.drongo.protocol.ScriptType;
|
||||
import com.sparrowwallet.drongo.wallet.DeterministicSeed;
|
||||
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.WalletTabData;
|
||||
import com.sparrowwallet.sparrow.event.*;
|
||||
import com.sparrowwallet.sparrow.io.Storage;
|
||||
import com.sparrowwallet.sparrow.net.HttpClientService;
|
||||
import com.sparrowwallet.sparrow.soroban.Soroban;
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.scene.input.KeyCodeCombination;
|
||||
import javafx.scene.input.KeyCombination;
|
||||
import javafx.stage.Window;
|
||||
import javafx.util.Duration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.sparrowwallet.sparrow.AppServices.getHttpClientService;
|
||||
import static com.sparrowwallet.sparrow.AppServices.getTorProxy;
|
||||
import static org.bitcoinj.crypto.MnemonicCode.SPARROW_FIX_NFKD_MNEMONIC;
|
||||
|
||||
public class WhirlpoolServices {
|
||||
private static final Logger log = LoggerFactory.getLogger(WhirlpoolServices.class);
|
||||
|
||||
private final Map<String, Whirlpool> whirlpoolMap = new HashMap<>();
|
||||
|
||||
private final SorobanConfig sorobanConfig;
|
||||
|
||||
public WhirlpoolServices() {
|
||||
ExtLibJConfig extLibJConfig = computeExtLibJConfig();
|
||||
this.sorobanConfig = new SorobanConfig(extLibJConfig);
|
||||
System.setProperty(SPARROW_FIX_NFKD_MNEMONIC, "true");
|
||||
}
|
||||
|
||||
private ExtLibJConfig computeExtLibJConfig() {
|
||||
HttpClientService httpClientService = AppServices.getHttpClientService();
|
||||
boolean onion = (getTorProxy() != null);
|
||||
SamouraiNetwork samouraiNetwork = getSamouraiNetwork();
|
||||
return new ExtLibJConfig(samouraiNetwork, onion, Drongo.getProvider(), httpClientService);
|
||||
}
|
||||
|
||||
public SamouraiNetwork getSamouraiNetwork() {
|
||||
try {
|
||||
return SamouraiNetwork.valueOf(Network.get().getName().toUpperCase(Locale.ROOT));
|
||||
} catch(IllegalArgumentException e) {
|
||||
return SamouraiNetwork.TESTNET;
|
||||
}
|
||||
}
|
||||
|
||||
public Whirlpool getWhirlpool(Wallet wallet) {
|
||||
Wallet masterWallet = wallet.isMasterWallet() ? wallet : wallet.getMasterWallet();
|
||||
for(Map.Entry<Wallet, Storage> entry : AppServices.get().getOpenWallets().entrySet()) {
|
||||
if(entry.getKey() == masterWallet) {
|
||||
return whirlpoolMap.get(entry.getValue().getWalletId(entry.getKey()));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Whirlpool getWhirlpool(String walletId) {
|
||||
Whirlpool whirlpool = whirlpoolMap.get(walletId);
|
||||
if(whirlpool == null) {
|
||||
Wallet wallet = AppServices.get().getWallet(walletId);
|
||||
whirlpool = new Whirlpool(wallet == null ? null : wallet.getStoredBlockHeight());
|
||||
whirlpoolMap.put(walletId, whirlpool);
|
||||
}
|
||||
|
||||
return whirlpool;
|
||||
}
|
||||
|
||||
private void bindDebugAccelerator() {
|
||||
List<Window> windows = whirlpoolMap.keySet().stream().map(walletId -> AppServices.get().getWindowForWallet(walletId)).filter(Objects::nonNull).distinct().collect(Collectors.toList());
|
||||
for(Window window : windows) {
|
||||
KeyCombination keyCombination = new KeyCodeCombination(KeyCode.W, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN, KeyCombination.ALT_DOWN);
|
||||
if(window.getScene() != null && !window.getScene().getAccelerators().containsKey(keyCombination)) {
|
||||
window.getScene().getAccelerators().put(keyCombination, () -> {
|
||||
for(Whirlpool whirlpool : whirlpoolMap.values()) {
|
||||
whirlpool.logDebug();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void startAllWhirlpool() {
|
||||
for(Map.Entry<String, Whirlpool> entry : whirlpoolMap.entrySet().stream().filter(entry -> entry.getValue().hasWallet() && !entry.getValue().isStarted()).collect(Collectors.toList())) {
|
||||
Wallet wallet = AppServices.get().getWallet(entry.getKey());
|
||||
Whirlpool whirlpool = entry.getValue();
|
||||
startWhirlpool(wallet, whirlpool, false);
|
||||
}
|
||||
}
|
||||
|
||||
public void startWhirlpool(Wallet wallet, Whirlpool whirlpool, boolean notifyIfMixToMissing) {
|
||||
if(wallet.getMasterMixConfig().getMixOnStartup() != Boolean.FALSE) {
|
||||
try {
|
||||
String mixToWalletId = getWhirlpoolMixToWalletId(wallet.getMasterMixConfig());
|
||||
whirlpool.setMixToWallet(mixToWalletId, wallet.getMasterMixConfig().getMinMixes());
|
||||
} catch(NoSuchElementException e) {
|
||||
if(notifyIfMixToMissing) {
|
||||
AppServices.showWarningDialog("Mix to wallet not open", wallet.getMasterName() + " is configured to mix to " + wallet.getMasterMixConfig().getMixToWalletName() + ", but this wallet is not open. Mix to wallets are required to be open to avoid address reuse.");
|
||||
}
|
||||
}
|
||||
|
||||
if(wallet.getMasterMixConfig() != null) {
|
||||
whirlpool.setPostmixIndexRange(wallet.getMasterMixConfig().getIndexRange());
|
||||
}
|
||||
|
||||
Whirlpool.StartupService startupService = whirlpool.createStartupService();
|
||||
if(whirlpool.getStartupServiceDelay() != null) {
|
||||
startupService.setDelay(whirlpool.getStartupServiceDelay());
|
||||
whirlpool.setStartupServiceDelay(null);
|
||||
}
|
||||
|
||||
startupService.setPeriod(Duration.minutes(2));
|
||||
startupService.setOnSucceeded(workerStateEvent -> {
|
||||
startupService.cancel();
|
||||
});
|
||||
startupService.setOnFailed(workerStateEvent -> {
|
||||
Throwable exception = workerStateEvent.getSource().getException();
|
||||
while(exception.getCause() != null) {
|
||||
exception = exception.getCause();
|
||||
}
|
||||
if(exception instanceof TimeoutException || exception instanceof SocketTimeoutException) {
|
||||
EventManager.get().post(new StatusEvent("Error connecting to Whirlpool server, will retry soon..."));
|
||||
HostAndPort torProxy = getTorProxy();
|
||||
if(torProxy != null) {
|
||||
whirlpool.refreshTorCircuits();
|
||||
}
|
||||
log.error("Error connecting to Whirlpool server: " + exception.getMessage());
|
||||
} else {
|
||||
log.error("Failed to start Whirlpool", workerStateEvent.getSource().getException());
|
||||
}
|
||||
});
|
||||
startupService.start();
|
||||
}
|
||||
}
|
||||
|
||||
private void stopAllWhirlpool() {
|
||||
for(Whirlpool whirlpool : whirlpoolMap.values().stream().filter(Whirlpool::isStarted).collect(Collectors.toList())) {
|
||||
stopWhirlpool(whirlpool, false);
|
||||
}
|
||||
}
|
||||
|
||||
public void stopWhirlpool(Whirlpool whirlpool, boolean notifyOnFailure) {
|
||||
Whirlpool.ShutdownService shutdownService = new Whirlpool.ShutdownService(whirlpool);
|
||||
shutdownService.setOnFailed(workerStateEvent -> {
|
||||
log.error("Failed to stop whirlpool", workerStateEvent.getSource().getException());
|
||||
if(notifyOnFailure) {
|
||||
AppServices.showErrorDialog("Failed to stop whirlpool", workerStateEvent.getSource().getException().getMessage());
|
||||
}
|
||||
});
|
||||
shutdownService.start();
|
||||
}
|
||||
|
||||
public String getWhirlpoolMixToWalletId(MixConfig mixConfig) {
|
||||
if(mixConfig == null || mixConfig.getMixToWalletFile() == null || mixConfig.getMixToWalletName() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return AppServices.get().getOpenWallets().entrySet().stream()
|
||||
.filter(entry -> entry.getValue().getWalletFile().equals(mixConfig.getMixToWalletFile()) && entry.getKey().getName().equals(mixConfig.getMixToWalletName()))
|
||||
.map(entry -> entry.getValue().getWalletId(entry.getKey()))
|
||||
.findFirst().orElseThrow();
|
||||
}
|
||||
|
||||
public Whirlpool getWhirlpoolForMixToWallet(String walletId) {
|
||||
return whirlpoolMap.values().stream().filter(whirlpool -> walletId.equals(whirlpool.getMixToWalletId())).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
public static boolean canWalletMix(Wallet wallet) {
|
||||
return Whirlpool.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 canWatchPostmix(Wallet wallet) {
|
||||
return Whirlpool.WHIRLPOOL_NETWORKS.contains(Network.get())
|
||||
&& wallet.getScriptType() != ScriptType.P2TR //Taproot not yet supported
|
||||
&& wallet.getKeystores().size() == 1;
|
||||
}
|
||||
|
||||
public static List<Wallet> prepareWhirlpoolWallet(Wallet decryptedWallet, String walletId, Storage storage) {
|
||||
Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(walletId);
|
||||
whirlpool.setScode(decryptedWallet.getMasterMixConfig().getScode());
|
||||
whirlpool.setHDWallet(walletId, decryptedWallet);
|
||||
whirlpool.setResyncMixesDone(true);
|
||||
|
||||
Soroban soroban = AppServices.getSorobanServices().getSoroban(walletId);
|
||||
soroban.setHDWallet(decryptedWallet);
|
||||
|
||||
List<Wallet> 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;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void newConnection(ConnectionEvent event) {
|
||||
ExtLibJConfig extLibJConfig = sorobanConfig.getExtLibJConfig();
|
||||
extLibJConfig.setOnion(getTorProxy() != null);
|
||||
getHttpClientService(); //Ensure proxy is updated
|
||||
|
||||
startAllWhirlpool();
|
||||
bindDebugAccelerator();
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void disconnection(DisconnectionEvent event) {
|
||||
//Cancel any scheduled attempts to try reconnect
|
||||
whirlpoolMap.values().stream().filter(whirlpool -> whirlpool.getStartupService() != null).forEach(whirlpool -> whirlpool.getStartupService().cancel());
|
||||
stopAllWhirlpool();
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void newBlock(NewBlockEvent event) {
|
||||
for(Whirlpool whirlpool : whirlpoolMap.values()) {
|
||||
whirlpool.checkIfMixing();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void walletOpened(WalletOpenedEvent event) {
|
||||
String walletId = event.getStorage().getWalletId(event.getWallet());
|
||||
Whirlpool whirlpool = whirlpoolMap.get(walletId);
|
||||
if(whirlpool != null && !whirlpool.isStarted() && AppServices.isConnected()) {
|
||||
startWhirlpool(event.getWallet(), whirlpool, true);
|
||||
}
|
||||
|
||||
Whirlpool mixFromWhirlpool = whirlpoolMap.entrySet().stream()
|
||||
.filter(entry -> {
|
||||
MixConfig mixConfig = AppServices.get().getWallet(entry.getKey()).getMasterMixConfig();
|
||||
return event.getStorage().getWalletFile().equals(mixConfig.getMixToWalletFile()) && event.getWallet().getName().equals(mixConfig.getMixToWalletName());
|
||||
})
|
||||
.map(Map.Entry::getValue).findFirst().orElse(null);
|
||||
|
||||
if(mixFromWhirlpool != null) {
|
||||
mixFromWhirlpool.setMixToWallet(walletId, AppServices.get().getWallet(mixFromWhirlpool.getWalletId()).getMasterMixConfig().getMinMixes());
|
||||
if(mixFromWhirlpool.isStarted()) {
|
||||
//Will automatically restart
|
||||
stopWhirlpool(mixFromWhirlpool, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void walletTabsClosed(WalletTabsClosedEvent event) {
|
||||
for(WalletTabData walletTabData : event.getClosedWalletTabData()) {
|
||||
String walletId = walletTabData.getStorage().getWalletId(walletTabData.getWallet());
|
||||
Whirlpool whirlpool = whirlpoolMap.remove(walletId);
|
||||
if(whirlpool != null) {
|
||||
if(whirlpool.isStarted()) {
|
||||
Whirlpool.ShutdownService shutdownService = new Whirlpool.ShutdownService(whirlpool);
|
||||
shutdownService.setOnSucceeded(workerStateEvent -> {
|
||||
WhirlpoolEventService.getInstance().unregister(whirlpool);
|
||||
});
|
||||
shutdownService.setOnFailed(workerStateEvent -> {
|
||||
log.error("Failed to shutdown whirlpool", workerStateEvent.getSource().getException());
|
||||
});
|
||||
shutdownService.start();
|
||||
} else {
|
||||
//Ensure http clients are shutdown
|
||||
whirlpool.shutdown();
|
||||
WhirlpoolEventService.getInstance().unregister(whirlpool);
|
||||
}
|
||||
}
|
||||
|
||||
Whirlpool mixToWhirlpool = getWhirlpoolForMixToWallet(walletId);
|
||||
if(mixToWhirlpool != null && event.getClosedWalletTabData().stream().noneMatch(walletTabData1 -> walletTabData1.getWalletForm().getWalletId().equals(mixToWhirlpool.getWalletId()))) {
|
||||
mixToWhirlpool.setMixToWallet(null, null);
|
||||
if(mixToWhirlpool.isStarted()) {
|
||||
//Will automatically restart
|
||||
stopWhirlpool(mixToWhirlpool, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void walletHistoryChanged(WalletHistoryChangedEvent event) {
|
||||
Whirlpool whirlpool = getWhirlpool(event.getWallet());
|
||||
if(whirlpool != null) {
|
||||
whirlpool.refreshUtxos();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void whirlpoolIndexHighFrequency(WhirlpoolIndexHighFrequencyEvent event) {
|
||||
Whirlpool whirlpool = getWhirlpool(event.getWallet());
|
||||
if(whirlpool != null && whirlpool.isStarted() && !whirlpool.isStopping()) {
|
||||
log.warn("Rapidly increasing address index detected, temporarily disconnecting " + event.getWallet().getMasterName() + " from Whirlpool");
|
||||
Platform.runLater(() -> {
|
||||
EventManager.get().post(new StatusEvent("Error communicating with Whirlpool, will retry soon..."));
|
||||
whirlpool.setStartupServiceDelay(Duration.minutes(5));
|
||||
stopWhirlpool(whirlpool, false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public SorobanConfig getSorobanConfig() {
|
||||
return sorobanConfig;
|
||||
}
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool.dataPersister;
|
||||
|
||||
import com.samourai.wallet.util.AbstractOrchestrator;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolWallet;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolWalletConfig;
|
||||
import com.samourai.whirlpool.client.wallet.data.dataPersister.DataPersister;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersistableSupplier;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigSupplier;
|
||||
import com.samourai.whirlpool.client.wallet.data.walletState.WalletStateSupplier;
|
||||
import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowWalletStateSupplier;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SparrowDataPersister implements DataPersister {
|
||||
private static final Logger log = LoggerFactory.getLogger(SparrowDataPersister.class);
|
||||
|
||||
private final WalletStateSupplier walletStateSupplier;
|
||||
private final UtxoConfigSupplier utxoConfigSupplier;
|
||||
|
||||
private AbstractOrchestrator persistOrchestrator;
|
||||
private final int persistDelaySeconds;
|
||||
|
||||
public SparrowDataPersister(WhirlpoolWallet whirlpoolWallet, int persistDelaySeconds) throws Exception {
|
||||
WhirlpoolWalletConfig config = whirlpoolWallet.getConfig();
|
||||
String walletIdentifier = whirlpoolWallet.getWalletIdentifier();
|
||||
this.walletStateSupplier = new SparrowWalletStateSupplier(walletIdentifier, config);
|
||||
this.utxoConfigSupplier = new UtxoConfigPersistableSupplier(new SparrowUtxoConfigPersister(walletIdentifier));
|
||||
this.persistDelaySeconds = persistDelaySeconds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open() throws Exception {
|
||||
startPersistOrchestrator();
|
||||
}
|
||||
|
||||
protected void startPersistOrchestrator() {
|
||||
persistOrchestrator = new AbstractOrchestrator(persistDelaySeconds * 1000) {
|
||||
@Override
|
||||
protected void runOrchestrator() {
|
||||
try {
|
||||
persist(false);
|
||||
} catch (Exception e) {
|
||||
log.error("Error persisting Whirlpool data", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
persistOrchestrator.start(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
persistOrchestrator.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() throws Exception {
|
||||
utxoConfigSupplier.load();
|
||||
walletStateSupplier.load();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void persist(boolean force) throws Exception {
|
||||
utxoConfigSupplier.persist(force);
|
||||
walletStateSupplier.persist(force);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WalletStateSupplier getWalletStateSupplier() {
|
||||
return walletStateSupplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UtxoConfigSupplier getUtxoConfigSupplier() {
|
||||
return utxoConfigSupplier;
|
||||
}
|
||||
}
|
|
@ -1,74 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool.dataPersister;
|
||||
|
||||
import com.google.common.collect.MapDifference;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigData;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersisted;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersisterFile;
|
||||
import com.sparrowwallet.drongo.protocol.Sha256Hash;
|
||||
import com.sparrowwallet.drongo.wallet.UtxoMixData;
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
import com.sparrowwallet.sparrow.EventManager;
|
||||
import com.sparrowwallet.sparrow.event.WalletUtxoMixesChangedEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SparrowUtxoConfigPersister extends UtxoConfigPersisterFile {
|
||||
private static final Logger log = LoggerFactory.getLogger(SparrowUtxoConfigPersister.class);
|
||||
|
||||
private final String walletId;
|
||||
|
||||
public SparrowUtxoConfigPersister(String walletId) {
|
||||
super(walletId);
|
||||
this.walletId = walletId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized UtxoConfigData read() throws Exception {
|
||||
Wallet wallet = getWallet();
|
||||
if(wallet == null) {
|
||||
throw new IllegalStateException("Can't find wallet with walletId " + walletId);
|
||||
}
|
||||
|
||||
Map<String, UtxoConfigPersisted> utxoConfigs = wallet.getUtxoMixes().entrySet().stream()
|
||||
.collect(Collectors.toMap(entry -> entry.getKey().toString(), entry -> new UtxoConfigPersisted(entry.getValue().getMixesDone(), entry.getValue().getExpired()),
|
||||
(u, v) -> { throw new IllegalStateException("Duplicate utxo config hashes"); },
|
||||
ConcurrentHashMap::new));
|
||||
|
||||
return new UtxoConfigData(utxoConfigs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doWrite(UtxoConfigData data) throws Exception {
|
||||
Wallet wallet = getWallet();
|
||||
if(wallet == null) {
|
||||
//Wallet is already closed
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, UtxoConfigPersisted> currentData = new HashMap<>(data.getUtxoConfigs());
|
||||
Map<Sha256Hash, UtxoMixData> changedUtxoMixes = currentData.entrySet().stream()
|
||||
.collect(Collectors.toMap(entry -> Sha256Hash.wrap(entry.getKey()), entry -> new UtxoMixData(entry.getValue().getMixsDone(), entry.getValue().getExpired()),
|
||||
(u, v) -> { throw new IllegalStateException("Duplicate utxo config hashes"); },
|
||||
HashMap::new));
|
||||
|
||||
MapDifference<Sha256Hash, UtxoMixData> mapDifference = Maps.difference(changedUtxoMixes, wallet.getUtxoMixes());
|
||||
Map<Sha256Hash, UtxoMixData> removedUtxoMixes = mapDifference.entriesOnlyOnRight();
|
||||
wallet.getUtxoMixes().putAll(changedUtxoMixes);
|
||||
wallet.getUtxoMixes().keySet().removeAll(removedUtxoMixes.keySet());
|
||||
|
||||
if(!changedUtxoMixes.isEmpty() || !removedUtxoMixes.isEmpty()) {
|
||||
EventManager.get().post(new WalletUtxoMixesChangedEvent(wallet, changedUtxoMixes, removedUtxoMixes));
|
||||
}
|
||||
}
|
||||
|
||||
private Wallet getWallet() {
|
||||
return AppServices.get().getOpenWallets().entrySet().stream().filter(entry -> entry.getValue().getWalletId(entry.getKey()).equals(walletId)).map(Map.Entry::getKey).findFirst().orElse(null);
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool.dataSource;
|
||||
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.samourai.wallet.api.backend.beans.WalletResponse;
|
||||
import com.samourai.wallet.chain.ChainSupplier;
|
||||
import com.sparrowwallet.drongo.Utils;
|
||||
import com.sparrowwallet.drongo.protocol.Sha256Hash;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
import com.sparrowwallet.sparrow.EventManager;
|
||||
import com.sparrowwallet.sparrow.event.NewBlockEvent;
|
||||
|
||||
public class SparrowChainSupplier implements ChainSupplier {
|
||||
private final int storedBlockHeight;
|
||||
private WalletResponse.InfoBlock latestBlock;
|
||||
|
||||
public SparrowChainSupplier(Integer storedBlockHeight) {
|
||||
this.storedBlockHeight = AppServices.getCurrentBlockHeight() == null ? (storedBlockHeight != null ? storedBlockHeight : 0) : AppServices.getCurrentBlockHeight();
|
||||
}
|
||||
|
||||
public void open() {
|
||||
this.latestBlock = computeLatestBlock();
|
||||
EventManager.get().register(this);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
EventManager.get().unregister(this);
|
||||
}
|
||||
|
||||
private WalletResponse.InfoBlock computeLatestBlock() {
|
||||
WalletResponse.InfoBlock latestBlock = new WalletResponse.InfoBlock();
|
||||
latestBlock.height = AppServices.getCurrentBlockHeight() == null ? storedBlockHeight : AppServices.getCurrentBlockHeight();
|
||||
latestBlock.hash = AppServices.getLatestBlockHeader() == null ? Sha256Hash.ZERO_HASH.toString() :
|
||||
Utils.bytesToHex(Sha256Hash.twiceOf(AppServices.getLatestBlockHeader().bitcoinSerialize()).getReversedBytes());
|
||||
latestBlock.time = AppServices.getLatestBlockHeader() == null ? 1 : AppServices.getLatestBlockHeader().getTime();
|
||||
return latestBlock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WalletResponse.InfoBlock getLatestBlock() {
|
||||
return latestBlock;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void newBlock(NewBlockEvent event) {
|
||||
this.latestBlock = computeLatestBlock();
|
||||
}
|
||||
}
|
|
@ -1,167 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool.dataSource;
|
||||
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.samourai.wallet.api.backend.IPushTx;
|
||||
import com.samourai.wallet.api.backend.ISweepBackend;
|
||||
import com.samourai.wallet.api.backend.seenBackend.ISeenBackend;
|
||||
import com.samourai.wallet.api.backend.seenBackend.SeenBackendWithFallback;
|
||||
import com.samourai.wallet.hd.HD_Wallet;
|
||||
import com.samourai.wallet.httpClient.HttpUsage;
|
||||
import com.samourai.wallet.httpClient.IHttpClient;
|
||||
import com.samourai.wallet.util.ExtLibJConfig;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolWallet;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolWalletConfig;
|
||||
import com.samourai.whirlpool.client.wallet.data.coordinator.CoordinatorSupplier;
|
||||
import com.samourai.whirlpool.client.wallet.data.dataSource.AbstractDataSource;
|
||||
import com.samourai.whirlpool.client.wallet.data.dataSource.DataSourceConfig;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxo.UtxoSupplier;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigSupplier;
|
||||
import com.samourai.whirlpool.client.wallet.data.walletState.WalletStateSupplier;
|
||||
import com.sparrowwallet.drongo.ExtendedKey;
|
||||
import com.sparrowwallet.drongo.Network;
|
||||
import com.sparrowwallet.drongo.Utils;
|
||||
import com.sparrowwallet.drongo.protocol.Transaction;
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
import com.sparrowwallet.sparrow.EventManager;
|
||||
import com.sparrowwallet.sparrow.event.WalletAddressesChangedEvent;
|
||||
import com.sparrowwallet.sparrow.event.WalletHistoryChangedEvent;
|
||||
import com.sparrowwallet.sparrow.net.ElectrumServer;
|
||||
import com.sparrowwallet.sparrow.whirlpool.Whirlpool;
|
||||
import javafx.application.Platform;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class SparrowDataSource extends AbstractDataSource {
|
||||
private static final Logger log = LoggerFactory.getLogger(SparrowDataSource.class);
|
||||
|
||||
private final ISeenBackend seenBackend;
|
||||
private final IPushTx pushTx;
|
||||
private final SparrowUtxoSupplier utxoSupplier;
|
||||
|
||||
public SparrowDataSource(
|
||||
WhirlpoolWallet whirlpoolWallet,
|
||||
HD_Wallet bip44w,
|
||||
WalletStateSupplier walletStateSupplier,
|
||||
UtxoConfigSupplier utxoConfigSupplier,
|
||||
DataSourceConfig dataSourceConfig)
|
||||
throws Exception {
|
||||
super(whirlpoolWallet, bip44w, walletStateSupplier, dataSourceConfig);
|
||||
this.seenBackend = computeSeenBackend(whirlpoolWallet.getConfig());
|
||||
this.pushTx = computePushTx();
|
||||
NetworkParameters params = whirlpoolWallet.getConfig().getSamouraiNetwork().getParams();
|
||||
this.utxoSupplier = new SparrowUtxoSupplier(walletSupplier, utxoConfigSupplier, dataSourceConfig, params);
|
||||
}
|
||||
|
||||
private ISeenBackend computeSeenBackend(WhirlpoolWalletConfig whirlpoolWalletConfig) {
|
||||
ExtLibJConfig extLibJConfig = whirlpoolWalletConfig.getSorobanConfig().getExtLibJConfig();
|
||||
IHttpClient httpClient = extLibJConfig.getHttpClientService().getHttpClient(HttpUsage.BACKEND);
|
||||
ISeenBackend sparrowSeenBackend = new SparrowSeenBackend(getWhirlpoolWallet().getWalletIdentifier(), httpClient);
|
||||
NetworkParameters params = whirlpoolWalletConfig.getSamouraiNetwork().getParams();
|
||||
return SeenBackendWithFallback.withOxt(sparrowSeenBackend, params);
|
||||
}
|
||||
|
||||
private IPushTx computePushTx() {
|
||||
return new IPushTx() {
|
||||
@Override
|
||||
public String pushTx(String hexTx) throws Exception {
|
||||
Transaction transaction = new Transaction(Utils.hexToBytes(hexTx));
|
||||
ElectrumServer electrumServer = new ElectrumServer();
|
||||
return electrumServer.broadcastTransactionPrivately(transaction).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String pushTx(String txHex, Collection<Integer> strictModeVouts) throws Exception {
|
||||
return pushTx(txHex);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open(CoordinatorSupplier coordinatorSupplier) throws Exception {
|
||||
super.open(coordinatorSupplier);
|
||||
EventManager.get().register(this);
|
||||
((SparrowChainSupplier)getDataSourceConfig().getChainSupplier()).open();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void load(boolean initial) throws Exception {
|
||||
super.load(initial);
|
||||
utxoSupplier.refresh();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
EventManager.get().unregister(this);
|
||||
((SparrowChainSupplier)getDataSourceConfig().getChainSupplier()).close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPushTx getPushTx() {
|
||||
return pushTx;
|
||||
}
|
||||
|
||||
public static Wallet getWallet(String zpub) {
|
||||
return AppServices.get().getOpenWallets().keySet().stream()
|
||||
.filter(wallet -> {
|
||||
try {
|
||||
List<ExtendedKey.Header> headers = ExtendedKey.Header.getHeaders(Network.get());
|
||||
ExtendedKey.Header header = headers.stream().filter(head -> head.getDefaultScriptType().equals(wallet.getScriptType()) && !head.isPrivateKey()).findFirst().orElse(ExtendedKey.Header.xpub);
|
||||
ExtendedKey extPubKey = wallet.getKeystores().get(0).getExtendedPublicKey();
|
||||
return extPubKey.toString(header).equals(zpub);
|
||||
} catch(Exception e) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void walletHistoryChanged(WalletHistoryChangedEvent event) {
|
||||
refreshWallet(event.getWalletId(), event.getWallet(), 0);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void walletAddressesChanged(WalletAddressesChangedEvent event) {
|
||||
refreshWallet(event.getWalletId(), event.getWallet(), 0);
|
||||
}
|
||||
|
||||
private void refreshWallet(String walletId, Wallet wallet, int i) {
|
||||
try {
|
||||
// prefix matching <prefix>:master, :Premix, :Postmix
|
||||
String walletIdentifierPrefix = getWhirlpoolWallet().getWalletIdentifier().replace(":master", "");
|
||||
// match <prefix>:master, :Premix, :Postmix
|
||||
if(walletId.startsWith(walletIdentifierPrefix) && (wallet.isWhirlpoolMasterWallet() || wallet.isWhirlpoolChildWallet())) {
|
||||
//Workaround to avoid refreshing the wallet after it has been opened, but before it has been started
|
||||
Whirlpool whirlpool = AppServices.getWhirlpoolServices().getWhirlpool(wallet);
|
||||
if(whirlpool != null && whirlpool.isStarting() && i < 1000) {
|
||||
Platform.runLater(() -> refreshWallet(walletId, wallet, i+1));
|
||||
} else {
|
||||
utxoSupplier.refresh();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error refreshing wallet", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ISweepBackend getSweepBackend() {
|
||||
return null; // not necessary
|
||||
}
|
||||
|
||||
@Override
|
||||
public ISeenBackend getSeenBackend() {
|
||||
return seenBackend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UtxoSupplier getUtxoSupplier() {
|
||||
return utxoSupplier;
|
||||
}
|
||||
}
|
|
@ -1,109 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool.dataSource;
|
||||
|
||||
import com.samourai.wallet.client.indexHandler.AbstractIndexHandler;
|
||||
import com.sparrowwallet.drongo.KeyPurpose;
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.drongo.wallet.WalletNode;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
import com.sparrowwallet.sparrow.EventManager;
|
||||
import com.sparrowwallet.sparrow.event.WalletGapLimitChangedEvent;
|
||||
import com.sparrowwallet.sparrow.event.WalletMixConfigChangedEvent;
|
||||
import com.sparrowwallet.sparrow.event.WhirlpoolIndexHighFrequencyEvent;
|
||||
|
||||
public class SparrowIndexHandler extends AbstractIndexHandler {
|
||||
private final Wallet wallet;
|
||||
private final WalletNode walletNode;
|
||||
private final int defaultValue;
|
||||
|
||||
private static final long PERIOD = 1000 * 60 * 10L; //Period of 10 minutes
|
||||
private long periodStart;
|
||||
private int periodCount;
|
||||
|
||||
public SparrowIndexHandler(Wallet wallet, WalletNode walletNode) {
|
||||
this(wallet, walletNode, 0);
|
||||
}
|
||||
|
||||
public SparrowIndexHandler(Wallet wallet, WalletNode walletNode, int defaultValue) {
|
||||
this.wallet = wallet;
|
||||
this.walletNode = walletNode;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int get() {
|
||||
return Math.max(getCurrentIndex(), getStoredIndex());
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int getAndIncrement() {
|
||||
int index = get();
|
||||
set(index + 1);
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void set(int value) {
|
||||
setStoredIndex(value);
|
||||
ensureSufficientGapLimit(value);
|
||||
}
|
||||
|
||||
private int getCurrentIndex() {
|
||||
Integer currentIndex = walletNode.getHighestUsedIndex();
|
||||
return currentIndex == null ? defaultValue : currentIndex + 1;
|
||||
}
|
||||
|
||||
private int getStoredIndex() {
|
||||
if(wallet.getMixConfig() != null) {
|
||||
if(walletNode.getKeyPurpose() == KeyPurpose.RECEIVE) {
|
||||
return wallet.getMixConfig().getReceiveIndex();
|
||||
} else if(walletNode.getKeyPurpose() == KeyPurpose.CHANGE) {
|
||||
return wallet.getMixConfig().getChangeIndex();
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
private void setStoredIndex(int index) {
|
||||
if(wallet.getMixConfig() != null) {
|
||||
if(walletNode.getKeyPurpose() == KeyPurpose.RECEIVE && wallet.getMixConfig().getReceiveIndex() != index) {
|
||||
wallet.getMixConfig().setReceiveIndex(index);
|
||||
EventManager.get().post(new WalletMixConfigChangedEvent(wallet));
|
||||
} else if(walletNode.getKeyPurpose() == KeyPurpose.CHANGE && wallet.getMixConfig().getChangeIndex() != index) {
|
||||
wallet.getMixConfig().setChangeIndex(index);
|
||||
EventManager.get().post(new WalletMixConfigChangedEvent(wallet));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureSufficientGapLimit(int index) {
|
||||
int highestUsedIndex = getCurrentIndex() - 1;
|
||||
int existingGapLimit = wallet.getGapLimit();
|
||||
if(index > highestUsedIndex + existingGapLimit) {
|
||||
wallet.setGapLimit(Math.max(wallet.getGapLimit(), index - highestUsedIndex));
|
||||
EventManager.get().post(new WalletGapLimitChangedEvent(getWalletId(), wallet, existingGapLimit));
|
||||
checkFrequency();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkFrequency() {
|
||||
if(periodStart > 0 && System.currentTimeMillis() - periodStart < PERIOD) {
|
||||
periodCount++;
|
||||
} else {
|
||||
periodStart = System.currentTimeMillis();
|
||||
periodCount = 0;
|
||||
}
|
||||
|
||||
if(periodCount >= Wallet.DEFAULT_LOOKAHEAD) {
|
||||
EventManager.get().post(new WhirlpoolIndexHighFrequencyEvent(wallet));
|
||||
}
|
||||
}
|
||||
|
||||
private String getWalletId() {
|
||||
try {
|
||||
return AppServices.get().getOpenWallets().get(wallet).getWalletId(wallet);
|
||||
} catch(Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool.dataSource;
|
||||
|
||||
import com.samourai.wallet.api.backend.MinerFeeTarget;
|
||||
import com.samourai.whirlpool.client.wallet.data.minerFee.MinerFeeSupplier;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SparrowMinerFeeSupplier implements MinerFeeSupplier {
|
||||
private static final int FALLBACK_FEE_RATE = 75;
|
||||
|
||||
public static SparrowMinerFeeSupplier instance;
|
||||
|
||||
public static SparrowMinerFeeSupplier getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new SparrowMinerFeeSupplier();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private SparrowMinerFeeSupplier() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFee(MinerFeeTarget feeTarget) {
|
||||
return getFee(Integer.parseInt(feeTarget.getValue()));
|
||||
}
|
||||
|
||||
public static int getFee(int targetBlocks) {
|
||||
if(AppServices.getTargetBlockFeeRates() == null) {
|
||||
return FALLBACK_FEE_RATE;
|
||||
}
|
||||
|
||||
return getMinimumFeeForTarget(targetBlocks);
|
||||
}
|
||||
|
||||
private static Integer getMinimumFeeForTarget(int targetBlocks) {
|
||||
List<Map.Entry<Integer, Double>> feeRates = new ArrayList<>(AppServices.getTargetBlockFeeRates().entrySet());
|
||||
Collections.reverse(feeRates);
|
||||
for(Map.Entry<Integer, Double> feeRate : feeRates) {
|
||||
if(feeRate.getKey() <= targetBlocks) {
|
||||
return feeRate.getValue().intValue();
|
||||
}
|
||||
}
|
||||
|
||||
return feeRates.get(0).getValue().intValue();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,86 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool.dataSource;
|
||||
|
||||
import com.samourai.wallet.client.indexHandler.IIndexHandler;
|
||||
import com.samourai.whirlpool.client.mix.handler.DestinationType;
|
||||
import com.samourai.whirlpool.client.mix.handler.IPostmixHandler;
|
||||
import com.samourai.whirlpool.client.mix.handler.MixDestination;
|
||||
import com.samourai.whirlpool.client.utils.ClientUtils;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolWalletService;
|
||||
import com.samourai.whirlpool.client.wallet.beans.IndexRange;
|
||||
import com.sparrowwallet.drongo.KeyPurpose;
|
||||
import com.sparrowwallet.drongo.address.Address;
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.drongo.wallet.WalletNode;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SparrowPostmixHandler implements IPostmixHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(SparrowPostmixHandler.class);
|
||||
|
||||
private final WhirlpoolWalletService whirlpoolWalletService;
|
||||
private final Wallet wallet;
|
||||
private final KeyPurpose keyPurpose;
|
||||
|
||||
protected MixDestination destination;
|
||||
|
||||
public SparrowPostmixHandler(WhirlpoolWalletService whirlpoolWalletService, Wallet wallet, KeyPurpose keyPurpose) {
|
||||
this.whirlpoolWalletService = whirlpoolWalletService;
|
||||
this.wallet = wallet;
|
||||
this.keyPurpose = keyPurpose;
|
||||
}
|
||||
|
||||
protected IndexRange getIndexRange() {
|
||||
return IndexRange.FULL;
|
||||
}
|
||||
|
||||
public Wallet getWallet() {
|
||||
return wallet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final MixDestination computeDestinationNext() throws Exception {
|
||||
// use "unconfirmed" index to avoid huge index gaps on multiple mix failures
|
||||
int index = ClientUtils.computeNextReceiveAddressIndex(getIndexHandler(), getIndexRange());
|
||||
this.destination = computeDestination(index);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(
|
||||
"Mixing to "
|
||||
+ destination.getType()
|
||||
+ " -> receiveAddress="
|
||||
+ destination.getAddress()
|
||||
+ ", path="
|
||||
+ destination.getPath());
|
||||
}
|
||||
return destination;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MixDestination computeDestination(int index) throws Exception {
|
||||
// address
|
||||
WalletNode node = new WalletNode(wallet, keyPurpose, index);
|
||||
Address address = node.getAddress();
|
||||
String path = "xpub/" + keyPurpose.getPathIndex().num() + "/" + index;
|
||||
|
||||
log.info("Mixing to external xPub -> receiveAddress=" + address + ", path=" + path);
|
||||
return new MixDestination(DestinationType.XPUB, index, address.toString(), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMixFail() {
|
||||
if(destination != null) {
|
||||
// cancel unconfirmed postmix index if output was not registered yet
|
||||
getIndexHandler().cancelUnconfirmed(destination.getIndex());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRegisterOutput() {
|
||||
// confirm postmix index on REGISTER_OUTPUT success
|
||||
getIndexHandler().confirmUnconfirmed(destination.getIndex());
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIndexHandler getIndexHandler() {
|
||||
return whirlpoolWalletService.whirlpoolWallet().getWalletStateSupplier().getIndexHandlerExternal();
|
||||
}
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool.dataSource;
|
||||
|
||||
import com.samourai.wallet.api.backend.seenBackend.ISeenBackend;
|
||||
import com.samourai.wallet.api.backend.seenBackend.SeenResponse;
|
||||
import com.samourai.wallet.httpClient.IHttpClient;
|
||||
import com.sparrowwallet.drongo.address.Address;
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.drongo.wallet.WalletNode;
|
||||
import com.sparrowwallet.sparrow.AppServices;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SparrowSeenBackend implements ISeenBackend {
|
||||
private final String walletId;
|
||||
private final IHttpClient httpClient;
|
||||
|
||||
public SparrowSeenBackend(String walletId, IHttpClient httpClient) {
|
||||
this.walletId = walletId;
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SeenResponse seen(Collection<String> addresses) throws Exception {
|
||||
Wallet wallet = AppServices.get().getWallet(walletId);
|
||||
Map<Address, WalletNode> addressMap = wallet.getWalletAddresses();
|
||||
for(Wallet childWallet : wallet.getChildWallets()) {
|
||||
if(!childWallet.isNested()) {
|
||||
addressMap.putAll(childWallet.getWalletAddresses());
|
||||
}
|
||||
}
|
||||
|
||||
Map<String,Boolean> map = new LinkedHashMap<>();
|
||||
for(String address : addresses) {
|
||||
WalletNode walletNode = addressMap.get(Address.fromString(address));
|
||||
if(walletNode != null) {
|
||||
int highestUsedIndex = walletNode.getWallet().getNode(walletNode.getKeyPurpose()).getHighestUsedIndex();
|
||||
map.put(address, walletNode.getIndex() <= highestUsedIndex);
|
||||
}
|
||||
}
|
||||
|
||||
return new SeenResponse(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean seen(String address) throws Exception {
|
||||
SeenResponse seenResponse = seen(List.of(address));
|
||||
return seenResponse.isSeen(address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IHttpClient getHttpClient() {
|
||||
return httpClient;
|
||||
}
|
||||
}
|
|
@ -1,151 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool.dataSource;
|
||||
|
||||
import com.samourai.wallet.api.backend.beans.UnspentOutput;
|
||||
import com.samourai.wallet.api.backend.beans.WalletResponse;
|
||||
import com.samourai.wallet.bipWallet.BipWallet;
|
||||
import com.samourai.wallet.bipWallet.WalletSupplier;
|
||||
import com.samourai.whirlpool.client.wallet.WhirlpoolWallet;
|
||||
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolUtxo;
|
||||
import com.samourai.whirlpool.client.wallet.data.dataSource.DataSourceConfig;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxo.BasicUtxoSupplier;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxo.UtxoData;
|
||||
import com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigSupplier;
|
||||
import com.sparrowwallet.drongo.KeyPurpose;
|
||||
import com.sparrowwallet.drongo.protocol.Sha256Hash;
|
||||
import com.sparrowwallet.drongo.protocol.TransactionInput;
|
||||
import com.sparrowwallet.drongo.protocol.TransactionOutput;
|
||||
import com.sparrowwallet.drongo.wallet.*;
|
||||
import com.sparrowwallet.sparrow.whirlpool.Whirlpool;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// manages utxos & wallet indexes
|
||||
public class SparrowUtxoSupplier extends BasicUtxoSupplier {
|
||||
private static final Logger log = LoggerFactory.getLogger(SparrowUtxoSupplier.class);
|
||||
|
||||
public SparrowUtxoSupplier(
|
||||
WalletSupplier walletSupplier,
|
||||
UtxoConfigSupplier utxoConfigSupplier,
|
||||
DataSourceConfig dataSourceConfig,
|
||||
NetworkParameters params) {
|
||||
super(walletSupplier, utxoConfigSupplier, dataSourceConfig, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh() throws Exception {
|
||||
Map<Sha256Hash, BlockTransaction> allTransactions = new HashMap<>();
|
||||
Map<Sha256Hash, String> allTransactionsXpubs = new HashMap<>();
|
||||
List<WalletResponse.Tx> txes = new ArrayList<>();
|
||||
List<UnspentOutput> unspentOutputs = new ArrayList<>();
|
||||
int storedBlockHeight = 0;
|
||||
|
||||
Collection<BipWallet> bipWallets = getWalletSupplier().getWallets();
|
||||
for(BipWallet bipWallet : bipWallets) {
|
||||
String zpub = bipWallet.getBipPub();
|
||||
Wallet wallet = SparrowDataSource.getWallet(zpub);
|
||||
if(wallet == null) {
|
||||
log.debug("No wallet for " + zpub + " found");
|
||||
continue;
|
||||
}
|
||||
|
||||
Map<Sha256Hash, BlockTransaction> walletTransactions = wallet.getWalletTransactions();
|
||||
allTransactions.putAll(walletTransactions);
|
||||
String xpub = bipWallet.getXPub();
|
||||
walletTransactions.keySet().forEach(txid -> allTransactionsXpubs.put(txid, xpub));
|
||||
if(wallet.getStoredBlockHeight() != null) {
|
||||
storedBlockHeight = Math.max(storedBlockHeight, wallet.getStoredBlockHeight());
|
||||
}
|
||||
|
||||
// update wallet index: receive
|
||||
int receiveIndex = wallet.getNode(KeyPurpose.RECEIVE).getHighestUsedIndex() == null ? 0 : wallet.getNode(KeyPurpose.RECEIVE).getHighestUsedIndex() + 1;
|
||||
int account_index = wallet.getMixConfig() != null ? Math.max(receiveIndex, wallet.getMixConfig().getReceiveIndex()) : receiveIndex;
|
||||
bipWallet.getIndexHandlerReceive().set(account_index, false);
|
||||
|
||||
// update wallet index: change
|
||||
int changeIndex = wallet.getNode(KeyPurpose.CHANGE).getHighestUsedIndex() == null ? 0 : wallet.getNode(KeyPurpose.CHANGE).getHighestUsedIndex() + 1;
|
||||
int change_index = wallet.getMixConfig() != null ? Math.max(changeIndex, wallet.getMixConfig().getChangeIndex()) : changeIndex;
|
||||
bipWallet.getIndexHandlerChange().set(change_index, false);
|
||||
|
||||
for(Map.Entry<BlockTransactionHashIndex, WalletNode> utxo : wallet.getSpendableUtxos().entrySet()) {
|
||||
BlockTransaction blockTransaction = wallet.getWalletTransaction(utxo.getKey().getHash());
|
||||
if(blockTransaction != null) {
|
||||
unspentOutputs.add(Whirlpool.getUnspentOutput(utxo.getValue(), blockTransaction, (int)utxo.getKey().getIndex()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(BlockTransaction blockTransaction : allTransactions.values()) {
|
||||
WalletResponse.Tx tx = new WalletResponse.Tx();
|
||||
tx.block_height = blockTransaction.getHeight();
|
||||
tx.hash = blockTransaction.getHashAsString();
|
||||
tx.locktime = blockTransaction.getTransaction().getLocktime();
|
||||
tx.version = (int)blockTransaction.getTransaction().getVersion();
|
||||
|
||||
tx.inputs = new WalletResponse.TxInput[blockTransaction.getTransaction().getInputs().size()];
|
||||
for(int i = 0; i < blockTransaction.getTransaction().getInputs().size(); i++) {
|
||||
TransactionInput txInput = blockTransaction.getTransaction().getInputs().get(i);
|
||||
tx.inputs[i] = new WalletResponse.TxInput();
|
||||
tx.inputs[i].vin = txInput.getIndex();
|
||||
tx.inputs[i].sequence = txInput.getSequenceNumber();
|
||||
if(allTransactionsXpubs.containsKey(txInput.getOutpoint().getHash())) {
|
||||
tx.inputs[i].prev_out = new WalletResponse.TxOut();
|
||||
tx.inputs[i].prev_out.txid = txInput.getOutpoint().getHash().toString();
|
||||
tx.inputs[i].prev_out.vout = (int)txInput.getOutpoint().getIndex();
|
||||
|
||||
BlockTransaction spentTransaction = allTransactions.get(txInput.getOutpoint().getHash());
|
||||
if(spentTransaction != null) {
|
||||
TransactionOutput spentOutput = spentTransaction.getTransaction().getOutputs().get((int)txInput.getOutpoint().getIndex());
|
||||
tx.inputs[i].prev_out.value = spentOutput.getValue();
|
||||
}
|
||||
|
||||
tx.inputs[i].prev_out.xpub = new UnspentOutput.Xpub();
|
||||
tx.inputs[i].prev_out.xpub.m = allTransactionsXpubs.get(txInput.getOutpoint().getHash());
|
||||
}
|
||||
}
|
||||
|
||||
tx.out = new WalletResponse.TxOutput[blockTransaction.getTransaction().getOutputs().size()];
|
||||
for(int i = 0; i < blockTransaction.getTransaction().getOutputs().size(); i++) {
|
||||
TransactionOutput txOutput = blockTransaction.getTransaction().getOutputs().get(i);
|
||||
tx.out[i] = new WalletResponse.TxOutput();
|
||||
tx.out[i].n = txOutput.getIndex();
|
||||
tx.out[i].value = txOutput.getValue();
|
||||
tx.out[i].xpub = new UnspentOutput.Xpub();
|
||||
tx.out[i].xpub.m = allTransactionsXpubs.get(blockTransaction.getHash());
|
||||
}
|
||||
|
||||
txes.add(tx);
|
||||
}
|
||||
|
||||
// update utxos
|
||||
UnspentOutput[] uos = unspentOutputs.toArray(new UnspentOutput[0]);
|
||||
WalletResponse.Tx[] txs = txes.toArray(new WalletResponse.Tx[0]);
|
||||
UtxoData utxoData = new UtxoData(uos, txs, storedBlockHeight);
|
||||
setValue(utxoData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] _getPrivKeyBip47(UnspentOutput utxo) throws Exception {
|
||||
BipWallet bipWallet = getWalletSupplier().getWalletByXPub(utxo.xpub.m);
|
||||
Wallet wallet = SparrowDataSource.getWallet(bipWallet.getBipPub());
|
||||
Map<BlockTransactionHashIndex, WalletNode> walletUtxos = wallet.getWalletUtxos();
|
||||
WalletNode node = walletUtxos.entrySet().stream()
|
||||
.filter(entry -> entry.getKey().getHash().equals(Sha256Hash.wrap(utxo.tx_hash)) && entry.getKey().getIndex() == utxo.tx_output_n)
|
||||
.map(Map.Entry::getValue)
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalStateException("Cannot find UTXO " + utxo));
|
||||
|
||||
if(node.getWallet().isBip47()) {
|
||||
try {
|
||||
Keystore keystore = node.getWallet().getKeystores().get(0);
|
||||
return keystore.getKey(node).getPrivKeyBytes();
|
||||
} catch(Exception e) {
|
||||
log.error("Error getting private key", e);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,141 +0,0 @@
|
|||
package com.sparrowwallet.sparrow.whirlpool.dataSource;
|
||||
|
||||
import com.samourai.wallet.bipWallet.BipDerivation;
|
||||
import com.samourai.wallet.bipWallet.BipWallet;
|
||||
import com.samourai.wallet.client.indexHandler.IIndexHandler;
|
||||
import com.samourai.wallet.constants.SamouraiAccount;
|
||||
import com.samourai.wallet.hd.Chain;
|
||||
import com.samourai.whirlpool.client.wallet.beans.ExternalDestination;
|
||||
import com.samourai.whirlpool.client.wallet.data.walletState.WalletStateSupplier;
|
||||
import com.samourai.whirlpool.client.whirlpool.WhirlpoolClientConfig;
|
||||
import com.sparrowwallet.drongo.KeyPurpose;
|
||||
import com.sparrowwallet.drongo.crypto.ChildNumber;
|
||||
import com.sparrowwallet.drongo.wallet.MixConfig;
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.drongo.wallet.WalletNode;
|
||||
import com.sparrowwallet.sparrow.whirlpool.Whirlpool;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SparrowWalletStateSupplier implements WalletStateSupplier {
|
||||
private final String walletId;
|
||||
private final Map<String, IIndexHandler> indexHandlerWallets;
|
||||
private final WhirlpoolClientConfig config;
|
||||
private IIndexHandler externalIndexHandler;
|
||||
|
||||
public SparrowWalletStateSupplier(String walletId, WhirlpoolClientConfig config) {
|
||||
this.walletId = walletId;
|
||||
this.indexHandlerWallets = new LinkedHashMap<>();
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIndexHandler getIndexHandlerWallet(BipWallet bipWallet, Chain chain) {
|
||||
SamouraiAccount samouraiAccount = bipWallet.getAccount();
|
||||
String key = mapKey(bipWallet, chain);
|
||||
IIndexHandler indexHandler = indexHandlerWallets.get(key);
|
||||
if(indexHandler == null) {
|
||||
Wallet wallet = findWallet(samouraiAccount);
|
||||
KeyPurpose keyPurpose = (chain == Chain.RECEIVE ? KeyPurpose.RECEIVE : KeyPurpose.CHANGE);
|
||||
WalletNode walletNode = wallet.getNode(keyPurpose);
|
||||
|
||||
//Ensure mix config is present
|
||||
MixConfig mixConfig = wallet.getMixConfig();
|
||||
if(mixConfig == null) {
|
||||
mixConfig = new MixConfig();
|
||||
wallet.setMixConfig(mixConfig);
|
||||
}
|
||||
|
||||
indexHandler = new SparrowIndexHandler(wallet, walletNode, 0);
|
||||
indexHandlerWallets.put(key, indexHandler);
|
||||
}
|
||||
|
||||
return indexHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIndexHandler getIndexHandlerExternal() {
|
||||
ExternalDestination externalDestination = config.getExternalDestination();
|
||||
if(externalDestination == null) {
|
||||
throw new IllegalStateException("External destination has not been set");
|
||||
}
|
||||
|
||||
if(externalIndexHandler == null) {
|
||||
Wallet externalWallet = null;
|
||||
if(externalDestination.getPostmixHandlerCustom() != null
|
||||
&& externalDestination.getPostmixHandlerCustom() instanceof SparrowPostmixHandler sparrowPostmixHandler) {
|
||||
externalWallet = sparrowPostmixHandler.getWallet();
|
||||
} else if(externalDestination.getXpub() != null) {
|
||||
externalWallet = SparrowDataSource.getWallet(externalDestination.getXpub());
|
||||
}
|
||||
|
||||
if(externalWallet == null) {
|
||||
throw new IllegalStateException("Cannot find wallet for external destination " + externalDestination);
|
||||
}
|
||||
|
||||
//Ensure mix config is present to save indexes
|
||||
MixConfig mixConfig = externalWallet.getMixConfig();
|
||||
if(mixConfig == null) {
|
||||
mixConfig = new MixConfig();
|
||||
externalWallet.setMixConfig(mixConfig);
|
||||
}
|
||||
|
||||
KeyPurpose keyPurpose = KeyPurpose.fromChildNumber(new ChildNumber(externalDestination.getChain()));
|
||||
WalletNode externalNode = externalWallet.getNode(keyPurpose);
|
||||
externalIndexHandler = new SparrowIndexHandler(externalWallet, externalNode);
|
||||
}
|
||||
|
||||
return externalIndexHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitialized() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInitialized(boolean b) {
|
||||
// nothing required
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNymClaimed() {
|
||||
return false; // nothing required
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNymClaimed(boolean value) {
|
||||
// nothing required
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() throws Exception {
|
||||
// nothing required
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean persist(boolean b) throws Exception {
|
||||
// nothing required
|
||||
return false;
|
||||
}
|
||||
|
||||
private String mapKey(BipWallet bipWallet, Chain chain) {
|
||||
SamouraiAccount samouraiAccount = bipWallet.getAccount();
|
||||
BipDerivation derivation = bipWallet.getDerivation();
|
||||
return samouraiAccount.name() + "_" + derivation.getPurpose() + "_" + chain.getIndex();
|
||||
}
|
||||
|
||||
private Wallet findWallet(SamouraiAccount samouraiAccount) {
|
||||
Wallet wallet = getWallet();
|
||||
if(wallet == null) {
|
||||
throw new IllegalStateException("Can't find wallet with walletId " + walletId);
|
||||
}
|
||||
|
||||
return Whirlpool.getStandardAccountWallet(samouraiAccount, wallet);
|
||||
}
|
||||
|
||||
private Wallet getWallet() {
|
||||
return Whirlpool.getWallet(walletId);
|
||||
}
|
||||
}
|
|
@ -64,10 +64,8 @@ open module com.sparrowwallet.sparrow {
|
|||
requires com.sparrowwallet.bokmakierie;
|
||||
requires java.smartcardio;
|
||||
requires com.jcraft.jzlib;
|
||||
requires com.samourai.whirlpool.client;
|
||||
requires com.samourai.whirlpool.protocol;
|
||||
requires com.samourai.extlibj;
|
||||
requires com.samourai.soroban.client;
|
||||
requires com.samourai.http.client;
|
||||
requires com.samourai.bitcoinj;
|
||||
requires org.eclipse.jetty.client;
|
||||
requires org.eclipse.jetty.http;
|
||||
requires org.eclipse.jetty.util;
|
||||
requires org.eclipse.jetty.io;
|
||||
}
|
|
@ -138,7 +138,6 @@
|
|||
<MenuItem fx:id="sendToMany" mnemonicParsing="false" text="Send To Many" onAction="#sendToMany"/>
|
||||
<MenuItem fx:id="sweepPrivateKey" mnemonicParsing="false" text="Sweep Private Key" onAction="#sweepPrivateKey"/>
|
||||
<SeparatorMenuItem />
|
||||
<MenuItem fx:id="findMixingPartner" mnemonicParsing="false" text="Find Mix Partner" onAction="#findMixingPartner"/>
|
||||
<MenuItem fx:id="showPayNym" mnemonicParsing="false" text="Show PayNym" onAction="#showPayNym"/>
|
||||
<SeparatorMenuItem />
|
||||
<Menu fx:id="switchServer" text="Switch Server"/>
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -1,171 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import java.lang.*?>
|
||||
<?import java.util.*?>
|
||||
<?import javafx.scene.*?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<?import javafx.scene.image.ImageView?>
|
||||
<?import javafx.scene.image.Image?>
|
||||
<?import org.controlsfx.glyphfont.Glyph?>
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import com.sparrowwallet.sparrow.control.TransactionDiagram?>
|
||||
<?import com.sparrowwallet.sparrow.control.ProgressTimer?>
|
||||
<?import com.sparrowwallet.sparrow.control.CopyableTextField?>
|
||||
<?import com.sparrowwallet.sparrow.control.PaymentCodeTextField?>
|
||||
<?import com.sparrowwallet.sparrow.control.PayNymAvatar?>
|
||||
|
||||
<StackPane prefHeight="460.0" prefWidth="600.0" stylesheets="@counterparty.css, @../wallet/send.css, @../general.css" styleClass="counterparty-pane" fx:controller="com.sparrowwallet.sparrow.soroban.CounterpartyController" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
|
||||
<VBox spacing="20">
|
||||
<HBox styleClass="title-area">
|
||||
<HBox alignment="CENTER_LEFT">
|
||||
<Label fx:id="title" text="Find Mix Partner" styleClass="title-label" />
|
||||
</HBox>
|
||||
<Region HBox.hgrow="ALWAYS"/>
|
||||
<ImageView AnchorPane.rightAnchor="0">
|
||||
<Image url="/image/useradd.png" requestedWidth="50" requestedHeight="50" smooth="false" />
|
||||
</ImageView>
|
||||
</HBox>
|
||||
<VBox fx:id="counterpartyBox" styleClass="content-area" spacing="20" prefHeight="390">
|
||||
<VBox fx:id="step1" spacing="15">
|
||||
<Label text="Share your PayNym or Payment code" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<Label text="Perform a collaborative transaction using the Samourai Soroban service. Your mix partner will start the mix, and will need either your PayNym or the Payment code shown below. Click Next once they have indicated they are ready." wrapText="true" styleClass="content-text" />
|
||||
<BorderPane>
|
||||
<padding>
|
||||
<Insets top="20" right="70" />
|
||||
</padding>
|
||||
<center>
|
||||
<VBox spacing="15">
|
||||
<HBox styleClass="field-box">
|
||||
<Label text="PayNym:" styleClass="field-label" />
|
||||
<HBox spacing="10">
|
||||
<CopyableTextField fx:id="payNym" promptText="Retrieving..." styleClass="field-control" editable="false"/>
|
||||
<Button fx:id="showPayNym" onAction="#showPayNym">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="ROBOT" />
|
||||
</graphic>
|
||||
<tooltip>
|
||||
<Tooltip text="Show PayNym" />
|
||||
</tooltip>
|
||||
</Button>
|
||||
</HBox>
|
||||
<Button fx:id="payNymButton" text="Retrieve PayNym" graphicTextGap="8" onAction="#retrievePayNym">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="ROBOT" />
|
||||
</graphic>
|
||||
<tooltip>
|
||||
<Tooltip text="Retrieves and claims the PayNym for this wallet" />
|
||||
</tooltip>
|
||||
</Button>
|
||||
</HBox>
|
||||
<HBox styleClass="field-box">
|
||||
<Label text="Payment code:" styleClass="field-label" />
|
||||
<HBox spacing="10">
|
||||
<PaymentCodeTextField fx:id="paymentCode" styleClass="field-control" editable="false"/>
|
||||
<Button fx:id="paymentCodeQR" onAction="#showPayNymQR">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="QRCODE" />
|
||||
</graphic>
|
||||
<tooltip>
|
||||
<Tooltip text="Show as QR code" />
|
||||
</tooltip>
|
||||
</Button>
|
||||
</HBox>
|
||||
</HBox>
|
||||
<HBox styleClass="field-box">
|
||||
<Label text="Mix using:" styleClass="field-label" />
|
||||
<ComboBox fx:id="mixWallet" />
|
||||
</HBox>
|
||||
</VBox>
|
||||
</center>
|
||||
<right>
|
||||
<PayNymAvatar fx:id="payNymAvatar" prefWidth="150" prefHeight="150" />
|
||||
</right>
|
||||
</BorderPane>
|
||||
</VBox>
|
||||
<VBox fx:id="step2" spacing="15">
|
||||
<HBox>
|
||||
<Label text="Review Mix Type" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<Region HBox.hgrow="ALWAYS" />
|
||||
<ProgressTimer fx:id="step2Timer" seconds="60" />
|
||||
</HBox>
|
||||
<Label fx:id="step2Desc" text="Your mix partner will now initiate the Soroban communication. Once communication is established, check the details of the mix transaction and click Next if you'd like to proceed." wrapText="true" styleClass="content-text" />
|
||||
<BorderPane>
|
||||
<padding>
|
||||
<Insets top="20" right="70" />
|
||||
</padding>
|
||||
<center>
|
||||
<VBox spacing="10">
|
||||
<HBox styleClass="field-box">
|
||||
<Label text="Mix partner:" styleClass="field-label" />
|
||||
<Label fx:id="mixingPartner" text="Waiting for mix partner..." styleClass="field-control" />
|
||||
<Hyperlink fx:id="meetingFail" text="Failed to find mix partner. Try again?" styleClass="failure" graphicTextGap="5">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="EXCLAMATION_CIRCLE" styleClass="failure" />
|
||||
</graphic>
|
||||
</Hyperlink>
|
||||
</HBox>
|
||||
<VBox fx:id="mixDetails" spacing="10">
|
||||
<HBox styleClass="field-box">
|
||||
<Label text="Type:" styleClass="field-label" />
|
||||
<Label fx:id="mixType" />
|
||||
</HBox>
|
||||
<HBox styleClass="field-box">
|
||||
<Label text="Fee:" styleClass="field-label" />
|
||||
<Label fx:id="mixFee" text="You pay half the miner fee" />
|
||||
</HBox>
|
||||
</VBox>
|
||||
</VBox>
|
||||
</center>
|
||||
<right>
|
||||
<PayNymAvatar fx:id="mixPartnerAvatar" prefWidth="150" prefHeight="150" />
|
||||
</right>
|
||||
</BorderPane>
|
||||
</VBox>
|
||||
<VBox fx:id="step3" spacing="15">
|
||||
<HBox>
|
||||
<Label text="Perform Mix" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<Region HBox.hgrow="ALWAYS" />
|
||||
<ProgressTimer fx:id="step3Timer" seconds="60" />
|
||||
</HBox>
|
||||
<Label fx:id="step3Desc" text="The mix transaction is now being created." wrapText="true" styleClass="content-text" />
|
||||
<HBox>
|
||||
<padding>
|
||||
<Insets top="20" />
|
||||
</padding>
|
||||
<ProgressBar fx:id="sorobanProgressBar" prefWidth="680" />
|
||||
</HBox>
|
||||
<VBox alignment="CENTER">
|
||||
<Label fx:id="sorobanProgressLabel" text="Waiting for mix partner..." styleClass="content-text" alignment="CENTER"/>
|
||||
<Glyph fx:id="mixDeclined" fontFamily="Font Awesome 5 Free Solid" fontSize="80" icon="USER_SLASH" />
|
||||
</VBox>
|
||||
</VBox>
|
||||
<VBox fx:id="step4" spacing="15">
|
||||
<Label text="Transaction Broadcasted" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="CHECK_CIRCLE" styleClass="title-icon,success" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<Label text="The broadcasted transaction is shown below." wrapText="true" styleClass="content-text" />
|
||||
<HBox>
|
||||
<padding>
|
||||
<Insets top="20" left="10" />
|
||||
</padding>
|
||||
<TransactionDiagram fx:id="transactionDiagram" maxWidth="700" final="true"/>
|
||||
</HBox>
|
||||
</VBox>
|
||||
</VBox>
|
||||
</VBox>
|
||||
</StackPane>
|
|
@ -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;
|
||||
}
|
|
@ -1,134 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import java.lang.*?>
|
||||
<?import java.util.*?>
|
||||
<?import javafx.scene.*?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
|
||||
<?import javafx.scene.image.ImageView?>
|
||||
<?import javafx.scene.image.Image?>
|
||||
<?import org.controlsfx.glyphfont.Glyph?>
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import com.sparrowwallet.sparrow.control.TransactionDiagram?>
|
||||
<?import com.sparrowwallet.sparrow.control.ProgressTimer?>
|
||||
<?import com.sparrowwallet.sparrow.control.ComboBoxTextField?>
|
||||
<?import com.sparrowwallet.sparrow.control.PayNymAvatar?>
|
||||
|
||||
<StackPane prefHeight="460.0" prefWidth="600.0" stylesheets="@initiator.css, @../wallet/send.css, @../general.css" styleClass="initiator-pane" fx:controller="com.sparrowwallet.sparrow.soroban.InitiatorController" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
|
||||
<VBox spacing="20">
|
||||
<HBox styleClass="title-area">
|
||||
<HBox alignment="CENTER_LEFT">
|
||||
<Label fx:id="title" text="Add Mix Partner" styleClass="title-label" />
|
||||
</HBox>
|
||||
<Region HBox.hgrow="ALWAYS"/>
|
||||
<ImageView AnchorPane.rightAnchor="0">
|
||||
<Image url="/image/useradd.png" requestedWidth="50" requestedHeight="50" smooth="false" />
|
||||
</ImageView>
|
||||
</HBox>
|
||||
<VBox fx:id="initiatorBox" styleClass="content-area" spacing="20" prefHeight="390">
|
||||
<VBox fx:id="step1" spacing="15">
|
||||
<Label text="Enter PayNym or Payment Code" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<Label text="Add a mix partner to your transaction using the Samourai Soroban service, breaking the common input ownership heuristic and obfuscating payment amounts." wrapText="true" styleClass="content-text"/>
|
||||
<Label text="Ask your partner for their PayNym, or use their payment code found in their Sparrow Tools menu → Find Mix Partner. They will need a Native Segwit software wallet like this one." wrapText="true" styleClass="content-text" />
|
||||
<BorderPane>
|
||||
<padding>
|
||||
<Insets top="15" right="70" />
|
||||
</padding>
|
||||
<center>
|
||||
<VBox spacing="15">
|
||||
<HBox styleClass="field-box">
|
||||
<Label text="PayNym or Payment code:" styleClass="field-label" />
|
||||
<HBox>
|
||||
<StackPane>
|
||||
<ComboBox fx:id="payNymFollowers" />
|
||||
<ComboBoxTextField fx:id="counterparty" styleClass="field-control" comboProperty="$payNymFollowers" />
|
||||
</StackPane>
|
||||
<ProgressIndicator fx:id="payNymLoading" />
|
||||
</HBox>
|
||||
</HBox>
|
||||
<HBox styleClass="field-box">
|
||||
<Label styleClass="field-label" />
|
||||
<Button fx:id="findPayNym" text="Find PayNym" graphicTextGap="10" onAction="#findPayNym">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="ROBOT" />
|
||||
</graphic>
|
||||
</Button>
|
||||
</HBox>
|
||||
</VBox>
|
||||
</center>
|
||||
<right>
|
||||
<PayNymAvatar fx:id="payNymAvatar" prefWidth="150" prefHeight="150"/>
|
||||
</right>
|
||||
</BorderPane>
|
||||
</VBox>
|
||||
<VBox fx:id="step2" spacing="15">
|
||||
<HBox>
|
||||
<Label text="Request Mix" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<Region HBox.hgrow="ALWAYS" />
|
||||
<ProgressTimer fx:id="step2Timer" seconds="60" />
|
||||
</HBox>
|
||||
<HBox spacing="5">
|
||||
<Label fx:id="step2Desc" text="Ask your mix partner to select Find Mix Partner in the Sparrow Tools menu or Receive Online Cahoots in the Samourai Receive menu." wrapText="true" styleClass="content-text" />
|
||||
<Hyperlink fx:id="meetingFail" text="Try again?" styleClass="content-text"/>
|
||||
</HBox>
|
||||
<HBox>
|
||||
<padding>
|
||||
<Insets top="20" />
|
||||
</padding>
|
||||
<ProgressBar fx:id="sorobanProgressBar" prefWidth="680" />
|
||||
</HBox>
|
||||
<VBox alignment="CENTER">
|
||||
<Label fx:id="sorobanProgressLabel" text="Waiting for mix partner..." styleClass="content-text" alignment="CENTER"/>
|
||||
<Glyph fx:id="mixDeclined" fontFamily="Font Awesome 5 Free Solid" fontSize="80" icon="USER_SLASH" />
|
||||
</VBox>
|
||||
</VBox>
|
||||
<VBox fx:id="step3" spacing="15">
|
||||
<HBox>
|
||||
<Label text="Review Transaction" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<Region HBox.hgrow="ALWAYS" />
|
||||
<ProgressTimer fx:id="step3Timer" seconds="60" />
|
||||
</HBox>
|
||||
<Label fx:id="step3Desc" text="Review the transaction and broadcast when ready." wrapText="true" styleClass="content-text" />
|
||||
<HBox>
|
||||
<padding>
|
||||
<Insets top="20" left="10" />
|
||||
</padding>
|
||||
<TransactionDiagram fx:id="transactionDiagram" maxWidth="700" final="true"/>
|
||||
</HBox>
|
||||
</VBox>
|
||||
<VBox fx:id="step4" spacing="15">
|
||||
<HBox>
|
||||
<Label text="Broadcast Transaction" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
</HBox>
|
||||
<Label fx:id="step4Desc" text="Broadcasting the mix transaction..." wrapText="true" styleClass="content-text" />
|
||||
<HBox>
|
||||
<padding>
|
||||
<Insets top="20" />
|
||||
</padding>
|
||||
<ProgressBar fx:id="broadcastProgressBar" prefWidth="680" />
|
||||
</HBox>
|
||||
<VBox alignment="CENTER" spacing="20">
|
||||
<Label fx:id="broadcastProgressLabel" text="Broadcasting..." styleClass="content-text" alignment="CENTER"/>
|
||||
<Glyph fx:id="broadcastSuccessful" fontFamily="Font Awesome 5 Free Solid" fontSize="80" icon="CHECK_CIRCLE" styleClass="success" />
|
||||
</VBox>
|
||||
</VBox>
|
||||
</VBox>
|
||||
</VBox>
|
||||
</StackPane>
|
|
@ -1,62 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import java.lang.*?>
|
||||
<?import java.util.*?>
|
||||
<?import javafx.scene.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<?import tornadofx.control.Form?>
|
||||
<?import tornadofx.control.Fieldset?>
|
||||
<?import tornadofx.control.Field?>
|
||||
<?import com.sparrowwallet.sparrow.control.HelpLabel?>
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.collections.FXCollections?>
|
||||
<?import javafx.scene.control.ComboBox?>
|
||||
<?import com.samourai.whirlpool.client.wallet.beans.IndexRange?>
|
||||
<?import com.sparrowwallet.sparrow.control.IntegerSpinner?>
|
||||
|
||||
<BorderPane stylesheets="@../general.css" styleClass="line-border" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="com.sparrowwallet.sparrow.wallet.MixToController">
|
||||
<center>
|
||||
<GridPane hgap="10.0" vgap="10.0">
|
||||
<padding>
|
||||
<Insets left="25.0" right="25.0" top="25.0" />
|
||||
</padding>
|
||||
<columnConstraints>
|
||||
<ColumnConstraints percentWidth="100" />
|
||||
</columnConstraints>
|
||||
<rowConstraints>
|
||||
<RowConstraints />
|
||||
</rowConstraints>
|
||||
|
||||
<Form GridPane.columnIndex="0" GridPane.rowIndex="0">
|
||||
<Fieldset inputGrow="SOMETIMES" text="Mix To Settings" styleClass="relaxedLabelFieldSet">
|
||||
<Field text="Mix to wallet:">
|
||||
<ComboBox fx:id="mixToWallets" prefWidth="160" promptText="None available" />
|
||||
<HelpLabel helpText="Select an open wallet to mix to."/>
|
||||
</Field>
|
||||
<Field text="Minimum mixes:">
|
||||
<IntegerSpinner fx:id="minMixes" editable="true" prefWidth="90" />
|
||||
<HelpLabel helpText="The minimum number of mixes required before mixing to the selected wallet.\nTo ensure privacy, each mix beyond this number will have a 75% probability to mix to the selected wallet."/>
|
||||
</Field>
|
||||
</Fieldset>
|
||||
</Form>
|
||||
|
||||
<Form GridPane.columnIndex="0" GridPane.rowIndex="1">
|
||||
<Fieldset inputGrow="SOMETIMES" text="Postmix Wallet Settings" styleClass="relaxedLabelFieldSet">
|
||||
<Field text="Index range:">
|
||||
<ComboBox fx:id="indexRange" prefWidth="160">
|
||||
<items>
|
||||
<FXCollections fx:factory="observableArrayList">
|
||||
<IndexRange fx:constant="FULL" />
|
||||
<IndexRange fx:constant="EVEN" />
|
||||
<IndexRange fx:constant="ODD" />
|
||||
</FXCollections>
|
||||
</items>
|
||||
</ComboBox>
|
||||
<HelpLabel helpText="Using different index ranges allows the same wallet to be mixed simultaneously on multiple clients.\nSelect Full if simultaneous mixing is not required to keep wallet indexing compact for quicker load times.\nSelect Even to mix on the Samourai mobile app simultaneously.\nSelect Odd to mix on Samourai CLI/GUI simultaneously."/>
|
||||
</Field>
|
||||
</Fieldset>
|
||||
</Form>
|
||||
</GridPane>
|
||||
</center>
|
||||
</BorderPane>
|
||||
|
|
@ -192,11 +192,6 @@
|
|||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="SATELLITE_DISH" />
|
||||
</graphic>
|
||||
</Button>
|
||||
<Button fx:id="premixButton" text="Broadcast Premix Transaction" contentDisplay="RIGHT" graphicTextGap="5" onAction="#broadcastPremix">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="RANDOM" />
|
||||
</graphic>
|
||||
</Button>
|
||||
<Button fx:id="createButton" text="Create Transaction" defaultButton="true" disable="true" contentDisplay="RIGHT" graphicTextGap="5" onAction="#createTransaction">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="ANGLE_DOUBLE_RIGHT" />
|
||||
|
|
|
@ -68,28 +68,10 @@
|
|||
</center>
|
||||
<bottom>
|
||||
<HBox>
|
||||
<HBox fx:id="mixButtonsBox" styleClass="utxos-buttons-box" spacing="20" alignment="BOTTOM_LEFT">
|
||||
<Button fx:id="startMix" text="Start Mixing" onAction="#startMixing">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" icon="RANDOM" fontSize="12" />
|
||||
</graphic>
|
||||
</Button>
|
||||
<Button fx:id="stopMix" text="Stop Mixing" onAction="#stopMixing">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" icon="STOP_CIRCLE" fontSize="12" />
|
||||
</graphic>
|
||||
</Button>
|
||||
<Button fx:id="mixTo" text="Mix to..." maxWidth="200" onAction="#showMixToDialog" />
|
||||
</HBox>
|
||||
<Region HBox.hgrow="ALWAYS" />
|
||||
<HBox styleClass="utxos-buttons-box" spacing="20" alignment="BOTTOM_RIGHT">
|
||||
<Button fx:id="selectAll" text="Select All" onAction="#selectAll"/>
|
||||
<Button fx:id="clear" text="Clear" onAction="#clear"/>
|
||||
<Button fx:id="mixSelected" text="Mix Selected" graphicTextGap="5" onAction="#mixSelected">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" icon="RANDOM" fontSize="12" />
|
||||
</graphic>
|
||||
</Button>
|
||||
<Button fx:id="sendSelected" text="Send Selected" graphicTextGap="5" onAction="#sendSelected">
|
||||
<graphic>
|
||||
<Glyph fontFamily="FontAwesome" icon="SEND" fontSize="12" />
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
.whirlpool-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;
|
||||
}
|
||||
|
||||
#whirlpoolBox, .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: 130px;
|
||||
}
|
||||
|
||||
.field-control {
|
||||
-fx-pref-width: 180px;
|
||||
}
|
||||
|
||||
.low-fee-warning {
|
||||
-fx-text-fill: rgb(238, 210, 2);
|
||||
}
|
|
@ -1,161 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import java.lang.*?>
|
||||
<?import java.util.*?>
|
||||
<?import javafx.scene.*?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
|
||||
<?import org.controlsfx.glyphfont.Glyph?>
|
||||
<?import javafx.scene.image.ImageView?>
|
||||
<?import javafx.scene.image.Image?>
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import com.sparrowwallet.sparrow.control.CopyableCoinLabel?>
|
||||
<?import com.sparrowwallet.sparrow.control.CopyableLabel?>
|
||||
<StackPane prefHeight="460.0" prefWidth="600.0" stylesheets="@whirlpool.css, @../general.css" styleClass="whirlpool-pane" fx:controller="com.sparrowwallet.sparrow.whirlpool.WhirlpoolController" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
|
||||
<VBox spacing="20">
|
||||
<HBox styleClass="title-area">
|
||||
<HBox alignment="CENTER_LEFT">
|
||||
<Label fx:id="title" text="Whirlpool Configuration" styleClass="title-label" />
|
||||
</HBox>
|
||||
<Region HBox.hgrow="ALWAYS"/>
|
||||
<ImageView AnchorPane.rightAnchor="0">
|
||||
<Image url="/image/whirlpool.png" requestedWidth="50" requestedHeight="50" smooth="false" />
|
||||
</ImageView>
|
||||
</HBox>
|
||||
<VBox fx:id="whirlpoolBox" styleClass="content-area" spacing="20" prefHeight="390">
|
||||
<VBox fx:id="step1" spacing="15">
|
||||
<Label text="Introduction" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<Label text="Mixing (CoinJoin) is provided in Sparrow through the Samourai Whirlpool coordinator. " wrapText="true" styleClass="content-text" />
|
||||
<Label text="Sparrow contains a Whirlpool client, which communicates with the coordinator using blinded inputs. As such, the privacy of your UTXOs is unchanged when using this service. If you are using Tor to connect to your server, or have configured a Tor proxy, communication with the coordinator will be over Tor." wrapText="true" styleClass="content-text" />
|
||||
<Label text="The fees for using the Whirlpool service are deducted from the UTXOs that you mix. These fees include the Whirlpool fee, and the miner fees required for the transactions. All fees are displayed for review before mixing begins." wrapText="true" styleClass="content-text" />
|
||||
</VBox>
|
||||
<VBox fx:id="step2" spacing="15">
|
||||
<Label text="Premix, Postmix and Badbank" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<HBox>
|
||||
<TabPane side="LEFT" rotateGraphic="true" styleClass="wallet-subtabs" minWidth="100" minHeight="280">
|
||||
<Tab text="" closable="false">
|
||||
<graphic>
|
||||
<Label text="Premix" contentDisplay="TOP">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="RANDOM" />
|
||||
</graphic>
|
||||
</Label>
|
||||
</graphic>
|
||||
<HBox/>
|
||||
</Tab>
|
||||
<Tab text="" closable="false">
|
||||
<graphic>
|
||||
<Label text="Postmix" contentDisplay="TOP">
|
||||
<graphic>
|
||||
<Glyph fontFamily="FontAwesome" fontSize="12" icon="SEND" />
|
||||
</graphic>
|
||||
</Label>
|
||||
</graphic>
|
||||
<HBox/>
|
||||
</Tab>
|
||||
<Tab text="" closable="false">
|
||||
<graphic>
|
||||
<Label text="Badbank" contentDisplay="TOP">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="BIOHAZARD" />
|
||||
</graphic>
|
||||
</Label>
|
||||
</graphic>
|
||||
<HBox/>
|
||||
</Tab>
|
||||
</TabPane>
|
||||
<VBox spacing="15">
|
||||
<Label text="Initiating your first CoinJoin in Sparrow will add three new wallets to your existing wallet: Premix, Postmix and Badbank." wrapText="true" styleClass="content-text" />
|
||||
<Label text="Premix contains UTXOs that have been split from your deposit UTXOs into equal amounts, waiting for their first mixing round. Postmix contains UTXOs that have been through at least one mixing round. Badbank contains any change from your premix transaction, and should be treated carefully." wrapText="true" styleClass="content-text" />
|
||||
<Label text="Click on the tabs on the left to use these wallets. Note that they will have reduced functionality (for example they will not display receiving addresses)." wrapText="true" styleClass="content-text" />
|
||||
</VBox>
|
||||
</HBox>
|
||||
</VBox>
|
||||
<VBox fx:id="step3" spacing="15">
|
||||
<Label text="Configure Whirlpool" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<Label text="Configure Whirlpool using the fields below. You can enter a Samourai SCODE for reduced cost mixing." wrapText="true" styleClass="content-text" />
|
||||
<HBox styleClass="field-box">
|
||||
<padding>
|
||||
<Insets top="20" />
|
||||
</padding>
|
||||
<Label text="SCODE:" styleClass="field-label" />
|
||||
<TextField fx:id="scode" styleClass="field-control"/>
|
||||
</HBox>
|
||||
<HBox styleClass="field-box">
|
||||
<padding>
|
||||
<Insets top="10" />
|
||||
</padding>
|
||||
<Label text="Premix Priority:" styleClass="field-label" />
|
||||
<Slider fx:id="premixPriority" snapToTicks="true" showTickLabels="true" showTickMarks="true" styleClass="field-control" />
|
||||
</HBox>
|
||||
<HBox styleClass="field-box">
|
||||
<padding>
|
||||
<Insets top="10" />
|
||||
</padding>
|
||||
<Label text="Premix Fee Rate:" styleClass="field-label" />
|
||||
<CopyableLabel fx:id="premixFeeRate" />
|
||||
<Label fx:id="lowPremixFeeRate">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="EXCLAMATION_TRIANGLE" styleClass="low-fee-warning"/>
|
||||
</graphic>
|
||||
<padding>
|
||||
<Insets left="12" />
|
||||
</padding>
|
||||
<tooltip>
|
||||
<Tooltip text="A low fee rate may delay the first mix."/>
|
||||
</tooltip>
|
||||
</Label>
|
||||
</HBox>
|
||||
</VBox>
|
||||
<VBox fx:id="step4" spacing="15">
|
||||
<Label text="Select Pool" styleClass="title-text">
|
||||
<graphic>
|
||||
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="20" icon="RANDOM" styleClass="title-icon" />
|
||||
</graphic>
|
||||
</Label>
|
||||
<Label text="Choose which pool to use below. You will then be able to preview your premix transaction. Your wallet password may be required to add the premix wallet." wrapText="true" styleClass="content-text" />
|
||||
<HBox spacing="20" alignment="CENTER_LEFT">
|
||||
<padding>
|
||||
<Insets top="20" bottom="5" />
|
||||
</padding>
|
||||
<Label text="Pool:" prefWidth="100"/>
|
||||
<ComboBox fx:id="pool" />
|
||||
<Label fx:id="poolInsufficient" text="No available pools." styleClass="failure"/>
|
||||
</HBox>
|
||||
<VBox fx:id="selectedPool" spacing="15">
|
||||
<HBox styleClass="field-box">
|
||||
<Label text="Anonset:" styleClass="field-label" />
|
||||
<Label fx:id="poolAnonset" />
|
||||
</HBox>
|
||||
<HBox styleClass="field-box">
|
||||
<Label text="Pool Fee:" styleClass="field-label" />
|
||||
<CopyableCoinLabel fx:id="poolFee" />
|
||||
<HBox fx:id="discountFeeBox" alignment="CENTER_LEFT">
|
||||
<Label text=" (discounted to " />
|
||||
<CopyableCoinLabel fx:id="discountFee" />
|
||||
<Label text=")" />
|
||||
</HBox>
|
||||
</HBox>
|
||||
<HBox fx:id="nbOutputsBox" styleClass="field-box">
|
||||
<Label text="Premix Outputs:" styleClass="field-label" />
|
||||
<Label fx:id="nbOutputsLoading" text="Calculating..." />
|
||||
<Label fx:id="nbOutputs" />
|
||||
</HBox>
|
||||
</VBox>
|
||||
</VBox>
|
||||
</VBox>
|
||||
</VBox>
|
||||
</StackPane>
|
Loading…
Reference in a new issue