Published
Edited
May 27, 2021
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class TransactionBlock {
constructor(index, timestamp, data, precedingHash = " ") {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.precedingHash = precedingHash;
this.hash = this.computeHash();
}
computeHash() {
return cryptoJS
.SHA1(
this.index +
this.precedingHash +
this.timestamp +
JSON.stringify(this.data)
)
.toString();
}
}
Insert cell
Insert cell
Insert cell
class RealEstateBlockchain {
constructor() {
this.blockchain = [this.startGenesisBlock()];
}
startGenesisBlock() {
var now = moment().format('Y-M-D-h:m:s:ms'); //Capture the current date/time
return new TransactionBlock(
0,
now,
{ Genesis: "Instantiation of chain" },
"0"
); //Creates the very first block
}
obtainLatestBlock() {
return this.blockchain[this.blockchain.length - 1]; //Grabs the latest block
}
addNewBlock(newBlock, index) {
newBlock.precedingHash = this.obtainLatestBlock().hash; //Takes the hash from the previous block
newBlock.hash = newBlock.computeHash(); //Computes the new hash for the block being added
this.blockchain.push(newBlock);
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function checkChainValidity(blockchain) {
for (let i = 1; i < this.blockchain.length; i++) {
const currentBlock = this.blockchain[i];
const precedingBlock = this.blockchain[i - 1];

if (currentBlock.hash !== currentBlock.computeHash()) {
return false;
}
if (currentBlock.precedingHash !== precedingBlock.hash) return false;
}
return true;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function addTransaction(mystring) {
RealEstateTransactions.addNewBlock(
new TransactionBlock(
fakeIt.random.number(),
fakeIt.date.between("2021-10-01", "2022-12-31"),
{
//buyer
buyerName: fakeIt.name.findName(),
buyerAddress:
fakeIt.address.streetAddress() +
', ' +
fakeIt.address.city() +
', ' +
fakeIt.address.state() +
', ' +
fakeIt.address.state(),

buyerEmail: fakeIt.internet.email(),
buyerPhone: fakeIt.phone.phoneNumber(),

//seller
sellerName: fakeIt.name.findName(),
sellerAddress:
fakeIt.address.streetAddress() +
', ' +
fakeIt.address.city() +
', ' +
fakeIt.address.state() +
', ' +
fakeIt.address.state(),

sellerEmail: fakeIt.internet.email(),
sellerPhone: fakeIt.phone.phoneNumber(),

//Property
propertyAddress:
fakeIt.address.streetAddress() +
', ' +
fakeIt.address.city() +
', ' +
fakeIt.address.state() +
', ' +
fakeIt.address.state(),

previousSoldValue: fakeIt.finance.amount(),
currentSaleValue: fakeIt.finance.amount()
}
)
);

return RealEstateTransactions;
}
Insert cell
function CreateTable() {
addTransaction();
let htmlString = "";

for (let i in RealEstateTransactions['blockchain']) {
let myString = "";
let myKeys = Object.keys(RealEstateTransactions['blockchain'][i]['data']);
let myVals = Object.values(RealEstateTransactions['blockchain'][i]['data']);

for (let j in myKeys) {
myString =
myString +
'<tr><td>' +
myKeys[j] +
'</td><td>' +
myVals[j] +
'</td></tr>';
}
htmlString =
htmlString +
'<table border="1px" border-radius:6px; bordercolor="silver"><tr><h4 align="center"> Preceding Hash: ' +
RealEstateTransactions['blockchain'][i]['precedingHash'] +
'</h4></tr><tr><h4 align="center"> Current Hash: ' +
RealEstateTransactions['blockchain'][i]['hash'] +
'</h4></tr><tr>' +
myString +
'</tr></table>';
}

return htmlString;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cryptoJS = require("https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js")
Insert cell
fakeIt = require("faker/dist/faker.min.js")
Insert cell
moment = require("moment")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more