always set charset to utf-8 when converting from bytes to string

This commit is contained in:
Craig Raw 2021-01-13 12:17:01 +02:00
parent fd7d2232a1
commit 38818d8222
9 changed files with 18 additions and 11 deletions

View file

@ -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");

View file

@ -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<String> lines = CharStreams.readLines(new InputStreamReader(inputStream));
List<String> lines = CharStreams.readLines(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
for (String line : lines) {
line = line.trim();
if (line.isEmpty()) {

View file

@ -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<Map<String, JsonElement>>() {
}.getType();
Map<String, JsonElement> map = gson.fromJson(new InputStreamReader(inputStream), stringStringMap);
Map<String, JsonElement> 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");

View file

@ -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 {

View file

@ -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;

View file

@ -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();

View file

@ -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);

View file

@ -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 {

View file

@ -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<VersionUpdatedEvent> {
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);
}