Understanding What a Block Chain Block Is

Image by tarakko from Pixabay

Updated Version

import { SHA256 } from "crypto-js";
import { nanoid } from "nanoid";
class Block {
constructor(
public index: number,
public timestamp: string,
public data: any,
public precedingHash: string,
public nonce?: string) {
this.nonce = this.nonce ? this.nonce : nanoid();
}
signature() {
return SHA256(
this.index +
this.precedingHash +
this.timestamp +
this.nonce +
JSON.stringify(this.data)).toString();
}
}
const transactionData = {sender: "Ole Ersoy",recipient: "Satoshi Nakamoto",quantity: 1};const block1 = new Block(0, "02/02/2021", transactionData, "0", "nonce");const block2 = new Block(0, "02/02/2021", transactionData, "0", "nonce");console.log(`Signature of Block 1: ${block1.signature()}`);console.log(`Signature of Block 2: ${block2.signature()}`);

Demo

Founder of Firefly Semantics Corporation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store