My goal is to watch a public bitcoin address and print to the console whenever money is sent to that address. That's all. I'm taking an address previously generated in Bitcoin Core, for now.
I'm doing the following:
NetworkParameters params = MainNetParams.get();
Wallet wallet = Wallet.loadFromFile(file);
BlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, wallet, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.addPeerDiscovery(new DnsDiscovery(params));
peerGroup.setUseLocalhostPeerWhenPossible(true);
peerGroup.startAsync();
Address add = new Address(params, "1NpxxxxxxxxxxxxxxxaSC4");
wallet.addWatchedAddress(add);
wallet.addEventListener(new AbstractWalletEventListener() {
@Override
public synchronized void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
System.out.println("[main]: COINS RECIEVED!");
System.out.println("\nReceived tx " + tx.getHashAsString());
System.out.println(tx.toString());
}
});
System.out.println("\nDone!\n");
System.out.println(wallet.toString());
I have a feeling that I'm not handling the AbstractWalletEventListener correctly. When I send money to the address, I don't get the text I expect to see in the console. Instead, I just see the continuous stream of "peer announced new transaction" from the [NioClientManager] from the peerGroup.startAsync() method.
What am I doing wrong and how can I correct it? I've spent way more time than I should have on something that seems like it should be such a simple task.
PS. The file I'm calling for "loadFromFile" is just a blank, default wallet file generated by bitcoinj. Nothing special about it.
Edit: Also, I'm not looking to see the total balance of the wallet. I only want to know when -new- transactions come in. Old ones are irrelevant in my program.