Bitcoin Forum
May 12, 2024, 06:04:09 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Question(BitcoinJ):get the balance from the priv or pubkey  (Read 4152 times)
ILoveUBtc (OP)
Newbie
*
Offline Offline

Activity: 14
Merit: 0


View Profile
September 19, 2013, 03:14:22 PM
Last edit: September 19, 2013, 03:29:29 PM by ILoveUBtc
 #1

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?
1715493849
Hero Member
*
Offline Offline

Posts: 1715493849

View Profile Personal Message (Offline)

Ignore
1715493849
Reply with quote  #2

1715493849
Report to moderator
1715493849
Hero Member
*
Offline Offline

Posts: 1715493849

View Profile Personal Message (Offline)

Ignore
1715493849
Reply with quote  #2

1715493849
Report to moderator
1715493849
Hero Member
*
Offline Offline

Posts: 1715493849

View Profile Personal Message (Offline)

Ignore
1715493849
Reply with quote  #2

1715493849
Report to moderator
"You Asked For Change, We Gave You Coins" -- casascius
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715493849
Hero Member
*
Offline Offline

Posts: 1715493849

View Profile Personal Message (Offline)

Ignore
1715493849
Reply with quote  #2

1715493849
Report to moderator
Mike Hearn
Moderator
Legendary
*
Offline Offline

Activity: 1526
Merit: 1129


View Profile
September 19, 2013, 04:27:32 PM
 #2

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.
ILoveUBtc (OP)
Newbie
*
Offline Offline

Activity: 14
Merit: 0


View Profile
September 19, 2013, 05:06:20 PM
 #3

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 .
blueadept
Full Member
***
Offline Offline

Activity: 225
Merit: 101


View Profile
September 19, 2013, 10:49:24 PM
 #4

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.

Like my posts?  Connect with me on LinkedIn and endorse my "Bitcoin" skill.
Decentralized, instant off-chain payments.
ILoveUBtc (OP)
Newbie
*
Offline Offline

Activity: 14
Merit: 0


View Profile
September 20, 2013, 01:23:01 AM
 #5

......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)?
Mike Hearn
Moderator
Legendary
*
Offline Offline

Activity: 1526
Merit: 1129


View Profile
September 20, 2013, 01:31:12 PM
 #6

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.
yuerski
Newbie
*
Offline Offline

Activity: 1
Merit: 0


View Profile
March 14, 2018, 10:15:07 AM
 #7

......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
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!