/** * Returns a wallet deserialized from the given input stream. */ public static Wallet loadFromFileStream(InputStream stream) throws UnreadableWalletException { return WalletProtobufSerializer.readWallet(stream); }
/** * <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); } }
@VisibleForTesting Protos.Wallet toProtobuf() { lock.lock(); try { return WalletProtobufSerializer.toProtobuf(this); } finally { lock.unlock(); } } /**
private static void applyWalletUpdates(Wallet wallet) { if (wallet.getVersion() < 2) { updateV1toV2(wallet); } if (wallet.getVersion() < 3) { updateV2toV3(wallet); } }
throw new UnreadableWalletException.FutureVersion(); walletProto = applyProtoUpdates(walletProto); final KeyCrypter crypter = getKeyCrypter(walletProto); NxtFamilyWalletProtobufSerializer nxtPocketSerializer = new NxtFamilyWalletProtobufSerializer(); for (Protos.WalletPocket pocketProto : walletProto.getPocketsList()) { CoinType type = getType(pocketProto); AbstractWallet pocket; applyWalletUpdates(wallet);
walletBuilder.setMasterKey(getMasterKeyProto(wallet));
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 */ } } } } }
/** * Formats the given wallet (transactions and keys) to the given output stream in protocol buffer format.<p> * * Equivalent to <tt>walletToProto(wallet).writeTo(output);</tt> */ public static void writeWallet(Wallet wallet, OutputStream output) throws IOException { Protos.Wallet walletProto = toProtobuf(wallet); walletProto.writeTo(output); }
@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()); }
/** * Returns the given wallet formatted as text. The text format is that used by protocol buffers and although it * can also be parsed using {@link TextFormat#merge(CharSequence, com.google.protobuf.Message.Builder)}, * it is designed more for debugging than storage. It is not well specified and wallets are largely binary data * structures anyway, consisting as they do of keys (large random numbers) and * {@link org.bitcoinj.core.Transaction}s which also mostly contain keys and hashes. */ public static String walletToText(Wallet wallet) { Protos.Wallet walletProto = toProtobuf(wallet); return TextFormat.printToString(walletProto); }
@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()); }