๐Ÿ‘ฉโ€๐Ÿ’ป ๊ฐ•์˜ ํ›„๊ธฐ

  • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ํƒ€์ž…์„ ๋ฏธ๋ฆฌ ์ง€์ •ํ•ด์ค€๋‹ค๋Š” ์ ์ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ๋‹ฌ๋ผ ์กฐ๊ธˆ ์ƒ์†Œํ–ˆ๋‹ค.
  • ์ธํ„ฐํŽ˜์ด์Šค๋‚˜ ์Šคํƒœํ‹ฑ ํ•จ์ˆ˜, constructor ๋“ฑ์„ ๊ตฌํ˜„ํ•ด๋ณผ ๊ธฐํšŒ๊ฐ€ ํฌ๊ฒŒ ์—†์—ˆ๋Š”๋ฐ ์ด๋ฒˆ ๊ธฐํšŒ๋ฅผ ํ†ตํ•ด ์ƒˆ๋กœ์šด ๊ฒฝํ—˜์„ ํ•ด๋ณธ ๊ฑฐ ๊ฐ™๋‹ค.
  • ๋ฆฌ์•กํŠธ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๊ต‰์žฅํžˆ ์ข‹๋‹ค๊ณ  ํ•˜๋Š”๋ฐ ๋‚˜์ค‘์— ๋ฆฌ์•กํŠธ๋ฅผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ์‹ถ์€ ์ƒ๊ฐ๋„ ๋“ ๋‹ค.
  • ์ฒ˜์Œ์—๋Š” ํ•˜๋‚˜ํ•˜๋‚˜ ํƒ€์ž…์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ๊ต‰์žฅํžˆ ๋ฒˆ๊ฑฐ๋กญ๊ฒŒ ๋Š๊ปด์กŒ๋Š”๋ฐ ํฐ ํ”„๋กœ์ ํŠธ์— ์žˆ์–ด ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€ํ•˜๋Š” ํ•˜๋‚˜์˜ ํฐ ์—ญํ• ์ด ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด ์™œ ์‚ฌ๋žŒ๋“ค์ด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์„ ํ˜ธํ•˜๋Š”์ง€ ์•Œ๊ฒŒ๋˜๋Š” ๊ณ„๊ธฐ์˜€๋‹ค.
  • ์•„์ง ๋ธ”๋ก์ฒด์ธ์— ๋Œ€ํ•œ ๊ฐœ๋…์ด ํ™•์‹คํ•˜๊ฒŒ ์ •๋ฆฌ๋˜์ง€ ์•Š์€ ์ฑ„ ๋“ค์€ ๊ฐ•์˜์˜€๊ธฐ์— ์กฐ๊ธˆ ์•„์‰ฌ์›€์ด ๋‚จ๋Š”๋‹ค.
  • crypto-js ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด Sha256 ์•”ํ˜ธํ™” ํ•ด์‹œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค.

โš™๏ธ ์ฝ”๋“œ

import * as CryptoJS from "crypto-js"

class Block {


    static cacluateBlockHash = (index: number, previousHash: string, timestamp: number, data: string): string => CryptoJS.SHA256(index + previousHash + timestamp + data).toString();

    static vaidateStructure = (aBlock: Block): boolean =>
        typeof aBlock.index === "number" &&
        typeof aBlock.hash === "string" &&
        typeof aBlock.previousHash === "string" &&
        typeof aBlock.timestamp === "number"
        && typeof aBlock.data === "string";

    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;
    }
}


const genesisBlock: Block = new Block(0, "20202020202", "", "hello", 123456);



let blockchain: [Block] = [genesisBlock];
const getBlockchain = (): Block[] => blockchain;

const getLatestBlock = (): Block => blockchain[blockchain.length - 1]

const getNewTimeStamp = (): number => Math.round(new Date().getTime() / 1000)

const createNewBlock = (data: string): Block => {
    const previousBlock: Block = getLatestBlock();
    const newIndex: number = previousBlock.index + 1;
    const newTimestamp: number = getNewTimeStamp();
    const newHash: string = Block.cacluateBlockHash(newIndex, previousBlock.hash, newTimestamp, data)
    const newBlock: Block = new Block(newIndex, newHash, previousBlock.hash, data, newTimestamp)
    addBlock(newBlock)
    return newBlock;
}

const getHashforBlock = (aBlock: Block): string => Block.cacluateBlockHash(aBlock.index, aBlock.previousHash, aBlock.timestamp, aBlock.data)

const isBlockValid = (candidateBlock: Block, previousBlock: Block): boolean => {
    if (!Block.vaidateStructure(candidateBlock)) {
        return false;
    } else if (previousBlock.index + 1 !== candidateBlock.index) {
        return false;
    } else if (previousBlock.hash !== candidateBlock.previousHash) {
        return false;
    } else if (getHashforBlock(candidateBlock) !== candidateBlock.hash) {
        return false;
    } else {
        return true;
    }
}

const addBlock = (candidateBlock: Block): void => {
    if (isBlockValid(candidateBlock, getLatestBlock())) {
        blockchain.push(candidateBlock);
    }
}

createNewBlock("second blocks");
createNewBlock("third block");
createNewBlock("fourth block");

console.log(blockchain)
export { };

์‚ฌ์ง„ ์ถœ์ฒ˜ : https://www.typescriptlang.org/

0๊ฐœ์˜ ๋Œ“๊ธ€