From ca833fbf6807e9db30135562000329b7423b9fb7 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 29 Jul 2022 09:44:57 +0200 Subject: [PATCH] use locale-insensitive lowercase and uppercase functions --- .../com/sparrowwallet/drongo/KeyDerivation.java | 3 ++- src/main/java/com/sparrowwallet/drongo/Network.java | 4 +++- .../com/sparrowwallet/drongo/PropertyDefiner.java | 10 ++++++---- .../com/sparrowwallet/drongo/address/Address.java | 3 ++- .../sparrowwallet/drongo/protocol/ScriptType.java | 2 +- .../com/sparrowwallet/drongo/uri/BitcoinURI.java | 2 +- .../sparrowwallet/drongo/wallet/WalletModel.java | 8 +++++--- .../drongo/protocol/TransactionTest.java | 5 +++-- .../sparrowwallet/drongo/uri/BitcoinUriTest.java | 4 +++- .../com/sparrowwallet/drongo/wallet/PolicyTest.java | 13 +++++++------ 10 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/KeyDerivation.java b/src/main/java/com/sparrowwallet/drongo/KeyDerivation.java index 3d6020b..123f046 100644 --- a/src/main/java/com/sparrowwallet/drongo/KeyDerivation.java +++ b/src/main/java/com/sparrowwallet/drongo/KeyDerivation.java @@ -5,6 +5,7 @@ import com.sparrowwallet.drongo.crypto.ChildNumber; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Locale; public class KeyDerivation { private final String masterFingerprint; @@ -16,7 +17,7 @@ public class KeyDerivation { } public KeyDerivation(String masterFingerprint, String derivationPath) { - this.masterFingerprint = masterFingerprint == null ? null : masterFingerprint.toLowerCase(); + this.masterFingerprint = masterFingerprint == null ? null : masterFingerprint.toLowerCase(Locale.ROOT); this.derivationPath = derivationPath; this.derivation = parsePath(derivationPath); } diff --git a/src/main/java/com/sparrowwallet/drongo/Network.java b/src/main/java/com/sparrowwallet/drongo/Network.java index 4175ac5..a20fa34 100644 --- a/src/main/java/com/sparrowwallet/drongo/Network.java +++ b/src/main/java/com/sparrowwallet/drongo/Network.java @@ -1,5 +1,7 @@ package com.sparrowwallet.drongo; +import java.util.Locale; + public enum Network { MAINNET("mainnet", 0, "1", 5, "3", "bc", ExtendedKey.Header.xprv, ExtendedKey.Header.xpub, 128, 8332), TESTNET("testnet", 111, "mn", 196, "2", "tb", ExtendedKey.Header.tprv, ExtendedKey.Header.tpub, 239, 18332), @@ -39,7 +41,7 @@ public enum Network { } public String toDisplayString() { - return name.substring(0, 1).toUpperCase() + name.substring(1); + return name.substring(0, 1).toUpperCase(Locale.ROOT) + name.substring(1); } public int getP2PKHAddressHeader() { diff --git a/src/main/java/com/sparrowwallet/drongo/PropertyDefiner.java b/src/main/java/com/sparrowwallet/drongo/PropertyDefiner.java index 7f36c21..e994663 100644 --- a/src/main/java/com/sparrowwallet/drongo/PropertyDefiner.java +++ b/src/main/java/com/sparrowwallet/drongo/PropertyDefiner.java @@ -2,6 +2,8 @@ package com.sparrowwallet.drongo; import ch.qos.logback.core.PropertyDefinerBase; +import java.util.Locale; + public class PropertyDefiner extends PropertyDefinerBase { private String application; @@ -11,15 +13,15 @@ public class PropertyDefiner extends PropertyDefinerBase { @Override public String getPropertyValue() { - if(System.getProperty(application.toLowerCase() + ".home") != null) { - return System.getProperty(application.toLowerCase() + ".home"); + if(System.getProperty(application.toLowerCase(Locale.ROOT) + ".home") != null) { + return System.getProperty(application.toLowerCase(Locale.ROOT) + ".home"); } - return isWindows() ? System.getenv("APPDATA") + "/" + application.substring(0, 1).toUpperCase() + application.substring(1).toLowerCase() : System.getProperty("user.home") + "/." + application.toLowerCase(); + return isWindows() ? System.getenv("APPDATA") + "/" + application.substring(0, 1).toUpperCase(Locale.ROOT) + application.substring(1).toLowerCase(Locale.ROOT) : System.getProperty("user.home") + "/." + application.toLowerCase(Locale.ROOT); } private boolean isWindows() { String osName = System.getProperty("os.name"); - return (osName != null && osName.toLowerCase().startsWith("windows")); + return (osName != null && osName.toLowerCase(Locale.ROOT).startsWith("windows")); } } diff --git a/src/main/java/com/sparrowwallet/drongo/address/Address.java b/src/main/java/com/sparrowwallet/drongo/address/Address.java index af55c62..fc9bb0d 100644 --- a/src/main/java/com/sparrowwallet/drongo/address/Address.java +++ b/src/main/java/com/sparrowwallet/drongo/address/Address.java @@ -7,6 +7,7 @@ import com.sparrowwallet.drongo.protocol.Script; import com.sparrowwallet.drongo.protocol.ScriptType; import java.util.Arrays; +import java.util.Locale; public abstract class Address { protected final byte[] data; @@ -106,7 +107,7 @@ public abstract class Address { } } - if(address.toLowerCase().startsWith(network.getBech32AddressHRP())) { + if(address.toLowerCase(Locale.ROOT).startsWith(network.getBech32AddressHRP())) { try { Bech32.Bech32Data data = Bech32.decode(address); if(data.hrp.equals(network.getBech32AddressHRP())) { diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/ScriptType.java b/src/main/java/com/sparrowwallet/drongo/protocol/ScriptType.java index ecbec90..f79cd7b 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/ScriptType.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/ScriptType.java @@ -1288,7 +1288,7 @@ public enum ScriptType { scriptTypes.sort((o1, o2) -> o2.getDescriptor().length() - o1.getDescriptor().length()); for(ScriptType scriptType : scriptTypes) { - if(descriptor.toLowerCase().startsWith(scriptType.getDescriptor())) { + if(descriptor.toLowerCase(Locale.ROOT).startsWith(scriptType.getDescriptor())) { return scriptType; } } diff --git a/src/main/java/com/sparrowwallet/drongo/uri/BitcoinURI.java b/src/main/java/com/sparrowwallet/drongo/uri/BitcoinURI.java index 9ab8e57..800d0b8 100644 --- a/src/main/java/com/sparrowwallet/drongo/uri/BitcoinURI.java +++ b/src/main/java/com/sparrowwallet/drongo/uri/BitcoinURI.java @@ -154,7 +154,7 @@ public class BitcoinURI { if(sepIndex == 0) { throw new BitcoinURIParseException("Malformed Bitcoin URI - empty name '" + nameValuePairToken + "'"); } - final String nameToken = nameValuePairToken.substring(0, sepIndex).toLowerCase(Locale.ENGLISH); + final String nameToken = nameValuePairToken.substring(0, sepIndex).toLowerCase(Locale.ROOT); final String valueToken = nameValuePairToken.substring(sepIndex + 1); // Parse the amount. diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java b/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java index b73e442..628958e 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java @@ -1,10 +1,12 @@ package com.sparrowwallet.drongo.wallet; +import java.util.Locale; + public enum WalletModel { SEED, SPARROW, BITCOIN_CORE, ELECTRUM, TREZOR_1, TREZOR_T, COLDCARD, LEDGER_NANO_S, LEDGER_NANO_X, DIGITALBITBOX_01, KEEPKEY, SPECTER_DESKTOP, COBO_VAULT, BITBOX_02, SPECTER_DIY, PASSPORT, BLUE_WALLET, KEYSTONE, SEEDSIGNER, CARAVAN, GORDIAN_SEED_TOOL, JADE, LEDGER_NANO_S_PLUS; public static WalletModel getModel(String model) { - return valueOf(model.toUpperCase()); + return valueOf(model.toUpperCase(Locale.ROOT)); } public String getType() { @@ -44,7 +46,7 @@ public enum WalletModel { return "seedtool"; } - return this.toString().toLowerCase(); + return this.toString().toLowerCase(Locale.ROOT); } public boolean alwaysIncludeNonWitnessUtxo() { @@ -74,7 +76,7 @@ public enum WalletModel { } public String toDisplayString() { - String line = this.toString().toLowerCase(); + String line = this.toString().toLowerCase(Locale.ROOT); String[] words = line.split("_"); StringBuilder builder = new StringBuilder(); for(String word : words) { diff --git a/src/test/java/com/sparrowwallet/drongo/protocol/TransactionTest.java b/src/test/java/com/sparrowwallet/drongo/protocol/TransactionTest.java index 9934020..6bd3425 100644 --- a/src/test/java/com/sparrowwallet/drongo/protocol/TransactionTest.java +++ b/src/test/java/com/sparrowwallet/drongo/protocol/TransactionTest.java @@ -10,6 +10,7 @@ import org.junit.Test; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.Locale; import java.util.Map; import java.util.TreeMap; @@ -484,9 +485,9 @@ public class TransactionTest { @Test public void signBip340() { ECKey privKey = ECKey.fromPrivate(Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000003")); - Assert.assertEquals("F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9", Utils.bytesToHex(privKey.getPubKeyXCoord()).toUpperCase()); + Assert.assertEquals("F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9", Utils.bytesToHex(privKey.getPubKeyXCoord()).toUpperCase(Locale.ROOT)); SchnorrSignature sig = privKey.signSchnorr(Sha256Hash.ZERO_HASH); - Assert.assertEquals("E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0", Utils.bytesToHex(sig.encode()).toUpperCase()); + Assert.assertEquals("E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0", Utils.bytesToHex(sig.encode()).toUpperCase(Locale.ROOT)); } @Test diff --git a/src/test/java/com/sparrowwallet/drongo/uri/BitcoinUriTest.java b/src/test/java/com/sparrowwallet/drongo/uri/BitcoinUriTest.java index 52c6628..8127ddf 100644 --- a/src/test/java/com/sparrowwallet/drongo/uri/BitcoinUriTest.java +++ b/src/test/java/com/sparrowwallet/drongo/uri/BitcoinUriTest.java @@ -3,13 +3,15 @@ package com.sparrowwallet.drongo.uri; import org.junit.Assert; import org.junit.Test; +import java.util.Locale; + public class BitcoinUriTest { @Test public void testSamourai() throws BitcoinURIParseException { String uri = "bitcoin:BC1QT4NRM47695YWDG9N30N68JARMXRJNKFMR36994?amount=0,001"; BitcoinURI bitcoinURI = new BitcoinURI(uri); - Assert.assertEquals("BC1QT4NRM47695YWDG9N30N68JARMXRJNKFMR36994".toLowerCase(), bitcoinURI.getAddress().toString()); + Assert.assertEquals("BC1QT4NRM47695YWDG9N30N68JARMXRJNKFMR36994".toLowerCase(Locale.ROOT), bitcoinURI.getAddress().toString()); Assert.assertEquals(Long.valueOf(100000), bitcoinURI.getAmount()); } } diff --git a/src/test/java/com/sparrowwallet/drongo/wallet/PolicyTest.java b/src/test/java/com/sparrowwallet/drongo/wallet/PolicyTest.java index 811579c..a0f985d 100644 --- a/src/test/java/com/sparrowwallet/drongo/wallet/PolicyTest.java +++ b/src/test/java/com/sparrowwallet/drongo/wallet/PolicyTest.java @@ -7,6 +7,7 @@ import org.junit.Assert; import org.junit.Test; import java.util.List; +import java.util.Locale; public class PolicyTest { @Test @@ -16,27 +17,27 @@ public class PolicyTest { Keystore keystore3 = new Keystore("Keystore 3"); Policy policy = Policy.getPolicy(PolicyType.SINGLE, ScriptType.P2PKH, List.of(keystore1), 1); - Assert.assertEquals("pkh(keystore1)", policy.getMiniscript().toString().toLowerCase()); + Assert.assertEquals("pkh(keystore1)", policy.getMiniscript().toString().toLowerCase(Locale.ROOT)); Assert.assertEquals(1, policy.getNumSignaturesRequired()); Policy policy2 = Policy.getPolicy(PolicyType.SINGLE, ScriptType.P2SH_P2WPKH, List.of(keystore1), 1); - Assert.assertEquals("sh(wpkh(keystore1))", policy2.getMiniscript().toString().toLowerCase()); + Assert.assertEquals("sh(wpkh(keystore1))", policy2.getMiniscript().toString().toLowerCase(Locale.ROOT)); Assert.assertEquals(1, policy2.getNumSignaturesRequired()); Policy policy3 = Policy.getPolicy(PolicyType.SINGLE, ScriptType.P2WPKH, List.of(keystore1), 1); - Assert.assertEquals("wpkh(keystore1)", policy3.getMiniscript().toString().toLowerCase()); + Assert.assertEquals("wpkh(keystore1)", policy3.getMiniscript().toString().toLowerCase(Locale.ROOT)); Assert.assertEquals(1, policy3.getNumSignaturesRequired()); Policy policy4 = Policy.getPolicy(PolicyType.MULTI, ScriptType.P2SH, List.of(keystore1, keystore2, keystore3), 2); - Assert.assertEquals("sh(sortedmulti(2,keystore1,keystore2,keystore3))", policy4.getMiniscript().toString().toLowerCase()); + Assert.assertEquals("sh(sortedmulti(2,keystore1,keystore2,keystore3))", policy4.getMiniscript().toString().toLowerCase(Locale.ROOT)); Assert.assertEquals(2, policy4.getNumSignaturesRequired()); Policy policy5 = Policy.getPolicy(PolicyType.MULTI, ScriptType.P2SH_P2WSH, List.of(keystore1, keystore2, keystore3), 2); - Assert.assertEquals("sh(wsh(sortedmulti(2,keystore1,keystore2,keystore3)))", policy5.getMiniscript().toString().toLowerCase()); + Assert.assertEquals("sh(wsh(sortedmulti(2,keystore1,keystore2,keystore3)))", policy5.getMiniscript().toString().toLowerCase(Locale.ROOT)); Assert.assertEquals(2, policy5.getNumSignaturesRequired()); Policy policy6 = Policy.getPolicy(PolicyType.MULTI, ScriptType.P2WSH, List.of(keystore1, keystore2, keystore3), 2); - Assert.assertEquals("wsh(sortedmulti(2,keystore1,keystore2,keystore3))", policy6.getMiniscript().toString().toLowerCase()); + Assert.assertEquals("wsh(sortedmulti(2,keystore1,keystore2,keystore3))", policy6.getMiniscript().toString().toLowerCase(Locale.ROOT)); Assert.assertEquals(2, policy6.getNumSignaturesRequired()); } }