Author

Topic: Question(BitcoinJ):get the balance from the priv or pubkey (Read 4150 times)

newbie
Activity: 1
Merit: 0
......Look at the examples again. You should only add the keys the first time you open the wallet.....

Thank you after fighting for a whole night ,I fixed it.

BTW  where are the examples(officially)?


bro , if u solved , why not post solution , i almost fight one whole night yesterday  , finnaly guessed what's wrong and working now  .

====>restoredWallet.saveToFile(file);

hope can help following guys  . gaga
legendary
Activity: 1526
Merit: 1128
In the examples directory of the bitcoinj source code, although there is example code in the tutorials and documentation as well.

Please do review the "Getting Started" document. It will show you how to do what you want, but more simply. If you use WalletAppKit this problem would not have happened because it'll set up autosaving automatically.
newbie
Activity: 14
Merit: 0
......Look at the examples again. You should only add the keys the first time you open the wallet.....

Thank you after fighting for a whole night ,I fixed it.

BTW  where are the examples(officially)?
full member
Activity: 225
Merit: 101
You're not saving the wallet. Look at the examples again. You should only add the keys the first time you open the wallet, and you Sikhs save it in a file (and enable auto save). The blockchain file doesn't have the full chain, just the headers. The wallet is where related transactions are stored.
newbie
Activity: 14
Merit: 0
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 .
legendary
Activity: 1526
Merit: 1128
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.
newbie
Activity: 14
Merit: 0
WHY !  WHY !  WHY!


I  want to use the bitcoinj to get the balance from priv key  OR  public key.

the code is like following .

                        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");


the wallet has had EKeys which have btc.

But why the balance show me is 0.00?  why? Any one help?
Jump to: