Programming common Bitcoin operations
Let's start by introducing bitcoinjs-lib
by working with private keys and public addresses. The module provides us with some useful components for working with elliptic curve keys.
To generate a new private key, we'll start Node.js
from our command line and enter the following commands:
~ node > var bitcoin = require('bitcoinjs-lib') > var private_key = bitcoin.ECKey.makeRandom() > console.log(private_key.toWIF()) KzgRK4nN6bcb5iQN8tLL85U5anc84uH7G9KtsZuqU23h5fN7Z6v4
From our example, you can see that we're importing the bitcoinjs library and calling ECKey.makeRandom()
, which returns a random private key. We store an object
that represents the key in a variable called private_key
. On the third line, we print out the key in WIF format to the console. The result is a valid key from which we can compute a public address from:
> console.log(key.pub.getAddress().toString()) 149TUxVkJzowbNDwExc34t4EfyNgAL1pco
Note
WIF stands for Wallet Import Format...