xxxxxxxxxx
const crypto = require('crypto');
class Block {
constructor(index, previousHash, merkleRoot, timestamp, nonce, difficulty, data) {
this.index = index;
this.previousHash = previousHash;
this.merkleRoot = merkleRoot;
this.timestamp = timestamp;
this.nonce = nonce;
this.difficulty = difficulty;
this.data = data;
this.hash = this.calculateHash();
}
calculateHash() {
const data = JSON.stringify(this.data);
return crypto
.createHash('sha256')
.update(
this.index +
this.previousHash +
this.merkleRoot +
this.timestamp +
this.nonce +
this.difficulty +
data
)
.digest('hex');
}
mineBlock() {
while (
this.hash.substring(0, this.difficulty) !== Array(this.difficulty + 1).join('0')
) {
this.nonce++;
this.hash = this.calculateHash();
}
console.log('Block mined:', this.hash);
}
}
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
this.difficulty = 4; // Number of leading zeros required in the block hash
}
createGenesisBlock() {
return new Block(0, '0', '', Date.now(), 0, this.difficulty, 'Genesis Block');
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.difficulty = this.difficulty;
newBlock.mineBlock();
this.chain.push(newBlock);
}
}
// Create a new blockchain
const blockchain = new Blockchain();
// Add blocks to the blockchain
blockchain.addBlock(new Block(1, '', 'merkleRoot1', Date.now(), 0, blockchain.difficulty, 'Block 1 Data'));
blockchain.addBlock(new Block(2, '', 'merkleRoot2', Date.now(), 0, blockchain.difficulty, 'Block 2 Data'));
blockchain.addBlock(new Block(3, '', 'merkleRoot3', Date.now(), 0, blockchain.difficulty, 'Block 3 Data'));
// Serialize the blockchain to JSON
const jsonBlockchain = JSON.stringify(blockchain, null, 2);
console.log(jsonBlockchain);
xxxxxxxxxx
const crypto = require('crypto');
class Block {
constructor(version, previousHash, merkleRoot, timestamp, difficultyTarget) {
this.version = version;
this.previousHash = previousHash;
this.merkleRoot = merkleRoot;
this.timestamp = timestamp;
this.difficultyTarget = difficultyTarget;
this.nonce = 0;
this.hash = '';
}
calculateHash() {
const blockHeader = JSON.stringify({
version: this.version,
previousHash: this.previousHash,
merkleRoot: this.merkleRoot,
timestamp: this.timestamp,
difficultyTarget: this.difficultyTarget,
nonce: this.nonce
});
return crypto.createHash('sha256').update(blockHeader).digest('hex');
}
mineBlock(difficulty) {
const targetPrefix = Array(difficulty + 1).join('0');
while (this.hash.substring(0, difficulty) !== targetPrefix) {
this.nonce++;
this.hash = this.calculateHash();
}
}
}
// Function to generate a random 64-character hexadecimal string
function generateRandomHash() {
return Array(64)
.fill(0)
.map(() => Math.floor(Math.random() * 16).toString(16))
.join('');
}
// Example usage
const difficulty = 4; // Number of leading zeros required in the block hash
const blocks = [];
// Create and mine six blocks
for (let i = 0; i < 6; i++) {
const version = 1;
const previousHash = i > 0 ? blocks[i - 1].hash : '0000000000000000000000000000000000000000000000000000000000000000';
const merkleRoot = generateRandomHash();
const timestamp = Math.floor(Date.now() / 1000);
const block = new Block(version, previousHash, merkleRoot, timestamp, difficulty);
block.mineBlock(difficulty);
blocks.push(block);
}
// Output the mined blocks
blocks.forEach((block, index) => {
console.log(`Block ${index + 1}:`);
console.log('Hash:', block.hash);
console.log(JSON.stringify(block, null, 2));
console.log();
});