{{theTime}}

Search This Blog

Total Pageviews

Block Chain Implementation using JavaScript and Node JS for Real Estate Transactions.


/**
 * RealEstateTransactionBlock Chain Implementation to record Real Estate Transactions. 

 * What is BlockChain?
    * BlockChain Chain is a data structure built through LinkedLists and Merkle Trees.
* BlockChain is immutable.  The blocks are not editable, hence can't forge the original recorded transaction.
 *
 * To understand more in detail or help compiling the code, please leave a comment or send me an email.  
 
 * - Create Block that holds the transactions and the necessary links and timestamp.
 * - Create BlockChain responsible for validating the new transactions and adding to the Chain.  
 * Software Required to run the below JavaScript Implementation of RealEstateTransactionBlock Chain:
 *
 * - Download Node.js  https://nodejs.org/en/
 * - Download Crypto-js Library (SHA256 hash function developed by the NSA and is irreversible hash function.
 * To install run : npm install -- save crypto-js
 *  
 * Time taken to write this code - 30 mins. 
 */

 /**
 *  The SHA256 hash function was developed by the NSA and is an irreversible hash function.
 */ 
const SHA256 = require('crypto-js/sha256')

class RealEstateTransactionBlock {
    constructor(ind, time, realestatetransaction, previousHash) {
        this.RealeStateTransactionForHouse1 = realestatetransaction;
        this.time = time;
        this.previousHash = previousHash;
        this.hash = this.getHashKey();
        this.staticconst = 0;
this.ind = ind;
    }
/**
* Generate HashKey by invoking SHA256 method.  Since the SHA inputs are contatination of index, previoushash,transactiondata and stasticconstant, the hash key generated would be unique.
* How this combination helps to identify the Forged Transaction data?
*  Since transaction data is one of the input the hash function, if the transaction data changes the generated key will be different and the BlockChain validations will fail lookup the key.
*/
    getHashKey() {
        return SHA256(this.ind + this.previousHash + this.time + this.realestatetransaction + this.staticconst).toString();
    }
/** 
* Template block for mining.
*/

    mineRealEstateTransactionBlock(difficulty) {

    }
}

/**
*
* TransactionBlockChain.  Code to add blocks.
*/
class RealEstateTransactionBlockchain{
    constructor() {
        this.chain = [this.createGenesisBlock()];
    }

    createGenesisBlock() {
        return new RealEstateTransactionBlock(0, "01/01/2016", "Mr X sold House1 to Mr Y; amount: $800000", "0")
    }

    latestRealEstateTransactionBlock() {
        return this.chain[this.chain.length - 1]
    }

    addRealEstateTransactionBlock(newRealEstateTransactionBlock){
        newRealEstateTransactionBlock.previousHash = this.latestRealEstateTransactionBlock().hash;
        newRealEstateTransactionBlock.hash = newRealEstateTransactionBlock.getHashKey();
        this.chain.push(newRealEstateTransactionBlock);
    }

    validateNewTransaction() {
        for(let i = 1; i < this.chain.length; i++) {
            const currentRealEstateTransactionBlock = this.chain[i];
            const previousRealEstateTransactionBlock = this.chain[i - 1];

            if (currentRealEstateTransactionBlock.hash !== currentRealEstateTransactionBlock.getHashKey()) {
                return false;
            }

            if (currentRealEstateTransactionBlock.previousHash !== previousRealEstateTransactionBlock.hash) {
                return false;
            }
        }

        return true;
    }
}
/**
*  The below code is to test the newly added transactions.   Goto Command line:  type node filename.js
*/
let transactionChain = new RealEstateTransactionBlockchain();
transactionChain.addRealEstateTransactionBlock(new RealEstateTransactionBlock(1, "01/01/2017", "Mr Y sold House1 to Mr Z; amount: $800000", "0"));
transactionChain.addRealEstateTransactionBlock(new RealEstateTransactionBlock(2, "10/01/2017", "Mr Z sold House1 to Mr A; amount: $870000", "0"));
transactionChain.addRealEstateTransactionBlock(new RealEstateTransactionBlock(3, "02/01/2018", "Mr A sold House1 to Mr B; amount: $880000", "0"));

console.log(JSON.stringify(transactionChain, null, 4));
console.log("Is RealEstateTransaction valid? " + transactionChain.validateNewTransaction());


Output:

{
    "chain": [
        {
            "RealeStateTransactionForHouse1": "Mr X sold House1 to Mr Y; amount: $800000",
            "time": "01/01/2016",
            "previousHash": "0",
            "hash": "7bfd8b5d292d1d1f84ce087c4e0d61ae809b72cc274d54b3601e8b75cf73605b",
            "staticconst": 0,
            "ind": 0
        },
        {
            "RealeStateTransactionForHouse1": "Mr Y sold House1 to Mr Z; amount: $800000",
            "time": "01/01/2017",
            "previousHash": "7bfd8b5d292d1d1f84ce087c4e0d61ae809b72cc274d54b3601e8b75cf73605b",
            "hash": "4fd8077d6a0bdfac9f84625e3994d4c8c1de000b3ec23e966df8d634dc129289",
            "staticconst": 0,
            "ind": 1
        },
        {
            "RealeStateTransactionForHouse1": "Mr Z sold House1 to Mr A; amount: $870000",
            "time": "10/01/2017",
            "previousHash": "4fd8077d6a0bdfac9f84625e3994d4c8c1de000b3ec23e966df8d634dc129289",
            "hash": "b2a674f5cfdae3b8810cb02bc884d49583b18e5536dd4a7409fdc0eb68a66fe9",
            "staticconst": 0,
            "ind": 2
        },
        {
            "RealeStateTransactionForHouse1": "Mr A sold House1 to Mr B; amount: $880000",
            "time": "02/01/2018",
            "previousHash": "b2a674f5cfdae3b8810cb02bc884d49583b18e5536dd4a7409fdc0eb68a66fe9",
            "hash": "1b1e032d141d03863f07d1d678ba283ab7665b9a4bfbc1538a0e2bc1fc2a6d53",
            "staticconst": 0,
            "ind": 3
        }
    ]
}
Is RealEstateTransaction valid? true


No comments:

Generate Insert Sql from Select Statement

SELECT 'INSERT INTO ReferenceTable (ID, Name) VALUES (' +        CAST(ID AS NVARCHAR) + ', ''' + Name + ''...