0

I'm trying to use bitcoinj library to receive bitcoin payments and monitor the balance on the address. My implementation is based on DeterministicKey watching public key. The code below is working fine for some time, it prints a message when coins received and print correct balance after it. But if I restart it, the balance becomes zero and the wallet loses all received previously transactions (getTransactionsByTime() is empty list), so it's working only if I send some coins to the static address while the code is running. Also if I start the code again and send new coins to the same address (it's static), I'll see only new transaction, all previous are lost. This is my demo code to check it's working (groovy):

def net = TestNet3Params.get()
def wlt = Wallet.fromWatchingKey(net, DeterministicKey.deserializeB58('tpubD9xtwS1WLdbdLQKCSiR8kiwBwWzL1C1qq7LTgMq12ugeqyGbDCoFic2zqrNgwthyvq8DUubn6CSev58Ja2o9KgMQxafDd6UZWG9JSZWcdGY', net))
def store = new SPVBlockStore(net, new File("/tmp/spvbstore.blockchain"))
def chain = new BlockChain(net, store)
def peers = new PeerGroup(net, chain)
peers.maxConnections = 5
peers.maxPeersToDiscoverCount = 5
peers.addPeerDiscovery(new DnsDiscovery.DnsSeedDiscovery(net, 'testnet-seed.bitcoin.jonasschnelli.ch'))
chain.addWallet(wlt)
peers.addWallet(wlt)
peers.start()
peers.downloadBlockChain()
wlt.addCoinsReceivedEventListener(
  new WalletCoinsReceivedEventListener() {
    @Override
    void onCoinsReceived(
      final Wallet wallet,
      final Transaction tx, final Coin prevBalance, final Coin newBalance) {
        println("RECEIVED: $tx: $prevBalance -> $newBalance")
    }
  }
)
while (true) {
  println("BALANCE: ${wlt.getBalance(Wallet.BalanceType.ESTIMATED)}")
  println("transactions:\n$wlt.transactionsByTime")
  Thread.sleep(10000)
}
  • i didn't understand as per your question: if I restart it, the balance becomes zero and the wallet loses all received previously transactions, if I start the code again and send new coins to the same address, how can you sent the coins with zero balance ? – Zombie Sep 20 '18 at 22:42
  • @Zombie my code only watches the balance, it can't send coins because the wallet doesn't have private key here. I mean when I send coins to the address of running code, not from. – g4s8 Sep 21 '18 at 4:29
  • when you run the code again, are you loading wallet from the file ? – Zombie Sep 21 '18 at 15:45
  • @Zombie no, I'm creating it from fixed public key (watching key) – g4s8 Sep 21 '18 at 17:00
  • Sounds like a watching wallet, but you need to save the wallet as well as you're saving spv file – Zombie Sep 21 '18 at 20:00
2

Sounds like a watching wallet, but you need to save the wallet file as well as you're saving spv file. As per documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.