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 roundTrip(Wallet wallet) throws UnreadableWalletException { Protos.Wallet protos = new WalletProtobufSerializer().walletToProto(wallet); return new WalletProtobufSerializer().readWallet(PARAMS, null, protos); }
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); }
/** * 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; }
readTransaction(txProto, wallet.getParams()); WalletTransaction wtx = connectTransactionOutputs(params, txProto); wallet.addWalletTransaction(wtx); wallet.setLastBlockSeenHash(null); } else { wallet.setLastBlockSeenHash(byteStringToHash(walletProto.getLastSeenBlockHash())); loadExtensions(wallet, extensions != null ? extensions : new WalletExtension[0], walletProto);
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); }
Protos.Wallet walletProto = parseToProto(input); final String paramsID = walletProto.getNetworkIdentifier(); NetworkParameters params = NetworkParameters.fromID(paramsID); if (params == null) throw new UnreadableWalletException("Unknown network parameters ID " + paramsID); return readWallet(params, extensions, walletProto, forceReset); } catch (IOException e) { throw new UnreadableWalletException("Could not parse input stream to protobuf", e);
Protos.Transaction txProto = makeTxProto(wtx); walletBuilder.addTransaction(txProto); walletBuilder.setLastSeenBlockHash(hashToByteString(lastSeenBlockHash)); walletBuilder.setLastSeenBlockHeight(wallet.getLastBlockSeenHeight()); populateExtensions(wallet, walletBuilder);
Protos.Transaction.Builder txBuilder = Protos.Transaction.newBuilder(); txBuilder.setPool(getProtoPool(wtx)) .setHash(hashToByteString(tx.getHash())) .setVersion((int) tx.getVersion()); Protos.TransactionInput.Builder inputBuilder = Protos.TransactionInput.newBuilder() .setScriptBytes(ByteString.copyFrom(input.getScriptBytes())) .setTransactionOutPointHash(hashToByteString(input.getOutpoint().getHash())) .setTransactionOutPointIndex((int) input.getOutpoint().getIndex()); if (input.hasSequence()) Sha256Hash spendingHash = spentBy.getParentTransaction().getHash(); int spentByTransactionIndex = spentBy.getParentTransaction().getInputs().indexOf(spentBy); outputBuilder.setSpentByTransactionHash(hashToByteString(spendingHash)) .setSpentByTransactionIndex(spentByTransactionIndex); if (appearsInHashes != null) { for (Map.Entry<Sha256Hash, Integer> entry : appearsInHashes.entrySet()) { txBuilder.addBlockHash(hashToByteString(entry.getKey())); txBuilder.addBlockRelativityOffsets(entry.getValue()); TransactionConfidence confidence = tx.getConfidence(); Protos.TransactionConfidence.Builder confidenceBuilder = Protos.TransactionConfidence.newBuilder(); writeConfidence(txBuilder, confidence, confidenceBuilder);
Protos.Wallet proto = WalletProtobufSerializer.parseToProto(stream); proto = attemptHexConversion(proto); System.out.println(proto.toString()); || (action == ActionEnum.SYNC && options.has("force")); WalletProtobufSerializer loader = new WalletProtobufSerializer(); if (options.has("ignore-mandatory-extensions")) loader.setRequireMandatoryExtensions(false); walletInputStream = new BufferedInputStream(new FileInputStream(walletFile)); wallet = loader.readWallet(walletInputStream, forceReset, (WalletExtension[])(null)); if (!wallet.getParams().equals(params)) { System.err.println("Wallet does not match requested network parameters: " +
/** * <p>Loads wallet data from the given protocol buffer and inserts it into the given Wallet object. This is primarily * useful when you wish to pre-register extension objects. Note that if loading fails the provided Wallet object * may be in an indeterminate state and should be thrown away.</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 Wallet readWallet(InputStream input, @Nullable WalletExtension... walletExtensions) throws UnreadableWalletException { return readWallet(input, false, walletExtensions); }
@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()); }
if (spendingTx == null) { throw new UnreadableWalletException(String.format(Locale.US, "Could not connect %s to %s", tx.getHashAsString(), byteStringToHash(spentByTransactionHash))); Protos.TransactionConfidence confidenceProto = txProto.getConfidence(); TransactionConfidence confidence = tx.getConfidence(); readConfidence(params, tx, confidenceProto, confidence);
/** * 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 void writeWallet(Wallet wallet, OutputStream output) throws IOException { Protos.Wallet walletProto = walletToProto(wallet); walletProto.writeTo(output); }
confidenceBuilder.setOverridingTransaction(hashToByteString(overridingHash));
TransactionOutPoint outpoint = new TransactionOutPoint(params, inputProto.getTransactionOutPointIndex() & 0xFFFFFFFFL, byteStringToHash(inputProto.getTransactionOutPointHash()) ); Coin value = inputProto.hasValue() ? Coin.valueOf(inputProto.getValue()) : null; if (txProto.getBlockRelativityOffsetsCount() > 0) relativityOffset = txProto.getBlockRelativityOffsets(i); tx.addBlockAppearance(byteStringToHash(blockHash), relativityOffset); Sha256Hash protoHash = byteStringToHash(txProto.getHash()); if (!tx.getHash().equals(protoHash)) throw new UnreadableWalletException(String.format(Locale.US, "Transaction did not deserialize completely: %s vs %s", tx.getHash(), protoHash)); if (txMap.containsKey(txProto.getHash())) throw new UnreadableWalletException("Wallet contained duplicate transaction " + byteStringToHash(txProto.getHash())); txMap.put(txProto.getHash(), tx);
readTransaction(txProto, wallet.getParams()); WalletTransaction wtx = connectTransactionOutputs(params, txProto); wallet.addWalletTransaction(wtx); wallet.setLastBlockSeenHash(null); } else { wallet.setLastBlockSeenHash(byteStringToHash(walletProto.getLastSeenBlockHash())); loadExtensions(wallet, extensions != null ? extensions : new WalletExtension[0], walletProto);
/** 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; }
Protos.Wallet walletProto = parseToProto(input); final String paramsID = walletProto.getNetworkIdentifier(); NetworkParameters params = NetworkParameters.fromID(paramsID); if (params == null) throw new UnreadableWalletException("Unknown network parameters ID " + paramsID); return readWallet(params, extensions, walletProto, forceReset); } catch (IOException e) { throw new UnreadableWalletException("Could not parse input stream to protobuf", e);