WalletAppKit: add constructor with BitcoinNetwork, deprecate others

* WalletAppKit: add a constructor that takes a BitcoinNetwork
* WalletAppKit: deprecate constructors taking netParams or Context
* WalletAppKit: add an accessor for network()
* Update examples, tools, and wallettemplate to use the new constructor
This commit is contained in:
Sean Gilligan 2022-07-11 17:13:11 -07:00 committed by Andreas Schildbach
parent e98b5e68fc
commit 9f02cd2af3
7 changed files with 46 additions and 12 deletions

View file

@ -85,6 +85,7 @@ import static com.google.common.base.Preconditions.checkState;
public class WalletAppKit extends AbstractIdleService {
protected static final Logger log = LoggerFactory.getLogger(WalletAppKit.class);
protected final BitcoinNetwork network;
protected final NetworkParameters params;
protected final ScriptType preferredOutputScriptType;
protected final KeyChainGroupStructure structure;
@ -113,26 +114,53 @@ public class WalletAppKit extends AbstractIdleService {
/**
* Creates a new WalletAppKit, with a newly created {@link Context}. Files will be stored in the given directory.
* @deprecated Use {@link #WalletAppKit(BitcoinNetwork, ScriptType, KeyChainGroupStructure, File, String)}
*/
@Deprecated
public WalletAppKit(NetworkParameters params, File directory, String filePrefix) {
this(new Context(params), ScriptType.P2PKH, KeyChainGroupStructure.BIP32, directory, filePrefix);
this(params.network(), ScriptType.P2PKH, KeyChainGroupStructure.BIP32, directory, filePrefix);
}
/**
* Creates a new WalletAppKit, with a newly created {@link Context}. Files will be stored in the given directory.
* @deprecated Use {@link #WalletAppKit(BitcoinNetwork, ScriptType, KeyChainGroupStructure, File, String)}
*/
@Deprecated
public WalletAppKit(NetworkParameters params, ScriptType preferredOutputScriptType,
@Nullable KeyChainGroupStructure structure, File directory, String filePrefix) {
this(new Context(params), preferredOutputScriptType, structure, directory, filePrefix);
this(params.network(), preferredOutputScriptType, structure, directory, filePrefix);
}
/**
* Creates a new WalletAppKit, on the specified {@link BitcoinNetwork}. Files will be stored in the given directory.
*
* @param network The network the wallet connects to
* @param preferredOutputScriptType The output script type (and therefore {@code Address} type) of the wallet
* @param structure The keychain group structure (e.g. {@link KeyChainGroupStructure#BIP43} or {@link KeyChainGroupStructure#BIP32}
* @param directory The directory for creating {@code .wallet} and {@code .spvchain} files
* @param filePrefix The base name for the {@code .wallet} and {@code .spvchain} files
*/
public WalletAppKit(BitcoinNetwork network, ScriptType preferredOutputScriptType,
KeyChainGroupStructure structure, File directory, String filePrefix) {
this.network = checkNotNull(network);
this.params = NetworkParameters.of(this.network);
this.context = new Context(params);
this.preferredOutputScriptType = checkNotNull(preferredOutputScriptType);
this.structure = checkNotNull(structure);
this.directory = checkNotNull(directory);
this.filePrefix = checkNotNull(filePrefix);
}
/**
* Creates a new WalletAppKit, with the given {@link Context}. Files will be stored in the given directory.
* @deprecated Use {@link #WalletAppKit(BitcoinNetwork, ScriptType, KeyChainGroupStructure, File, String)}
*/
@Deprecated
public WalletAppKit(Context context, ScriptType preferredOutputScriptType,
@Nullable KeyChainGroupStructure structure, File directory, String filePrefix) {
this.context = context;
this.params = checkNotNull(context.getParams());
this.network = params.network();
this.preferredOutputScriptType = checkNotNull(preferredOutputScriptType);
this.structure = structure != null ? structure : KeyChainGroupStructure.BIP32;
this.directory = checkNotNull(directory);
@ -493,6 +521,10 @@ public class WalletAppKit extends AbstractIdleService {
}
}
public BitcoinNetwork network() {
return network;
}
public NetworkParameters params() {
return params;
}

View file

@ -38,7 +38,7 @@ public class DoubleSpend {
public static void main(String[] args) throws Exception {
BriefLogFormatter.init();
final RegTestParams params = RegTestParams.get();
WalletAppKit kit = new WalletAppKit(params, ScriptType.P2WPKH, KeyChainGroupStructure.BIP32, new File("."), "doublespend");
WalletAppKit kit = new WalletAppKit(params.network(), ScriptType.P2WPKH, KeyChainGroupStructure.BIP32, new File("."), "doublespend");
kit.connectToLocalHost();
kit.setAutoSave(false);
kit.startAsync();

View file

@ -87,7 +87,7 @@ public class ForwardingService {
this.params = NetworkParameters.of(network);
// Start up a basic app using a class that automates some boilerplate.
kit = new WalletAppKit(NetworkParameters.of(network),
kit = new WalletAppKit(network,
ScriptType.P2WPKH,
KeyChainGroupStructure.BIP32,
new File("."),

View file

@ -16,6 +16,7 @@
package org.bitcoinj.examples;
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.ScriptType;
import org.bitcoinj.core.*;
import org.bitcoinj.kits.WalletAppKit;
@ -44,19 +45,20 @@ public class Kit {
// First we configure the network we want to use.
// The available options are:
// - MainNetParams
// - TestNet3Params
// - RegTestParams
// - BitcoinNetwork.MAINNET
// - BitcoinNetwork.TESTTEST
// - BitcoinNetwork.SIGNET
// - BitcoinNetwork.REGTEST
// While developing your application you probably want to use the Regtest mode and run your local bitcoin network. Run bitcoind with the -regtest flag
// To test you app with a real network you can use the testnet. The testnet is an alternative bitcoin network that follows the same rules as main network.
// Coins are worth nothing and you can get coins from a faucet.
//
// For more information have a look at: https://bitcoinj.github.io/testing and https://bitcoin.org/en/developer-examples#testing-applications
NetworkParameters params = TestNet3Params.get();
BitcoinNetwork network = BitcoinNetwork.TESTNET;
// Now we initialize a new WalletAppKit. The kit handles all the boilerplate for us and is the easiest way to get everything up and running.
// Have a look at the WalletAppKit documentation and its source to understand what's happening behind the scenes: https://github.com/bitcoinj/bitcoinj/blob/master/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java
WalletAppKit kit = new WalletAppKit(params, ScriptType.P2WPKH, KeyChainGroupStructure.BIP32, new File("."), "walletappkit-example");
WalletAppKit kit = new WalletAppKit(network, ScriptType.P2WPKH, KeyChainGroupStructure.BIP32, new File("."), "walletappkit-example");
// In case you want to connect with your local bitcoind tell the kit to connect to localhost.
// You must do that in reg test mode.

View file

@ -37,7 +37,7 @@ public class SendRequest {
// We use the WalletAppKit that handles all the boilerplate for us. Have a look at the Kit.java example for more details.
NetworkParameters params = TestNet3Params.get();
WalletAppKit kit = new WalletAppKit(params, ScriptType.P2WPKH, KeyChainGroupStructure.BIP32, new File("."), "sendrequest-example");
WalletAppKit kit = new WalletAppKit(params.network(), ScriptType.P2WPKH, KeyChainGroupStructure.BIP32, new File("."), "sendrequest-example");
kit.startAsync();
kit.awaitRunning();

View file

@ -50,7 +50,7 @@ public class TestFeeLevel {
Coin feeRateToTest = Coin.valueOf(Long.parseLong(args[0]));
System.out.println("Fee rate to test is " + feeRateToTest.toFriendlyString() + "/kB");
kit = new WalletAppKit(PARAMS, ScriptType.P2WPKH, KeyChainGroupStructure.BIP32, new File("."), "testfeelevel");
kit = new WalletAppKit(PARAMS.network(), ScriptType.P2WPKH, KeyChainGroupStructure.BIP32, new File("."), "testfeelevel");
kit.startAsync();
kit.awaitRunning();
try {

View file

@ -151,7 +151,7 @@ public abstract class WalletApplication implements AppDelegate {
public void setupWalletKit(@Nullable DeterministicSeed seed) {
// If seed is non-null it means we are restoring from backup.
File appDataDirectory = AppDataDirectory.get(applicationName).toFile();
walletAppKit = new WalletAppKit(params, preferredOutputScriptType, keyChainGroupStructure, appDataDirectory, walletFileName) {
walletAppKit = new WalletAppKit(params.network(), preferredOutputScriptType, keyChainGroupStructure, appDataDirectory, walletFileName) {
@Override
protected void onSetupCompleted() {
Platform.runLater(controller::onBitcoinSetup);