diff --git a/src/main/java/com/sparrowwallet/sparrow/MainApp.java b/src/main/java/com/sparrowwallet/sparrow/MainApp.java index 54272bc9..d903ce19 100644 --- a/src/main/java/com/sparrowwallet/sparrow/MainApp.java +++ b/src/main/java/com/sparrowwallet/sparrow/MainApp.java @@ -41,9 +41,9 @@ public class MainApp extends Application { wallet.setScriptType(ScriptType.P2SH); KeystoreImportDialog dlg = new KeystoreImportDialog(wallet); - dlg.showAndWait(); + //dlg.showAndWait(); - //stage.show(); + stage.show(); } public static void main(String[] args) { diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index b2bd0771..39ac2e32 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -9,9 +9,7 @@ import com.sparrowwallet.drongo.wallet.Wallet; import java.io.*; import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; -import java.util.zip.DataFormatException; -import java.util.zip.Deflater; -import java.util.zip.Inflater; +import java.util.zip.*; public class Storage { public static final String SPARROW_DIR = ".sparrow"; @@ -49,31 +47,11 @@ public class Storage { } public Wallet loadWallet(File file, ECKey encryptionKey) throws IOException { - BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); - byte[] encrypted = ByteStreams.toByteArray(inputStream); - byte[] decrypted = encryptionKey.decryptEcies(encrypted, getEncryptionMagic()); - String jsonWallet = inflate(decrypted); - - return gson.fromJson(jsonWallet, Wallet.class); - } - - private static String inflate(byte[] encryptedWallet) { - Inflater inflater = new Inflater(); - inflater.setInput(encryptedWallet); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - byte[] buf = new byte[1024]; - while(!inflater.finished()) { - int byteCount = inflater.inflate(buf); - baos.write(buf, 0, byteCount); - } - inflater.end(); - } catch(DataFormatException e) { - throw new RuntimeException(e); - } + Reader reader = new InputStreamReader(new InflaterInputStream(new ECIESInputStream(new FileInputStream(file), encryptionKey, getEncryptionMagic())), StandardCharsets.UTF_8); + Wallet wallet = gson.fromJson(reader, Wallet.class); + reader.close(); - return baos.toString(StandardCharsets.UTF_8); + return wallet; } public void storeWallet(File file, Wallet wallet) throws IOException { @@ -93,29 +71,9 @@ public class Storage { throw new IOException("Could not create folder " + parent); } - String jsonWallet = gson.toJson(wallet); - byte[] compressedWallet = deflate(jsonWallet); - byte[] encryptedWallet = encryptionKey.encryptEcies(compressedWallet, getEncryptionMagic()); - - BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file)); - outputStream.write(encryptedWallet); - outputStream.close(); - } - - private static byte[] deflate(String jsonWallet) { - Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); - deflater.setInput(jsonWallet.getBytes(StandardCharsets.UTF_8)); - deflater.finish(); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buf = new byte[1024]; - while(!deflater.finished()) { - int byteCount = deflater.deflate(buf); - baos.write(buf, 0, byteCount); - } - deflater.end(); - - return baos.toByteArray(); + OutputStreamWriter writer = new OutputStreamWriter(new DeflaterOutputStream(new ECIESOutputStream(new FileOutputStream(file), encryptionKey, getEncryptionMagic())), StandardCharsets.UTF_8); + gson.toJson(wallet, writer); + writer.close(); } private static byte[] getEncryptionMagic() { diff --git a/src/test/java/com/sparrowwallet/sparrow/io/IoTest.java b/src/test/java/com/sparrowwallet/sparrow/io/IoTest.java index 2aae742a..588f3a4c 100644 --- a/src/test/java/com/sparrowwallet/sparrow/io/IoTest.java +++ b/src/test/java/com/sparrowwallet/sparrow/io/IoTest.java @@ -1,10 +1,16 @@ package com.sparrowwallet.sparrow.io; +import java.io.File; import java.io.InputStream; public class IoTest { + public static final String IO_TEST_PATH = "/com/sparrowwallet/sparrow/io/"; + + protected File getFile(String filename) { + return new File(this.getClass().getResource(IO_TEST_PATH + filename).getFile()); + } protected InputStream getInputStream(String filename) { - return this.getClass().getResourceAsStream("/com/sparrowwallet/sparrow/io/" + filename); + return this.getClass().getResourceAsStream(IO_TEST_PATH + filename); } } diff --git a/src/test/java/com/sparrowwallet/sparrow/io/StorageTest.java b/src/test/java/com/sparrowwallet/sparrow/io/StorageTest.java new file mode 100644 index 00000000..760c9c43 --- /dev/null +++ b/src/test/java/com/sparrowwallet/sparrow/io/StorageTest.java @@ -0,0 +1,36 @@ +package com.sparrowwallet.sparrow.io; + +import com.sparrowwallet.drongo.crypto.ECKey; +import com.sparrowwallet.drongo.wallet.Wallet; +import org.junit.Assert; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; + +public class StorageTest extends IoTest { + @Test + public void loadWallet() throws IOException { + ECKey decryptionKey = ECKey.createKeyPbkdf2HmacSha512("pass"); + Wallet wallet = Storage.getStorage().loadWallet(getFile("sparrow-single-wallet"), decryptionKey); + Assert.assertTrue(wallet.isValid()); + } + + @Test + public void saveWallet() throws IOException { + ECKey decryptionKey = ECKey.createKeyPbkdf2HmacSha512("pass"); + Wallet wallet = Storage.getStorage().loadWallet(getFile("sparrow-single-wallet"), decryptionKey); + Assert.assertTrue(wallet.isValid()); + + ECKey encyptionKey = ECKey.fromPublicOnly(decryptionKey); + File tempWallet = File.createTempFile("sparrow", "tmp"); + tempWallet.deleteOnExit(); + + ByteArrayOutputStream dummyFileOutputStream = new ByteArrayOutputStream(); + Storage.getStorage().storeWallet(tempWallet, encyptionKey, wallet); + + wallet = Storage.getStorage().loadWallet(tempWallet, decryptionKey); + Assert.assertTrue(wallet.isValid()); + } +} diff --git a/src/test/resources/com/sparrowwallet/sparrow/io/sparrow-single-wallet b/src/test/resources/com/sparrowwallet/sparrow/io/sparrow-single-wallet new file mode 100644 index 00000000..74bf083d --- /dev/null +++ b/src/test/resources/com/sparrowwallet/sparrow/io/sparrow-single-wallet @@ -0,0 +1 @@ +QklFMQNI/quQo9N7RtbygK+yhlrMNxkSnXtlaC9Ia5trm7AufOtbKhGqrtv5bQ/YcRVVaj/eKhO7LWTGbC6EWFYbIle/tpTyQB5XdceCCWmbUDwyob+thVpMLLrVe9PQD+EH6GM2cWGFUZNMHdYM2N/EaLU4Z2nnDz9pLzg1jpOtU9n3D1IeivULxfkupsd0AqxkpkXJlc0y7udh2qzXk/BPffYkEN0NexspO2+I1+o81g1IcVRXNV7LR8o/woKRM4MPBhUNVOy2F5JyvKnsteBKpEpKa4AyHmhGRtIdyKIZK4+osIU9Ig+b/AItDj9OG354gpL7oiU65s7rF8UsJpDLtxIyONUL6becqsNNem0rTbHQ0PI1uoWHmQj8dUl8sqhdIwC13Hhnx0+M5ICrqs3gk5tkUyiCDA7684jrWLGRjUzUXRPmNJsWPqlnCD2+MY93dduMwbJqV1USrOZDXsMd9LuGAV+UqEDMuBRjwXDxXQldrIBp9QKYac1mKFvj9UOJr062T2gwGsSyKY2R6oCiGJPkOZjRoQ0HHwJukFYJgoRRI34Hnh49LUfJybv+VEfqz9VJZhWnDhCgcFZ9r1BwY4CZ \ No newline at end of file