From a1703725626bd8c461ddba42a97ccd81e4a8cbfc Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 6 Apr 2012 14:58:36 +0200 Subject: [PATCH] Add a static method to go from string ID to NetworkParameters and use it to simplify the WalletProtobufSerializer API. --- .../bitcoin/core/NetworkParameters.java | 11 ++++++++ .../store/WalletProtobufSerializer.java | 25 +++++++------------ .../store/WalletProtobufSerializerTest.java | 2 +- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java b/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java index 85c389e80..c0119672b 100644 --- a/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java +++ b/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java @@ -215,4 +215,15 @@ public class NetworkParameters implements Serializable { } return id; } + + /** Returns the network parameters for the given string ID or NULL if not recognized. */ + public static NetworkParameters fromID(String id) { + if (id.equals(ID_PRODNET)) { + return prodNet(); + } else if (id.equals(ID_TESTNET)) { + return testNet(); + } else { + return null; + } + } } diff --git a/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java b/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java index 96c7f8752..41800fa22 100644 --- a/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java +++ b/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java @@ -36,7 +36,7 @@ import java.util.Map; * Serialize and de-serialize a wallet to a byte stream containing a * protocol buffer. Protocol buffers are * a data interchange format developed by Google with an efficient binary representation, a type safe specification - * languaeg and compilers that generate code to work with those data structures for many languages. Protocol buffers + * language and compilers that generate code to work with those data structures for many languages. Protocol buffers * can have their format evolved over time: conceptually they represent data using (tag, length, value) tuples. The * format is defined by the bitcoin.proto file in the BitCoinJ source distribution.

* @@ -73,9 +73,10 @@ public class WalletProtobufSerializer { /** * 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()}, 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 Transaction}s which also mostly contain keys and hashes. + * 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 Transaction}s which also + * mostly contain keys and hashes. */ public static String walletToText(Wallet wallet) { Protos.Wallet walletProto = walletToProto(wallet); @@ -199,26 +200,18 @@ public class WalletProtobufSerializer { /** * Parses a wallet from the given stream. The stream is expected to contain a binary serialization of a - * {@link Protos.Wallet} object. You must also specify the {@link NetworkParameters} the wallet will use, - * it will be checked against the stream to ensure the right params have been specified. In future this - * parameter will probably go away.

+ * {@link Protos.Wallet} object.

* * If the stream is invalid or the serialized wallet contains unsupported features, * {@link IllegalArgumentException} is thrown. * - * @param input - * @param params - * @return - * @throws IOException */ - public static Wallet readWallet(InputStream input, NetworkParameters params) throws IOException { + public static Wallet readWallet(InputStream input) throws IOException { // TODO: This method should throw more specific exception types than IllegalArgumentException. WalletProtobufSerializer serializer = new WalletProtobufSerializer(); Protos.Wallet walletProto = Protos.Wallet.parseFrom(input); - if (!params.getId().equals(walletProto.getNetworkIdentifier())) - throw new IllegalArgumentException("Trying to read a wallet with a different network id " + - walletProto.getNetworkIdentifier()); - + + NetworkParameters params = NetworkParameters.fromID(walletProto.getNetworkIdentifier()); Wallet wallet = new Wallet(params); // Read all keys diff --git a/core/src/test/java/com/google/bitcoin/store/WalletProtobufSerializerTest.java b/core/src/test/java/com/google/bitcoin/store/WalletProtobufSerializerTest.java index 58403cfe0..8e0225cac 100644 --- a/core/src/test/java/com/google/bitcoin/store/WalletProtobufSerializerTest.java +++ b/core/src/test/java/com/google/bitcoin/store/WalletProtobufSerializerTest.java @@ -110,6 +110,6 @@ public class WalletProtobufSerializerTest { //System.out.println(WalletProtobufSerializer.walletToText(wallet)); WalletProtobufSerializer.writeWallet(wallet, output); ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); - return WalletProtobufSerializer.readWallet(input, params); + return WalletProtobufSerializer.readWallet(input); } }