Bitcoin Forum
May 25, 2024, 07:39:59 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: What classes to use?  (Read 2369 times)
CashBitango (OP)
Newbie
*
Offline Offline

Activity: 53
Merit: 0


View Profile
January 14, 2015, 09:45:53 AM
 #1

Hi

First off, thanks to the devs for making the BitcoinJ library. The examples in there were very useful. Nice to see some code that humans can read for a change.

I'm doing research to find an API where I can do the following:

1) My program A should be able to "listen" to activity on a specific address, without having private key for that address.
The public key will not change, and can be hardcoded into the program, stored on disk or whatever works for the BitcoinJ API.

2) My program A should be able to create an unsigned transaction. (This transaction should then be stored to disk or something).
Next another program B should be able to read the content of the transaction, and if the transaction is acceptable, it should sign this transaction.
Then the transaction should be published to peers on the bitcoin network, using program A or B.

Can these things be done with bitcoinJ?

What classes / interfaces should I use to achieve these goals?
CashBitango (OP)
Newbie
*
Offline Offline

Activity: 53
Merit: 0


View Profile
January 16, 2015, 12:10:06 PM
 #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<ECKey> keys) {
            System.out.println("new key added");
        }

//        @Override
        public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
            System.out.println("new script added");
        }
    }
   
   
Creating unsigned transaction might not be the best way to do it. Pass instead the required information; address and amount, and have the program on the other side create a transaction from that information.

Sample: (assuming you got a class variable WalletAppKit kit)

public Transaction(NetworkParameters params, String addressTo, Coin value){
   Transaction tx1 = kit.wallet().createSend(new Address(params, addressTo), value);
}

Mike Hearn
Moderator
Legendary
*
Offline Offline

Activity: 1526
Merit: 1129


View Profile
January 18, 2015, 01:50:58 PM
 #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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!