diff --git a/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java b/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java index bb6cd19fe..fcd22453b 100644 --- a/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java +++ b/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java @@ -21,6 +21,7 @@ import com.google.protobuf.ByteString; import com.google.protobuf.CodedInputStream; import com.google.protobuf.CodedOutputStream; import com.google.protobuf.WireFormat; +import org.bitcoinj.base.BitcoinNetwork; import org.bitcoinj.base.Coin; import org.bitcoinj.base.Network; import org.bitcoinj.core.LockTime; @@ -179,7 +180,7 @@ public class WalletProtobufSerializer { */ public Protos.Wallet walletToProto(Wallet wallet) { Protos.Wallet.Builder walletBuilder = Protos.Wallet.newBuilder(); - walletBuilder.setNetworkIdentifier(wallet.getNetworkParameters().getId()); + walletBuilder.setNetworkIdentifier(wallet.network().id()); if (wallet.getDescription() != null) { walletBuilder.setDescription(wallet.getDescription()); } @@ -446,10 +447,9 @@ public class WalletProtobufSerializer { try { Protos.Wallet walletProto = parseToProto(input); final String paramsID = walletProto.getNetworkIdentifier(); - NetworkParameters params = BitcoinNetworkParams.fromID(paramsID); - if (params == null) - throw new UnreadableWalletException("Unknown network parameters ID " + paramsID); - return readWallet(params, extensions, walletProto, forceReset); + Network network = BitcoinNetwork.fromIdString(paramsID).orElseThrow(() -> + new UnreadableWalletException("Unknown network parameters ID " + paramsID)); + return readWallet(network, extensions, walletProto, forceReset); } catch (IOException | IllegalArgumentException | IllegalStateException e) { throw new UnreadableWalletException("Could not parse input stream to protobuf", e); } @@ -466,9 +466,16 @@ public class WalletProtobufSerializer { * * @throws UnreadableWalletException thrown in various error conditions (see description). */ + public Wallet readWallet(Network network, @Nullable WalletExtension[] extensions, + Protos.Wallet walletProto) throws UnreadableWalletException { + return readWallet(network, extensions, walletProto, false); + } + + /** @deprecated use {@link #readWallet(Network, WalletExtension[], Protos.Wallet)} */ + @Deprecated public Wallet readWallet(NetworkParameters params, @Nullable WalletExtension[] extensions, Protos.Wallet walletProto) throws UnreadableWalletException { - return readWallet(params, extensions, walletProto, false); + return readWallet(params.network(), extensions, walletProto); } /** @@ -487,11 +494,11 @@ public class WalletProtobufSerializer { * * @throws UnreadableWalletException thrown in various error conditions (see description). */ - public Wallet readWallet(NetworkParameters params, @Nullable WalletExtension[] extensions, + public Wallet readWallet(Network network, @Nullable WalletExtension[] extensions, Protos.Wallet walletProto, boolean forceReset) throws UnreadableWalletException { if (walletProto.getVersion() > CURRENT_WALLET_VERSION) throw new UnreadableWalletException.FutureVersion(); - if (!walletProto.getNetworkIdentifier().equals(params.getId())) + if (!walletProto.getNetworkIdentifier().equals(network.id())) throw new UnreadableWalletException.WrongNetwork(); // Read the scrypt parameters that specify how encryption and decryption is performed. @@ -499,11 +506,11 @@ public class WalletProtobufSerializer { if (walletProto.hasEncryptionParameters()) { Protos.ScryptParameters encryptionParameters = walletProto.getEncryptionParameters(); final KeyCrypterScrypt keyCrypter = new KeyCrypterScrypt(encryptionParameters); - keyChainGroup = KeyChainGroup.fromProtobufEncrypted(params.network(), walletProto.getKeyList(), keyCrypter, keyChainFactory); + keyChainGroup = KeyChainGroup.fromProtobufEncrypted(network, walletProto.getKeyList(), keyCrypter, keyChainFactory); } else { - keyChainGroup = KeyChainGroup.fromProtobufUnencrypted(params.network(), walletProto.getKeyList(), keyChainFactory); + keyChainGroup = KeyChainGroup.fromProtobufUnencrypted(network, walletProto.getKeyList(), keyChainFactory); } - Wallet wallet = factory.create(params.network(), keyChainGroup); + Wallet wallet = factory.create(network, keyChainGroup); List