diff --git a/core/src/main/java/org/bitcoinj/core/Wallet.java b/core/src/main/java/org/bitcoinj/core/Wallet.java index 76b5e7bf..e4d98044 100644 --- a/core/src/main/java/org/bitcoinj/core/Wallet.java +++ b/core/src/main/java/org/bitcoinj/core/Wallet.java @@ -1340,14 +1340,18 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha } /** - * Returns a wallet deserialized from the given file. + *

Returns a wallet deserialized from the given file. Extensions previously saved with the wallet can be + * deserialized by calling @{@link WalletExtension#deserializeWalletExtension(Wallet, byte[])}}

+ * + * @param file the wallet file to read + * @param walletExtensions extensions possibly added to the wallet. */ - public static Wallet loadFromFile(File f) throws UnreadableWalletException { + public static Wallet loadFromFile(File file, @Nullable WalletExtension... walletExtensions) throws UnreadableWalletException { try { FileInputStream stream = null; try { - stream = new FileInputStream(f); - return loadFromFileStream(stream); + stream = new FileInputStream(file); + return loadFromFileStream(stream, walletExtensions); } finally { if (stream != null) stream.close(); } @@ -1355,7 +1359,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha throw new UnreadableWalletException("Could not open file", e); } } - + public boolean isConsistent() { lock.lock(); try { @@ -1407,11 +1411,9 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha } } - /** - * Returns a wallet deserialized from the given input stream. - */ - public static Wallet loadFromFileStream(InputStream stream) throws UnreadableWalletException { - Wallet wallet = new WalletProtobufSerializer().readWallet(stream); + /** 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"); } diff --git a/core/src/main/java/org/bitcoinj/store/WalletProtobufSerializer.java b/core/src/main/java/org/bitcoinj/store/WalletProtobufSerializer.java index c9f8c85a..54c7ed1b 100644 --- a/core/src/main/java/org/bitcoinj/store/WalletProtobufSerializer.java +++ b/core/src/main/java/org/bitcoinj/store/WalletProtobufSerializer.java @@ -367,9 +367,9 @@ public class WalletProtobufSerializer { } /** - *

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.

+ *

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.

* *

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 @@ -377,14 +377,14 @@ public class WalletProtobufSerializer { * * @throws UnreadableWalletException thrown in various error conditions (see description). */ - public Wallet readWallet(InputStream input) throws UnreadableWalletException { + public Wallet readWallet(InputStream input, @Nullable WalletExtension... walletExtensions) throws UnreadableWalletException { try { 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, null, walletProto); + return readWallet(params, walletExtensions, walletProto); } catch (IOException e) { throw new UnreadableWalletException("Could not parse input stream to protobuf", e); } catch (IllegalStateException e) {