Post
Topic
Board BitcoinJ
Re: Question(BitcoinJ):get the balance from the priv or pubkey
by
ILoveUBtc
on 19/09/2013, 17:06:20 UTC
Your code is incomplete. You're not showing where the wallet is set up. Perhaps the bug is there.

BTW that code seems quite old - IRC hasn't been used for a long time.

following is the whole code:

public static void main(String[] args) throws AddressFormatException, IOException {
   
      boolean testNet = args.length > 0 && args[0].equalsIgnoreCase("testnet");
      final NetworkParameters params = testNet ? NetworkParameters.testNet() : NetworkParameters.prodNet();

       Wallet wallet=new Wallet(params);
      
      byte bytes[]=Utils.parseAsHexOrBase58("500E3714FA3FD232C3885BEA78FB9418054AE4BB7000E589B1CF6CFD36598F6A");
      byte bytes1[]=Utils.parseAsHexOrBase58("A65DE055E85412BF6EC98B3459E7773F1102504504F077C3E9DD212580C26A57");
      byte bytes2[]=Utils.parseAsHexOrBase58("21");
      
      
             ECKey  key=new ECKey(new BigInteger(1,bytes),null,false);
      ECKey  key1=new ECKey(new BigInteger(1,bytes1),null,true);
      ECKey  key2=new ECKey(new BigInteger(1,bytes2));
      wallet.addKey(key);
      wallet.addKey(key1);
      wallet.addKey(key2);
      
      try
      {
         System.out.println("Reading block store from disk");

         File file=new File("temp.blockchain");
   
         BlockStore blockStore = new BoundedOverheadBlockStore(params, file);
         BlockChain chain = new BlockChain(params, wallet, blockStore);
         System.out.println("Connecting ...");
         PeerGroup peerGroup = new PeerGroup(params, chain);
         peerGroup.setUserAgent("PingService", "1.0");
         if (testNet) {
           peerGroup.addPeerDiscovery(new IrcDiscovery("#bitcoinTEST3"));
         } else {
           peerGroup.addPeerDiscovery(new DnsDiscovery(params));
         }
         peerGroup.addWallet(wallet);
         peerGroup.startAndWait();
         peerGroup.downloadBlockChain();
         System.out.println("You have " + Utils.bitcoinValueToFriendlyString(wallet.getBalance()) + " BTC");


      }
      catch (Exception e)
      {
         e.printStackTrace();
      }

   
           
   }


for the first time it works ok. and show me the balance correctly . and under my project there is a  new "temp.blockchain"   file;

and then I shut down the app and run again ,it only shows me  "0.00 BTC" with the same wallet ,same Ekeys.

and there is no error reported to me .

and then I deleted the  temp.blockchain ,and run again ,everything goes ok.

So It seems I find the solution . BUT ,everytime the app tries to download  the new  blockchain , it takes me nearly half an hour(about 25M)

I tried to change the BoundedOverheadBlockStore to DiskBlockStore ,but I don't know where to download it .