js์์๋ ์ธํฐํ์ด์ค์ ๋น์ทํ class๋ผ๋๊ฒ ์๋ค.
class Human {
public name: string;
public age: number;
public gender: string;
// ์์ฑ์- ํด๋์ค๊ฐ ์์ํ ๋๋ง๋ค ํธ์ถ๋๋ ๋ฉ์๋
// ?๋ ์ ํ์ฌํญ
constructor(name: string, age: number, gender?: string){
this.name = name;
this.age = age;
this.gender = gender;
}
}
const rin = new Human("Rin", 23, "female");
const greeting = (person: Human): string=>{
return `Hello ${person.name} ${person.age} ${person.gender}`;
}
console.log(greeting(rin));
export {};
์์ ๋ค์๋ ๊ฐ์๋ด์ฉ์ผ๋ก ๋ธ๋ก์ ํ๋ ๋ง๋ค์๋ค.
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;
}
}
const genesisBlock: Block = new Block(0, "110101101011010", "", "Hello", 12345);
// ts๊ฐ ๋ธ๋ก(object)๋ง ๋ธ๋ก์ฒด์ธ์ ์ถ๊ฐํ๋๋ก ์ฒดํฌ
let blockchain: [Block] = [genesisBlock];
console.log(blockchain);
export {};
์ ๋ธ๋ก์ ๋ง๋ค์ด๋ณผ ์ฐจ๋ก์ด๋ค.
์๋ก์ด ๋ธ๋ญ์ ๋ง๋ค๊ธฐ ์ํด์๋ ํด์ฌ๋ฅผ ๊ณ์ฐํด์ผํ๋ค.
class์์์ ์ผ๋ฐ์ ์ผ๋ก sayHello=>... ์ด๋ ๊ฒ ๋ฉ์๋๋ฅผ ๋ง๋ค๋ฉด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ ๋ ๋ธ๋ญ์ ๋จผ์ ๋ง๋ค๊ณ ๋์์ผ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ฐ.
๋ธ๋ก์ ์์ฑํ์ง ์์๋ ์ฌ์ฉ๊ฐ๋ฅํ method๋ฅผ ๋ง๋ค๊ณ ์ถ๋ค.
Block ํด๋์ค ์์ static์ผ๋ก ๋ง๋ค์ด์คฌ๋ค.
static calculateBlockHash = (
index: number,
previousHash: string,
timestamp: number,
data: string
): string =>
CryptoJS.SHA256(index + previousHash + timestamp + data).toString();
๐ฝ index.ts ์์ฑ์ฝ๋
import * as CryptoJS from "crypto-js"
import { createTypeOf, NumberLiteralType } from "typescript";
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, "110101101011010", "", "Hello", 12345);
// ts๊ฐ ๋ธ๋ก(object)๋ง ๋ธ๋ก์ฒด์ธ์ ์ถ๊ฐํ๋๋ก ์ฒดํฌ
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;
};
// console.log(createNewBlock("hello"), createNewBlock("byebye"));
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 {};
'WEB > Front-End๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React Hooks] useEffect, useTitle, useClick (0) | 2021.12.18 |
---|---|
[React Hooks] useInput, useTabs (0) | 2021.12.13 |
[Typescript] predictable Typescript (0) | 2021.12.12 |
[Typescript] ๋ธ๋ก์ฒด์ธ ๋ฏธ๋ํ๋ก์ ํธ (0) | 2021.12.10 |
[React JS] Movie App 03 - React Route (0) | 2021.12.05 |