Writing Your First Bitcoin Program in JavaScript

By convention, the first program a programmer writes when learning a new technology is a Hello World program: It's the simplest program possible in that it just prints the message Hello World! on the screen. Essentially, it indicates to the novice that your initial code seems to be working at the outset.

However, the Bitcoin programs we need to write must perform two main tasks: receive money and send money. In this chapter, we'll focus solely on receiving money and write a program called Hello Money!. In Appendix B we'll write a Bye-Bye Money program to send money.

Why Use JavaScript?

JavaScript is arguably the most well-known programming language in use today, because most developers who build software for the Web must learn it sooner or later (it is the only language that can natively run in a standard web browser). However, the JavaScript code we create will not run in a web browser: This is because web browsers have strong protections called cross-domain restrictions that shield them against communicating with external programs and websites. These restrictions limit what you can do with JavaScript within a web browser, making it difficult to interact with any external wallets (which is no surprise, because you wouldn't want your wallet emptied by visiting a malicious website).

However, currently you can also run JavaScript on a server, as is commonly done using the Nodejs library. When run on a server, no such restrictions apply, and we can write simple programs that run in a console and can interact with Bitcoin Core and bitcoind.

Bitcoin Core vs. Bitcoind

As discussed earlier, the programming techniques described in this chapter will work with either of the wallet apps, Bitcoin Core or bitcoind. The only substantive difference between the two programs is that Bitcoin Core has a graphical UI; basically, it is just bitcoind with a frontend attached to it. Because of the friendliness of the graphical UI, we'll use Bitcoin Core for

the example in this chapter. However, bitcoind is slightly less resource-intensive and can more easily run on computer servers via a terminal console. Consequently, it's best for you to use bitcoind if you actually deploy a finished program that uses the concepts in this chapter.

Preparing Your Machine for JavaScript Bitcoin Programming

You'll need a few tools to ready your machine before you begin programming, so let's get them now. The instructions provided here assume a Windows development environment; skip ahead to "For Mac Hackers" on page 219 or "For Linux Folks" on page 219 if you're using either of those platforms.

Installing Node.js

You first need to download an installer for Nodejs from the nodejs.org/ website. When you run the installer for Node.js, it will also install the Node Package Manager (npm) that we'll rely on next.

Installing node-bitcoin

Now you'll need a JavaScript library that connects to Bitcoin Core and bitcoind. We'll use the node-bitcoin library. To install node-bitcoin, go to a command prompt and enter npm install bitcoin. This command invokes the (previously installed) Node Package Manager, which downloads everything in the library in a simple step.

Starting Bitcoin Core

If you haven't already, download Bitcoin Core from its official website at bitcoin.org/en/download.

Next, you need to fire up Bitcoin Core in server mode. The wallet app will open an extra socket that we'll use to connect to the wallet from our JavaScript program. To do this on Mac and Linux machines, go to the Bitcoin Core installation directory from the console and run ./Bitcoin-Ot -server. On Windows machines, open a command prompt, go to the C:Program Files (x86)Bitcoin directory, and enter bitcoin-qt.exe -server.

The first time you run Bitcoin Core in server mode, it will ask you to create a file named bitcoin.conf in a specific location and add a user ID and password to this file. Follow the directions as suggested because we'll use the userid and password shortly. Then, restart Bitcoin Core.

If you already have Bitcoin Core (previously called Bitcoin-Qt) installed but are following this tutorial for the first time, then you will need to locate your previously created bitcoin.conf file. For Windows users you might want to look in C:Users<username>AppDataRoamingBitcoinitcoin.conf. For Mac users, try /Users/< username >/Library/ApplicationSupport/Bitcoin/bitcoin. conf. For Linux users, try /home/<username>/ .bicoin/bitcoin.conf.

Gotcha for Windows users: If you try to create the bitcoin.conf file with Windows

Notepad, be aware that Notepad will (oh so helpfully) tack on a .txt extension, which Windows will also (oh so helpfully) keep completely hidden from you for your own protection (a prime example of why most hackers these days try to avoid using Windows). To circumvent this problem, put quotes around the name bitcoin.conf when you enter it into the Save As dialog in Notepad.

At this point, as is usual for a Bitcoin wallet, Bitcoin Core will take several hours downloading the blockchain before it's ready for the next steps—a perfect time for an extended lunch break!

For Mac Hackers

If you're using a Mac and are comfortable using a terminal, you can run specific commands to automatically search for, download, and install the required programs.

On Mac, download homebrew, a command-line tool that will handle the entire process for you:

For Linux Folks

If you're using flavors of Debian Linux, such as Ubuntu, you'll just use the PPA feature to install the libraries:

With a working Bitcoin Core server, we're now ready to start programming.

< Prev   CONTENTS   Next >