From 63a9a798f3161805c6c20dfe8d4fdb55ee1838e6 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 16 Oct 2020 12:55:02 +0200 Subject: [PATCH] order available wallets to prioritise seed or usb keystores --- .../sparrow/transaction/HeadersController.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java index 8b5101dc..fb37c9dd 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java @@ -837,7 +837,7 @@ public class HeadersController extends TransactionFormController implements Init @Subscribe public void openWallets(OpenWalletsEvent event) { if(headersForm.getPsbt() != null && headersForm.getBlockTransaction() == null) { - List availableWallets = event.getWallets().stream().filter(wallet -> wallet.canSign(headersForm.getPsbt())).collect(Collectors.toList()); + List availableWallets = event.getWallets().stream().filter(wallet -> wallet.canSign(headersForm.getPsbt())).sorted(new WalletSignComparator()).collect(Collectors.toList()); Map availableWalletsMap = new LinkedHashMap<>(event.getWalletsMap()); availableWalletsMap.keySet().retainAll(availableWallets); headersForm.getAvailableWallets().keySet().retainAll(availableWallets); @@ -1016,4 +1016,18 @@ public class HeadersController extends TransactionFormController implements Init futureBlockWarning.setVisible(AppController.getCurrentBlockHeight() != null && locktimeBlock.getValue() > event.getHeight()); } } + + private static class WalletSignComparator implements Comparator { + private static final List sourceOrder = List.of(KeystoreSource.SW_WATCH, KeystoreSource.HW_AIRGAPPED, KeystoreSource.HW_USB, KeystoreSource.SW_SEED); + + @Override + public int compare(Wallet wallet1, Wallet wallet2) { + return getHighestSourceIndex(wallet2) - getHighestSourceIndex(wallet1); + + } + + private int getHighestSourceIndex(Wallet wallet) { + return wallet.getKeystores().stream().map(keystore -> sourceOrder.indexOf(keystore.getSource())).mapToInt(v -> v).max().orElse(0); + } + } } \ No newline at end of file