diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java index 564b354..311125f 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -101,29 +101,32 @@ public class Wallet { } public int getLookAhead(WalletNode node) { - //TODO: Calculate using seen transactions int lookAhead = DEFAULT_LOOKAHEAD; - Integer maxIndex = node.getHighestUsedIndex(); - if(maxIndex != null) { - lookAhead = Math.max(maxIndex + lookAhead/2, lookAhead); + Integer highestUsed = node.getHighestUsedIndex(); + if(highestUsed != null) { + lookAhead = Math.max(highestUsed + lookAhead/2, lookAhead); } return lookAhead; } public WalletNode getFreshNode(KeyPurpose keyPurpose) { - //TODO: Calculate using seen transactions return getFreshNode(keyPurpose, null); } public WalletNode getFreshNode(KeyPurpose keyPurpose, WalletNode current) { - //TODO: Calculate using seen transactions int index = 0; - if(current != null) { + + WalletNode node = getNode(keyPurpose); + Integer highestUsed = node.getHighestUsedIndex(); + if(highestUsed != null) { + index = highestUsed + 1; + } + + if(current != null && current.getIndex() >= index) { index = current.getIndex() + 1; } - WalletNode node = getNode(keyPurpose); if(index >= node.getChildren().size()) { node.fillToIndex(index); } diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java b/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java index 042ff2b..f388c52 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java @@ -107,6 +107,15 @@ public class WalletNode implements Comparable { return unspentTXOs.stream().filter(txo -> !txo.isSpent()).collect(Collectors.toCollection(HashSet::new)); } + public long getUnspentValue() { + long value = 0L; + for(BlockTransactionHashIndex utxo : getUnspentTransactionOutputs()) { + value += utxo.getValue(); + } + + return value; + } + public void fillToIndex(int index) { for(int i = 0; i <= index; i++) { WalletNode node = new WalletNode(getKeyPurpose(), i); @@ -114,6 +123,9 @@ public class WalletNode implements Comparable { } } + /** + * @return The highest used index, or null if no addresses are used + */ public Integer getHighestUsedIndex() { WalletNode highestNode = null; for(WalletNode childNode : getChildren()) {