From 488752c142765bacd0373390faccbdb11b47487a Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 9 Sep 2020 11:17:08 +0200 Subject: [PATCH] handle keystore default label deduping better, validate 1of1 wallets --- .../sparrowwallet/drongo/wallet/Wallet.java | 13 +++++++++---- .../drongo/wallet/WalletTest.java | 19 +++++++++++++++++++ 2 files changed, 28 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 3126ac8..199c26f 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -776,7 +776,7 @@ public class Wallet { return false; } - if(policyType.equals(PolicyType.MULTI) && (numSigs <= 1 || numSigs > keystores.size())) { + if(policyType.equals(PolicyType.MULTI) && (numSigs < 1 || numSigs > keystores.size())) { return false; } @@ -819,9 +819,10 @@ public class Wallet { private int makeLabelsUnique(Keystore newKeystore, boolean duplicateFound) { int max = 0; for(Keystore keystore : getKeystores()) { - if(newKeystore != keystore && keystore.getLabel().startsWith(newKeystore.getLabel())) { + String newKeystoreLabel = newKeystore.getLabel().equals(Keystore.DEFAULT_LABEL) ? Keystore.DEFAULT_LABEL.substring(0, Keystore.DEFAULT_LABEL.length() - 2) : newKeystore.getLabel(); + if(newKeystore != keystore && keystore.getLabel().startsWith(newKeystoreLabel)) { duplicateFound = true; - String remainder = keystore.getLabel().substring(newKeystore.getLabel().length()); + String remainder = keystore.getLabel().substring(newKeystoreLabel.length()); if(remainder.length() == 0) { max = makeLabelsUnique(keystore, true); } else { @@ -837,7 +838,11 @@ public class Wallet { if(duplicateFound) { max++; - newKeystore.setLabel(newKeystore.getLabel() + " " + max); + if(newKeystore.getLabel().equals(Keystore.DEFAULT_LABEL)) { + newKeystore.setLabel(Keystore.DEFAULT_LABEL.substring(0, Keystore.DEFAULT_LABEL.length() - 2) + " " + max); + } else { + newKeystore.setLabel(newKeystore.getLabel() + " " + max); + } } return max; diff --git a/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java b/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java index bf78eb7..0de954b 100644 --- a/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java +++ b/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java @@ -66,6 +66,25 @@ public class WalletTest { wallet.makeLabelsUnique(eekeystore2); Assert.assertEquals("Electrum 1", eekeystore.getLabel()); Assert.assertEquals("Electrum 2", eekeystore2.getLabel()); + + Keystore defaultKeystore = new Keystore(); + wallet.getKeystores().add(defaultKeystore); + Keystore defaultKeystore2 = new Keystore(); + wallet.makeLabelsUnique(defaultKeystore2); + Assert.assertEquals("Keystore 2", defaultKeystore2.getLabel()); + wallet.getKeystores().add(defaultKeystore2); + Keystore defaultKeystore3 = new Keystore(); + wallet.makeLabelsUnique(defaultKeystore3); + Assert.assertEquals("Keystore 3", defaultKeystore3.getLabel()); + wallet.getKeystores().add(defaultKeystore3); + Keystore defaultKeystore4 = new Keystore("Keystore"); + wallet.makeLabelsUnique(defaultKeystore4); + Assert.assertEquals("Keystore 4", defaultKeystore4.getLabel()); + wallet.getKeystores().add(defaultKeystore4); + Keystore defaultKeystore5 = new Keystore("Keystore 4"); + wallet.makeLabelsUnique(defaultKeystore5); + Assert.assertEquals("Keystore 4 2", defaultKeystore5.getLabel()); + wallet.getKeystores().add(defaultKeystore5); } @Test