3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-30 14:52:16 +00:00

Wallet.loadFromFile takes WalletExtensions

This commit is contained in:
Carlos Lopez 2014-11-29 00:41:04 -06:00 committed by Mike Hearn
parent 96a82800fd
commit a2ac847ee9
2 changed files with 17 additions and 15 deletions

View File

@ -1340,14 +1340,18 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
} }
/** /**
* Returns a wallet deserialized from the given file. * <p>Returns a wallet deserialized from the given file. Extensions previously saved with the wallet can be
* deserialized by calling @{@link WalletExtension#deserializeWalletExtension(Wallet, byte[])}}</p>
*
* @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 { try {
FileInputStream stream = null; FileInputStream stream = null;
try { try {
stream = new FileInputStream(f); stream = new FileInputStream(file);
return loadFromFileStream(stream); return loadFromFileStream(stream, walletExtensions);
} finally { } finally {
if (stream != null) stream.close(); 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); throw new UnreadableWalletException("Could not open file", e);
} }
} }
public boolean isConsistent() { public boolean isConsistent() {
lock.lock(); lock.lock();
try { try {
@ -1407,11 +1411,9 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
} }
} }
/** /** Returns a wallet deserialized from the given input stream and wallet extensions. */
* Returns a wallet deserialized from the given input stream. public static Wallet loadFromFileStream(InputStream stream, @Nullable WalletExtension... walletExtensions) throws UnreadableWalletException {
*/ Wallet wallet = new WalletProtobufSerializer().readWallet(stream, walletExtensions);
public static Wallet loadFromFileStream(InputStream stream) throws UnreadableWalletException {
Wallet wallet = new WalletProtobufSerializer().readWallet(stream);
if (!wallet.isConsistent()) { if (!wallet.isConsistent()) {
log.error("Loaded an inconsistent wallet"); log.error("Loaded an inconsistent wallet");
} }

View File

@ -367,9 +367,9 @@ public class WalletProtobufSerializer {
} }
/** /**
* <p>Parses a wallet from the given stream, using the provided Wallet instance to load data into. This is primarily * <p>Loads wallet data from the given protocol buffer and inserts it into the given Wallet object. This is primarily
* used when you want to register extensions. Data in the proto will be added into the wallet where applicable and * useful when you wish to pre-register extension objects. Note that if loading fails the provided Wallet object
* overwrite where not.</p> * 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 * <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 * 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). * @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 { try {
Protos.Wallet walletProto = parseToProto(input); Protos.Wallet walletProto = parseToProto(input);
final String paramsID = walletProto.getNetworkIdentifier(); final String paramsID = walletProto.getNetworkIdentifier();
NetworkParameters params = NetworkParameters.fromID(paramsID); NetworkParameters params = NetworkParameters.fromID(paramsID);
if (params == null) if (params == null)
throw new UnreadableWalletException("Unknown network parameters ID " + paramsID); throw new UnreadableWalletException("Unknown network parameters ID " + paramsID);
return readWallet(params, null, walletProto); return readWallet(params, walletExtensions, walletProto);
} catch (IOException e) { } catch (IOException e) {
throw new UnreadableWalletException("Could not parse input stream to protobuf", e); throw new UnreadableWalletException("Could not parse input stream to protobuf", e);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {