storage refactor and test

This commit is contained in:
Craig Raw 2020-04-30 13:41:17 +02:00
parent 981b379615
commit 60c1c17d26
5 changed files with 54 additions and 53 deletions

View file

@ -41,9 +41,9 @@ public class MainApp extends Application {
wallet.setScriptType(ScriptType.P2SH); wallet.setScriptType(ScriptType.P2SH);
KeystoreImportDialog dlg = new KeystoreImportDialog(wallet); KeystoreImportDialog dlg = new KeystoreImportDialog(wallet);
dlg.showAndWait(); //dlg.showAndWait();
//stage.show(); stage.show();
} }
public static void main(String[] args) { public static void main(String[] args) {

View file

@ -9,9 +9,7 @@ import com.sparrowwallet.drongo.wallet.Wallet;
import java.io.*; import java.io.*;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.zip.DataFormatException; import java.util.zip.*;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class Storage { public class Storage {
public static final String SPARROW_DIR = ".sparrow"; public static final String SPARROW_DIR = ".sparrow";
@ -49,31 +47,11 @@ public class Storage {
} }
public Wallet loadWallet(File file, ECKey encryptionKey) throws IOException { public Wallet loadWallet(File file, ECKey encryptionKey) throws IOException {
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); Reader reader = new InputStreamReader(new InflaterInputStream(new ECIESInputStream(new FileInputStream(file), encryptionKey, getEncryptionMagic())), StandardCharsets.UTF_8);
byte[] encrypted = ByteStreams.toByteArray(inputStream); Wallet wallet = gson.fromJson(reader, Wallet.class);
byte[] decrypted = encryptionKey.decryptEcies(encrypted, getEncryptionMagic()); reader.close();
String jsonWallet = inflate(decrypted);
return gson.fromJson(jsonWallet, Wallet.class); return wallet;
}
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);
}
return baos.toString(StandardCharsets.UTF_8);
} }
public void storeWallet(File file, Wallet wallet) throws IOException { public void storeWallet(File file, Wallet wallet) throws IOException {
@ -93,29 +71,9 @@ public class Storage {
throw new IOException("Could not create folder " + parent); throw new IOException("Could not create folder " + parent);
} }
String jsonWallet = gson.toJson(wallet); OutputStreamWriter writer = new OutputStreamWriter(new DeflaterOutputStream(new ECIESOutputStream(new FileOutputStream(file), encryptionKey, getEncryptionMagic())), StandardCharsets.UTF_8);
byte[] compressedWallet = deflate(jsonWallet); gson.toJson(wallet, writer);
byte[] encryptedWallet = encryptionKey.encryptEcies(compressedWallet, getEncryptionMagic()); writer.close();
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();
} }
private static byte[] getEncryptionMagic() { private static byte[] getEncryptionMagic() {

View file

@ -1,10 +1,16 @@
package com.sparrowwallet.sparrow.io; package com.sparrowwallet.sparrow.io;
import java.io.File;
import java.io.InputStream; import java.io.InputStream;
public class IoTest { 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) { protected InputStream getInputStream(String filename) {
return this.getClass().getResourceAsStream("/com/sparrowwallet/sparrow/io/" + filename); return this.getClass().getResourceAsStream(IO_TEST_PATH + filename);
} }
} }

View file

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

View file

@ -0,0 +1 @@
QklFMQNI/quQo9N7RtbygK+yhlrMNxkSnXtlaC9Ia5trm7AufOtbKhGqrtv5bQ/YcRVVaj/eKhO7LWTGbC6EWFYbIle/tpTyQB5XdceCCWmbUDwyob+thVpMLLrVe9PQD+EH6GM2cWGFUZNMHdYM2N/EaLU4Z2nnDz9pLzg1jpOtU9n3D1IeivULxfkupsd0AqxkpkXJlc0y7udh2qzXk/BPffYkEN0NexspO2+I1+o81g1IcVRXNV7LR8o/woKRM4MPBhUNVOy2F5JyvKnsteBKpEpKa4AyHmhGRtIdyKIZK4+osIU9Ig+b/AItDj9OG354gpL7oiU65s7rF8UsJpDLtxIyONUL6becqsNNem0rTbHQ0PI1uoWHmQj8dUl8sqhdIwC13Hhnx0+M5ICrqs3gk5tkUyiCDA7684jrWLGRjUzUXRPmNJsWPqlnCD2+MY93dduMwbJqV1USrOZDXsMd9LuGAV+UqEDMuBRjwXDxXQldrIBp9QKYac1mKFvj9UOJr062T2gwGsSyKY2R6oCiGJPkOZjRoQ0HHwJukFYJgoRRI34Hnh49LUfJybv+VEfqz9VJZhWnDhCgcFZ9r1BwY4CZ