Search in sources :

Example 1 with OS_TYPE

use of com.vaklinov.zcashui.OSUtil.OS_TYPE in project zencash-swing-wallet-ui by ZencashOfficial.

the class ZCashUI method main.

public static void main(String[] argv) throws IOException {
    try {
        OS_TYPE os = OSUtil.getOSType();
        if ((os == OS_TYPE.WINDOWS) || (os == OS_TYPE.MAC_OS)) {
            possiblyCreateZENConfigFile();
        }
        LanguageUtil langUtil = LanguageUtil.instance();
        Log.info("Starting ZENCash Swing Wallet ...");
        Log.info("OS: " + System.getProperty("os.name") + " = " + os);
        Log.info("Current directory: " + new File(".").getCanonicalPath());
        Log.info("Class path: " + System.getProperty("java.class.path"));
        Log.info("Environment PATH: " + System.getenv("PATH"));
        // Look and feel settings - a custom OS-look and feel is set for Windows
        if (os == OS_TYPE.WINDOWS) {
            // Custom Windows L&F and font settings
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        // This font looks good but on Windows 7 it misses some chars like the stars...
        // FontUIResource font = new FontUIResource("Lucida Sans Unicode", Font.PLAIN, 11);
        // UIManager.put("Table.font", font);
        } else if (os == OS_TYPE.MAC_OS) {
            // The MacOS L&F is active by default - the property sets the menu bar Mac style
            System.setProperty("apple.laf.useScreenMenuBar", "true");
        } else {
            for (LookAndFeelInfo ui : UIManager.getInstalledLookAndFeels()) {
                Log.info("Available look and feel: " + ui.getName() + " " + ui.getClassName());
                if (ui.getName().equals("Nimbus")) {
                    Log.info("Setting look and feel: {0}", ui.getClassName());
                    UIManager.setLookAndFeel(ui.getClassName());
                    break;
                }
                ;
            }
        }
        // If zend is currently not running, do a startup of the daemon as a child process
        // It may be started but not ready - then also show dialog
        ZCashInstallationObserver initialInstallationObserver = new ZCashInstallationObserver(OSUtil.getProgramDirectory());
        DaemonInfo zcashdInfo = initialInstallationObserver.getDaemonInfo();
        initialInstallationObserver = null;
        ZCashClientCaller initialClientCaller = new ZCashClientCaller(OSUtil.getProgramDirectory());
        boolean daemonStartInProgress = false;
        try {
            if (zcashdInfo.status == DAEMON_STATUS.RUNNING) {
                NetworkAndBlockchainInfo info = initialClientCaller.getNetworkAndBlockchainInfo();
                // If more than 20 minutes behind in the blockchain - startup in progress
                if ((System.currentTimeMillis() - info.lastBlockDate.getTime()) > (20 * 60 * 1000)) {
                    Log.info("Current blockchain synchronization date is " + new Date(info.lastBlockDate.getTime()));
                    daemonStartInProgress = true;
                }
            }
        } catch (WalletCallException wce) {
            if (// Started but not ready
            (wce.getMessage().indexOf("{\"code\":-28") != -1) || (wce.getMessage().indexOf("error code: -28") != -1)) {
                Log.info("zend is currently starting...");
                daemonStartInProgress = true;
            }
        }
        StartupProgressDialog startupBar = null;
        if ((zcashdInfo.status != DAEMON_STATUS.RUNNING) || (daemonStartInProgress)) {
            Log.info("zend is not runing at the moment or has not started/synchronized 100% - showing splash...");
            startupBar = new StartupProgressDialog(initialClientCaller);
            startupBar.setVisible(true);
            startupBar.waitForStartup();
        }
        initialClientCaller = null;
        // Main GUI is created here
        ZCashUI ui = new ZCashUI(startupBar);
        ui.setVisible(true);
    } catch (InstallationDetectionException ide) {
        Log.error("Unexpected error: ", ide);
        JOptionPane.showMessageDialog(null, LanguageUtil.instance().getString("main.frame.option.pane.installation.error.text", OSUtil.getProgramDirectory(), ide.getMessage()), LanguageUtil.instance().getString("main.frame.option.pane.installation.error.title"), JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    } catch (WalletCallException wce) {
        Log.error("Unexpected error: ", wce);
        if ((wce.getMessage().indexOf("{\"code\":-28,\"message\"") != -1) || (wce.getMessage().indexOf("error code: -28") != -1)) {
            JOptionPane.showMessageDialog(null, LanguageUtil.instance().getString("main.frame.option.pane.wallet.communication.error.text"), LanguageUtil.instance().getString("main.frame.option.pane.wallet.communication.error.title"), JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, LanguageUtil.instance().getString("main.frame.option.pane.wallet.communication.error.2.text", wce.getMessage()), LanguageUtil.instance().getString("main.frame.option.pane.wallet.communication.error.2.title"), JOptionPane.ERROR_MESSAGE);
        }
        System.exit(2);
    } catch (Exception e) {
        Log.error("Unexpected error: ", e);
        JOptionPane.showMessageDialog(null, LanguageUtil.instance().getString("main.frame.option.pane.wallet.critical.error.text", e.getMessage()), LanguageUtil.instance().getString("main.frame.option.pane.wallet.critical.error.title"), JOptionPane.ERROR_MESSAGE);
        System.exit(3);
    } catch (Error err) {
        // Last resort catch for unexpected problems - just to inform the user
        err.printStackTrace();
        JOptionPane.showMessageDialog(null, LanguageUtil.instance().getString("main.frame.option.pane.wallet.critical.error.2.text", err.getMessage()), LanguageUtil.instance().getString("main.frame.option.pane.wallet.critical.error.2.title"), JOptionPane.ERROR_MESSAGE);
        System.exit(4);
    }
}
Also used : LookAndFeelInfo(javax.swing.UIManager.LookAndFeelInfo) OS_TYPE(com.vaklinov.zcashui.OSUtil.OS_TYPE) Date(java.util.Date) InstallationDetectionException(com.vaklinov.zcashui.ZCashInstallationObserver.InstallationDetectionException) IOException(java.io.IOException) WalletCallException(com.vaklinov.zcashui.ZCashClientCaller.WalletCallException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DaemonInfo(com.vaklinov.zcashui.ZCashInstallationObserver.DaemonInfo) NetworkAndBlockchainInfo(com.vaklinov.zcashui.ZCashClientCaller.NetworkAndBlockchainInfo) InstallationDetectionException(com.vaklinov.zcashui.ZCashInstallationObserver.InstallationDetectionException) WalletCallException(com.vaklinov.zcashui.ZCashClientCaller.WalletCallException) File(java.io.File)

Example 2 with OS_TYPE

use of com.vaklinov.zcashui.OSUtil.OS_TYPE in project zencash-swing-wallet-ui by ZencashOfficial.

the class IPFSWrapper method getIPFSExecutableName.

private String getIPFSExecutableName() {
    String ipfs = "ipfs";
    OS_TYPE os = OSUtil.getOSType();
    if (os == OS_TYPE.WINDOWS) {
        ipfs += ".exe";
    }
    return ipfs;
}
Also used : OS_TYPE(com.vaklinov.zcashui.OSUtil.OS_TYPE)

Example 3 with OS_TYPE

use of com.vaklinov.zcashui.OSUtil.OS_TYPE in project zencash-swing-wallet-ui by ZencashOfficial.

the class ZCashClientCaller method getWalletPublicTransactions.

public synchronized String[][] getWalletPublicTransactions() throws WalletCallException, IOException, InterruptedException {
    String notListed = "\u26D4";
    OS_TYPE os = OSUtil.getOSType();
    if (os == OS_TYPE.WINDOWS) {
        notListed = " \u25B6";
    }
    JsonArray jsonTransactions = executeCommandAndGetJsonArray("listtransactions", wrapStringParameter(""), "300");
    String[][] strTransactions = new String[jsonTransactions.size()][];
    for (int i = 0; i < jsonTransactions.size(); i++) {
        strTransactions[i] = new String[7];
        JsonObject trans = jsonTransactions.get(i).asObject();
        // Needs to be the same as in getWalletZReceivedTransactions()
        // TODO: some day refactor to use object containers
        strTransactions[i][0] = "\u2606T (Public)";
        strTransactions[i][1] = trans.getString("category", "ERROR!");
        strTransactions[i][2] = trans.get("confirmations").toString();
        strTransactions[i][3] = trans.get("amount").toString();
        strTransactions[i][4] = trans.get("time").toString();
        strTransactions[i][5] = trans.getString("address", notListed + " (Z Address not listed by wallet!)");
        strTransactions[i][6] = trans.get("txid").toString();
    }
    return strTransactions;
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) OS_TYPE(com.vaklinov.zcashui.OSUtil.OS_TYPE) JsonObject(com.eclipsesource.json.JsonObject)

Example 4 with OS_TYPE

use of com.vaklinov.zcashui.OSUtil.OS_TYPE in project zencash-swing-wallet-ui by ZencashOfficial.

the class ProvingKeyFetcher method verifyOrFetch.

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");
    }
    zCashParams = zCashParams.getCanonicalFile();
    boolean needsFetch = false;
    if (!zCashParams.exists()) {
        needsFetch = true;
        zCashParams.mkdirs();
    }
    // verifying key is small, always copy it
    File verifyingKeyFile = new File(zCashParams, "sprout-verifying.key");
    FileOutputStream fos = new FileOutputStream(verifyingKeyFile);
    InputStream is = ProvingKeyFetcher.class.getClassLoader().getResourceAsStream("keys/sprout-verifying.key");
    copy(is, fos);
    fos.close();
    is = null;
    File provingKeyFile = new File(zCashParams, "sprout-proving.key");
    provingKeyFile = provingKeyFile.getCanonicalFile();
    if (!provingKeyFile.exists()) {
        needsFetch = true;
    } else if (provingKeyFile.length() != PROVING_KEY_SIZE) {
        needsFetch = true;
    }
    if (!needsFetch) {
        return;
    }
    JOptionPane.showMessageDialog(parent, langUtil.getString("proving.key.fetcher.option.pane.verify.message"));
    parent.setProgressText(langUtil.getString("proving.key.fetcher.option.pane.verify.progress.text"));
    provingKeyFile.delete();
    OutputStream os = new BufferedOutputStream(new FileOutputStream(provingKeyFile));
    URL keyURL = new URL(pathURL);
    URLConnection urlc = keyURL.openConnection();
    urlc.setRequestProperty("User-Agent", "Wget/1.17.1 (linux-gnu)");
    try {
        is = urlc.getInputStream();
        ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent, langUtil.getString("proving.key.fetcher.option.pane.verify.progress.monitor.text"), is);
        pmis.getProgressMonitor().setMaximum(PROVING_KEY_SIZE);
        pmis.getProgressMonitor().setMillisToPopup(10);
        copy(pmis, os);
        os.close();
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException ignore) {
        }
    }
    parent.setProgressText(langUtil.getString("proving.key.fetcher.option.pane.verify.key.text"));
    if (!checkSHA256(provingKeyFile, parent)) {
        JOptionPane.showMessageDialog(parent, langUtil.getString("proving.key.fetcher.option.pane.verify.key.failed.text"));
        System.exit(-4);
    }
}
Also used : ProgressMonitorInputStream(javax.swing.ProgressMonitorInputStream) OS_TYPE(com.vaklinov.zcashui.OSUtil.OS_TYPE) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ProgressMonitorInputStream(javax.swing.ProgressMonitorInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 5 with OS_TYPE

use of com.vaklinov.zcashui.OSUtil.OS_TYPE in project zencash-swing-wallet-ui by ZencashOfficial.

the class StartupProgressDialog method waitForStartup.

public void waitForStartup() throws IOException, InterruptedException, WalletCallException, InvocationTargetException {
    // special handling of Windows/Mac OS app launch
    OS_TYPE os = OSUtil.getOSType();
    if ((os == OS_TYPE.WINDOWS) || (os == OS_TYPE.MAC_OS)) {
        ProvingKeyFetcher keyFetcher = new ProvingKeyFetcher();
        keyFetcher.fetchIfMissing(this);
    }
    Log.info("Splash: checking if zend is already running...");
    boolean shouldStartZCashd = false;
    try {
        clientCaller.getDaemonRawRuntimeInfo();
    } catch (IOException e) {
        // may be thrown for an unexpected reason!!! - so message is checked
        if (e.getMessage() != null && e.getMessage().toLowerCase(Locale.ROOT).contains("error: couldn't connect to server")) {
            shouldStartZCashd = true;
        }
    }
    if (!shouldStartZCashd) {
        Log.info("Splash: zend already running...");
    // What if started by hand but taking long to initialize???
    // doDispose();
    // return;
    } else {
        Log.info("Splash: zend will be started...");
    }
    final Process daemonProcess = shouldStartZCashd ? clientCaller.startDaemon() : null;
    // just a little extra
    Thread.sleep(POLL_PERIOD);
    int iteration = 0;
    while (true) {
        iteration++;
        Thread.sleep(POLL_PERIOD);
        JsonObject info = null;
        try {
            info = clientCaller.getDaemonRawRuntimeInfo();
        } catch (IOException e) {
            if (iteration > 4) {
                throw e;
            } else {
                continue;
            }
        }
        JsonValue code = info.get("code");
        if (code == null || (code.asInt() != STARTUP_ERROR_CODE))
            break;
        final String message = info.getString("message", "???");
        setProgressText(message);
    }
    if (// Shutdown only if we started it
    daemonProcess != null)
        Runtime.getRuntime().addShutdownHook(new Thread() {

            public void run() {
                Log.info("Stopping zend because we started it - now it is alive: " + StartupProgressDialog.this.isAlive(daemonProcess));
                try {
                    clientCaller.stopDaemon();
                    long start = System.currentTimeMillis();
                    while (!StartupProgressDialog.this.waitFor(daemonProcess, 3000)) {
                        long end = System.currentTimeMillis();
                        Log.info("Waiting for " + ((end - start) / 1000) + " seconds for zend to exit...");
                        if (end - start > 15 * 1000) {
                            clientCaller.stopDaemon();
                            daemonProcess.destroy();
                        }
                        if (end - start > 1 * 60 * 1000) {
                            break;
                        }
                    }
                    if (StartupProgressDialog.this.isAlive(daemonProcess)) {
                        Log.info("zend is still alive although we tried to stop it. " + "Hopefully it will stop later!");
                    // System.out.println("zend is still alive, killing forcefully");
                    // daemonProcess.destroyForcibly();
                    } else
                        Log.info("zend shut down successfully");
                } catch (Exception bad) {
                    Log.error("Couldn't stop zend!", bad);
                }
            }
        });
}
Also used : OS_TYPE(com.vaklinov.zcashui.OSUtil.OS_TYPE) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) IOException(java.io.IOException) IOException(java.io.IOException) WalletCallException(com.vaklinov.zcashui.ZCashClientCaller.WalletCallException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

OS_TYPE (com.vaklinov.zcashui.OSUtil.OS_TYPE)7 IOException (java.io.IOException)3 JsonObject (com.eclipsesource.json.JsonObject)2 WalletCallException (com.vaklinov.zcashui.ZCashClientCaller.WalletCallException)2 File (java.io.File)2 DecimalFormat (java.text.DecimalFormat)2 Date (java.util.Date)2 JsonArray (com.eclipsesource.json.JsonArray)1 JsonValue (com.eclipsesource.json.JsonValue)1 NetworkAndBlockchainInfo (com.vaklinov.zcashui.ZCashClientCaller.NetworkAndBlockchainInfo)1 DaemonInfo (com.vaklinov.zcashui.ZCashInstallationObserver.DaemonInfo)1 InstallationDetectionException (com.vaklinov.zcashui.ZCashInstallationObserver.InstallationDetectionException)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1