Author

Topic: What classes to use? (Read 2369 times)

legendary
Activity: 1526
Merit: 1129
January 18, 2015, 09:50:58 AM
#3
Yep, that sounds right. You can inherit from AbstractWalletListener to avoid having to implement empty methods.

Unless you use an external index of the block chain, there is no way to know about transactions to an address without actually scanning through it.
newbie
Activity: 53
Merit: 0
January 16, 2015, 08:10:06 AM
#2
Big thank you to Mike and other devs. There's a lot of good work behind BitcoinJ. I've been using quite a few libraries, and few are as well documented as BitcoinJ.


I've experimented a bit, and wanted to provide the solutions if someone else comes across similar problems:


>Can it be done with BitcoinJ?
Yes, but since BitcoinJ is very lightweight, it won't scan the blockchain from before the program was started. So you only get the updates that happens to your address when your program is live.

Here's how you do it:
(I've borrowed a lot from the examples provided in BitcoinJ on github)

 public static void main(String[] args) {

      NetworkParameters params = MainNetParams.get();

      WalletAppKit kit = new WalletAppKit(params, new File("."), "walletappkit-example");

        kit.startAsync();
        kit.awaitRunning();

        WalletListener wListener = new WalletListener();
        kit.wallet().addEventListener(wListener);

        Address address = null;
      try {
         address = new Address(params, "1BitangoaBiPiX6dqcLCHZUjsvzsuauB2x");
      } catch (AddressFormatException e) {
         e.printStackTrace();
      }
        kit.wallet().addWatchedAddress(address);
}

//Then you need a wallet listener class:

 static class WalletListener extends AbstractWalletEventListener {

        @Override
        public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
            System.out.println("-----> coins resceived: " + tx.getHashAsString());
            System.out.println("received: " + tx.getValue(wallet));
            System.out.println("wallet balance: " +wallet.getBalance());
            System.out.println("prev bal: "+prevBalance.value);
            System.out.println("new bal " + newBalance.value);
            System.out.println(tx.getVersion());
        }

        @Override
        public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
            System.out.println("-----> confidence changed: " + tx.getHashAsString());
            TransactionConfidence confidence = tx.getConfidence();
            System.out.println("new block depth: " + confidence.getDepthInBlocks());
        }

        @Override
        public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
            System.out.println("coins sent");
        }

        @Override
        public void onReorganize(Wallet wallet) {
        }

        @Override
        public void onWalletChanged(Wallet wallet) {
        }

        @Override
        public void onKeysAdded(List keys) {
            System.out.println("new key added");
        }

//        @Override
        public void onScriptsChanged(Wallet wallet, List