3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-15 03:35:52 +00:00

Add a static method to go from string ID to NetworkParameters and use it to simplify the WalletProtobufSerializer API.

This commit is contained in:
Mike Hearn 2012-04-06 14:58:36 +02:00
parent ece8f548a9
commit a170372562
3 changed files with 21 additions and 17 deletions

View File

@ -215,4 +215,15 @@ public class NetworkParameters implements Serializable {
} }
return id; 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;
}
}
} }

View File

@ -36,7 +36,7 @@ import java.util.Map;
* Serialize and de-serialize a wallet to a byte stream containing a * Serialize and de-serialize a wallet to a byte stream containing a
* <a href="http://code.google.com/apis/protocolbuffers/docs/overview.html">protocol buffer</a>. Protocol buffers are * <a href="http://code.google.com/apis/protocolbuffers/docs/overview.html">protocol buffer</a>. Protocol buffers are
* a data interchange format developed by Google with an efficient binary representation, a type safe specification * 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 * can have their format evolved over time: conceptually they represent data using (tag, length, value) tuples. The
* format is defined by the <tt>bitcoin.proto</tt> file in the BitCoinJ source distribution.<p> * format is defined by the <tt>bitcoin.proto</tt> file in the BitCoinJ source distribution.<p>
* *
@ -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 * 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 * can also be parsed using {@link TextFormat#merge(CharSequence, com.google.protobuf.Message.Builder)},
* well specified and wallets are largely binary data structures anyway, consisting as they do of keys (large * it is designed more for debugging than storage. It is not well specified and wallets are largely binary data
* random numbers) and {@link Transaction}s which also mostly contain keys and hashes. * 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) { public static String walletToText(Wallet wallet) {
Protos.Wallet walletProto = walletToProto(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 * 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, * {@link Protos.Wallet} object.<p>
* it will be checked against the stream to ensure the right params have been specified. In future this
* parameter will probably go away.<p>
* *
* If the stream is invalid or the serialized wallet contains unsupported features, * If the stream is invalid or the serialized wallet contains unsupported features,
* {@link IllegalArgumentException} is thrown. * {@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. // TODO: This method should throw more specific exception types than IllegalArgumentException.
WalletProtobufSerializer serializer = new WalletProtobufSerializer(); WalletProtobufSerializer serializer = new WalletProtobufSerializer();
Protos.Wallet walletProto = Protos.Wallet.parseFrom(input); 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 " + NetworkParameters params = NetworkParameters.fromID(walletProto.getNetworkIdentifier());
walletProto.getNetworkIdentifier());
Wallet wallet = new Wallet(params); Wallet wallet = new Wallet(params);
// Read all keys // Read all keys

View File

@ -110,6 +110,6 @@ public class WalletProtobufSerializerTest {
//System.out.println(WalletProtobufSerializer.walletToText(wallet)); //System.out.println(WalletProtobufSerializer.walletToText(wallet));
WalletProtobufSerializer.writeWallet(wallet, output); WalletProtobufSerializer.writeWallet(wallet, output);
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
return WalletProtobufSerializer.readWallet(input, params); return WalletProtobufSerializer.readWallet(input);
} }
} }