In this blog, I will share with you how you can build a simple blockchain application. But before that let’s get an idea about what is blockchain and one of its major applications.
What is Blockchain?
Blockchain is the technology at the core of digital currencies. It’s a digital ledger of transactions spread across a network of computers. Each node in the network can securely change the ledger without the need for a central authority. This is how this independent system is able to operate without relying on banks or any other middlemen.
The blockchain is receiving a lot of recognition as the technology poised to disrupt many industries especially the Financial Services. But its potential is far-reaching and has implications for many industries.
Building the application Catena – SQL on a blockchain can act as the database for front-end of the application for which we can use simple HTML and CSS and for the backend we can use PHP. To build the application you can follow the below-mentioned steps.
First, we shall install Catena on the local machine and then set up the server to run the application locally. I have used Ubuntu and for Windows, users can use Docker. All the commands should be executed by a superuser.
apt install clang build-essential libicu-dev libcurl4-openssl-dev openssl libssl-dev
This command ensures required libraries are present, now clone the catena to the local machine.
git clone https://github.com/pixelspark/catena.git catena
After this go inside the catena folder
cd catena
Now check if Swift is installed in the system and the path is given. To check whether Swift 4 is present or not type swift build.
Swift build
If swift build gives an error, then it means Swift is not installed. You can then download Swift 4 and install it in the Catena folder. After installing the Swift again run the swift build command.
Building the web client
Catena comprises of a web client, resources from the resources folder where a built-in version of the web client is included. To build from the scratch, first, install the required build tools, then use gulp to compile and bundle files.
cd Resources/
Install gulp if it is not installed already
npm install gulp
After installation run the gulp to compile and bundle files.
Running gulp natively
To run the Catena natively, open the terminal in the Catena folder. The following command starts Catena and initializes a new chain (replace 'debug' with 'release' after building a release version):
./.build/debug/Catena -p 8338 -m -i -s 'my seed string'
As seen above, we can connect Catena to the PQ interface i.e. we can use PostgreSQL command line to execute query directly to the Catena. We will see now how to use Catena.
Web client
The web client of Catena runs on 127.0.0.1:8338 here the port number is the same as you have mentioned in the command to build Catena. When you run the Catena, the below screen is shown.
In this case, an identity is already created if no identity is present, you can go to the identity tab and create an identity.
When you create an identity, it is advisable to copy the public key and the secret key, as mentioned in the start Catena is SQL on blockchain so the public key is the username and the secret key (private key) is the password. You can also grant permission to the user of the database using grants as blockchain is a distributed system but for this demo, we will be using a single node application.
Now create a database (you can do this step using PostgresSQL CLI also)
When the database is created, the information is stored in the blockchain.
SQL interface
The (private) SQL interface is available on port 8339 (by default). If you set a different HTTP port (using the '-p' command line switch), the SQL interface will assume that port+1. You can connect to the SQL interface using the PostgreSQL (if PostgreSQL is not installed, first install PostgreSQL) command line client:
psql -h localhost -p 8339 -U <publicKey> <databaseName>
When this command is running, it will prompt for the password. Use the secret key as a password.
Statements
The following statement types are supported in Catena.
Create Database
Creates a database and makes the invoker of the statement the owner of the database. If the database already exists, the transaction is rolled back. Note: the 'current database' for the transaction must be the database that is about to be created.
Drop Database
Removes the indicated database, but only if there are no tables left in the database. Only the owner of the database can execute this statement.
Select
Insert
Update
Delete
Create Table
Drop Table
Now that web client is running and the queries are executed using the PostgresSQL CLI it is now the time to connect Catena with PHP.
Connecting Catena with PHP
First to run PHP we need to install Apache. When the Apache is installed go to /var/www/html/ and create a new PHP file connection.php this will be used to connect to Catena for connection PostgreSQL wire-protocol will be used. The code for connection.php is
$host = "host = 127.0.0.1";
$port = "port = 8339";
$dbname = "dbname = blockchain";
$credentials = "user = 3xaeMnTqbKw5J5513yun5kBYSoofHuEiHtgUqxU477jygHTMxuc
password=7MSfWr6QMrdk9HFntkskf5MFrmcNaJbHY8u4VCbiSE3ETeuNxz7muChaCN2YwkJBFTQkh
2ohH1yVnXLJZKbNbuZEfaJnm";
Here the user is the public key of the identity and the password is the secret key. Now that the database is connected, you can connect it to the frontend and take input from the frontend and store it in the blockchain. You can now easily implement any of the use cases of the blockchain technology.
You can list your suggestions and idea in the comment section.