Home Education Bitcoin for the befuddled
BITCOIN PROGRAMMING WITH BITCOINJ
previously, Bitcoin programs written this way—meaning those that do not rely on APIs from external companies and avoid dependencies on separate wallet programs—are usually the smartest solution for serious Bitcoin development projects.
The Best Programming Language for Connecting to the Bitcoin Network
If you're an experienced programmer, most likely you have a favorite programming language you would prefer to use to write your Bitcoin programs. However, only a few mature libraries currently exist that can connect
directly to the Bitcoin network. One is the C++ reference implementation with which all the first full clients of the Bitcoin system were written.1 Large enterprise companies interested in supporting bitcoins should use this library. However, working with C++ is difficult for novices.
Another mature library is written in Java and is called bitcoinJ.2 Because Java is easier to work with than C++, it is the library we'll use. (The bitcoinJ library can also be used easily from languages built on top of the Java Virtual Machine, such as Scala or Clojure.)
Currently, the options for other languages are rather limited. By searching the Internet, you will find Bitcoin libraries for other languages, such as Ruby, C#, and Python. However, most of these libraries are either in extremely early development or simply use the more limited JSON-RPC interface, which was discussed in Appendix A.
Some serious attempts are being made to offer full Bitcoin client support within the Go programming language. However, at the time of this writing, these libraries are either still in early development (such as gocoin) or are missing major features (such as btcd4).
No matter which library you choose, keep in mind that you must be vigilant about security. Essentially, when you use any of these libraries, you'll be giving the library authors the keys to your Bitcoin kingdom. As we warned previously, technically it would be relatively easy to introduce rogue code into one of these libraries to steal all your money!
Installing Java, Maven, and the BitcoinJ Library
Let's ready your computer to do Java and bitcoinJ programming. The following steps should work on any major OS: Windows, Mac, or Linux.
Step 1: Installing Java
The Java programming language is maintained by Oracle Corporation. Your computer may already have Java installed, but if you've never done Java development, your computer may only contain the Java JRE ( Java Runtime Engine), which can run Java programs but is not adequate for developing them. Instead, you need the Java JDK (Java Development Kit). To download the JDK, do a Google search for Java JDK. The first link (which should be a link on the official Oracle website) should direct you to the right place to access the download.
1. The C++ reference implementation is available at https://github.com/bitcoin/bitcoin/.
2. BitcoinJ is available at bitcoinj.github.io/
4. https://github. com/conformal/btcd/
Linux developers: Some versions of Linux may install the Open JDK version of Java by default. At the time of this writing, this unofficial Java variant is missing some components that this tutorial depends on. Instead, look online for information about installing the Oracle JDK on your version of Linux and make it the default Java version on your system.
Step 2: Installing Maven
Maven is a packaging tool for Java. Essentially, you specify which libraries your program needs, and Maven automatically downloads them from the Internet and makes them available to your program. This is analogous to the Node Package Manager used in Appendix A.
You can download Maven and find instructions for installing Maven on each OS at https://maven.apache.org/. Just follow the instructions at the main Maven website to install it or search Google for tutorials. Because more than a million other people have had to install Maven, if you run into installation problems, just type your problem into Google. It is very likely you'll get helpful information on how to solve your problem.
Windows users: At the time of this writing, detailed instructions for installing Maven on Windows are cleverly hidden at the very bottom of maven.apache.org/ download.cgi. Mac/Linux users: You can use your package managers here: brew install maven for Mac and sudo apt-get install maven for Debian Linux.
To ensure Maven is properly installed, a version message should display when you type mvn --version from a console.
For typical Java programming, we could stop here because any needed additional libraries could be downloaded through the Maven package system. However, because we're writing programs that work with money, the bitcoinJ maintainers require all developers to take a few extra precautions and install a couple of additional tools that directly relate to security issues.
Step 3: Installing Git
For security reasons, we'll install Git, which is a popular source code management tool. But it also offers features for securely downloading source code from online repositories that we'll rely on. Download Git from git-scm.com/.
Once again, Mac and Linux users can use their package managers: brew install git and apt-get install git, respectively. Do you see a pattern?
Step 4: Installing BitcoinJ
BitcoinJ is a Bitcoin library that can "talk bitcoin" and can connect directly to the Bitcoin network. By calling functions in this library, we can send and receive bitcoins in real time.
We'll build bitcoinJ directly from its authoritative source. Navigate to a directory from the console where you want to install the bitcoinJ library (your Home directory would be a good place).
A new subdirectory will be created, so other existing files in this directory will not be affected by this installation.
Enter the following into your console (on Windows you may need to use the special Git Bash console that was installed when you installed Git):
The first line downloads the bitcoinJ code from the authoritative website O. Then, we use git to switch to an older version of this library using the git checkout command 0. During this tutorial, this command will shield us from any problems that might be caused by newer versions of the library. However, when you've completed the tutorials, you can switch to the newest version of bitcoinJ (git checkout master) to experiment with its shiny new features. The last line installs the package into the local Maven package repository ©. When we reference bitcoinJ from our programs, Maven will fetch it from this repository (instead of grabbing it from the Internet), and we can be confident we're using an uncorrupted version of this library.
Now we can finally start programming!
|< Prev||CONTENTS||Next >|