From 47855228d3740ea40887b6722820a78f7ee6f5e9 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sun, 30 Aug 2020 11:15:45 +0200 Subject: [PATCH] wallet load order, dont clear history on trivial changes --- drongo | 2 +- .../sparrowwallet/sparrow/AppController.java | 6 +-- .../com/sparrowwallet/sparrow/MainApp.java | 14 +++++- .../sparrow/control/QRScanDialog.java | 3 +- .../sparrow/transaction/InputController.java | 7 --- .../java/com/sparrowwallet/sparrow/ur/UR.java | 1 + .../sparrow/wallet/SettingsWalletForm.java | 48 +++++++++++++++++-- .../sparrow/transaction/input.fxml | 5 -- 8 files changed, 65 insertions(+), 21 deletions(-) diff --git a/drongo b/drongo index 6da81860..55717c31 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit 6da818606a506e43bdafcfea93cad85d8cf82f74 +Subproject commit 55717c31bf8046e558ee90a5997c1de769517813 diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 267763ea..f730bacd 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -1067,11 +1067,11 @@ public class AppController implements Initializable { text += event.getValueAsText(event.getTotalValue()); } else { if(event.getTotalBlockchainValue() > 0 && event.getTotalMempoolValue() > 0) { - text = "New transactions: " + event.getValueAsText(event.getTotalValue()) + " (" + event.getValueAsText(event.getTotalMempoolValue()) + " in mempool)"; + text = "New transactions: " + event.getValueAsText(event.getTotalValue()) + " total (" + event.getValueAsText(event.getTotalMempoolValue()) + " in mempool)"; } else if(event.getTotalMempoolValue() > 0) { - text = "New mempool transactions: " + event.getValueAsText(event.getTotalMempoolValue()); + text = "New mempool transactions: " + event.getValueAsText(event.getTotalMempoolValue()) + " total"; } else { - text = "New transactions: " + event.getValueAsText(event.getTotalValue()); + text = "New transactions: " + event.getValueAsText(event.getTotalValue()) + " total"; } } diff --git a/src/main/java/com/sparrowwallet/sparrow/MainApp.java b/src/main/java/com/sparrowwallet/sparrow/MainApp.java index ddb65233..54d63da2 100644 --- a/src/main/java/com/sparrowwallet/sparrow/MainApp.java +++ b/src/main/java/com/sparrowwallet/sparrow/MainApp.java @@ -4,6 +4,8 @@ import com.sparrowwallet.sparrow.control.WelcomeDialog; import com.sparrowwallet.sparrow.glyphfont.FontAwesome5; import com.sparrowwallet.sparrow.glyphfont.FontAwesome5Brands; import com.sparrowwallet.sparrow.io.Config; +import com.sparrowwallet.sparrow.io.FileType; +import com.sparrowwallet.sparrow.io.IOUtils; import com.sparrowwallet.sparrow.preferences.PreferenceGroup; import com.sparrowwallet.sparrow.preferences.PreferencesDialog; import javafx.application.Application; @@ -18,8 +20,11 @@ import org.controlsfx.glyphfont.GlyphFontRegistry; import org.slf4j.LoggerFactory; import java.io.File; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; public class MainApp extends Application { public static final String APP_NAME = "Sparrow"; @@ -82,7 +87,14 @@ public class MainApp extends Application { List recentWalletFiles = Config.get().getRecentWalletFiles(); if(recentWalletFiles != null) { - for(File walletFile : recentWalletFiles) { + //Resort to preserve wallet order as far as possible. Unencrypted wallets will still be opened first. + List encryptedWalletFiles = recentWalletFiles.stream().filter(file -> FileType.BINARY.equals(IOUtils.getFileType(file))).collect(Collectors.toList()); + Collections.reverse(encryptedWalletFiles); + List sortedWalletFiles = new ArrayList<>(recentWalletFiles); + sortedWalletFiles.removeAll(encryptedWalletFiles); + sortedWalletFiles.addAll(encryptedWalletFiles); + + for(File walletFile : sortedWalletFiles) { if(walletFile.exists()) { Platform.runLater(() -> appController.openWalletFile(walletFile)); } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java index 14f3b53d..fa75ff2b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java @@ -75,7 +75,8 @@ public class QRScanDialog extends Dialog { if(decoder.getResult() != null) { URDecoder.Result urResult = decoder.getResult(); if(urResult.type == ResultType.SUCCESS) { - if(urResult.ur.getType().equals(UR.BYTES_TYPE)) { + //TODO: Confirm once UR type registry is updated + if(urResult.ur.getType().contains(UR.BYTES_TYPE) || urResult.ur.getType().equals(UR.CRYPTO_PSBT_TYPE)) { try { PSBT psbt = new PSBT(urResult.ur.toBytes()); result = new Result(psbt); diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java index cc660195..14a88af9 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java @@ -42,9 +42,6 @@ public class InputController extends TransactionFormController implements Initia @FXML private Hyperlink linkedOutpoint; - @FXML - private Button outpointSelect; - @FXML private CoinLabel spends; @@ -143,7 +140,6 @@ public class InputController extends TransactionFormController implements Initia if(txInput.isCoinBase()) { outpoint.setText("Coinbase"); - outpointSelect.setVisible(false); long totalAmt = 0; for(TransactionOutput output : inputForm.getTransaction().getOutputs()) { totalAmt += output.getValue(); @@ -164,9 +160,6 @@ public class InputController extends TransactionFormController implements Initia } else if(inputForm.getInputTransactions() != null) { updateSpends(inputForm.getInputTransactions()); } - - //TODO: Enable select outpoint when wallet present - outpointSelect.setDisable(true); } private void updateOutpoint(Map inputTransactions) { diff --git a/src/main/java/com/sparrowwallet/sparrow/ur/UR.java b/src/main/java/com/sparrowwallet/sparrow/ur/UR.java index 9d011b3b..85c9834f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/ur/UR.java +++ b/src/main/java/com/sparrowwallet/sparrow/ur/UR.java @@ -19,6 +19,7 @@ import java.util.Objects; public class UR { public static final String UR_PREFIX = "ur"; public static final String BYTES_TYPE = "bytes"; + public static final String CRYPTO_PSBT_TYPE = "crypto-psbt"; private final String type; private final byte[] data; diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java index 7b969800..630f0a6e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java @@ -1,5 +1,6 @@ package com.sparrowwallet.sparrow.wallet; +import com.sparrowwallet.drongo.wallet.Keystore; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent; @@ -31,10 +32,51 @@ public class SettingsWalletForm extends WalletForm { @Override public void saveAndRefresh() throws IOException { - //TODO: Detect trivial changes and don't clear everything - walletCopy.clearNodes(); + boolean refreshAll = changesScriptHashes(wallet, walletCopy); + if(refreshAll) { + walletCopy.clearNodes(); + } + wallet = walletCopy.copy(); save(); - EventManager.get().post(new WalletSettingsChangedEvent(wallet, getWalletFile())); + + if(refreshAll) { + EventManager.get().post(new WalletSettingsChangedEvent(wallet, getWalletFile())); + } + } + + private boolean changesScriptHashes(Wallet original, Wallet changed) { + if(!original.isValid() || !changed.isValid()) { + return true; + } + + if(original.getPolicyType() != changed.getPolicyType()) { + return true; + } + + if(original.getScriptType() != changed.getScriptType()) { + return true; + } + + //TODO: Determine if Miniscript has changed for custom policies + + if(original.getKeystores().size() != changed.getKeystores().size()) { + return true; + } + + for(int i = 0; i < original.getKeystores().size(); i++) { + Keystore originalKeystore = original.getKeystores().get(i); + Keystore changedKeystore = changed.getKeystores().get(i); + + if(!originalKeystore.getKeyDerivation().equals(changedKeystore.getKeyDerivation())) { + return true; + } + + if(!originalKeystore.getExtendedPublicKey().equals(changedKeystore.getExtendedPublicKey())) { + return true; + } + } + + return false; } } diff --git a/src/main/resources/com/sparrowwallet/sparrow/transaction/input.fxml b/src/main/resources/com/sparrowwallet/sparrow/transaction/input.fxml index 93e46623..2af1b75c 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/transaction/input.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/transaction/input.fxml @@ -35,11 +35,6 @@ -