Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
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
€13.99 €15.99
Paperback
€19.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781789537000
Category :
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Aug 30, 2018
Length: 90 pages
Edition : 1st
Language : English
ISBN-13 : 9781789537000
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
Hands-On Bitcoin Programming with Python
€19.99
Blockchain By Example
€36.99
Mastering Blockchain
€41.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%
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
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
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.