DailyJS

Bitcoins and JavaScript

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

node modules security

Bitcoins and JavaScript

Posted by Alex R. Young on .
Featured

node modules security

Bitcoins and JavaScript

Posted by Alex R. Young on .

Bitcoin

Bitcoin might seem like a crazy cryptocurrency, mainly because it is, but it's also becoming more widely adopted. I find it fascinating because it feels like I'm finally living in a 90s Neal Stephenson or William Gibson novel, although the possibility of making online payments outside of the standard merchant account/bank model has its appeal.

I've been researching the state of bitcoin payments in JavaScript to see how easy it is to set up. The first thing I found was CoinWidget -- this allows you to accept donations using client-side JavaScript. I've added a widget to DailyJS in case you want to donate, but it should be easy enough to add to your own sites as well.

Address validation

A bitcoin address includes a four byte checksum, which means addresses can be validated without talking to any servers. The bitcoin-address module (GitHub: shtylman / bitcoin-address, npm: bitcoin-address) by Roman Shtylman implements the algorithm: a double SHA-256 digest of the 21 bytes before the checksum is calculated to validate the address.

var btcAddr = require('bitcoin-address');  
btcAddr.validate('1Gtov23WTQPbj4c6dMaXnXxbvFKc87Lutb');  
// true

Addresses are encoded with base 58, so Roman's module includes his own decoding library. He also uses Node Buffer objects to work with the decoded data. Node's crypto core module is used to create SHA-256 hashes.

Wallets

If you install a wallet like Bitcoin-QT, you should get bitcoind running in the background. This has an optional JSON API. There are several npm modules for this API, like: bitcoin. This allows you to query the daemon for transactions, account balances, and you can even create backups of wallets. There's a full API call list on the Bitcoin wiki.

Accepting payments

There's no easy way to accept payments for web-based transactions, because you really need to talk to a bitcoin wallet to see if a customer's payment has been made. I've seen people hosting Java-based wallets on Heroku, and wallets usually have APIs that we can talk to from Node, but this isn't exactly ideal.

BIPS

That's where bitcoin merchants come in. They handle all the messy stuff so you can keep your web application code simple. Popular merchants with sensible APIs include BIPS and Coinbase.

This is the BIPS REST API (PDF), and the Coinbase API. There are Coinbase modules on npm and a BIPS API library.

Exchanges

Exchanges, like the infamous Mt.Gox, also have APIs. Through exchanges you can track interesting information about bitcoins, particularly exchange rate values, and some even allow trades to be made. There's an Mt.Gox module on npm, which is light on documentation: check out MtGox/API for a general overview.

If you've written any bitcoin-related Node or client-side projects, send them in!