Skip to content

Commit

Permalink
Merge pull request #33 from BTCPrivate/rework
Browse files Browse the repository at this point in the history
i18n
  • Loading branch information
Pega88 authored May 10, 2018
2 parents 9ca239a + 82e0ac8 commit 09efadf
Show file tree
Hide file tree
Showing 38 changed files with 7,728 additions and 5,388 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ plugins {
sourceCompatibility = 1.8
targetCompatibility = 1.8


mainClassName = "org.btcprivate.wallets.fullnode.ui.BTCPWalletUI"


repositories {
mavenCentral()
}
Expand Down Expand Up @@ -48,8 +52,6 @@ dependencies {
compile group: 'org.bitcoinj', name: 'bitcoinj-core', version: '0.14.5'
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.21.0'
compile group: 'org.vafer', name: 'jdeb', version: '1.5'

//TODO replace by GSON
compile group: 'com.eclipsesource.minimal-json', name: 'minimal-json', version: '0.9.5'


Expand Down
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Wed Mar 07 08:25:47 CET 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static synchronized DaemonInfo getDaemonInfoForUNIXLikeOS(String daemonNa
StringTokenizer st = new StringTokenizer(line, " \t", false);
boolean foundZCash = false;
for (int i = 0; i < 11; i++) {
String token = null;
String token;
if (st.hasMoreTokens()) {
token = st.nextToken();
} else {
Expand All @@ -100,17 +100,23 @@ public static synchronized DaemonInfo getDaemonInfoForUNIXLikeOS(String daemonNa
if (i == 2) {
try {
info.cpuPercentage = Double.valueOf(token);
} catch (NumberFormatException nfe) { /* TODO: Log or handle exception */ }
} catch (NumberFormatException nfe) {
Log.error("cant parse CPU precentage " + token);
}
;
} else if (i == 4) {
try {
info.virtualSizeMB = Double.valueOf(token) / 1000;
} catch (NumberFormatException nfe) { /* TODO: Log or handle exception */ }
} catch (NumberFormatException nfe) {
Log.error("cant parse virtual MB size" + token);
}
;
} else if (i == 5) {
try {
info.residentSizeMB = Double.valueOf(token) / 1000;
} catch (NumberFormatException nfe) { /* TODO: Log or handle exception */ }
} catch (NumberFormatException nfe) {
Log.error("cant parse resident MB size " + token);
}
;
} else if (i == 10) {
// account for the case where Application names in Mac OS X commonly have spaces in them
Expand Down Expand Up @@ -185,7 +191,9 @@ public static synchronized DaemonInfo getDaemonInfoForWindowsOS(String daemonNam
if (size.endsWith("K")) {
size = size.substring(0, size.length() - 1);
}
} catch (NumberFormatException nfe) { /* TODO: Log or handle exception */ }
} catch (NumberFormatException nfe) {
Log.error("cant parse number " + token);
}
;
}
} // End parsing row
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
package org.btcprivate.wallets.fullnode.daemon;

import org.btcprivate.wallets.fullnode.util.Log;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;


/**
* Executes a command and retruns the result.
* Executes a command and retruns sthe result.
*
* @author Ivan Vaklinov <ivan@vaklinov.com>
*/
public class CommandExecutor
{
public class CommandExecutor {
private String args[];

public CommandExecutor(String args[])
throws IOException
{
throws IOException {
this.args = args;
}


public Process startChildProcess()
throws IOException
{
throws IOException {
return Runtime.getRuntime().exec(args);
}


public String execute()
throws IOException, InterruptedException
{
throws IOException, InterruptedException {
final StringBuffer result = new StringBuffer();

Runtime rt = Runtime.getRuntime();
Expand All @@ -42,44 +40,28 @@ public String execute()
final Reader err = new InputStreamReader(proc.getErrorStream());

Thread inThread = new Thread(
new Runnable()
{
@Override
public void run()
{
try
{
int c;
while ((c = in.read()) != -1)
{
result.append((char)c);
}
} catch (IOException ioe)
{
// TODO: log or handle the exception
() -> {
try {
int c;
while ((c = in.read()) != -1) {
result.append((char) c);
}
} catch (IOException ioe) {
Log.error("Error while executing command on daemon. Command attempted: " + args + ". Error: " + ioe.getMessage());
}
}
);
inThread.start();

Thread errThread = new Thread(
new Runnable()
{
@Override
public void run()
{
try
{
int c;
while ((c = err.read()) != -1)
{
result.append((char)c);
}
} catch (IOException ioe)
{
// TODO: log or handle the exception
Thread errThread = new Thread(
() -> {
try {
int c;
while ((c = err.read()) != -1) {
result.append((char) c);
}
} catch (IOException ioe) {
Log.error("Error while executing command on daemon. Command attempted: " + args + ". Error: " + ioe.getMessage());
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.ProgressMonitorInputStream;
Expand All @@ -24,123 +23,123 @@
import org.btcprivate.wallets.fullnode.ui.StartupProgressDialog;
import org.btcprivate.wallets.fullnode.util.OSUtil;
import org.btcprivate.wallets.fullnode.util.OSUtil.*;
import org.btcprivate.wallets.fullnode.util.Util;
import static org.btcprivate.wallets.fullnode.util.Util.*;


/**
* Fetches the proving key. Deliberately hardcoded.
*
* @author zab
*/
public class ProvingKeyFetcher {

private static final int PROVING_KEY_SIZE = 910173851;
private static final String SHA256 = "8bc20a7f013b2b58970cddd2e7ea028975c88ae7ceb9259a5344a16bc2c0eef7";
private static final String pathURL = "https://storage.googleapis.com/btcp-sprout-key/sprout-proving.key";
//private static final String altPathURL = "https://zensystem.io/downloads/sprout-proving.key";






private static final String LOCAL_MSG_PROVINGKEY_DOWNLOAD_REQURED = "LOCAL_MSG_PROVINGKEY_DOWNLOAD_REQURED";
private static final String LOCAL_MSG_DOWNLOADING_PROVING_KEY = "LOCAL_MSG_DOWNLOADING_PROVING_KEY";
private static final String LOCAL_MSG_VERIFYING_PROVING_KEY = "LOCAL_MSG_VERIFYING_PROVING_KEY";
private static final String LOCAL_MSG_NO_PROVING_KEY = "LOCAL_MSG_NO_PROVING_KEY";
private static final String LOCAL_MSG_VERIFYING_DOWNLOADED_PROVING_KEY = "LOCAL_MSG_VERIFYING_DOWNLOADED_PROVING_KEY";
private static final String LOCAL_MSG_VERIFYING_DOWNLOADED_PROVING_KEY_FAILED = "LOCAL_MSG_VERIFYING_DOWNLOADED_PROVING_KEY_FAILED";


public void fetchIfMissing(StartupProgressDialog parent) throws IOException {
try {
verifyOrFetch(parent);
} catch (InterruptedIOException iox) {
JOptionPane.showMessageDialog(parent, "The Bitcoin Private wallet cannot proceed without a proving key.");
JOptionPane.showMessageDialog(parent, LOCAL_MSG_NO_PROVING_KEY);
System.exit(-3);
}
}

private void verifyOrFetch(StartupProgressDialog parent)
throws IOException
{
OS_TYPE ost = OSUtil.getOSType();

File zCashParams = null;
// TODO: isolate getting ZcashParams in a utility method
if (ost == OS_TYPE.WINDOWS)
{
zCashParams = new File(System.getenv("APPDATA") + "/ZcashParams");
} else if (ost == OS_TYPE.MAC_OS)
{
File userHome = new File(System.getProperty("user.home"));
zCashParams = new File(userHome, "Library/Application Support/ZcashParams");
}
throws IOException {

zCashParams = zCashParams.getCanonicalFile();
File zCashParams = getZCashParamsFile();

boolean needsFetch = false;
if (!zCashParams.exists())
{
if (!zCashParams.exists()) {
needsFetch = true;
zCashParams.mkdirs();
}

// verifying key is small, always copy it
File verifyingKeyFile = new File(zCashParams,"sprout-verifying.key");
File verifyingKeyFile = new File(zCashParams, VERIFYING_KEY_FILE);
FileOutputStream fos = new FileOutputStream(verifyingKeyFile);
InputStream is = ProvingKeyFetcher.class.getClassLoader().getResourceAsStream("keys/sprout-verifying.key");
copy(is,fos);
InputStream is = ProvingKeyFetcher.class.getClassLoader().getResourceAsStream(VERIFYING_KEY_FILE_SOURCE);
copy(is, fos);
fos.close();
is = null;

File provingKeyFile = new File(zCashParams,"sprout-proving.key");
File provingKeyFile = new File(zCashParams, PROVING_KEY_FILE);
provingKeyFile = provingKeyFile.getCanonicalFile();
if (!provingKeyFile.exists())
{
if (!provingKeyFile.exists()) {
needsFetch = true;
} else if (provingKeyFile.length() != PROVING_KEY_SIZE)
{
} else if (provingKeyFile.length() != PROVING_KEY_SIZE) {
needsFetch = true;
}
/*
* We skip proving key verification every start - this is impractical.
* If the proving key exists and is the correct size, then it should be OK.
else
{
parent.setProgressText("Verifying proving key...");
needsFetch = !checkSHA256(provingKeyFile,parent);
}*/

if (!needsFetch)
{

if (!needsFetch) {
return;
}

JOptionPane.showMessageDialog(
parent,
"The wallet needs to download the Z cryptographic proving key (approx. 900 MB).\n" +
"This will be done only once. Please be patient... Press OK to continue");
JOptionPane.showMessageDialog(parent, Util.local(LOCAL_MSG_PROVINGKEY_DOWNLOAD_REQURED));

parent.setProgressText("Downloading proving key...");
parent.setProgressText(LOCAL_MSG_DOWNLOADING_PROVING_KEY);
provingKeyFile.delete();
OutputStream os = new BufferedOutputStream(new FileOutputStream(provingKeyFile));
URL keyURL = new URL(pathURL);
URL keyURL = new URL(PROVING_KEY_PATH_URL);
URLConnection urlc = keyURL.openConnection();
urlc.setRequestProperty("User-Agent", "Wget/1.17.1 (linux-gnu)");

try
{
try {
is = urlc.getInputStream();
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent, "Downloading proving key", is);
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent, LOCAL_MSG_DOWNLOADING_PROVING_KEY, is);
pmis.getProgressMonitor().setMaximum(PROVING_KEY_SIZE);
pmis.getProgressMonitor().setMillisToPopup(10);

copy(pmis,os);
copy(pmis, os);
os.close();
} finally
{
try { if (is != null) is.close(); } catch (IOException ignore){}
} finally {
try {
if (is != null) is.close();
} catch (IOException ignore) {
}
}
parent.setProgressText("Verifying downloaded proving key...");
if (!checkSHA256(provingKeyFile, parent))
{
JOptionPane.showMessageDialog(parent, "Failed to download proving key properly. Cannot continue!");
parent.setProgressText(LOCAL_MSG_VERIFYING_DOWNLOADED_PROVING_KEY);
if (!checkSHA256(provingKeyFile, parent)) {
JOptionPane.showMessageDialog(parent, LOCAL_MSG_VERIFYING_DOWNLOADED_PROVING_KEY_FAILED);
System.exit(-4);
}
}

private File getZCashParamsFile() throws IOException {
OS_TYPE ost = OSUtil.getOSType();
File zCashParams = null;

if (ost == OS_TYPE.WINDOWS) {
zCashParams = new File(System.getenv(WINDOWS_ENV_FOLDER) + OSX_WINDOWS_ZCASH_KEY_FOLDER);
} else if (ost == OS_TYPE.MAC_OS) {
File userHome = new File(System.getProperty("user.home"));
zCashParams = new File(userHome, OSX_ENV_FOLDER + OSX_WINDOWS_ZCASH_KEY_FOLDER);
}

zCashParams = zCashParams.getCanonicalFile();
return zCashParams;
}


private static void copy(InputStream is, OutputStream os) throws IOException {
byte[] buf = new byte[0x1 << 13];
int read;
while ((read = is.read(buf)) >- 0) {
os.write(buf,0,read);
while ((read = is.read(buf)) > -0) {
os.write(buf, 0, read);
}
os.flush();
}
Expand All @@ -153,14 +152,14 @@ private static boolean checkSHA256(File provingKey, Component parent) throws IOE
throw new IOException(impossible);
}
try (InputStream is = new BufferedInputStream(new FileInputStream(provingKey))) {
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent,"Verifying proving key",is);
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent, LOCAL_MSG_VERIFYING_PROVING_KEY, is);
pmis.getProgressMonitor().setMaximum(PROVING_KEY_SIZE);
pmis.getProgressMonitor().setMillisToPopup(10);
DigestInputStream dis = new DigestInputStream(pmis, sha256);
byte [] temp = new byte[0x1 << 13];
while(dis.read(temp) >= 0);
byte [] digest = sha256.digest();
return SHA256.equalsIgnoreCase(DatatypeConverter.printHexBinary(digest));
byte[] temp = new byte[0x1 << 13];
while (dis.read(temp) >= 0) ;
byte[] digest = sha256.digest();
return PROVING_KEY_SHA256.equalsIgnoreCase(DatatypeConverter.printHexBinary(digest));
}
}
}
Loading

0 comments on commit 09efadf

Please sign in to comment.