Running and Testing the hello-money Java Program
We're ready to run and test the program! As before, we first compile and then run the program:
Some messages should then appear as your program connects to the Bitcoin network and downloads the blockchain. The first time the program is run, this may take a while:
After the blockchain has finished downloading, you can test the hello-money program and send it some tiny sum from your favorite wallet app. Simply send 0.0002 BTC to the public address and record the private key (we'll be using this money in the follow-up example program later in this appendix). The program should detect when the money arrives and display a message like this:
Hello Money! Balance: 20000 satoshis
The new balance in the wallet should display in satoshis (divide by 100,000,000 to see that this number is indeed 0.0002 BTC).
You've successfully written a bitcoinJ program that creates a Bitcoin wallet and reports on any money received. Now let's write a second program that uses the newly stored money!
Now, let's write a brand-new program that can send money from an arbitrary Bitcoin address. To create a new bye-bye-money program, run the following from your top-level program directory:
Then, make the exact additions to the pom.xml file as we did in the hello-money example, except for the line that reads <mainClass>hellomoney .App</mainClass>. Change that line to <mainClass>byebyemoney.App</mainClass>. (These steps are analogous to those we followed for our hello-money program in "Creating a Starter Project for hello-money" on page 228.)
Just as before, open the file src/main/java/byebyemoney/App.java and replace its contents with the following program:
Many of the lines in this new program are shared with our previous hello-money program, but let's look carefully at the new parts.
Importing a Private Key
To send money from our program, we need to import the private key of the Bitcoin address from the previous example. Here is the code that does this:
In the first lines, we're explicitly adding a new, preexisting private key to our wallet O0. This is the key associated with the Bitcoin address that received the money in the hello-money program. You need to replace the private key shown on this line with the private key you wrote down when running the previous example. Also, in this new program we're not using the SPVBlockStore function; instead, we're using bitcoinJ's MemoryBlockStore 0 function for variety. This block store won't create a file, but by using it, our program will need to redownload the blockchain every time the program runs. (This also guarantees that bitcoinJ will assign the correct balance to the wallet. We'll discuss why in "Gotchas When Using Wallets in BitcoinJ" on page 239.)
Sending the Money
Now let's look at the code that actually sends the money:
First, we get the balance contained in the wallet © and display it 0. Next, we declare the destination address the money should be sent to ®. In this example, we input the main donation address for the Bitcoin Foundation; feel free to substitute a public address of one of your own wallets.
Nowadays, it's best to include a transaction fee when sending bitcoins, which we declare to be 10,000 satoshis 0. Next, we create a SendRequest object 0, which is a structure to hold the basic information about what we're sending and includes the destination address and the amount to be sent (which is the balance minus the fee). Then, we set the fee on this object ® and send our money 0!
Ensuring the Money Transmission
If we try to send more money than we have, if the fee is inadequate, or if the Internet connection drops out at the wrong moment, the money might never be accepted by the network. Therefore, we need to write code that waits and ensures that the money we sent is transmitted to the network. Here's what we'll add to do this:
The first line of code © retrieves a Java future object, which indicates that the send transaction has been properly broadcast to the network. (A standard in Java, futures retrieve information about a separate execution thread—in this case the thread that monitors communication with the Bitcoin network.) If this line completes without throwing an exception, we display a message indicating that the money was sent ®.
We can run bye-bye-money in the usual way (remember to input your own private key) :
EXCEPTION TYPES IN BITCOINJ
One feature we skimped on in this example is error handling: The main function simply rethrows a variety of different exceptions for operations that can go wrong as we attempt to send our money. These include the following exceptions:
• BlockStoreException: This is thrown when the block store cannot be created (most commonly, this happens with block store types that write to a file when something corrupts the file).
• AddressFormatException: This is thrown when the format of the address is incorrect.
• InterruptedException: This is thrown when network connection problems occur.
• ExecutionException: This is thrown when we're using a future object and an exception occurs in the other thread (as happens when we check for completion of the transaction broadcast).
In a more sophisticated Bitcoin app, you should catch all of these exception types separately and add more descriptive error messages for your app's users.
Because this program churns through the blockchain in memory, you'll need to wait a few minutes or more for it to complete (even when you rerun it). If the program is successful, you'll see the message The money was sent!, and the money should arrive at the destination wallet. You can also access a blockchain information site (such as blockchain.info/), enter the source or destination address, and see that the details of the transaction are part of the public record.
Congratulations! You now understand the basics of writing a Bitcoin application!