Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 머클트리
- immer
- 리액트
- 일반유저
- 변수
- 환경변수
- wsl
- node.js 교과서 따라하기
- 자바스크립트
- 벨로포터
- 리눅스
- MariaDB
- 노드
- 우분투
- Docker
- 쉘스크립트
- 라우터
- centos
- 머클루트
- 블록체인
- 리액트를 다루는 기술
- 설치
- npm
- 이더리움
- 전역설치
- 솔리디티
- 시퀄라이즈
- wget
- 깃허브
- Sequelize
Archives
- Today
- Total
코드코코
[블록체인] 자바스크립트로 블록체인 만들기 (1) - 블록생성 본문
블록생성
//block.js
const cryptoJs = require('crypto-js')
const fs = require('fs')
const merkle = require('merkle')
//블록 구조체 선언 : 헤더, 바디
class Block {
constructor(header, body) {
this.header = header
this.body = body
}
}
//블록헤더 구조체 선언 : 헤더 구성 요소 나열
class BlockHeader {
constructor(index, version, previousHash, timestamp, merkleRoot, bit, nonce) {
this.index = index
this.version = version
this.previousHash = previousHash
this.timestamp = timestamp
this.merkleRoot = merkleRoot
this.bit = bit
this.nonce = nonce
}
}
function getVersion() {
const package = fs.readFileSync('package.json')
//console.log(JSON.parse(package).version)
return JSON.parse(package).version
}
function createGenesisBlock() {
const index = 0
const version = getVersion()
const previousHash = '0'.repeat(64) // 0을 64번 반복
const timestamp = parseInt(Date.now() / 1000) // 1000 나눈 이유 : 초 단위로 환산하기 위해
const body = ['Hello block!']
const tree = merkle('sha256').sync(body)
const merkleRoot = tree.root() || '0'.repeat(64)
const bit = 0
const nonce = 0
// console.log("version : %s, timestamp : %d, body: %s", version, timestamp, body);
// console.log("previousHash : %d", previousHash);
// console.log(tree);
// console.log("merkleRoot: %s", merkleRoot);
const header = new BlockHeader(index, version, previousHash, timestamp, merkleRoot, bit, nonce)
return new Block(header, body)
}
// const block = createGenesisBlock()
// console.log(block);
let Blocks = [createGenesisBlock()]
function getBlocks() {
return Blocks
}
function getLastBlock() {
return Blocks[Blocks.length - 1]
}
function createHash(data) {
const { index, version, previousHash, timestamp, merkleRoot, bit, nonce } = data.header
const blockString = index + version + previousHash + timestamp + merkleRoot + bit + nonce
const hash = cryptoJs.SHA256(blockString).toString()
return hash
}
// //제네시스 블럭생성
// const block = createGenesisBlock()
// //제네시스 블럭의 해쉬값
// const testHash = createHash(block)
// console.log(testHash);
function nextBlock(bodyData) {
const prevBlock = getLastBlock()
const version = getVersion()
const index = prevBlock.header.index + 1
const previousHash = createHash(prevBlock)
const timestamp = parseInt(Date.now() / 1000)
const tree = merkle('sha256').sync(bodyData)
const merkleRoot = tree.root() || '0'.repeat(64)
const bit = 0
const nonce = 0
const header = new BlockHeader(index, version, previousHash, timestamp, merkleRoot, bit, nonce)
return new Block(header, bodyData)
}
// const block1 = nextBlock(['transaction1'])
// console.log(block1);
function addBlock(bodyData) {
const newBlock = nextBlock(bodyData)
Blocks.push(newBlock)
}
addBlock(['transaction1'])
addBlock(['transaction2'])
addBlock(['transaction3'])
addBlock(['transaction4'])
addBlock(['transaction5'])
console.log(Blocks);
module.exports = { nextBlock, getLastBlock, createHash, Blocks, getVersion, getBlocks }
실행결과
'블록체인' 카테고리의 다른 글
[블록체인] 블록체인만들기 (4) P2P 서버 (p2pServer.js) (0) | 2021.12.31 |
---|---|
[블록체인] 블록체인만들기 (3) HTTP 서버 (httpServer.js) (0) | 2021.12.31 |
[블록체인] 블록체인만들기 (2) 블록생성과 블록검증(checkValidBlock.js) (0) | 2021.12.30 |
[블록체인] 블록체인만들기 (1) 블록생성(chainedBlock.js) (0) | 2021.12.30 |
[블록체인] Websocket - 사전개념 (0) | 2021.12.29 |