xxxxxxxxxx
129
class Blockchain{
constructor(){
this.blocks = [];
this.genesisBlock = this.generateGenisisBlock();
console.log(this.blocks[0]);
this.generateNextBlock("shiiit")
this.generateNextBlock("mkayyy")
this.isValidChain(this.blocks);
}
generateGenisisBlock(){
const index = 0;
const timestamp = new Date().getTime() / 1000;
const previousHash = 0;
const blockData = "The Genesis Block";
const nextHash = this.calculateHash(index, previousHash, timestamp, blockData);
const newBlock = new Block(index, nextHash, previousHash, timestamp, blockData);
this.blocks.push(newBlock);
return newBlock;
}
generateNextBlock(blockData){
const previousBlock = this.getLatestBlock();
const nextIndex = previousBlock.index + 1;
const nextTimestamp = new Date().getTime() / 1000;
const nextHash = this.calculateHash(nextIndex, previousBlock.hash, nextTimestamp, blockData);
const newBlock = new Block(nextIndex, nextHash, previousBlock.hash, nextTimestamp, blockData);
// should add a step to validate the block no ? // maybe we dont need to validate do we ? or validate like that idk
if(this.isValidNewBlock(newBlock, previousBlock)){
console.log("new block is valid")
this.blocks.push(newBlock);
}
console.log(this.blocks[nextIndex]);
}
getLatestBlock(){
return this.blocks[this.blocks.length -1];
}
calculateHash(index, previousHash, timestamp, data){
const str = index.toString() + previousHash.toString() + timestamp.toString() + data.toString
let h = 0;
for(let i = 0; i < str.length; i++){
h = Math.imul(31, h) + str.charCodeAt(i) | 0;
}
return h.toString();
}
calculateHashForBlock(block){
return this.calculateHash(block.index, block.previousHash, block.timestamp, block.data);
}
// CHECK IF A SINGLE BLOCK IS VALID
isValidNewBlock(newBlock, previousBlock){
if (previousBlock.index + 1 !== newBlock.index) {
console.log('invalid index');
return false;
} else if (previousBlock.hash !== newBlock.previousHash) {
console.log('invalid previoushash');
return false;
} else if (this.calculateHashForBlock(newBlock) !== newBlock.hash) {
console.log(typeof (newBlock.hash) + ' ' + typeof this.calculateHashForBlock(newBlock));
console.log('invalid hash: ' + this.calculateHashForBlock(newBlock) + ' ' + newBlock.hash);
return false;
}
return true;
}
// CHECK IF A CHAIN OF BLOCKS IS VALID
isValidChain(blockchainToValidate){
if (!this.isValidGenesis(blockchainToValidate[0])) {
return false;
}
for (let i = 1; i < blockchainToValidate.length; i++) {
if (!this.isValidNewBlock(blockchainToValidate[i], blockchainToValidate[i - 1])) {
return false;
}
}
console.log("theblockchain is valid")
return true;
}
// CHECK IF IT IS THE GENESIS BLOCK
isValidGenesis(block){
return JSON.stringify(block) === JSON.stringify(this.genesisBlock);
}
}
class Block {
constructor(index, hash, previousHash, timestamp, data) {
this.index = index;
this.previousHash = previousHash;
this.timestamp = timestamp;
this.data = data;
this.hash = hash;
}
// index : The height of the block in the blockchain
// hash: A sha256 hash taken from the content of the block
// previousHash: A reference to the hash of the previous block. This value explicitly defines the previous block.
// timestamp: A timestamp
// data: Any data that is included in the block.
}
let b = new Blockchain();
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
}