Skip to content

Commit

Permalink
[java] set the currency field correctly when finalizing wallet (#693)
Browse files Browse the repository at this point in the history
We can determine the currency (MAIN_NET or TEST_NET) by looking at the Base58-checked encoding of the public key returned from subzero core.

This addresses #254

Manually tested with a core binary built with both CURRENCY=btc-testnet and CURRENCY=btc-mainnet.

Test plan for TEST_NET:
1. `cd core && rm -rf build && mkdir build && cd build && TARGET=dev CURRENCY=btc-testnet cmake ../ && ./subzero` (leave this running)
2. `cd java && rm -rf test-wallets && mkdir test-wallets && touch test-wallets/.subzero_702e63a9 && ./gradlew clean build && java -jar ./gui/build/libs/gui-1.0.0-SNAPSHOT-shaded.jar --wallet-dir ./test-wallets --generate-wallet-files-test` (then type "yes" a bunch of times, ctrl-C when done)
3. Check that the wallet files in `test-wallets/finalized/` have a currency field with the value "TEST_NET"

Test plan for MAIN_NET:
1. Edit core/src/checks/self_checks.c and disable the verify_sign_tx check (it appears to be broken with `CURRENCY=btc-mainnet`).
2. `cd core && rm -rf build && mkdir build && cd build && TARGET=dev CURRENCY=btc-mainnet cmake ../ && ./subzero` (leave this running)
3. `cd java && rm -rf test-wallets && mkdir test-wallets && touch test-wallets/.subzero_702e63a9 && ./gradlew clean build && java -jar ./gui/build/libs/gui-1.0.0-SNAPSHOT-shaded.jar --wallet-dir ./test-wallets --generate-wallet-files-test` (then type "yes" a bunch of times, ctrl-C when done)
4. Check that the wallet files in `test-wallets/finalized/` have a currency field with the value "MAIN_NET"
  • Loading branch information
ivmaykov authored Oct 18, 2023
1 parent e164ecf commit 010aa5a
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import com.squareup.subzero.proto.service.Service.CommandResponse;
import com.squareup.subzero.proto.wallet.WalletProto.Wallet;
import com.squareup.subzero.wallet.WalletLoader;
import org.bitcoinj.crypto.DeterministicKey;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.params.TestNet3Params;

import java.io.IOException;

import static java.lang.String.format;
Expand Down Expand Up @@ -63,7 +67,7 @@ public static CommandResponse.FinalizeWalletResponse finalizeWallet(

// Send Request
internalRequest.setFinalizeWallet(finalizeWallet);
InternalCommandResponse.FinalizeWalletResponse iresp =
InternalCommandResponse.FinalizeWalletResponse response =
conn.run(internalRequest.build()).getFinalizeWallet();

if (subzero.nCipher) {
Expand All @@ -76,10 +80,28 @@ public static CommandResponse.FinalizeWalletResponse finalizeWallet(
// Save the encrypted pubkeys to the wallet
Wallet newWallet = Wallet.newBuilder(wallet)
.addAllEncryptedPubKeys(request.getFinalizeWallet().getEncryptedPubKeysList())
.setCurrency(getCurrencyFromFinalizeResponse(response))
.build();
walletLoader.save(request.getWalletId(), newWallet);

// Build response
return CommandResponse.FinalizeWalletResponse.newBuilder().setPubKey(iresp.getPubKey()).build();
return CommandResponse.FinalizeWalletResponse.newBuilder().setPubKey(response.getPubKey()).build();
}

private static Wallet.Currency getCurrencyFromFinalizeResponse(
InternalCommandResponse.FinalizeWalletResponse response) {
String base58PublicKey = response.getPubKey().toStringUtf8();
try {
DeterministicKey.deserializeB58(base58PublicKey, MainNetParams.get());
return Wallet.Currency.MAIN_NET;
} catch (Exception e) {
// ignore the error, try again assuming test net
}
try {
DeterministicKey.deserializeB58(base58PublicKey, TestNet3Params.get());
return Wallet.Currency.TEST_NET;
} catch (Exception e) {
throw new RuntimeException("Public key " + base58PublicKey + " does not belong to either MAIN_NET or TEST_NET");
}
}
}

0 comments on commit 010aa5a

Please sign in to comment.