Nodejs 写个简单的无币区块链

Nodejs 写个简单的无币区块链

安装

git clone https://github.com/forkchain/alpha.git
npm install

启动

启动节点 1

HTTP_PORT=3001 P2P_PORT=6001 npm start

启动节点 2

HTTP_PORT=3002 P2P_PORT=6002 npm start

启动节点 3

HTTP_PORT=3003 P2P_PORT=6003 npm start

配置节点

curl -H "Content-type:application/json" --data '{"peer" : "ws://localhost:6001"}' http://localhost:3001/addPeer
curl -H "Content-type:application/json" --data '{"peer" : "ws://localhost:6002"}' http://localhost:3001/addPeer
curl -H "Content-type:application/json" --data '{"peer" : "ws://localhost:6003"}' http://localhost:3001/addPeer

查看节点

curl http://localhost:3001/peers

创世

curl http://localhost:3001/g

查询区块

curl http://localhost:3001/blocks/all
curl http://localhost:3002/blocks/all
curl http://localhost:3003/blocks/all

用户操作

新建用户

node newuser.js
 你要把助忆词和地址抄在安全的地方,key 文件里仅仅保存私钥。 

±-------------±--------------------------------------------------------------------------------------+ | mnemonic | analyst merit gown expect lake crouch either radio achieve provide half total | | Privatekey | 5JdGzR28pdh6c3vKrwLuQvWXbznJmrPwE81qurSVZzf8SwCtsDY | | Publickey | GPH6HqQmNUBb3AqTfW7MtBqVCPXDJuKnZVEeyxPNk64sE6QYQKyH5 | | Address | GPH9Jw8v7vmqKTaMjJpac2xefehQ7d9nJkAm | ±----------- ±--------------------------------------------------------------------------------------+

显示用户

node showuser.js
这是你的信息 

±-------------±--------------------------------------------------------------------------------------+ | Privatekey | 5JdGzR28pdh6c3vKrwLuQvWXbznJmrPwE81qurSVZzf8SwCtsDY | | Publickey | GPH6HqQmNUBb3AqTfW7MtBqVCPXDJuKnZVEeyxPNk64sE6QYQKyH5 | | Address | GPH9Jw8v7vmqKTaMjJpac2xefehQ7d9nJkAm | ±----------- ±--------------------------------------------------------------------------------------+

信息上链

node msg.js haha localhost 3001

交流

欢迎关注公众号 ForkChain

欢迎进入芥末圈

备注

区块链的部分是我自己写的,p2p 这部分主要来源于 https://github.com/zfzGit/P2PNodejs/blob/master/main.js 项目。

https://github.com/forkchain/alpha


2 回复

很强可以加一下 wx 交流一下吗? wx:vilink


当然,下面是一个简单的无币区块链(Blockchain)的Node.js实现示例。这个示例只展示了区块链的基本结构和工作原理,而不涉及任何加密货币或交易。

class Block {
    constructor(index, previousHash, timestamp, data) {
        this.index = index;
        this.previousHash = previousHash;
        this.timestamp = timestamp;
        this.data = data;
        this.hash = this.calculateHash();
    }

    calculateHash() {
        return `${this.index}${this.previousHash}${this.timestamp}${this.data}`.toString();
    }
}

class Blockchain {
    constructor() {
        this.chain = [this.createGenesisBlock()];
    }

    createGenesisBlock() {
        return new Block(0, "0", Date.now(), "Genesis Block");
    }

    addBlock(data) {
        const previousBlock = this.chain[this.chain.length - 1];
        const newBlock = new Block(
            this.chain.length,
            previousBlock.hash,
            Date.now(),
            data
        );
        this.chain.push(newBlock);
    }

    printChain() {
        this.chain.forEach(block => console.log(block));
    }
}

const blockchain = new Blockchain();
blockchain.addBlock("First Block after Genesis");
blockchain.addBlock("Second Block");
blockchain.printChain();

这个代码定义了一个Block类和一个Blockchain类。Block类包含区块的索引、前一个区块的哈希、时间戳和数据,并计算当前区块的哈希。Blockchain类包含创建创世区块和添加新区块的方法。最后,我们创建一个区块链实例,添加几个区块,并打印区块链。

注意:这个示例中的哈希计算非常简单,仅用于演示。在实际应用中,需要使用更复杂的哈希算法(如SHA-256)来确保区块链的安全性。

回到顶部