From 2b7b650faeeeda7fc25ab0962a6132e6531ced4c Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 30 Aug 2023 15:21:31 +0200 Subject: [PATCH] improve renaming wallet keystore labels for uniqueness --- .../com/sparrowwallet/drongo/wallet/Wallet.java | 7 ++++++- .../com/sparrowwallet/drongo/wallet/WalletTest.java | 13 ++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java index 8551ca4..d825b21 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -1773,7 +1773,10 @@ public class Wallet extends Persistable implements Comparable { } public void makeLabelsUnique(Keystore newKeystore) { - makeLabelsUnique(newKeystore, false); + Set labels = getKeystores().stream().map(Keystore::getLabel).collect(Collectors.toSet()); + if(!labels.add(newKeystore.getLabel())) { + makeLabelsUnique(newKeystore, false); + } } private int makeLabelsUnique(Keystore newKeystore, boolean duplicateFound) { @@ -1800,6 +1803,8 @@ public class Wallet extends Persistable implements Comparable { max++; if(newKeystore.getLabel().equals(Keystore.DEFAULT_LABEL)) { newKeystore.setLabel(Keystore.DEFAULT_LABEL.substring(0, Keystore.DEFAULT_LABEL.length() - 2) + " " + max); + } else if(newKeystore.getLabel().length() + Integer.toString(max).length() + 1 > Keystore.MAX_LABEL_LENGTH) { + newKeystore.setLabel(newKeystore.getLabel().substring(0, Keystore.MAX_LABEL_LENGTH - (Integer.toString(max).length() + 1)) + " " + max); } else { newKeystore.setLabel(newKeystore.getLabel() + " " + max); } diff --git a/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java b/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java index 2dc3155..1231c53 100644 --- a/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java +++ b/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java @@ -85,12 +85,19 @@ public class WalletTest { wallet.getKeystores().add(defaultKeystore3); Keystore defaultKeystore4 = new Keystore("Keystore"); wallet.makeLabelsUnique(defaultKeystore4); - Assert.assertEquals("Keystore 4", defaultKeystore4.getLabel()); + Assert.assertEquals("Keystore", defaultKeystore4.getLabel()); wallet.getKeystores().add(defaultKeystore4); - Keystore defaultKeystore5 = new Keystore("Keystore 4"); + Keystore defaultKeystore5 = new Keystore("Keystore 3"); wallet.makeLabelsUnique(defaultKeystore5); - Assert.assertEquals("Keystore 4 2", defaultKeystore5.getLabel()); + Assert.assertEquals("Keystore 3 2", defaultKeystore5.getLabel()); wallet.getKeystores().add(defaultKeystore5); + + Keystore longKeystore1 = new Keystore("1234567890ABCDEFG"); + wallet.getKeystores().add(longKeystore1); + Keystore longKeystore2 = new Keystore("1234567890ABCDEFG"); + wallet.makeLabelsUnique(longKeystore2); + Assert.assertEquals("1234567890ABCD 1", longKeystore1.getLabel()); + Assert.assertEquals("1234567890ABCD 2", longKeystore2.getLabel()); } @Test