@kaiserb1/

BlockChainExample

Nodejs

No description

fork
loading
Files
  • index.js
index.js
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
31
32
33
34
35
36
const lighitingHash = (data) =>{
    return data + '*';
}

class Block{
    constructor(data, hash, lastHash){
        this.data = data;
        this.hash = hash;
        this.lastHash = lastHash;
    }
}

class Blockchain{
    constructor(){
        const genesis = new Block('gen-data', 'gen-hash', 'gen-lasthash');
        this.chain = [genesis];
    }
    addBlock(data){
        const lastHash = this.chain[this.chain.length-1].hash;
        const hash = lighitingHash(data+lastHash);
        const block = new Block(data, hash, lastHash);
        this.chain.push(block);
    }
}

const fooBlockchain = new Blockchain();
fooBlockchain.addBlock("One");
fooBlockchain.addBlock('Two');
fooBlockchain.addBlock('Three');
fooBlockchain.addBlock('4');
fooBlockchain.addBlock('5');
fooBlockchain.addBlock('6');
fooBlockchain.addBlock('7');
fooBlockchain.addBlock('8');

console.log(fooBlockchain);
node v10.16.0