Last active
          December 28, 2017 06:35 
        
      - 
      
- 
        Save spenserhuang/057b1c4c19dd3d8baf20465bafb7aeb9 to your computer and use it in GitHub Desktop. 
    Blockchain Object
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class Blockchain{ | |
| constructor() { | |
| this.chain = [this.createGenesis()]; | |
| } | |
| createGenesis() { | |
| return new Block(0, "01/01/2017", "Genesis block", "0") | |
| } | |
| latestBlock() { | |
| return this.chain[this.chain.length - 1] | |
| } | |
| addBlock(newBlock){ | |
| newBlock.previousHash = this.latestBlock().hash; | |
| newBlock.hash = newBlock.calculateHash(); | |
| this.chain.push(newBlock); | |
| } | |
| checkValid() { | |
| for(let i = 1; i < this.chain.length; i++) { | |
| const currentBlock = this.chain[i]; | |
| const previousBlock = this.chain[i - 1]; | |
| if (currentBlock.hash !== currentBlock.calculateHash()) { | |
| return false; | |
| } | |
| if (currentBlock.previousHash !== previousBlock.hash) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment