private Wallet restoreWalletFromBackup() { InputStream is = null; try { is = openFileInput(Constants.Files.WALLET_KEY_BACKUP_PROTOBUF); final Wallet wallet = new WalletProtobufSerializer().readWallet(is); if (!wallet.isConsistent()) throw new Error("inconsistent backup"); resetBlockchain(); Toast.makeText(this, R.string.toast_wallet_reset, Toast.LENGTH_LONG).show(); log.info("wallet restored from backup: '" + Constants.Files.WALLET_KEY_BACKUP_PROTOBUF + "'"); return wallet; } catch (final IOException x) { throw new Error("cannot read backup", x); } catch (final UnreadableWalletException x) { throw new Error("cannot read backup", x); } finally { try { is.close(); } catch (final IOException x) { // swallow } } }
private void loadWalletFromProtobuf() { if (walletFile.exists()) { final long start = System.currentTimeMillis(); FileInputStream walletStream = null; try { walletStream = new FileInputStream(walletFile); wallet = new WalletProtobufSerializer().readWallet(walletStream); if (!wallet.getParams().equals(Constants.NETWORK_PARAMETERS)) throw new UnreadableWalletException( "bad wallet network parameters: " + wallet.getParams().getId()); log.info( "wallet loaded from: '" + walletFile + "', took " + (System.currentTimeMillis() - start) + "ms"); } catch (final FileNotFoundException x) { log.error("problem loading wallet", x); Toast.makeText(WalletApplication.this, x.getClass().getName(), Toast.LENGTH_LONG).show(); wallet = restoreWalletFromBackup(); } catch (final UnreadableWalletException x) { log.error("problem loading wallet", x); Toast.makeText(WalletApplication.this, x.getClass().getName(), Toast.LENGTH_LONG).show(); wallet = restoreWalletFromBackup(); } finally { if (walletStream != null) { try { walletStream.close(); } catch (final IOException x) { // swallow } } } if (!wallet.isConsistent()) { Toast.makeText(this, "inconsistent wallet: " + walletFile, Toast.LENGTH_LONG).show(); wallet = restoreWalletFromBackup(); } if (!wallet.getParams().equals(Constants.NETWORK_PARAMETERS)) throw new Error("bad wallet network parameters: " + wallet.getParams().getId()); } else { wallet = new Wallet(Constants.NETWORK_PARAMETERS); backupWallet(); config.armBackupReminder(); log.info("new wallet created"); } }