mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-25 13:16:44 +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;
|
package com.sparrowwallet.sparrow.event;
|
||||||
|
|
||||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
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.
|
* 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 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);
|
super(wallet);
|
||||||
|
this.walletId = walletId;
|
||||||
|
this.previousGapLimit = previousGapLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getWalletId() {
|
||||||
|
return walletId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getGapLimit() {
|
public int getGapLimit() {
|
||||||
return getWallet().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) {
|
public void getNewAddress(ActionEvent event) {
|
||||||
refreshAddress();
|
refreshAddress();
|
||||||
|
if(currentEntry != null) {
|
||||||
|
ensureSufficientGapLimit(currentEntry.getNode().getIndex());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refreshAddress() {
|
public void refreshAddress() {
|
||||||
|
@ -196,6 +199,17 @@ public class ReceiveController extends WalletFormController implements Initializ
|
||||||
setNodeEntry(freshEntry);
|
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")
|
@SuppressWarnings("unchecked")
|
||||||
public void displayAddress(ActionEvent event) {
|
public void displayAddress(ActionEvent event) {
|
||||||
Wallet wallet = getWalletForm().getWallet();
|
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
|
@Subscribe
|
||||||
public void existingWalletImported(ExistingWalletImportedEvent event) {
|
public void existingWalletImported(ExistingWalletImportedEvent event) {
|
||||||
if(event.getExistingWalletId().equals(getWalletForm().getWalletId())) {
|
if(event.getExistingWalletId().equals(getWalletForm().getWalletId())) {
|
||||||
|
|
|
@ -253,4 +253,8 @@ public class SettingsWalletForm extends WalletForm {
|
||||||
masterWalletCopy.getChildWallets().add(childWallet.copy());
|
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();
|
Optional<WalletNode> optPurposeNode = wallet.getPurposeNodes().stream().filter(node -> node.getKeyPurpose() == keyPurpose).findFirst();
|
||||||
if(optPurposeNode.isPresent()) {
|
if(optPurposeNode.isPresent()) {
|
||||||
WalletNode purposeNode = optPurposeNode.get();
|
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.KeyPurpose;
|
||||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||||
import com.sparrowwallet.drongo.wallet.WalletNode;
|
import com.sparrowwallet.drongo.wallet.WalletNode;
|
||||||
|
import com.sparrowwallet.sparrow.AppServices;
|
||||||
import com.sparrowwallet.sparrow.EventManager;
|
import com.sparrowwallet.sparrow.EventManager;
|
||||||
import com.sparrowwallet.sparrow.event.WalletGapLimitChangedEvent;
|
import com.sparrowwallet.sparrow.event.WalletGapLimitChangedEvent;
|
||||||
import com.sparrowwallet.sparrow.event.WalletMixConfigChangedEvent;
|
import com.sparrowwallet.sparrow.event.WalletMixConfigChangedEvent;
|
||||||
|
@ -75,7 +76,15 @@ public class SparrowIndexHandler extends AbstractIndexHandler {
|
||||||
int existingGapLimit = wallet.getGapLimit();
|
int existingGapLimit = wallet.getGapLimit();
|
||||||
if(index > highestUsedIndex + existingGapLimit) {
|
if(index > highestUsedIndex + existingGapLimit) {
|
||||||
wallet.setGapLimit(Math.max(wallet.getGapLimit(), index - highestUsedIndex));
|
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