JavaScript【代码】使用JS实现简单的区块链(签名+工作量证明机制)

//区块链 block chain
//data 之前区块的哈希值 当前区块的哈希值:是由存储在区块里的信息算出来的(data + 之前区块的哈希值)

const sha256 = require('./crypto-js/sha256')

//区块
class Block{
    constructor(data){
        this.data = data
        this.previousHash = ''
        this.nonce = 1
        this.hash = this.computeHash()
    }

    computeHash(){
        return sha256(this.data + this.previousHash + this.nonce).toString()
    }

    // 计算符合区块链难度的hash值
    mine(difficulty){
        while(true){
            this.hash = this.computeHash()
            if(this.hash.substring(0, difficulty) !== this.getAnswer(difficulty)){
                this.nonce++
            }else{
                break
            }
        }
    }

    getAnswer(difficulty){
        // 开头前n位为0的hash
        let answer = ''
        while(difficulty-- !== 0){
            answer += '0'
        }
        return answer
    }
}

//区块 的 链 
//生成祖先区块
class Chain{
    constructor(){
        this.chain = [this.bigBang()]
        this.difficulty = 4
    }

    bigBang(){
        const genesisBlock = new Block('祖先区块')
        return genesisBlock
    }

    //获取最新一个区块
    getLatestBlock(){
        return this.chain[this.chain.length-1]
    }

    //添加新区块
    addBlockToChain(newBlock){
        // 1、data 2、previousHash
        newBlock.previousHash = this.getLatestBlock().hash
        newBlock.hash = newBlock.computeHash()
        // 进行挖矿
        newBlock.mine(this.difficulty)
        this.chain.push(newBlock)
    }

    //区块链验证 当前数据是否被篡改 当前区块的previousHash是否等于它的previous的hash值
    validateChain(){

        // 验证祖先区块数据是否被篡改
        if(this.chain.length===1){
            if(this.chain[0].hash !== this.chain[0].computeHash()){
                return false
            }
            return true
        }

        // 验证其他区块
        for(let i = 1, len = this.chain.length-1; i <= len; i++){
            const blockToValidate = this.chain[i]
            // 验证数据是否被篡改
            if(blockToValidate.hash !== blockToValidate.computeHash()){
                console.log("数据被篡改!")
                return false
            }
            // 验证hash值
            if(blockToValidate.previousHash !== this.chain[i-1].hash){
                console.log("前后区块断裂!")
                return false
            }
        }
        return true
    }

}

const zzBlock = new Block('转账1000')
const zzBlock2 = new Block('转账3210')
const zzBlock3 = new Block('转账210')
const blockChain = new Chain()
blockChain.addBlockToChain(zzBlock)
blockChain.addBlockToChain(zzBlock2)
blockChain.addBlockToChain(zzBlock3)
console.log(blockChain.chain.length)

//尝试篡改数据
blockChain.chain[1].data = '转账10W'
blockChain.chain[1].mine(4)
console.log(blockChain)
console.log(blockChain.validateChain())
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuanzhengme.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值