Updated Version
There’s an updated version of this article here:
A block chain block contains the following:
- Transaction Data
- A cryptographic hash ( fingerprint / signature ) of it’s content
- A nonce value ( A random number used for cryptographic computation )
- The hash of the previous block
- A time stamp of a recent verified transaction
Lets look at an example. We will use the NPM Packages CryptoJS and NanoID to generate the signature
and nonce
.
Here’s our block:
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();
}
}
Lets create two blocks with the same data. They should have the same signature:
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()}`);
So we see that a Block
has a unique signature that is determined by what it contains. This makes it very difficult to tamper with a block.