Blockchain in Swift
Simple blockchain implementation in #Swift
The source code for is available here.
Before we start, we should have a fair understanding of Swift programming language, know how HTTP requests work and an HTTP Client, like Postman or cURL.
What is a blockchain?
A blockchain is a distributed and decentralized way of storing data such as transactions, and that is publicly shared across multiple nodes. As the name suggests Block-Chain, in a blockchain, multiple such blocks of transactions are chained together using hashes. The hash of the one a block is dependent on the hash of the previous block in the chain.
I will be using Swift Package Manager to manage the dependencies in this project. You can skip this step if you are using Cocoapods or some other dependency manager. All you need is to include the following three libraries.
SwiftyRequest for HTTP requests and
CryptoSwift for hashing.
// Create a new directory and follow these steps.
$ mkdir Swift-Blockchain && cd Swift-Blockchain
$ swift package init --type=executable
# Open the
Pacakge.swift and make the changes so your file looks similar to the one below.
// Fetch the dependencies we mentioned in the '
$ swift package resolve
// Generate Xcode Project.
$ swift package generate-xcodeproj
// Build the project.
$ swift build
Building the Blockchain
Blockchainclass will store the chain and the transactions as well. We will be implementing methods to
mine a block, find
proof of work,
hash and a few others as well. Below is the structure of our Blockchain class.
Each Block in a blockchain has an index, a timestamp, a list of transactions, a proof, and the hash of the previous Block. Here’s how a block looks like.
Mining in Bitcoin
Mining, in the context of blockchain technology, is the process of adding transactions. Our
newTransaction(:sender, :recipient, :amount), method creates a new transaction and appends it to the list of existing transactions.
Calculating “proof of work (PoW)” and “Hashing”
A proof of work is a piece of data which is difficult (costly, time-consuming) to produce but easy for others to verify. In Blockchain, this algorithm is used to confirm transactions and produce new blocks to the chain. With PoW, miners compete against each other to complete transactions on the network and get rewarded.
The first block (Genesis Block) of a blockchain is created in the initializer of our
Blockchain class. And our
newBlock(:previous_hash, :proof) method takes care of creating a new block.
# Find a number p that when hashed with the previous block’s solution a hash with 4 leading
0s is produced.
Bitcoin uses the Hashcash proof of work system.
Blockchain class to include the methods to create a distributed architecture by allowing multiple nodes on the network. We will be adding an HTTP server after this to deploy our blockchain over the network.
# Creating new transactions………..………….…
# Mining new blocks..….…………………………
/mine# Fetching the blockchain.….….….….….….…...
/chain# Registering new nodes in the network….…..…
/nodes/register# Verifying the valid chain….….….….….….…...
At this point, our server is running on
Fetching the blockchain
Adding a new Transaction
"message": "Transaction will be added to Block 2"
Mining a new Block
"message": "New Block Foreged",
Registering a new Node in the Network
"message": "New nodes have been added",
Verifying the Blockchain
"message": "Our chain is authoritative",
"message": "Our chain was replaced",
If you use Postman, you can download the postman collection here.
If you missed, the source code for “Siwft-Blockchain” is available here.
This article is a swift port to “Learn Blockchains by Building One”
Many thanks to Daniel van Flymen for writing “Learning blockchain By Building One”, it is such an awesome article to read. I hope you have enjoyed this post as much as I’ve enjoyed writing it. As always, your suggestions are most welcome.