Q&A — Cryptography Basics — Blockchain

1.What Encryption is used in Blockchain ?

Asymmetric Encryption.

2. What is Asymmetric Encryption ?

It’s an encryption technique which uses public key and private key to encrypt /decrypt data. It’s also called Public Key cryptography.

3. How does Asymmetric Encryption works ?

Assume there are two participants A and B , where A wants to share a message to B . To encrypt and share the message over the network the following happens.

a) Both A and B creates their own public key and private key .

b) Generated public key is shared between two parties , i.e A knows B Public key and B knows A Public key.

c) When A wants to encrypt the message , then A encrypts the message with B Public key(message + B Public key) , which can be only decrypted by B with it’s private key.

4) How is Asymmetric Encryption / Public key cryptography used in Blockchain ?

Whenever a transaction is recorded between two parties A and B ,then following rule applies

Encrypted message = F(M, Privatekey (A)) will always be equal to F( Encrypted message, publickey(A)).

5) How are Public / Private Key generated ?

It’s generated via RSA Algorithm , In specific ECDSA algorithm is used to generate random prime numbers that influences decision making of RSA.

#5 Whisper and Swarm protocols in Ethereum

Whisper : 

It’s a light-weight protocol that can be used to communicate between D-Apps. It’s ideal for limited message transfer that would last long for limited time and it  also provides encryption during message transfer.

Configuring Whisper is  only available via geth console(consumable via Web3) ,but not with solidity (might come up in future ).

Usage : https://github.com/ethereum/wiki/wiki/Whisper-Overview

Swarm :

It’s a distributed de-centralized storage mechanism. Storing large piece of data is always costly in blockchain and Swarm provides a decentralized network where you could store all the large piece of information and get back the hash and push the hash to blockchain instead dumping all of the data to the blockchain. IPFS and Filecoin are other example of system like swarm.

Usage :  https://swarm-guide.readthedocs.io/en/latest/installation.html


#4 Dealing with fractions in Solidity -Smart contract

Fixed point numbers is not fully supported in solidity , but the fixed point numbers or fractions are used frequently in contract. For instance when writing a ERC20 Token,you would value 1 token to 0.5 ether and you may have to value token based upon ether paid to the contract as well return excess ethers in fractions.  In such cases converting all of your units to “wei(1 ether =1,000,000,000,000,000,000 wei)” ,would ease arithmetic operations .

Problem :

Assume a case where we want to have a payable function that accepts 0.5 ether or more and all ethers less than 0.5 should get reverted.

Note :  ufixed can only be declared and it cannot be either returned or assigned in  contract, its not an ideal solution for the problem.

Solution :

If you use uint types,you would land no where as decimals would be truncated , Instead we can use wei (denomination of ether  – 1 ether =1,000,000,000,000,000,000 wei).  All of the ethers supplied to the contract defaults to wei, we could take advantage of this and represent our value of token condition in wei as below

var reqfraction= (1 ether * minvaltoken) / mintokendenomination;











#3 Consensus algorithms in Blockchain

What is Consensus ?

Consensus is a mechanism by which all nodes participating in a network agree upon proposed value by a node.

Most common types of Consensus algorithm in blockchain :

1.Proof of work – Bitcoin

2.Proof of stake – Ethereum

3. Proof of Authority – Ropsten/ Kovan network.

4. PBFT (Byzantie Fault tolerant systems) – Consensus is achieved by selecting node leaders by voting,round robin fashions etc.. (Corda ,Hyperledger).

Explanation of each of consensus algorithm above would be shared as a separate post.


#2 Simple Structure of Blockchain

A blockchain is simply a shared ledger that holds transactions and every  transactions fits in a block  which is of following structure

a) Previous Hash  : A Hashpointer that points to previous block ,its null if its genesis block(starting block).

b)Transactions : List of transactions that pertains to current block.

This is generic structure that is used upon various blockchains like bitcoin,ethereum, but they use various parameters along with transactions which would be listed separately in other post.




#Etherum -Creating A Simle DApp with Truffle – Etherum – Web3 -Part 1

In this article we will create a sample D-App application over Etherum using Truffle Framework .

What we will build at the end of this article ?

We will build a simple contract with solidity that has 3 functions

a) IncreaseBalance



We would deploy the contract over  test Etherum network and communicate with the contract with Javascript via web3. We would make use of truffle framework to achieve the same.

 Installation/Set up :

What all you need to develop a D-App ?

Make sure you have node(npm) installed.

1.Get truffle via command line.

npm install truffle

2. Get Etherum test client to deploy our contract locally.This is more of a test newtork

npm install -g ethereumjs-testrpc

3. Get Web3 and truffle contract to communicate with our deployed contract with javascript.

npm install truffle-contract


We are all set to write our first contract with truffle and I would be using Visual code as an IDE to write  my contract. We will see in the next article to compile,migrate and deploy our contract to test etherum network.


#BlockChain-Etherum -DAPPS- Accessing Functions between contracts in Solidity.

I am using Remix-Solidity IDE for browser – https://ethereum.github.io/browser-solidity and have started learning solidity from udemy course- Ravinder Deol

In this post ,I will share how to access function between two contracts using solidity over etherum.

Step 1 : Creating a simple contract with Get/Set Function

Lets create a simple contract with a get and set function :

contract SampleContract1
uint myvar;
function SetVar(uint varvalue){
myvar = varvalue;
function GetVar() constant returns(uint)
return myvar;

Step 2 : Creating contract with constructor function that consumes other contracts

Lets create another contract where we got use function of contract1. In this contract we create constructor where we pass the adress of contract1.

contract SampleContract2
SampleContract1 instanceofsc1;
function SampleContract2(address addressofsc1) // constructor



Step 3 : Accessing Function from other contract 

After we pass the address of contract1,we would be able to access the functions directly in contract2.

contract SampleContract2
SampleContract1 instanceofsc1;
function SampleContract2(address addressofsc1)

function GetvarofSc1() constant returns(uint)
return instanceofsc1.GetVar();

Output in Solidity Browser :



Using TopShelf for windows service

Windows service runs continuously in the background and more often used as a back-end job. Its a pain if you want to debug it,even though you use logger to capture all events.we either attach the service as a process and debug it. But TopShelf lets you eliminate all of these and allows you to debug like console  application.

Topshelf has a start and stop method just like a service and also an separate command to install windows service with ease.

DocLink : https://topshelf.readthedocs.io/en/latest/installation/index.html


A simple use case with OData Web Api

Scenario :

Assume a case where we have an Employee object and we have an nullable  integer named Age (int? Age).  There are two cases we want to achieve,the first case is to not update the Age property and we could achieve this by passing null value to Age and ignoring the Age Property while saving and second case is to reset the Age i.e Accept whatever is passed by Age Property and save it in db.

Solution :

We want to only update properties of Employee object that is being passed to the controller and we call it as “partial update”(PATCH).  In this case we update only what is being passed and leave the system unchanged. Microsoft provides OData for partial update.

  1. Get Microsoft ASP.NET Web API OData package from Nuget.
  2. In your controller  HTTP verb Method,you could make use of Delta<T>  to map your Employee object and this will take care of partial update with Patch Method provided by ODATA .

public ApiResponse<Statuscode> Patch(Delta<Employee>; employee)


var employeebyId == dbcontext.FirstorDefault(x=>x.EmpId ==employee.EmpId);

if(employee !=null)


employee.Patch(employeebyId ); // Partial update by ODATA