mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-11-04 21:36:45 +00:00
change wallet gap limit and subscribe to new addresses if an address beyond gap limit range is requested
This commit is contained in:
parent
258d46a253
commit
4f6981b869
6 changed files with 63 additions and 3 deletions
|
@ -1,17 +1,41 @@
|
|||
package com.sparrowwallet.sparrow.event;
|
||||
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.drongo.wallet.WalletNode;
|
||||
|
||||
/**
|
||||
* This event is posted if the wallet's gap limit has changed, and triggers a history fetch for the new nodes.
|
||||
*
|
||||
*/
|
||||
public class WalletGapLimitChangedEvent extends WalletChangedEvent {
|
||||
public WalletGapLimitChangedEvent(Wallet wallet) {
|
||||
private final String walletId;
|
||||
private final int previousGapLimit;
|
||||
|
||||
public WalletGapLimitChangedEvent(String walletId, Wallet wallet, int previousGapLimit) {
|
||||
super(wallet);
|
||||
this.walletId = walletId;
|
||||
this.previousGapLimit = previousGapLimit;
|
||||
}
|
||||
|
||||
public String getWalletId() {
|
||||
return walletId;
|
||||
}
|
||||
|
||||
public int getGapLimit() {
|
||||
return getWallet().getGapLimit();
|
||||
}
|
||||
|
||||
public int getPreviousGapLimit() {
|
||||
return previousGapLimit;
|
||||
}
|
||||
|
||||
public int getPreviousLookAheadIndex(WalletNode node) {
|
||||
int lookAheadIndex = getPreviousGapLimit() - 1;
|
||||
Integer highestUsed = node.getHighestUsedIndex();
|
||||
if(highestUsed != null) {
|
||||
lookAheadIndex = highestUsed + getPreviousGapLimit();
|
||||
}
|
||||
|
||||
return lookAheadIndex;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -189,6 +189,9 @@ public class ReceiveController extends WalletFormController implements Initializ
|
|||
|
||||
public void getNewAddress(ActionEvent event) {
|
||||
refreshAddress();
|
||||
if(currentEntry != null) {
|
||||
ensureSufficientGapLimit(currentEntry.getNode().getIndex());
|
||||
}
|
||||
}
|
||||
|
||||
public void refreshAddress() {
|
||||
|
@ -196,6 +199,17 @@ public class ReceiveController extends WalletFormController implements Initializ
|
|||
setNodeEntry(freshEntry);
|
||||
}
|
||||
|
||||
private void ensureSufficientGapLimit(int index) {
|
||||
Wallet wallet = getWalletForm().getWallet();
|
||||
Integer highestIndex = wallet.getNode(KeyPurpose.RECEIVE).getHighestUsedIndex();
|
||||
int highestUsedIndex = highestIndex == null ? -1 : highestIndex;
|
||||
int existingGapLimit = wallet.getGapLimit();
|
||||
if(index > highestUsedIndex + existingGapLimit) {
|
||||
wallet.setGapLimit(Math.max(wallet.getGapLimit(), index - highestUsedIndex));
|
||||
EventManager.get().post(new WalletGapLimitChangedEvent(getWalletForm().getWalletId(), wallet, existingGapLimit));
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void displayAddress(ActionEvent event) {
|
||||
Wallet wallet = getWalletForm().getWallet();
|
||||
|
|
|
@ -730,6 +730,13 @@ public class SettingsController extends WalletFormController implements Initiali
|
|||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void walletGapLimitChanged(WalletGapLimitChangedEvent event) {
|
||||
if(walletForm.getWalletId().equals(event.getWalletId())) {
|
||||
((SettingsWalletForm)walletForm).gapLimitChanged(event.getGapLimit());
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void existingWalletImported(ExistingWalletImportedEvent event) {
|
||||
if(event.getExistingWalletId().equals(getWalletForm().getWalletId())) {
|
||||
|
|
|
@ -253,4 +253,8 @@ public class SettingsWalletForm extends WalletForm {
|
|||
masterWalletCopy.getChildWallets().add(childWallet.copy());
|
||||
}
|
||||
}
|
||||
|
||||
public void gapLimitChanged(int gapLimit) {
|
||||
walletCopy.setGapLimit(gapLimit);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -604,7 +604,9 @@ public class WalletForm {
|
|||
Optional<WalletNode> optPurposeNode = wallet.getPurposeNodes().stream().filter(node -> node.getKeyPurpose() == keyPurpose).findFirst();
|
||||
if(optPurposeNode.isPresent()) {
|
||||
WalletNode purposeNode = optPurposeNode.get();
|
||||
newNodes.addAll(purposeNode.fillToIndex(wallet, wallet.getLookAheadIndex(purposeNode)));
|
||||
purposeNode.fillToIndex(wallet, wallet.getLookAheadIndex(purposeNode));
|
||||
int previousLookAheadIndex = event.getPreviousLookAheadIndex(purposeNode);
|
||||
newNodes.addAll(purposeNode.getChildren().stream().filter(node -> node.getIndex() > previousLookAheadIndex).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ 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;
|
||||
|
@ -75,7 +76,15 @@ public class SparrowIndexHandler extends AbstractIndexHandler {
|
|||
int existingGapLimit = wallet.getGapLimit();
|
||||
if(index > highestUsedIndex + existingGapLimit) {
|
||||
wallet.setGapLimit(Math.max(wallet.getGapLimit(), index - highestUsedIndex));
|
||||
EventManager.get().post(new WalletGapLimitChangedEvent(wallet));
|
||||
EventManager.get().post(new WalletGapLimitChangedEvent(getWalletId(), wallet, existingGapLimit));
|
||||
}
|
||||
}
|
||||
|
||||
private String getWalletId() {
|
||||
try {
|
||||
return AppServices.get().getOpenWallets().get(wallet).getWalletId(wallet);
|
||||
} catch(Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue