Introduction of Blockchain
Blockchain is a technology that uses cryptography to create a secure linkage between records.
Blockchain was first described in 1991 by Stuart Haber and W. Scott Stornetta.
Blockchain has nodes, and these nodes are called Blocks. Each block contains the hash, hash of the previous block, timestamp and the data. Tempering with blockchain is not easy. If a block has been corrupted than the chain gets corrupted.
The Concept of Blockchain
A Block is similar to the linked list but with more complexity. A block’s hash is generated by the data from the block. But it is simple to generate the hash from data. To prevent this it also contains the hash of the previous block. so, if any data is touched then block get invalid and the chain as well. If someone wants to hack the blockchain then hacker needs to update all the blocks. It is not that simple and it is time-consuming as well.

What is Salesforce Blockchain
Salesforce Blockchain is a new way to create, secure and share data from applications with a network of partners. This is because Salesforce Blockchain provides the infrastructure of a distributed ledger(A distributed ledger is a database that is consensually shared and synchronized across multiple sites) and adds on the power of Salesforce metadata. A partner can securely publish and independently verify records on Salesforce Blockchain from whatever system they use.
Benefits that you can get from aligning Blockchain with Salesforce
A). The Increase of CRM Data Security
That is the point at which your organization can get demonstrated information that is ensured by blockchain innovation. It is a huge element while utilizing cloud arrangements.
B). The Ability to Be Closer to Your Customers and Grasp their Demands
Blockchain can give you a top to the bottom outline of the prospects and clients, break down their requests and choose where to convey the business assets. That is the reason for applying the blockchain innovation inside CRM can support consumer loyalty and give you an inventive apparatus for dealing with the business procedure.
C). The Customer Experience Management Improvement
That is how you will guarantee the customer’s information security and deal with the customer’s experience in any case of the business.
Consequently, the blockchain together with CRM can expand the security, quality, and speed of your client administration higher than ever.
Build Your First Blockchain with Salesforce Apex
1.The first step is to create a class called Block. This class will further be utilized to create new blocks in the chain.
public class Block
{
public integer index;
public long timestamp;
public string data;
public string prevHash;
public string hash;
//The constructor of the class will have three parameters.index,data and prevhash
public Block( integer index,string data,string prevHash)
{
this.index=index;
this.timestamp=generateTimeStamp();
this.data=data;
this.prevHash=prevHash;
this.hash = this.getHash();
}
//This method will generate timestamp for the block.
private long generateTimeStamp()
{
return DateTime.Now().getTime();
}
//This method creates hash code from data + prevHash + index + timestamp.
public string getHash()
{
Blob dataToEncrypt = Blob.valueOf( this.data + this.prevHash + this.index + this.timestamp);
//using the generateMac method from Crypto class,selecting HmacSHA256 in the algorithm,choosing Private key to generate a message authentication code (Mac).
Blob encrypted = crypto.generateMac('HmacSHA256', dataToEncrypt, Blob.valueOf('key'));
return EncodingUtil.base64Encode(encrypted);
}
}
2. Create a Class called Blockchain. This class will be used to create the new blocks in the chain and Validate chain. This class method will check for the valid block. if the block is not valid then it will return false or it will return true
public class BlockChain
{
public List<Block> chain;
public BlockChain()
{
chain = new List<Block>();
}
public void addBlock(string data)
{
//Defining Index from chain size
integer index = chain.size();
//Checking for previous block's hash, If it is 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
chain.add(newBlock);
}
//This method is checking for the valid chain.
public boolean isChainValid(){
for(integer i=0; i < chain.size() ; i++ ){
//if the chain is not valid then it will return false or it will return true
if( !isBlockValid(i) ){
return false;
}
}
return true;
}
//Checking block's hash with run time calculated a hash from the block
public boolean isBlockValid(integer index){
//If someone has changed the block data then getHash will return a new data and 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(index > 0 && chain[index].prevHash != chain[index-1].hash ){
return false;
}
return true;
}
}
Test Class
@isTest
public class TestBlockchain{
//Testing Blockchain
@isTest
public static void testBlockChain(){
/**Data Setup**/
//Creating Instance of Blockchain
BlockChain bChain = new BlockChain();
//addBlock method take data as the string
//Changing data to the string with the JSON format
//Adding the first block to the chain
bChain.addBlock( json.serialize( new BCTestDataWrapper('Iron Man', '2334343434') ) );
//Adding the second block to the chain
bChain.addBlock( json.serialize( new BCTestDataWrapper('Thor', '34343434') ) );
/**Positive Testing**/
//isChainValid will return true, as no data has been modified from block
system.assertEquals(true, bChain.isChainValid() );
//Print Blockchain
system.debug('-Blockchain Data Before Modifying--'+Json.serialize( bChain.chain));
system.debug('-Blockchain Data Before Modifying--' +bChain.isChainValid());
/**Negative Testing**/
//Now updating the 0 index's block
BCTestDataWrapper tData = (BCTestDataWrapper)JSON.deserialize(bChain.chain[0].data, BCTestDataWrapper.class);
tData.name = 'Thanos';
bChain.chain[0].data = json.serialize(tData);
//isChainValid will return false, as the data has been modified from block
system.assertEquals(false, bChain.isChainValid() );
//Print Blockchain
system.debug('-Blockchain Data After Modifying--'+Json.serialize( bChain.chain) );
system.debug('-Blockchain Data After Modifying--' +bChain.isChainValid());
}
Test Data Wrapper Class
public class BCTestDataWrapper{
public string name;
public string accountNumber;
public BCTestDataWrapper(string name, string accountNumber){
this.name = name;
this.accountNumber = accountNumber;
}
}
Output
On the execution of the above code, the obtained output is as follows:
isChainValid will return true, as no data has been modified from block
–Blockchain Data Before Modifying–[{“timestamp”:1561687116,”prevHash”:”0″,”index”:0,”hash”:”GCS4SGVQ=”,”data”:”{\”name\”:\”IronMan\”,\”accountNumber\”:\”23343434\”}”},{“timestamp”:1561687118,”prevHash”:”GCS4SGVQ=”,”index”:1,”hash”:”uEFcuKCfhhlIVx
–Blockchain Data Before Modifying—TRUE
isChainValid will return false, as data has been modified from block
-Blockchain Data After Modifying–[{“timestamp”:1561687116,”prevHash”:”0″,”index”:0,”hash”:”GCS4SGVQ=”,”data”:”{\”name\”:\”Thanos\”,\”accountNumber\”:\”23343434\”}”},{“timestamp”:1561687118,”prevHash”:”GCS4SGVQ=”,”index”:1,”hash”:”uEFcuKCfhhlIVxFn0
–Blockchain Data After Modifying—FALSE
Thanks for sharing the content. Nice Stuff
LikeLike