블럭을 이제 연결해 보자.
블록 검증을 해보자.
hash가 정확한지 등등
import * as CryptoJS from 'crypto-js';
class Block {
public index: number;
public hash: string;
public previousHash: string;
public data: string;
public timestamp: number;
constructor(
index: number,
hash: string,
previousHash: string,
data: string,
timestamp: number,
) {
this.index = index;
this.hash = hash;
this.previousHash = previousHash;
this.data = data;
this.timestamp = timestamp;
}
static calculateBlockHash = (
index: number,
previousHash: string,
timestamp: number,
data: string
): string => CryptoJS.SHA256(index + previousHash + timestamp + data).toString();
static validateStructure = (aBlock: Block): boolean =>
typeof aBlock.index === 'number' &&
typeof aBlock.hash === 'string' &&
typeof aBlock.previousHash === 'string' &&
typeof aBlock.data === 'string' &&
typeof aBlock.timestamp === 'number';
}
const genesisBlock: Block = new Block(0, 'abc123', '','genesis', 123456);
let blockchain: Block[] = [genesisBlock];
const getBlockchain = (): Block[] => blockchain;
const getLatestBlock = (): Block => blockchain[blockchain.length - 1];
const getNewTimestamp = (): number => Math.round(Date.now() / 1000);
const createNewBlock = (data: string): Block => {
const previousBlock: Block = getLatestBlock();
console.log(previousBlock);
const newIndex: number = previousBlock.index + 1;
const newTimestamp: number = getNewTimestamp();
const newHash: string = Block.calculateBlockHash(newIndex, previousBlock.hash, newTimestamp, data);
return new Block(newIndex, newHash, previousBlock.hash, data, newTimestamp);
};
const isBlockValid = (candidateBlock: Block, previousBlock: Block): boolean => {
if (!Block.validateStructure(candidateBlock)) return false;
if (previousBlock.index + 1 !== candidateBlock.index) return false;
if (previousBlock.hash !== candidateBlock.previousHash) return false;
return true;
};
console.log(createNewBlock("hi"));
console.log(createNewBlock("hello"));
// console.log(getBlockchain());