diff --git a/drongo b/drongo index 66ff275f..2468578e 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit 66ff275f4641f650dc1cddf28e9b6ba683b16f04 +Subproject commit 2468578e723653344579aac857ee76d1a69fecde diff --git a/src/main/java/com/sparrowwallet/sparrow/io/FileType.java b/src/main/java/com/sparrowwallet/sparrow/io/FileType.java deleted file mode 100644 index 74ace824..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/io/FileType.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.sparrowwallet.sparrow.io; - -public enum FileType { - TEXT, JSON, BINARY, UNKNOWN; -} diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Hwi.java b/src/main/java/com/sparrowwallet/sparrow/io/Hwi.java index f3a2aa52..ffa10745 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Hwi.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Hwi.java @@ -1,6 +1,7 @@ package com.sparrowwallet.sparrow.io; import com.sparrowwallet.drongo.ExtendedKey; +import com.sparrowwallet.drongo.IOUtils; import com.sparrowwallet.drongo.OsType; import com.sparrowwallet.drongo.OutputDescriptor; import com.sparrowwallet.drongo.protocol.ScriptType; diff --git a/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java b/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java deleted file mode 100644 index 9122fd0f..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java +++ /dev/null @@ -1,153 +0,0 @@ -package com.sparrowwallet.sparrow.io; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.*; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; -import java.nio.file.FileSystems; -import java.nio.file.Files; -import java.nio.file.Path; -import java.security.SecureRandom; -import java.util.*; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; - -public class IOUtils { - private static final Logger log = LoggerFactory.getLogger(IOUtils.class); - - public static FileType getFileType(File file) { - try { - String type = Files.probeContentType(file.toPath()); - if(type == null) { - if(file.getName().toLowerCase(Locale.ROOT).endsWith("txn") || file.getName().toLowerCase(Locale.ROOT).endsWith("psbt")) { - return FileType.TEXT; - } - - if(file.exists()) { - try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) { - String line = br.readLine(); - if(line != null) { - if(line.startsWith("01000000") || line.startsWith("cHNid")) { - return FileType.TEXT; - } else if(line.startsWith("{")) { - return FileType.JSON; - } - } - } - } - - return FileType.BINARY; - } else if (type.equals("application/json")) { - return FileType.JSON; - } else if (type.startsWith("text")) { - return FileType.TEXT; - } - } catch (IOException e) { - //ignore - } - - return FileType.UNKNOWN; - } - - /** - * List directory contents for a resource folder. Not recursive. - * This is basically a brute-force implementation. - * Works for regular files, JARs and Java modules. - * - * @param clazz Any java class that lives in the same place as the resources you want. - * @param path Should end with "/", but not start with one. - * @return Just the name of each member item, not the full paths. - * @throws URISyntaxException - * @throws IOException - */ - public static String[] getResourceListing(Class clazz, String path) throws URISyntaxException, IOException { - URL dirURL = clazz.getClassLoader().getResource(path); - if(dirURL != null && dirURL.getProtocol().equals("file")) { - /* A file path: easy enough */ - return new File(dirURL.toURI()).list(); - } - - if(dirURL == null) { - /* - * In case of a jar file, we can't actually find a directory. - * Have to assume the same jar as clazz. - */ - String me = clazz.getName().replace(".", "/")+".class"; - dirURL = clazz.getClassLoader().getResource(me); - } - - if(dirURL.getProtocol().equals("jar")) { - /* A JAR path */ - String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file - JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); - Enumeration entries = jar.entries(); //gives ALL entries in jar - Set result = new HashSet(); //avoid duplicates in case it is a subdirectory - while(entries.hasMoreElements()) { - String name = entries.nextElement().getName(); - if(name.startsWith(path)) { //filter according to the path - String entry = name.substring(path.length()); - int checkSubdir = entry.indexOf("/"); - if (checkSubdir >= 0) { - // if it is a subdirectory, we just return the directory name - entry = entry.substring(0, checkSubdir); - } - if(!entry.isEmpty()) { - result.add(entry); - } - } - } - - return result.toArray(new String[result.size()]); - } - - if(dirURL.getProtocol().equals("jrt")) { - java.nio.file.FileSystem jrtFs = FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap()); - Path resourcePath = jrtFs.getPath("modules/com.sparrowwallet.sparrow", path); - return Files.list(resourcePath).map(filePath -> filePath.getFileName().toString()).toArray(String[]::new); - } - - throw new UnsupportedOperationException("Cannot list files for URL " + dirURL); - } - - public static boolean deleteDirectory(File directory) { - try { - Files.walk(directory.toPath()) - .sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); - } catch(IOException e) { - return false; - } - - return true; - } - - public static boolean secureDelete(File file) { - if(file.exists()) { - long length = file.length(); - SecureRandom random = new SecureRandom(); - byte[] data = new byte[1024*1024]; - random.nextBytes(data); - try(RandomAccessFile raf = new RandomAccessFile(file, "rws")) { - raf.seek(0); - raf.getFilePointer(); - int pos = 0; - while(pos < length) { - raf.write(data); - pos += data.length; - } - } catch(IOException e) { - log.warn("Error overwriting file for deletion: " + file.getName(), e); - } - - return file.delete(); - } - - return false; - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/io/JsonPersistence.java b/src/main/java/com/sparrowwallet/sparrow/io/JsonPersistence.java index 12d89e3f..3bb12dad 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/JsonPersistence.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/JsonPersistence.java @@ -2,6 +2,8 @@ package com.sparrowwallet.sparrow.io; import com.google.gson.*; import com.sparrowwallet.drongo.ExtendedKey; +import com.sparrowwallet.drongo.FileType; +import com.sparrowwallet.drongo.IOUtils; import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.address.Address; import com.sparrowwallet.drongo.address.InvalidAddressException; diff --git a/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java b/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java index fdd7a571..a4c216e3 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java @@ -1,6 +1,7 @@ package com.sparrowwallet.sparrow.io.db; import com.google.common.eventbus.Subscribe; +import com.sparrowwallet.drongo.IOUtils; import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.crypto.Argon2KeyDeriver; import com.sparrowwallet.drongo.crypto.AsymmetricKeyDeriver;