Basic Implementation of Blockchain with Salesforce Apex

Reading Time: 3 minutes

Screen Shot 2018-08-28 at 12.36.33 AM

Basic Implementation of Blockchain with Salesforce Apex

I have been seeing a lot of buzz about blockchain. I have gone through a few of the blogs and basic programmes in NodeJs. Truly, I still know about it very less. But my interest of salesforce brings me to write the Basic blockchain program at the apex. The blockchain is, still Interstellar and Inception for me.


Let’s Code:

1. Create a class called Block.
This class will be used to create new blocks in the chain.

public Block(integer index, string data, string prevHash)
The constructor of the class will have three parameters.
a. Index: Index of the block.
b. Data: Data for the Block.
c. prevHash: Hash code of the previous block.
private long generateTimeStamp()
This method will generate timestamp for the block.
public string getHash(){
    Blob dataToEncrypt = Blob.valueOf( + this.prevHash + this.index + this.timestamp );
    Blob encrypted = crypto.generateMac('HmacSHA256', dataToEncrypt, Blob.valueOf('key'));
    return EncodingUtil.base64Encode(encrypted);
This method creates hash code from data + prevHash + index + timestamp. 
I am using the generateMac method from Crypto class. 
I am selecting HmacSHA256 in the algorithm. 
You can choose any Private key to generate a message authentication code (Mac).

2. Create a Class called Blockchain.
This class will be used to create the new blocks in the chain and Validate chain.

public void addBlock(string data){
   //Defining Index from chain size
   integer index = chain.size();

   //Checking for previous block's hash, If it is a the first block then it set previous block as '0'
   string prevHash = chain.isEmpty()==true ? '0' : chain.get(chain.size()-1).hash;

   //Creating a block from index, data and previous block's hash
   Block newBlock = new Block(index, data, prevHash);

   //Adding the new block in the chain
This method will add a new block to the chain with the given data.
public boolean isChainValid()
This method is checking for the valid chain. 
if the chain is not valid then it will return false, 
if it is valid then it will return true.
public boolean isBlockValid(integer index){
  //Checking block's hash with run time calculated a hash from the block
  //If someone has changed the block data then getHash will return a new data
  //This will not be same as the block's Hash
  if(chain[index].hash != chain[index].getHash() ){
    return false;

  //If the index is greater than zero then it is also checking the block's prevHash from previous block's hash.
  //If someone changed the data and but still he needs to change the hash as well
  //Hash is generated by the combination of data+index+timestap+prevHash.
  //If someone wants to make the hash correct, he has to change the prevHash as well
  //Now the block seems good as all the needed values are changed
  //But now this line will check prevHash from previous index's block hash.
  //It will not be the same. Now it sends the false in return.
  if(index > 0 && chain[index].prevHash != chain[index-1].hash ){
    return false;
  return true;
This method is checking for the valid block. 
if the block is not valid then it will return false, 
if it is valid then it will return true.
As you have see that if someone wants to change a block then the person needs to change the entire chain.

Testing Time:

I hope it cleared the basic concept of Blockchain.

Happy BlockChain


Crypto Class – Salesforce
Apex Code (Github)
Unmanaged Package 

Leave a Reply