## Blockchain
- ë¸ë¡ì²´ì¸ì ìí¸íê³¼ ìí ëë¶ì ê°ë¥í, **ì구ì ì ë³´ì ë³´ê´ ê¸°ì **ì ë»í¨.
- ìë¡ì´ íë«í¼ (ê°ë°ìê° ì¬ì©í ììë íë«í¼)
- chain of block ë¸ë¡ë¤ì´ 모ì¬ìë ì²´ì¸.
- BlockChain ì **ì¶ê°ë§ ê°ë¥í ë°ì´í°ë² ì´ì¤ë¼ê³ ìê°. ( í¸ì§ , ìì ë¶ê° )**
- íì¤ìí : í¹ì ê°ì¸ì´ DB를 ê´ë¦¬í ì ìë¤ë ì미 (모ëê° DBì ë³µì¬ë³¸ì ê°ì§ê³ ìì)
ð ì¦ **ë¶ì°ë DB**ë¼ê³ í ì ìë¤. ( => ê°ìíê±°ë íµì ê° ì ëì ì¼ë¡ íë¬ )
## ë¸ë¡ì´ ì 보를 DBì ì¶ê°íë ë°©ë².
- ë¸ë¡ííë¡ ì ë³´ê° ì¶ê°ë¨.
- ë°ì´í° + í´ì(ìíí¨ì, ì¼ë°©í¥ í¨ì, ê²°ì ë¡ ì )
â¼í´ì : ìíí¨ì, ì¼ë°©í¥ í¨ì(ouputì ê°ì§ê³ inputì ì»ì ì ìì) , ê²°ì ë¡ ì ( input ìëí outputì´ ì í´ì ¸ìì. )
=> ê²°êµ íìí ê²: ( ë°ì´í° + ì´ì ë¸ë¡ì í´ì ) í´ì
typescriptë¡ ë§ë blockchain
ê²ì¦ê³¼ì ì ìµìíìí¨ë²ì ¼
```typescript
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,
"202031210312313",
"",
"hello",
123456
);
let blockChain: [Block] = [genesisBlock]; //Block ê°ì²´ë§ ë°ìììë ë°°ì´
const getBlockchain = (): Block[] => blockChain;
const getLatestBlock = (): Block => getBlockchain()[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 {};
```