Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Hands-On Bitcoin Programming with Python
Hands-On Bitcoin Programming with Python

Hands-On Bitcoin Programming with Python: Build powerful online payment centric applications with Python

eBook
€10.99 €15.99
Paperback
€19.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Hands-On Bitcoin Programming with Python

Programming Bitcoin and Blockchain with Python

This chapter focuses on using Raspberry Pi Bitcoin tools to program bitcoin with Python and interacting with Blockchain APIs programmatically. The reader will also get a general idea of the process of mining bitcoin and its initial stages.

In this chapter, we will learn about the following topics:

  • Programming bitcoin using Python
  • Creating a multisignature bitcoin address
  • Blockchain API programming with Python
  • Installing the Blockchain.info
  • Python library
  • Learning to mine bitcoin
  • How to mine bitcoin
  • Increasing difficulties in mining bitcoins

Programming bitcoin with Python

In this section, we are going to introduce the following topics:

  • The Raspberry Pi Bitcoin tools library and how to start using it
  • How to generate private keys and public keys
  • How to create a simple bitcoin address from the private keys and public keys you generated
To get started with bitcoin using Python, you must install Python 3.x and the bitcoin Python library called Pi Bitcoin tools in the system.

Pi Bitcoin tools library

To install the Pi Bitcoin tools library, open the command-line program and execute the following command:

pip install bitcoin
The best thing about this library is that it does not need to have a bitcoin node on your computer in order for you to start using it.

This library...

Creating a multisignature bitcoin address

A multisignature address is an address that is associated with more than one private key; therefore, we need to create three private keys.

Go through the following steps to create a multisignature bitcoin address:

  1. Create three private keys:
#!/usr/bin/env python
'''
Title - Create multi-signature address

This program demonstrates the creation of
Multi-signature bitcoin address.
'''
# import bitcoin
from bitcoin import *

# Create Private Keys
my_private_key1 = random_key()
my_private_key2 = random_key()
my_private_key3 = random_key()

print("Private Key1: %s" % my_private_key1)
print("Private Key2: %s" % my_private_key2)
print("Private Key3: %s" % my_private_key3)
print('\n')
  1. Create three public keys from those private keys using the privtopub function:
# Create Public keys
my_public_key1...

Blockchain API programming with Python

Blockchain.info is one of the most popular blockchain and bitcoin network explorers and wallet providers. From the web, you can view the block level and see all the transactions that have happened. For example, by going to this particular block—that is, Block #536081—you can see all the transactions, as well as some other information, as shown in the following screenshot:

The following screenshot shows the statistical data (DATA | Stats). This is great, and useful as well; however, for a developer building applications or performing analysis based on this data, it is important to get this data programmatically:

The following screenshot shows the market data (DATA | Markets):

Installing the Blockchain.info Python library

The following are the steps to install the blockchain Python library:

  1. Open the command-line program on your computer.
  2. Run the pip install blockchain command to install the blockchain library.

The following screenshot shows the installation of bitcoin:

Getting bitcoin exchange rates from Blockchain.info

The following steps shows the method for bitcoin exchange rates:

  1. First, import the exchangerates classes from the blockchain library:
#!/usr/bin/env python

# import blockchain library
from blockchain import exchangerates
  1. Exchange rates define a get_ticker method, which returns the exchange rates data in a dictionary object. Call this method and save the resulting object. The...

Learning to mine bitcoin

Some features of bitcoin mining are as follows:

  • Bitcoin mining is the process of adding bitcoin transactional data to bitcoin's global public ledger of past transactions. Each bitcoin miner gets involved with all the other miners in order to gather the outstanding transactions into a block by dealing with specialized analytical and arithmetical problems.
  • In order to gain accuracy and solve the problems, bitcoin miners gain all of the transactions they process.
  • In addition to transaction fees, miners also receive an additional reward for each block they mine. Anyone can participate in bitcoin mining by running a computer program. In addition to running on traditional computers, some companies have designed specialized bitcoin mining hardware that can process transactions and build blocks much more quickly.

One can choose to mine bitcoin in the cloud...

How to mine bitcoin

There is also plenty of bitcoin mining software available, which can run on any machine. However, it may not be as efficient anymore. For example, let's go to http://www.bitcoinx.com/bitcoin-mining-software/ for a long list of such software. They can run on all kinds of operating systems: Windows, Linux, and macOS. There are UI-based bitcoin miners, as well as command-line-based bitcoin miners—for example, Pyminer, which is an implementation in Python.

