
๐ง Typescript ๋ฐฐ์ฐ๊ธฐ
- typescript ๋ณ๊ฑฐ ์๋ค๊ณ ์๊ฐํ์ง๋ง, ์ค์ ๋ก ์ฝ๋๋ฅผ ์ฐ๊ธฐ๊น์ง ์ค๋ ์๊ฐ์ด ๊ฑธ๋ ธ๋ค.
- Nicolas์ Nomadcoder ๋ฌด๋ฃ ๊ฐ์๋ฅผ ํตํด ๊ณต๋ถํด๋ณด์๋ค.
- ํด๋น ๊ฐ์ข๋ ๋ธ๋ก์ฒด์ธ์ ๋ํ ๊ธฐ๋ณธ ์ง์์ด ์ ํ๋์ด์ผ ์ดํดํ ์ ์์ ๊ฒ์ด๋ค.
- ํ์
์คํฌ๋ฆฝํธ๋ฅผ ๋ธ๋ก์ฒด์ธ์ ์ ์ฉํด ๋ฐ๋ผํ๋ค๋ณด๋ ์์ ๊ฐ์ ๊ฐ์ง ์ ์์๋ค.
- ๊ณ ๋ง์์ ๋๊ผฌ!
๐ฑโโ๏ธ ์์ฑํ ์ฝ๋
import * as CryptoJS from "crypto-js";
class Block {
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.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, "2020202020202", "", "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.calculateBlockHash(
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.calculateBlockHash(
aBlock.index,
aBlock.previousHash,
aBlock.timestamp,
aBlock.data
);
const isBlockValid = (candidateBlock: Block, previousBlock: Block): boolean => {
if (!Block.validateStructure(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 block");
createNewBlock("third block");
createNewBlock("fourth block");
console.log(blockchain);
export {};