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.CodedInputStream;
import com.google.protobuf.CodedOutputStream; import com.google.protobuf.CodedOutputStream;
import com.google.protobuf.WireFormat; import com.google.protobuf.WireFormat;
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.Coin; import org.bitcoinj.base.Coin;
import org.bitcoinj.base.Network; import org.bitcoinj.base.Network;
import org.bitcoinj.core.LockTime; import org.bitcoinj.core.LockTime;
@ -179,7 +180,7 @@ public class WalletProtobufSerializer {
*/ */
public Protos.Wallet walletToProto(Wallet wallet) { public Protos.Wallet walletToProto(Wallet wallet) {
Protos.Wallet.Builder walletBuilder = Protos.Wallet.newBuilder(); Protos.Wallet.Builder walletBuilder = Protos.Wallet.newBuilder();
walletBuilder.setNetworkIdentifier(wallet.getNetworkParameters().getId()); walletBuilder.setNetworkIdentifier(wallet.network().id());
if (wallet.getDescription() != null) { if (wallet.getDescription() != null) {
walletBuilder.setDescription(wallet.getDescription()); walletBuilder.setDescription(wallet.getDescription());
} }
@ -446,10 +447,9 @@ public class WalletProtobufSerializer {
try { try {
Protos.Wallet walletProto = parseToProto(input); Protos.Wallet walletProto = parseToProto(input);
final String paramsID = walletProto.getNetworkIdentifier(); final String paramsID = walletProto.getNetworkIdentifier();
NetworkParameters params = BitcoinNetworkParams.fromID(paramsID); Network network = BitcoinNetwork.fromIdString(paramsID).orElseThrow(() ->
if (params == null) new UnreadableWalletException("Unknown network parameters ID " + paramsID));
throw new UnreadableWalletException("Unknown network parameters ID " + paramsID); return readWallet(network, extensions, walletProto, forceReset);
return readWallet(params, extensions, walletProto, forceReset);
} catch (IOException | IllegalArgumentException | IllegalStateException e) { } catch (IOException | IllegalArgumentException | IllegalStateException e) {
throw new UnreadableWalletException("Could not parse input stream to protobuf", 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). * @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, public Wallet readWallet(NetworkParameters params, @Nullable WalletExtension[] extensions,
Protos.Wallet walletProto) throws UnreadableWalletException { 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). * @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 { Protos.Wallet walletProto, boolean forceReset) throws UnreadableWalletException {
if (walletProto.getVersion() > CURRENT_WALLET_VERSION) if (walletProto.getVersion() > CURRENT_WALLET_VERSION)
throw new UnreadableWalletException.FutureVersion(); throw new UnreadableWalletException.FutureVersion();
if (!walletProto.getNetworkIdentifier().equals(params.getId())) if (!walletProto.getNetworkIdentifier().equals(network.id()))
throw new UnreadableWalletException.WrongNetwork(); throw new UnreadableWalletException.WrongNetwork();
// Read the scrypt parameters that specify how encryption and decryption is performed. // Read the scrypt parameters that specify how encryption and decryption is performed.
@ -499,11 +506,11 @@ public class WalletProtobufSerializer {
if (walletProto.hasEncryptionParameters()) { if (walletProto.hasEncryptionParameters()) {
Protos.ScryptParameters encryptionParameters = walletProto.getEncryptionParameters(); Protos.ScryptParameters encryptionParameters = walletProto.getEncryptionParameters();
final KeyCrypterScrypt keyCrypter = new KeyCrypterScrypt(encryptionParameters); final KeyCrypterScrypt keyCrypter = new KeyCrypterScrypt(encryptionParameters);
keyChainGroup = KeyChainGroup.fromProtobufEncrypted(params.network(), walletProto.getKeyList(), keyCrypter, keyChainFactory); keyChainGroup = KeyChainGroup.fromProtobufEncrypted(network, walletProto.getKeyList(), keyCrypter, keyChainFactory);
} else { } 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<>(); List<Script> scripts = new ArrayList<>();
for (Protos.Script protoScript : walletProto.getWatchedScriptList()) { for (Protos.Script protoScript : walletProto.getWatchedScriptList()) {
@ -579,6 +586,13 @@ public class WalletProtobufSerializer {
return wallet; 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 { private void loadExtensions(Wallet wallet, WalletExtension[] extensionsList, Protos.Wallet walletProto) throws UnreadableWalletException {
final Map<String, WalletExtension> extensions = new HashMap<>(); final Map<String, WalletExtension> extensions = new HashMap<>();
for (WalletExtension e : extensionsList) for (WalletExtension e : extensionsList)

View File

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

View File

@ -2927,7 +2927,7 @@ public class WalletTest extends TestWithWallet {
ScriptType outputScriptType = activeKeyChain.getOutputScriptType(); ScriptType outputScriptType = activeKeyChain.getOutputScriptType();
Protos.Wallet protos = new WalletProtobufSerializer().walletToProto(wallet); 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()); assertEquals(numActiveKeyChains, roundTrippedWallet.getActiveKeyChains().size());
DeterministicKeyChain roundTrippedActiveKeyChain = roundTrippedWallet.getActiveKeyChain(); DeterministicKeyChain roundTrippedActiveKeyChain = roundTrippedWallet.getActiveKeyChain();