Increasing difficulty in mining bitcoin

Because of the increasing competition and difficulties, there are many factors that must be borne in mind when mining bitcoin, as shown in the following list:

  • Bitcoins are getting more expensive day by day because of competition
  • Many supercomputers across the globe are in competition to mine the next block and bitcoin
  • As the number of bitcoin miners has increased, it has become more problematic and overpriced to start mining new bitcoin

For example, the following screenshot shows a chart of how the difficulty of bitcoin mining has been increasing; for more information refer to https://bitcoinwisdom.com/bitcoin/difficulty. This particular chart shows values from the last two months. This recent trend reflects the increasing difficulty that began when bitcoin was first created:

Summary

In this chapter, we learned how to get started programming bitcoin with Python. We explored Blockchain.info API programming with Python to get statistics and other bitcoin market data.

We also learned how to get started with mining bitcoin. We looked at the various ways to mine bitcoin, and we learned about why bitcoin mining may not be for everybody because of its increasing competition and difficulty.

In the next chapter, we will learn how to start running bitcoin programmatically by way of accepting bitcoin on your website, running API-based microservices, or by building a bitcoin trading bot.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Build Bitcoin applications in Python with the help of simple examples
  • Mine Bitcoins, program Bitcoin-enabled APIs and transaction graphs, and build trading bots
  • Analyze Bitcoin transactions and produce visualizations using Python data analysis tools

Description

Bitcoin is a cryptocurrency that’s changing the face of online payments. Hands-On Bitcoin Programming with Python teaches you to build software applications for mining and creating Bitcoins using Python. This book starts with the basics of both Bitcoin and blockchain and gives you an overview of these inherent concepts by showing you how to build Bitcoin-driven applications with Python. Packed with clear instructions and practical examples, you will learn to understand simple Python coding examples that work with this cryptocurrency. By the end of the book, you’ll be able to mine Bitcoins, accept Bitcoin payments on the app, and work with the basics of blockchain technology to create simply distributed ledgers.

Who is this book for?

Hands-On Bitcoin Programming with Python consists of examples that will teach you to build your own Bitcoin application. You will learn to write scripts, build software for mining, and create Bitcoins using Python. Anyone with prior Python experience, who wants to explore Python Bitcoin programming and start building Bitcoin-driven Python apps, will find this book useful.

What you will learn

  • Master the Bitcoin APIs in Python to manipulate Bitcoin from your Python apps
  • Build your own Bitcoin trading bots to buy Bitcoins at a lower price and sell them at a higher price
  • Write scripts to process Bitcoin payments through a website or app
  • Develop software for Bitcoin mining to create Bitcoin currency on your own computer hardware
  • Create your own keys, addresses, and wallets in Python code
  • Write software to analyze Bitcoin transactions and produce reports, graphs, and other visualizations

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 30, 2018
Length: 90 pages
Edition : 1st
Language : English
ISBN-13 : 9781789533163
Category :
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Aug 30, 2018
Length: 90 pages
Edition : 1st
Language : English
ISBN-13 : 9781789533163
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 98.97
Mastering Blockchain
€41.99
Blockchain By Example
€36.99
Hands-On Bitcoin Programming with Python
€19.99
Total 98.97 Stars icon

Table of Contents

5 Chapters
Getting Started with Bitcoin Chevron down icon Chevron up icon
Programming Bitcoin and Blockchain with Python Chevron down icon Chevron up icon
Earning Bitcoin Programmatically Chevron down icon Chevron up icon
Bitcoin Data Analysis Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(2 Ratings)
5 star 50%
4 star 0%
3 star 0%
2 star 0%
1 star 50%
Andrew Bricker May 12, 2020
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Don’t waste your money. This book teaches you nothing about programming bitcoin or the bitcoin API. Truly a complete wast of money and time.
Amazon Verified review Amazon
Sonal Jayaprakash Oct 29, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The author, Harish Garg, has served complex concepts of Bitcoin and Blockchain – two transformational technologies of the modern era – via an interesting read that is a great balance between technical depth and its application into the world. The book is time well spent and a recommended read for anyone looking to simply explore or actually apply Bitcoin to their web-based business.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.