From 38818d822235b3de7e1d48d7e7d9e6849f532645 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 13 Jan 2021 12:17:01 +0200 Subject: [PATCH] always set charset to utf-8 when converting from bytes to string --- .../com/sparrowwallet/sparrow/io/CoboVaultSinglesig.java | 3 ++- .../java/com/sparrowwallet/sparrow/io/ColdcardMultisig.java | 5 +++-- .../java/com/sparrowwallet/sparrow/io/ColdcardSinglesig.java | 3 ++- src/main/java/com/sparrowwallet/sparrow/io/Electrum.java | 4 ++-- src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java | 3 ++- src/main/java/com/sparrowwallet/sparrow/io/SpecterDIY.java | 3 ++- .../java/com/sparrowwallet/sparrow/io/SpecterDesktop.java | 2 +- .../java/com/sparrowwallet/sparrow/net/TcpTransport.java | 3 ++- .../com/sparrowwallet/sparrow/net/VersionCheckService.java | 3 ++- 9 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/io/CoboVaultSinglesig.java b/src/main/java/com/sparrowwallet/sparrow/io/CoboVaultSinglesig.java index 0c229b9d..dc0b40cf 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/CoboVaultSinglesig.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/CoboVaultSinglesig.java @@ -11,6 +11,7 @@ import com.sparrowwallet.drongo.wallet.*; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; public class CoboVaultSinglesig implements KeystoreFileImport, WalletImport { @Override @@ -32,7 +33,7 @@ public class CoboVaultSinglesig implements KeystoreFileImport, WalletImport { public Keystore getKeystore(ScriptType scriptType, InputStream inputStream, String password) throws ImportException { try { Gson gson = new Gson(); - CoboVaultSinglesigKeystore coboKeystore = gson.fromJson(new InputStreamReader(inputStream), CoboVaultSinglesigKeystore.class); + CoboVaultSinglesigKeystore coboKeystore = gson.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), CoboVaultSinglesigKeystore.class); if(coboKeystore.MasterFingerprint == null || coboKeystore.AccountKeyPath == null || coboKeystore.ExtPubKey == null) { throw new ImportException("Not a valid " + getName() + " keystore export"); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/ColdcardMultisig.java b/src/main/java/com/sparrowwallet/sparrow/io/ColdcardMultisig.java index 172ae84d..75a5c8c6 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/ColdcardMultisig.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/ColdcardMultisig.java @@ -13,6 +13,7 @@ import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.WalletModel; import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -30,7 +31,7 @@ public class ColdcardMultisig implements WalletImport, KeystoreFileImport, Walle @Override public Keystore getKeystore(ScriptType scriptType, InputStream inputStream, String password) throws ImportException { - InputStreamReader reader = new InputStreamReader(inputStream); + InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); ColdcardKeystore cck = Storage.getGson().fromJson(reader, ColdcardKeystore.class); Keystore keystore = new Keystore("Coldcard"); @@ -94,7 +95,7 @@ public class ColdcardMultisig implements WalletImport, KeystoreFileImport, Walle String derivation = null; try { - List lines = CharStreams.readLines(new InputStreamReader(inputStream)); + List lines = CharStreams.readLines(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); for (String line : lines) { line = line.trim(); if (line.isEmpty()) { diff --git a/src/main/java/com/sparrowwallet/sparrow/io/ColdcardSinglesig.java b/src/main/java/com/sparrowwallet/sparrow/io/ColdcardSinglesig.java index f7c1a450..5ccf25ce 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/ColdcardSinglesig.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/ColdcardSinglesig.java @@ -14,6 +14,7 @@ import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; import java.util.Map; public class ColdcardSinglesig implements KeystoreFileImport, WalletImport { @@ -53,7 +54,7 @@ public class ColdcardSinglesig implements KeystoreFileImport, WalletImport { Gson gson = new Gson(); Type stringStringMap = new TypeToken>() { }.getType(); - Map map = gson.fromJson(new InputStreamReader(inputStream), stringStringMap); + Map map = gson.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), stringStringMap); if (map.get("xfp") == null) { throw new ImportException("File was not a valid Coldcard wallet export"); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java b/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java index 15546a64..618197a0 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java @@ -58,9 +58,9 @@ public class Electrum implements KeystoreFileImport, WalletImport, WalletExport Reader reader; if(password != null) { ECKey decryptionKey = Pbkdf2KeyDeriver.DEFAULT_INSTANCE.deriveECKey(password); - reader = new InputStreamReader(new InflaterInputStream(new ECIESInputStream(inputStream, decryptionKey))); + reader = new InputStreamReader(new InflaterInputStream(new ECIESInputStream(inputStream, decryptionKey)), StandardCharsets.UTF_8); } else { - reader = new InputStreamReader(inputStream); + reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); } try { diff --git a/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java b/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java index 624e94b8..1ae02cf8 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java @@ -1,6 +1,7 @@ package com.sparrowwallet.sparrow.io; import java.io.*; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; public class IOUtils { @@ -13,7 +14,7 @@ public class IOUtils { } if(file.exists()) { - try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) { + try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) { String line = br.readLine(); if(line.startsWith("01000000") || line.startsWith("cHNid")) { return FileType.TEXT; diff --git a/src/main/java/com/sparrowwallet/sparrow/io/SpecterDIY.java b/src/main/java/com/sparrowwallet/sparrow/io/SpecterDIY.java index 8265b27a..637437e8 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/SpecterDIY.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/SpecterDIY.java @@ -12,12 +12,13 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; public class SpecterDIY implements KeystoreFileImport { @Override public Keystore getKeystore(ScriptType scriptType, InputStream inputStream, String password) throws ImportException { try { - String text = CharStreams.toString(new InputStreamReader(inputStream)); + String text = CharStreams.toString(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); String outputDesc = "sh(" + text + ")"; OutputDescriptor outputDescriptor = OutputDescriptor.getOutputDescriptor(outputDesc); Wallet wallet = outputDescriptor.toWallet(); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/SpecterDesktop.java b/src/main/java/com/sparrowwallet/sparrow/io/SpecterDesktop.java index b9dc7c52..fbc17eec 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/SpecterDesktop.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/SpecterDesktop.java @@ -51,7 +51,7 @@ public class SpecterDesktop implements WalletImport, WalletExport { public Wallet importWallet(InputStream inputStream, String password) throws ImportException { try { Gson gson = new Gson(); - SpecterWallet specterWallet = gson.fromJson(new InputStreamReader(inputStream), SpecterWallet.class); + SpecterWallet specterWallet = gson.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), SpecterWallet.class); if(specterWallet.descriptor != null) { OutputDescriptor outputDescriptor = OutputDescriptor.getOutputDescriptor(specterWallet.descriptor); diff --git a/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java b/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java index 5751e159..b9e1d040 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java @@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory; import javax.net.SocketFactory; import java.io.*; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.Objects; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; @@ -130,7 +131,7 @@ public class TcpTransport implements Transport, Closeable { Thread.currentThread().interrupt(); } - BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); while(running) { try { diff --git a/src/main/java/com/sparrowwallet/sparrow/net/VersionCheckService.java b/src/main/java/com/sparrowwallet/sparrow/net/VersionCheckService.java index bf60ca9f..39640221 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/VersionCheckService.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/VersionCheckService.java @@ -16,6 +16,7 @@ import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.security.SignatureException; import java.util.Map; @@ -45,7 +46,7 @@ public class VersionCheckService extends ScheduledService { URL url = new URL(VERSION_CHECK_URL); HttpsURLConnection conn = (HttpsURLConnection)url.openConnection(); - try(InputStreamReader reader = new InputStreamReader(conn.getInputStream())) { + try(InputStreamReader reader = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)) { Gson gson = new Gson(); return gson.fromJson(reader, VersionCheck.class); }