728x90
반응형
먼저 블록 구조를 만들어보자. Block 클래스를 만들어준다.
index.ts
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;
}
}
export {};
인스턴스를 만들어준다.
index.ts
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 TeepoBlock: Block = new Block(0, "20202020202", "", "Hello", 123456);
export {};
블록체인을 만들어준다. 블록체인이랑 블록들이 배열로 연결된 형태이다.
index.ts
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 TeepoBlock: Block = new Block(0, "20202020202", "", "Hello", 123456);
let blockchain: Block[] = [TeepoBlock];
console.log(blockchain)
export {};
tsc-watch 를 보면
완벽하게 블록이 들어간 것을 확인할 수 있다.
728x90
반응형
'Typescript > 블록체인' 카테고리의 다른 글
TypeScript | 블록체인 | 결과 (0) | 2021.10.05 |
---|---|
TypeScript | 블록체인 | 블록 검증하기 (0) | 2021.10.05 |
TypeScript | 블록체인 | 블록 만들기(3) (0) | 2021.10.05 |
TypeScript | 블록체인 | 블록 만들기(2) (0) | 2021.10.05 |