Bitcoinj get transactions from blockchain

356 views Asked by At

I am readying the documentation at https://bitcoinj.github.io/working-with-the-wallet and I am not sure what I am missing.

Suppose I create a wallet, get its receive address and save wallet using saveToFileStream(OutputStream). Then I send bitcoins to my address, while my wallet is not running and I do not have a WalletEventListener listening for changes. I can then restore my wallet using loadFromFileStream(InputStream) to restore my wallet. How do I find transactions that may have been made and my new balance?

1

There are 1 answers

0
Flo Ryan On

You need to sync your wallet with the blockchain. The easiest option is probably to use the WalletAppKit:

// for test net
NetworkParameters networkParameters = TestNet3Params.get();
// given the path to your wallet is "<walletFolderPath>/<walletFilePrefix>.wallet"
WalletAppKit kit = new WalletAppKit(networkParameters, new File(walletFolderPath), walletFilePrefix);
// start syncing with the blockchain
kit.startAsync();
// wait until syncing is done
kit.awaitRunning();

If you do not want to use the WalletAppKit, you could also connect to the blockchain more "manually", as is shown in the second part of one of the official examples from Bitcoinj here