fix improvement to renaming wallet keystore labels for uniqueness

This commit is contained in:
Craig Raw 2023-11-09 09:08:58 +02:00
parent c63b492326
commit 28551247c6
3 changed files with 12 additions and 3 deletions

View file

@ -48,6 +48,10 @@ public class Keystore extends Persistable {
return label;
}
public String getBaseLabel() {
return label.replaceAll(" \\d*$", "");
}
public String getScriptName() {
return label.replace(" ", "");
}

View file

@ -1809,8 +1809,8 @@ public class Wallet extends Persistable implements Comparable<Wallet> {
}
public void makeLabelsUnique(Keystore newKeystore) {
Set<String> labels = getKeystores().stream().map(Keystore::getLabel).collect(Collectors.toSet());
if(!labels.add(newKeystore.getLabel())) {
Set<String> labels = getKeystores().stream().map(Keystore::getBaseLabel).collect(Collectors.toSet());
if(!labels.add(newKeystore.getBaseLabel())) {
makeLabelsUnique(newKeystore, false);
}
}

View file

@ -85,13 +85,18 @@ public class WalletTest {
wallet.getKeystores().add(defaultKeystore3);
Keystore defaultKeystore4 = new Keystore("Keystore");
wallet.makeLabelsUnique(defaultKeystore4);
Assert.assertEquals("Keystore", defaultKeystore4.getLabel());
Assert.assertEquals("Keystore 4", defaultKeystore4.getLabel());
wallet.getKeystores().add(defaultKeystore4);
Keystore defaultKeystore5 = new Keystore("Keystore 3");
wallet.makeLabelsUnique(defaultKeystore5);
Assert.assertEquals("Keystore 3 2", defaultKeystore5.getLabel());
wallet.getKeystores().add(defaultKeystore5);
Keystore keystore6 = new Keystore("Coldcard -1");
wallet.makeLabelsUnique(keystore6);
Assert.assertEquals("Coldcard -1 2", keystore6.getLabel());
wallet.getKeystores().add(keystore6);
Keystore longKeystore1 = new Keystore("1234567890ABCDEFG");
wallet.getKeystores().add(longKeystore1);
Keystore longKeystore2 = new Keystore("1234567890ABCDEFG");