ForwardingService: create singleton class, simplify main()
@@ -51,8 +51,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
public class ForwardingService {
static final int requiredConfirmations = 1;
- private static Address forwardingAddress;
- private static WalletAppKit kit;
+ private final BitcoinNetwork network;
+ private final NetworkParameters params;
+ private final Address forwardingAddress;
+ private final WalletAppKit kit;
public static void main(String[] args) throws Exception {
// This line makes the log output more compact and easily read, especially when using the JDK log adapter.
@@ -71,20 +73,45 @@ public class ForwardingService {
} else {
network = BitcoinNetwork.MAIN;
}
- NetworkParameters params = NetworkParameters.of(network);
// Parse the address given as the first parameter.
- forwardingAddress = Address.fromString(params, args[0]);
+ var address = Address.fromString(NetworkParameters.of(network), args[0]);
System.out.println("Network: " + network.id());
- System.out.println("Forwarding address: " + forwardingAddress);
+ System.out.println("Forwarding address: " + address);
+
+ // Create the Service (and WalletKit)
+ ForwardingService forwardingService = new ForwardingService(address, network);
+
+ // Start the Service (and WalletKit)
+ forwardingService.start();
+
+ // Start listening and forwarding
+ forwardingService.forward();
+ }
+
+ /**
+ * Forwarding service. Creating this object creates the {@link WalletAppKit} object.
+ *
+ * @param forwardingAddress Address to forward to
+ * @param network Network to listen on
+ */
+ public ForwardingService(Address forwardingAddress, BitcoinNetwork network) {
+ this.forwardingAddress = forwardingAddress;
+ this.network = network;
+ this.params = NetworkParameters.of(network);
// Start up a basic app using a class that automates some boilerplate.
- kit = new WalletAppKit(params,
+ kit = new WalletAppKit(NetworkParameters.of(network),
ScriptType.P2WPKH,
KeyChainGroupStructure.BIP32,
new File("."),
getPrefix(network));
+ }
+ /**
+ * Start the WalletAppKit
+ */
+ public void start() {
if (network == BitcoinNetwork.REGTEST) {
// Regression test mode is designed for testing and development only, so there's no public network for it.
// If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance.
@@ -94,7 +121,12 @@ public class ForwardingService {
// Download the blockchain and wait until it's done.
kit.startAsync();
kit.awaitRunning();
+ }
+ /**
+ * Setup the listener to forward received coins and wait
+ */
+ public void forward() {
// We want to know when we receive money.
kit.wallet().addCoinsReceivedEventListener((w, tx, prevBalance, newBalance) -> {
// Runs in the dedicated "user thread" (see bitcoinj docs for more info on this).
@@ -138,7 +170,7 @@ public class ForwardingService {
}
}
- private static void forwardCoins() {
+ private void forwardCoins() {
try {
// Now send the coins onwards.
SendRequest sendRequest = SendRequest.emptyWallet(forwardingAddress);
