/** * Uses protobuf serialization to save the wallet to the given file stream. To learn more about this file format, see * {@link WalletProtobufSerializer}. */ public void saveToFileStream(OutputStream f) throws IOException { lock.lock(); try { new WalletProtobufSerializer().writeWallet(this, f); } finally { lock.unlock(); } }
/** * Uses protobuf serialization to save the wallet to the given file stream. To learn more about this file format, see * {@link WalletProtobufSerializer}. */ public void saveToFileStream(OutputStream f) throws IOException { lock.lock(); try { new WalletProtobufSerializer().writeWallet(this, f); } finally { lock.unlock(); } }
/** * Uses protobuf serialization to save the wallet to the given file stream. To learn more about this file format, see * {@link WalletProtobufSerializer}. */ public void saveToFileStream(OutputStream f) throws IOException { lock.lock(); try { new WalletProtobufSerializer().writeWallet(this, f); } finally { lock.unlock(); } }
/** * Uses protobuf serialization to save the wallet to the given file stream. To learn more about this file format, see * {@link WalletProtobufSerializer}. */ public void saveToFileStream(OutputStream f) throws IOException { lock.lock(); try { new WalletProtobufSerializer().writeWallet(this, f); } finally { lock.unlock(); } }
/** Returns a wallet deserialized from the given input stream and wallet extensions. */ public static Wallet loadFromFileStream(InputStream stream, @Nullable WalletExtension... walletExtensions) throws UnreadableWalletException { Wallet wallet = new WalletProtobufSerializer().readWallet(stream, walletExtensions); if (!wallet.isConsistent()) { log.error("Loaded an inconsistent wallet"); } return wallet; }
/** Returns a wallet deserialized from the given input stream and wallet extensions. */ public static Wallet loadFromFileStream(InputStream stream, @Nullable WalletExtension... walletExtensions) throws UnreadableWalletException { Wallet wallet = new WalletProtobufSerializer().readWallet(stream, walletExtensions); if (!wallet.isConsistent()) { log.error("Loaded an inconsistent wallet"); } return wallet; }
/** Returns a wallet deserialized from the given input stream and wallet extensions. */ public static Wallet loadFromFileStream(InputStream stream, @Nullable WalletExtension... walletExtensions) throws UnreadableWalletException { Wallet wallet = new WalletProtobufSerializer().readWallet(stream, walletExtensions); if (!wallet.isConsistent()) { log.error("Loaded an inconsistent wallet"); } return wallet; }
/** Returns a wallet deserialized from the given input stream and wallet extensions. */ public static Wallet loadFromFileStream(InputStream stream, @Nullable WalletExtension... walletExtensions) throws UnreadableWalletException { Wallet wallet = new WalletProtobufSerializer().readWallet(stream, walletExtensions); if (!wallet.isConsistent()) { log.error("Loaded an inconsistent wallet"); } return wallet; }
private Wallet loadWallet(boolean shouldReplayWallet) throws Exception { Wallet wallet; FileInputStream walletStream = new FileInputStream(vWalletFile); try { List<WalletExtension> extensions = provideWalletExtensions(); WalletExtension[] extArray = extensions.toArray(new WalletExtension[extensions.size()]); Protos.Wallet proto = WalletProtobufSerializer.parseToProto(walletStream); final WalletProtobufSerializer serializer; if (walletFactory != null) serializer = new WalletProtobufSerializer(walletFactory); else serializer = new WalletProtobufSerializer(); wallet = serializer.readWallet(params, extArray, proto); if (shouldReplayWallet) wallet.reset(); } finally { walletStream.close(); } return wallet; }
private Wallet loadWallet(boolean shouldReplayWallet) throws Exception { Wallet wallet; FileInputStream walletStream = new FileInputStream(vWalletFile); try { List<WalletExtension> extensions = provideWalletExtensions(); WalletExtension[] extArray = extensions.toArray(new WalletExtension[extensions.size()]); Protos.Wallet proto = WalletProtobufSerializer.parseToProto(walletStream); final WalletProtobufSerializer serializer; if (walletFactory != null) serializer = new WalletProtobufSerializer(walletFactory); else serializer = new WalletProtobufSerializer(); wallet = serializer.readWallet(params, extArray, proto); if (shouldReplayWallet) wallet.reset(); } finally { walletStream.close(); } return wallet; }
private Wallet loadWallet(boolean shouldReplayWallet) throws Exception { Wallet wallet; FileInputStream walletStream = new FileInputStream(vWalletFile); try { List<WalletExtension> extensions = provideWalletExtensions(); WalletExtension[] extArray = extensions.toArray(new WalletExtension[extensions.size()]); Protos.Wallet proto = WalletProtobufSerializer.parseToProto(walletStream); final WalletProtobufSerializer serializer; if (walletFactory != null) serializer = new WalletProtobufSerializer(walletFactory); else serializer = new WalletProtobufSerializer(); wallet = serializer.readWallet(params, extArray, proto); if (shouldReplayWallet) wallet.reset(); } finally { walletStream.close(); } return wallet; }
private Wallet loadWallet(boolean shouldReplayWallet) throws Exception { Wallet wallet; FileInputStream walletStream = new FileInputStream(vWalletFile); try { List<WalletExtension> extensions = provideWalletExtensions(); WalletExtension[] extArray = extensions.toArray(new WalletExtension[extensions.size()]); Protos.Wallet proto = WalletProtobufSerializer.parseToProto(walletStream); final WalletProtobufSerializer serializer; if (walletFactory != null) serializer = new WalletProtobufSerializer(walletFactory); else serializer = new WalletProtobufSerializer(); wallet = serializer.readWallet(params, extArray, proto); if (shouldReplayWallet) wallet.reset(); } finally { walletStream.close(); } return wallet; }
private Wallet readWallet(File walletFile) throws FileNotFoundException, UnreadableWalletException { InputStream inputStream = new FileInputStream(walletFile); Wallet wallet = new WalletProtobufSerializer().readWallet(inputStream); wallet.autosaveToFile(walletFile, 3 * 1000, TimeUnit.MILLISECONDS, null); wallet.cleanup(); return wallet; }
private Wallet roundTrip(Wallet wallet) throws UnreadableWalletException { Protos.Wallet protos = new WalletProtobufSerializer().walletToProto(wallet); return new WalletProtobufSerializer().readWallet(PARAMS, null, protos); }
private static Wallet roundTrip(Wallet wallet) throws Exception { ByteArrayOutputStream output = new ByteArrayOutputStream(); new WalletProtobufSerializer().writeWallet(wallet, output); ByteArrayInputStream test = new ByteArrayInputStream(output.toByteArray()); assertTrue(WalletProtobufSerializer.isWallet(test)); ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); return new WalletProtobufSerializer().readWallet(input); }
private static Wallet roundTripClientWallet(Wallet wallet) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); new WalletProtobufSerializer().writeWallet(wallet, bos); org.bitcoinj.wallet.Protos.Wallet proto = WalletProtobufSerializer.parseToProto(new ByteArrayInputStream(bos.toByteArray())); StoredPaymentChannelClientStates state = new StoredPaymentChannelClientStates(null, failBroadcaster); return new WalletProtobufSerializer().readWallet(wallet.getParams(), new WalletExtension[] { state }, proto); }
private static Wallet roundTripServerWallet(Wallet wallet) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); new WalletProtobufSerializer().writeWallet(wallet, bos); StoredPaymentChannelServerStates state = new StoredPaymentChannelServerStates(null, failBroadcaster); org.bitcoinj.wallet.Protos.Wallet proto = WalletProtobufSerializer.parseToProto(new ByteArrayInputStream(bos.toByteArray())); return new WalletProtobufSerializer().readWallet(wallet.getParams(), new WalletExtension[] { state }, proto); }
@Test(expected = UnreadableWalletException.FutureVersion.class) public void versions() throws Exception { Protos.Wallet.Builder proto = Protos.Wallet.newBuilder(new WalletProtobufSerializer().walletToProto(myWallet)); proto.setVersion(2); new WalletProtobufSerializer().readWallet(PARAMS, null, proto.build()); } }
@Test public void extensions() throws Exception { myWallet.addExtension(new FooWalletExtension("com.whatever.required", true)); Protos.Wallet proto = new WalletProtobufSerializer().walletToProto(myWallet); // Initial extension is mandatory: try to read it back into a wallet that doesn't know about it. try { new WalletProtobufSerializer().readWallet(PARAMS, null, proto); fail(); } catch (UnreadableWalletException e) { assertTrue(e.getMessage().contains("mandatory")); } Wallet wallet = new WalletProtobufSerializer().readWallet(PARAMS, new WalletExtension[]{ new FooWalletExtension("com.whatever.required", true) }, proto); assertTrue(wallet.getExtensions().containsKey("com.whatever.required")); // Non-mandatory extensions are ignored if the wallet doesn't know how to read them. Wallet wallet2 = new Wallet(PARAMS); wallet2.addExtension(new FooWalletExtension("com.whatever.optional", false)); Protos.Wallet proto2 = new WalletProtobufSerializer().walletToProto(wallet2); Wallet wallet5 = new WalletProtobufSerializer().readWallet(PARAMS, null, proto2); assertEquals(0, wallet5.getExtensions().size()); }
@Test public void testLastBlockSeenHash() throws Exception { // Test the lastBlockSeenHash field works. // LastBlockSeenHash should be empty if never set. Wallet wallet = new Wallet(PARAMS); Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(wallet); ByteString lastSeenBlockHash = walletProto.getLastSeenBlockHash(); assertTrue(lastSeenBlockHash.isEmpty()); // Create a block. Block block = PARAMS.getDefaultSerializer().makeBlock(BlockTest.blockBytes); Sha256Hash blockHash = block.getHash(); wallet.setLastBlockSeenHash(blockHash); wallet.setLastBlockSeenHeight(1); // Roundtrip the wallet and check it has stored the blockHash. Wallet wallet1 = roundTrip(wallet); assertEquals(blockHash, wallet1.getLastBlockSeenHash()); assertEquals(1, wallet1.getLastBlockSeenHeight()); // Test the Satoshi genesis block (hash of all zeroes) is roundtripped ok. Block genesisBlock = MainNetParams.get().getGenesisBlock(); wallet.setLastBlockSeenHash(genesisBlock.getHash()); Wallet wallet2 = roundTrip(wallet); assertEquals(genesisBlock.getHash(), wallet2.getLastBlockSeenHash()); }