ForwardingService: create singleton class, simplify main()

This commit is contained in:
Sean Gilligan 2022-07-09 12:10:11 -07:00 committed by Andreas Schildbach
parent d3599482c8
commit cbe1833e04

View File

@ -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,30 +73,60 @@ 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.
kit.connectToLocalHost();
}
// Download the block chain and wait until it's done.
// 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);