Reader small image

You're reading from  Blockchain By Example

Product typeBook
Published inNov 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788475686
Edition1st Edition
Languages
Concepts
Right arrow
Authors (3):
Bellaj Badr
Bellaj Badr
author image
Bellaj Badr

Bellaj Badr is an experienced security and software engineer who loves blockchain with a passion. Currently, he is the CTO at Mchain, a blockchain start-up that develops blockchain solutions for companies. Alongside his role as CTO, he acts as technical consultant, offering strategic and technical consulting to many companies worldwide. Aside from this, he is involved in many blockchain projects involving the establishment of new blockchain business-oriented protocols. Badr is a frequent speaker at developer conferences and is father to two angels.
Read more about Bellaj Badr

Richard Horrocks
Richard Horrocks
author image
Richard Horrocks

Richard Horrocks is a freelance Ethereum and full-stack developer based in the UK, and holds a BA and MSc in natural sciences from the University of Cambridge. He worked for many years as a technical lead for Cisco Systems, where he worked on the operating systems of carrier-grade routing hardware, before leaving the world of IT to work as an English teacher. The advent of cryptocurrency piqued his interest sufficiently to lead him back to IT, and, since 2015, he has been working with Ethereum and other cryptocurrencies. His specialist interests are cryptoeconomics and incentive layers, with a particular focus on mechanism design and token engineering. When not in front of a computer, he enjoys yoga and falling off motorbikes.
Read more about Richard Horrocks

Xun (Brian) Wu
Xun (Brian) Wu
author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu

View More author details
Right arrow

Building a Bitcoin Payment System

The previous chapter was an excellent starting point for understanding the blockchain and learning about bitcoin. In fact, we covered many basic concepts and elementary operations, such as signing, sending raw transactions, and storing data in the blockchain. In this chapter, we'll continue to explore the blockchain, firstly by building a customer-friendly payment system based on the Bitcoin payment protocol, and secondly, by building our first smart contract in the bitcoin system.

The end goal of this chapter is to help you acquire the necessary technical background to understand bitcoin's mechanics and build your first bitcoin applications using very common languages such as JavaScript and Java. If you're an experienced programmer, most likely you are familiar with one of these technologies; if not, I recommend you spend time...

What is Bitcoin?

As outlined in the previous chapter, Bitcoin is a peer-to-peer electronic cash system based on blockchain technology. Technically speaking, Bitcoin is a protocol maintaining the blockchain data structure and ensuring consensus between different network parties (senders, recipients, miners, and so on). This protocol defines the ruleset for validating transactions, Bitcoin mining, and avoiding counterfeiting or double spending.

Bitcoin is referenced as a cryptocurrency because it uses cryptography to control the creation and transfer of money. Specifically, it uses digital signatures (based on ECDSA public key encryption) to process and verify the transactions, and cryptographic hash functions (SHA-256) to secure the blockchain (integrity, mining process, and so on).

This book's scope isn't about presenting the Bitcoin protocol in fine detail; instead...

Getting started with Bitcoin

To get started using Bitcoin, there are plenty of Bitcoin clients and different implementations of the Bitcoin protocol. As we saw in Chapter 1, Say Hello to Blockchain, the commonly used Bitcoin client is Bitcoin Core, which is maintained by the Bitcoin Core team representing the C++ implementation of the Bitcoin protocol and the continuity of the Satoshi client. I'll assume that you have already installed this client and have become familiar with it.

Setting up a Regtest environment

For learning or testing purposes, it's safer and cheaper to use Bitcoin's test network --testnet (for more information, refer to https://bitcoin.org/en/glossary/testnet) or regression test mode --regtest...

Building a payment gateway

If you have never developed bitcoin applications before, this section is the best place to start. We plan here to integrate bitcoin payments into an online e-commerce website. The idea is to create a payment option for the e-commerce customers to pay simply by clicking a direct bitcoin payment URL or scanning a QR code, which opens a payment form with payment details in the wallet, making the payment process very straightforward and easy. Moreover, such a payment option avoids customers filling in any forms or providing personal data.

Project description

This project generally represents how to implement the bitcoin payment protocol proposed in BIP 70 in order to build an online payment gateway....

Payment protocol using BitcoinJ

If you're a Java developer who would like to get started with building bitcoin applications, this part will be your best starting point. We will build a Java client that implements the payment protocol (BIP 70) using BitcoinJ, and interacts with our payment Node.js server.

Prerequisites

The first thing you should do to follow this guide is to set up your own Java development environment. This walkthrough assumes that the latest version of the following elements is installed and running on your platform of choice:

You can refer to their official documentation in order to download and install them properly.

...

Bitcoin smart contracts

In the previous chapter, we briefly introduced the smart contract concept as self-executing programs that define a set of clauses stored on the blockchain. Although bitcoin was the first blockchain to propose a scripting language (limited for security reasons) to control funds, the smart contract concept is usually associated with the Ethereum blockchain, which provides Turing-complete smart contract languages. However, to redress the balance, many projects were initiated, such as Rootstock, Counterparty, and Ivy, to enable building and running advanced smart contracts on bitcoin. Welcome to bitcoin 2.0.

What is Rootstock?

Rootstock is a project to bring smart contracts (Turing-complete smart contracts...

Summary

Congratulations! You are now qualified to write production applications using the most popular blockchain – bitcoin – whether as a payment system or as a platform for running advanced smart contracts.

In this walkthrough, we have consolidated our understanding of bitcoin and dissected in depth its underlying scripting mechanism. More importantly, we now know how to integrate bitcoin payments into applications and websites using the bitcore library. Lastly, we discovered how to use an RSK network for deploying and interacting with Solidity smart contracts in a bitcoin environment.

Stay tuned for the next chapter, where we will learn something very exciting: we will discover how to build our own cryptcurrency using bitcoin's code base. For sure, it will be a golden opportunity for you to deepen your understanding of bitcoin's protocol and the blockchain...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Blockchain By Example
Published in: Nov 2018Publisher: PacktISBN-13: 9781788475686
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.
undefined
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 $15.99/month. Cancel anytime

Authors (3)

author image
Bellaj Badr

Bellaj Badr is an experienced security and software engineer who loves blockchain with a passion. Currently, he is the CTO at Mchain, a blockchain start-up that develops blockchain solutions for companies. Alongside his role as CTO, he acts as technical consultant, offering strategic and technical consulting to many companies worldwide. Aside from this, he is involved in many blockchain projects involving the establishment of new blockchain business-oriented protocols. Badr is a frequent speaker at developer conferences and is father to two angels.
Read more about Bellaj Badr

author image
Richard Horrocks

Richard Horrocks is a freelance Ethereum and full-stack developer based in the UK, and holds a BA and MSc in natural sciences from the University of Cambridge. He worked for many years as a technical lead for Cisco Systems, where he worked on the operating systems of carrier-grade routing hardware, before leaving the world of IT to work as an English teacher. The advent of cryptocurrency piqued his interest sufficiently to lead him back to IT, and, since 2015, he has been working with Ethereum and other cryptocurrencies. His specialist interests are cryptoeconomics and incentive layers, with a particular focus on mechanism design and token engineering. When not in front of a computer, he enjoys yoga and falling off motorbikes.
Read more about Richard Horrocks

author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu