/** * Returns a wallet deserialized from the given input stream. */ public static Wallet loadFromFileStream(InputStream stream) throws UnreadableWalletException { return WalletProtobufSerializer.readWallet(stream); }
private void loadWallet() { if (walletFile.exists()) { final long start = System.currentTimeMillis(); FileInputStream walletStream = null; try { walletStream = new FileInputStream(walletFile); setWallet(WalletProtobufSerializer.readWallet(walletStream)); log.info("wallet loaded from: '" + walletFile + "', took " + (System.currentTimeMillis() - start) + "ms"); } catch (final FileNotFoundException e) { ACRA.getErrorReporter().handleException(e); Toast.makeText(WalletApplication.this, R.string.error_could_not_read_wallet, Toast.LENGTH_LONG).show(); } catch (final UnreadableWalletException e) { Toast.makeText(WalletApplication.this, R.string.error_could_not_read_wallet, Toast.LENGTH_LONG).show(); ACRA.getErrorReporter().handleException(e); } finally { if (walletStream != null) { try { walletStream.close(); } catch (final IOException x) { /* ignore */ } } } } }
/** * <p>Parses a wallet from the given stream, using the provided Wallet instance to load data into. This is primarily * used when you want to register extensions. Data in the proto will be added into the wallet where applicable and * overwrite where not.</p> * * <p>A wallet can be unreadable for various reasons, such as inability to open the file, corrupt data, internally * inconsistent data, a wallet extension marked as mandatory that cannot be handled and so on. You should always * handle {@link UnreadableWalletException} and communicate failure to the user in an appropriate manner.</p> * * @throws UnreadableWalletException thrown in various error conditions (see description). */ public static Wallet readWallet(InputStream input) throws UnreadableWalletException { try { Protos.Wallet walletProto = parseToProto(input); return readWallet(walletProto); } catch (IOException e) { throw new UnreadableWalletException("Could not parse input stream to protobuf", e); } }
@Test public void serializeUnencryptedNormal() throws Exception { wallet.maybeInitializeAllPockets(); Protos.Wallet walletProto = wallet.toProtobuf(); Wallet newWallet = WalletProtobufSerializer.readWallet(walletProto); assertEquals(walletProto.toString(), newWallet.toProtobuf().toString()); assertArrayEquals(MNEMONIC.toArray(), newWallet.getMnemonicCode().toArray()); }
@Test public void serializeEncryptedNormal() throws Exception { wallet.maybeInitializeAllPockets(); wallet.encrypt(crypter, aesKey); assertNull(wallet.getSeed().getMnemonicCode()); Protos.Wallet walletProto = wallet.toProtobuf(); Wallet newWallet = WalletProtobufSerializer.readWallet(walletProto); assertEquals(walletProto.toString(), newWallet.toProtobuf().toString()); wallet.decrypt(aesKey); // One is encrypted, so they should not match assertNotEquals(wallet.toProtobuf().toString(), newWallet.toProtobuf().toString()); newWallet.decrypt(aesKey); assertEquals(wallet.toProtobuf().toString(), newWallet.toProtobuf().toString()); assertArrayEquals(MNEMONIC.toArray(), newWallet.getMnemonicCode().toArray()); }