WalletProtobufSerializer: migrate to Network from NetworkParameters

This commit is contained in:
Andreas Schildbach 2023-04-22 16:32:35 +02:00
parent 4187f2328d
commit 9a747e6e96
3 changed files with 31 additions and 17 deletions

View File

@ -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<Script> scripts = new ArrayList<>();
for (Protos.Script protoScript : walletProto.getWatchedScriptList()) {
@ -579,6 +586,13 @@ public class WalletProtobufSerializer {
return wallet;
}
/** @deprecated use {@link #readWallet(Network, WalletExtension[], Protos.Wallet, boolean)} */
@Deprecated
public Wallet readWallet(NetworkParameters params, @Nullable WalletExtension[] extensions,
Protos.Wallet walletProto, boolean forceReset) throws UnreadableWalletException {
return readWallet(params.network(), extensions, walletProto, forceReset);
}
private void loadExtensions(Wallet wallet, WalletExtension[] extensionsList, Protos.Wallet walletProto) throws UnreadableWalletException {
final Map<String, WalletExtension> extensions = new HashMap<>();
for (WalletExtension e : extensionsList)

View File

@ -404,12 +404,12 @@ public class WalletProtobufSerializerTest {
Protos.Wallet proto = new WalletProtobufSerializer().walletToProto(myWallet);
// Initial extension is mandatory: try to read it back into a wallet that doesn't know about it.
try {
new WalletProtobufSerializer().readWallet(TESTNET, null, proto);
new WalletProtobufSerializer().readWallet(BitcoinNetwork.TESTNET, null, proto);
fail();
} catch (UnreadableWalletException e) {
assertTrue(e.getMessage().contains("mandatory"));
}
Wallet wallet = new WalletProtobufSerializer().readWallet(TESTNET,
Wallet wallet = new WalletProtobufSerializer().readWallet(BitcoinNetwork.TESTNET,
new WalletExtension[]{ new FooWalletExtension("com.whatever.required", true) },
proto);
assertTrue(wallet.getExtensions().containsKey("com.whatever.required"));
@ -418,7 +418,7 @@ public class WalletProtobufSerializerTest {
Wallet wallet2 = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
wallet2.addExtension(new FooWalletExtension("com.whatever.optional", false));
Protos.Wallet proto2 = new WalletProtobufSerializer().walletToProto(wallet2);
Wallet wallet5 = new WalletProtobufSerializer().readWallet(TESTNET, null, proto2);
Wallet wallet5 = new WalletProtobufSerializer().readWallet(BitcoinNetwork.TESTNET, null, proto2);
assertEquals(0, wallet5.getExtensions().size());
}
@ -447,7 +447,7 @@ public class WalletProtobufSerializerTest {
};
myWallet.addExtension(extension);
Protos.Wallet proto = new WalletProtobufSerializer().walletToProto(myWallet);
Wallet wallet = new WalletProtobufSerializer().readWallet(TESTNET, new WalletExtension[]{extension}, proto);
Wallet wallet = new WalletProtobufSerializer().readWallet(BitcoinNetwork.TESTNET, new WalletExtension[]{extension}, proto);
assertEquals(0, wallet.getExtensions().size());
}
@ -455,7 +455,7 @@ public class WalletProtobufSerializerTest {
public void versions() throws Exception {
Protos.Wallet.Builder proto = Protos.Wallet.newBuilder(new WalletProtobufSerializer().walletToProto(myWallet));
proto.setVersion(2);
new WalletProtobufSerializer().readWallet(TESTNET, null, proto.build());
new WalletProtobufSerializer().readWallet(BitcoinNetwork.TESTNET, null, proto.build());
}
@Test

View File

@ -2927,7 +2927,7 @@ public class WalletTest extends TestWithWallet {
ScriptType outputScriptType = activeKeyChain.getOutputScriptType();
Protos.Wallet protos = new WalletProtobufSerializer().walletToProto(wallet);
Wallet roundTrippedWallet = new WalletProtobufSerializer().readWallet(TESTNET, null, protos);
Wallet roundTrippedWallet = new WalletProtobufSerializer().readWallet(BitcoinNetwork.TESTNET, null, protos);
assertEquals(numActiveKeyChains, roundTrippedWallet.getActiveKeyChains().size());
DeterministicKeyChain roundTrippedActiveKeyChain = roundTrippedWallet.getActiveKeyChain();