Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Bitcoin

You're reading from   Learning Bitcoin Embrace the new world of fiance by leveraging the power of crypto-currencies using Bitcoin and the Blockchain

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781785287305
Length 236 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (10) Chapters Close

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image