Bitcoin wallet configuration
In the first line of our code, we activate BitcoinJ's logging using a Java logging formatter that writes more compact output than the default:
BriefLogFormatter.init();
Then we indicate which bitcoin network we want to use. In this scenario, we choose testnet, as our Node.js server is already running on the testnet network by default:
final static NetworkParameters params = TestNet3Params.get();
The other available options are MainNetParams and RegTestParams.
Next, we start by initializing (inside the main function) a WalletAppKit object, to create a lightweight SPV (short for Simplified Payment Verification) BitcoinJ wallet. As mentioned in the BitcoinJ documentation, the WalletAppKit class wraps the boilerplate (Peers, BlockChain, BlockStorage, Wallet) needed to set up a new SPV BitcoinJ app:
WalletAppKit kit = new WalletAppKit(params, new File("."), "walletappkit");
As a result, two files are also created locally – .wallet (wallet ) and .spvchain (blockchain information) – with the specified prefix (the third argument) and stored in the indicated directory (the project root folder). Once created, we download the blockchain and wait until it's done:
kit.startAsync();
kit.awaitRunning();
In this case, the kit will behave as a bitcoin node connecting to other nodes and syncing with them the blockchain (downloading only headers).
We then generate a bitcoin address for the client and print it along with its balance:
Address CustomerAddress=kit.wallet().currentReceiveAddress();
System.out.println("Customer's address : " + CustomerAddress);
System.out.println("Customer's Balance : "+kit.wallet().getBalance());
The wallet is now ready to run but we need to send a few bitcoins from a faucet source to the bitcoin address returned by the currentReceiveAddress function. If you run the current code, you'll get output similar to the following about blockchain's synchronization and the account details in Eclipse's log viewer: