From 3f3cdca94fa4bb8eac3c78c5da15891840fbd383 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 12 Dec 2022 09:14:17 +0200 Subject: [PATCH] refactor out unnecessary parameter --- .../event/WalletNodeHistoryChangedEvent.java | 4 +-- .../sparrow/net/ElectrumServer.java | 34 +++++++++---------- .../sparrow/wallet/SendController.java | 4 +-- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/event/WalletNodeHistoryChangedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/WalletNodeHistoryChangedEvent.java index 366ab651..483a9d14 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/WalletNodeHistoryChangedEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/WalletNodeHistoryChangedEvent.java @@ -37,7 +37,7 @@ public class WalletNodeHistoryChangedEvent { Wallet notificationWallet = wallet.getNotificationWallet(); if(notificationWallet != null) { WalletNode notificationNode = notificationWallet.getNode(KeyPurpose.NOTIFICATION); - if(ElectrumServer.getScriptHash(notificationWallet, notificationNode).equals(scriptHash)) { + if(ElectrumServer.getScriptHash(notificationNode).equals(scriptHash)) { return notificationNode; } } @@ -59,7 +59,7 @@ public class WalletNodeHistoryChangedEvent { private WalletNode getWalletNode(Wallet wallet, KeyPurpose keyPurpose) { WalletNode purposeNode = wallet.getNode(keyPurpose); for(WalletNode addressNode : new ArrayList<>(purposeNode.getChildren())) { - if(ElectrumServer.getScriptHash(wallet, addressNode).equals(scriptHash)) { + if(ElectrumServer.getScriptHash(addressNode).equals(scriptHash)) { return addressNode; } } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java b/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java index 4fcf1b70..f4d2e099 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java @@ -184,9 +184,9 @@ public class ElectrumServer { getCalculatedScriptHashes(wallet).forEach(retrievedScriptHashes::putIfAbsent); } - private static void addCalculatedScriptHashes(Wallet wallet, WalletNode walletNode) { + private static void addCalculatedScriptHashes(WalletNode walletNode) { Map calculatedScriptHashStatuses = new HashMap<>(); - addScriptHashStatus(calculatedScriptHashStatuses, wallet, walletNode); + addScriptHashStatus(calculatedScriptHashStatuses, walletNode); calculatedScriptHashStatuses.forEach(retrievedScriptHashes::putIfAbsent); } @@ -200,14 +200,14 @@ public class ElectrumServer { private static Map calculateScriptHashes(Wallet wallet, KeyPurpose keyPurpose) { Map calculatedScriptHashes = new LinkedHashMap<>(); for(WalletNode walletNode : wallet.getNode(keyPurpose).getChildren()) { - addScriptHashStatus(calculatedScriptHashes, wallet, walletNode); + addScriptHashStatus(calculatedScriptHashes, walletNode); } return calculatedScriptHashes; } - private static void addScriptHashStatus(Map calculatedScriptHashes, Wallet wallet, WalletNode walletNode) { - String scriptHash = getScriptHash(wallet, walletNode); + private static void addScriptHashStatus(Map calculatedScriptHashes, WalletNode walletNode) { + String scriptHash = getScriptHash(walletNode); String scriptHashStatus = getScriptHashStatus(walletNode); calculatedScriptHashes.put(scriptHash, scriptHashStatus); } @@ -246,8 +246,8 @@ public class ElectrumServer { } public static void clearRetrievedScriptHashes(Wallet wallet) { - wallet.getNode(KeyPurpose.RECEIVE).getChildren().stream().map(node -> getScriptHash(wallet, node)).forEach(scriptHash -> retrievedScriptHashes.remove(scriptHash)); - wallet.getNode(KeyPurpose.CHANGE).getChildren().stream().map(node -> getScriptHash(wallet, node)).forEach(scriptHash -> retrievedScriptHashes.remove(scriptHash)); + wallet.getNode(KeyPurpose.RECEIVE).getChildren().stream().map(ElectrumServer::getScriptHash).forEach(scriptHash -> retrievedScriptHashes.remove(scriptHash)); + wallet.getNode(KeyPurpose.CHANGE).getChildren().stream().map(ElectrumServer::getScriptHash).forEach(scriptHash -> retrievedScriptHashes.remove(scriptHash)); } public Map> getHistory(Wallet wallet) throws ServerException { @@ -337,7 +337,7 @@ public class ElectrumServer { log.debug("Fetched history for: " + nodeTransactionMap.keySet()); //Set the remaining WalletNode keys in nodeTransactionMap to empty sets to indicate no history (if no script hash history has already been retrieved in a previous call) - getAddressNodes(wallet, purposeNode).stream().filter(node -> !nodeTransactionMap.containsKey(node) && retrievedScriptHashes.get(getScriptHash(wallet, node)) == null).forEach(node -> nodeTransactionMap.put(node, Collections.emptySet())); + getAddressNodes(wallet, purposeNode).stream().filter(node -> !nodeTransactionMap.containsKey(node) && retrievedScriptHashes.get(getScriptHash(node)) == null).forEach(node -> nodeTransactionMap.put(node, Collections.emptySet())); } private void getHistoryToGapLimit(Wallet wallet, Map> nodeTransactionMap, WalletNode purposeNode) throws ServerException { @@ -376,7 +376,7 @@ public class ElectrumServer { Map pathScriptHashes = new LinkedHashMap<>(nodes.size()); for(WalletNode node : nodes) { if(node.getIndex() >= startIndex) { - pathScriptHashes.put(node.getDerivationPath(), getScriptHash(wallet, node)); + pathScriptHashes.put(node.getDerivationPath(), getScriptHash(node)); } } @@ -445,7 +445,7 @@ public class ElectrumServer { } if(node != null && node.getIndex() >= startIndex) { - String scriptHash = getScriptHash(wallet, node); + String scriptHash = getScriptHash(node); String subscribedStatus = getSubscribedScriptHashStatus(scriptHash); if(subscribedStatus != null) { //Already subscribed, but still need to fetch history from a used node if not previously fetched or present @@ -473,7 +473,7 @@ public class ElectrumServer { WalletNode node = pathNodes.computeIfAbsent(path, p -> nodes.stream().filter(n -> n.getDerivationPath().equals(p)).findFirst().orElse(null)); if(node != null) { - String scriptHash = getScriptHash(wallet, node); + String scriptHash = getScriptHash(node); //Check if there is history for this script hash, and if the history has changed since last fetched if(status != null && !status.equals(retrievedScriptHashes.get(scriptHash))) { @@ -888,7 +888,7 @@ public class ElectrumServer { public Set getMempoolScriptHashes(Wallet wallet, Sha256Hash txId, Set transactionNodes) throws ServerException { Map pathScriptHashes = new LinkedHashMap<>(transactionNodes.size()); for(WalletNode node : transactionNodes) { - pathScriptHashes.put(node.getDerivationPath(), getScriptHash(wallet, node)); + pathScriptHashes.put(node.getDerivationPath(), getScriptHash(node)); } Set mempoolScriptHashes = new LinkedHashSet<>(); @@ -948,14 +948,14 @@ public class ElectrumServer { Map scriptHashes = new HashMap<>(); for(KeyPurpose keyPurpose : KeyPurpose.DEFAULT_PURPOSES) { for(WalletNode childNode : wallet.getNode(keyPurpose).getChildren()) { - scriptHashes.put(getScriptHash(wallet, childNode), childNode); + scriptHashes.put(getScriptHash(childNode), childNode); } } return scriptHashes; } - public static String getScriptHash(Wallet wallet, WalletNode node) { + public static String getScriptHash(WalletNode node) { byte[] hash = Sha256Hash.hash(node.getOutputScript().getProgram()); byte[] reversed = Utils.reverseBytes(hash); return Utils.bytesToHex(reversed); @@ -1394,7 +1394,7 @@ public class ElectrumServer { Set updatedNodes = new HashSet<>(); Map> walletNodes = wallet.getWalletNodes(); for(WalletNode node : (nodes == null ? walletNodes.keySet() : nodes)) { - String scriptHash = getScriptHash(wallet, node); + String scriptHash = getScriptHash(node); String subscribedStatus = getSubscribedScriptHashStatus(scriptHash); if(!Objects.equals(subscribedStatus, retrievedScriptHashes.get(scriptHash))) { updatedNodes.add(node); @@ -1414,7 +1414,7 @@ public class ElectrumServer { //Clear transaction outputs for nodes that have no history - this is useful when a transaction is replaced in the mempool if(nodes != null) { for(WalletNode node : nodes) { - String scriptHash = getScriptHash(wallet, node); + String scriptHash = getScriptHash(node); if(retrievedScriptHashes.get(scriptHash) == null && !node.getTransactionOutputs().isEmpty()) { log.debug("Clearing transaction history for " + node); node.getTransactionOutputs().clear(); @@ -1791,7 +1791,7 @@ public class ElectrumServer { } } - addCalculatedScriptHashes(notificationWallet, notificationNode); + addCalculatedScriptHashes(notificationNode); ElectrumServer electrumServer = new ElectrumServer(); Map> nodeTransactionMap = electrumServer.getHistory(notificationWallet, List.of(notificationNode)); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java index 6c7f5e92..7c52a0e0 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java @@ -1161,11 +1161,11 @@ public class SendController extends WalletFormController implements Initializabl Map> inputHashes = new LinkedHashMap<>(); for(WalletNode node : walletTransaction.getSelectedUtxos().values()) { List nodeHashes = inputHashes.computeIfAbsent(node, k -> new ArrayList<>()); - nodeHashes.add(ElectrumServer.getScriptHash(walletForm.getWallet(), node)); + nodeHashes.add(ElectrumServer.getScriptHash(node)); } Map> changeHash = new LinkedHashMap<>(); for(WalletNode changeNode : walletTransaction.getChangeMap().keySet()) { - changeHash.put(changeNode, List.of(ElectrumServer.getScriptHash(walletForm.getWallet(), changeNode))); + changeHash.put(changeNode, List.of(ElectrumServer.getScriptHash(changeNode))); } log.debug("Creating tx " + walletTransaction.getTransaction().getTxId() + ", expecting notifications for \ninputs \n" + inputHashes + " and \nchange \n" + changeHash); }