//block.js
const { Console } = require('console')
const fs = require('fs')
const merkle = require('merkle')
class Block{
constructor(header, body){
this.header = header
this.body = body
}
}
class BlockHeader {
constructor (version, previousHash, timestamp, merkleRoot, bit, nonce){
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
}
//getVersion()
function createGenesisBlock() {
const version = getVersion()
const previousHash = '0'.repeat(64)
const timestamp = parseInt(Date.now()/1000)
const body = ['hello block']
const tree = merkle('sha256').sync(body)
const merkleRoot = tree.root() || '0'.repeat(64)
const bit = 0
const nonce = 0
const header = new BlockHeader(version, previousHash, timestamp,merkleRoot, bit, nonce)
return new Block(header, body)
}
const block = createGenesisBlock()
console.log(block)
//chainedBlock.js
const cryptojs = require('crypto-js')
const fs = require('fs')
const merkle = requie('merkle')
class Block {
constructor(header, body) {
this.header= header
this.body = body
}
}
class BlockHeader {
constructor(version, index, previousHash, timestamp, merkleRoot, bit, nonce) {
this.version = version
this.index = index
this.previousHash = previousHash
this.timestamp = timestamp
this.merkleRoot = merkleRoot
this.bit = bit
this.nonce = nonce
}
}
function getVersion() {
const package = fs.readFileSync("package.json")
return JSON.parse(package).version
}
//getVersion()
function createGenesisBlock() {
const version = getVersion()
const index = 0
const previousBlockHash = '0'.repeat(64)
const timestamp = parseInt(Date.now()/1000)
const body = ['hello block']
const tree = merkle('sha256').sync(body)
const merkleRoot = tree.root() || '0'.repeat(64)
const bit = 0
const nonce = 0
const header = new BlockHeader(version, index, previousHash, timestamp, merkleRoot, bit, nonce)
return new Block(header, body)
}
let Blocks = [createGenesisBlock()]
function getBlocks() {
return Blocks
}
function getLastBlock() {
return Blocks[Blocks.length -1]
}
function createHash(data) {
const {version, index,previousHash, timestamp, merkleRoot, bit, nonce} = data.header
const blockString = version + index + previousHash + timestamp + merkleRoot + bit + nonce
const hash = cryptojs.SHA256(blockString).toString()
return hash
}
function nextBlock(bodyData) {
const prevBlock = getLastBlock()
const version = getVersion()
const index = prevBlock.header.indx +1
const timestamp = parseInt(Date.now()/1000)
const tree = merkle("sha256").sync(bodyData)
const merkleRoot = tree.root() || '0'.repeat()
const bit = 0
const nonce = 0
const header = new BlockHeader(version, index, previousBlockHash, merkleRoot, timestamp, bit, nonce)
return new Block(header, bodyData)
}
function addBlock(bodyData) {
const newBlock = nextBlock(bodyData)
Blocks.push(newBlocks)
}
const genesisBlock = createGenesisBlock()
console.log(genesisBlock)
const block1 = nextBlock(["transaction"])
addBlock(['transaction2'])
addBlock(['transaction3'])
addBlock(['transaction4'])
addBlock(['transaction5'])
console.log(Blocks)
학원에서 배우는대로 적으면서 콘솔도 찍어보고 있는데 하나도 이해 안간다.. 돌겠다
'BlockChain' 카테고리의 다른 글
블록체인 제네시스 블록 만들기 by JavaScript (0) | 2021.12.30 |
---|---|
블록체인 블록 만들어보기 (0) | 2021.12.29 |
PoS(Proof of Stake) 고찰 (0) | 2021.12.26 |
해시트리, 이진트리 차이점 (0) | 2021.12.24 |
MerkleTree & MerkleRoot (0) | 2021.12.24 |