Guest User

Untitled

a guest
Jun 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. const SHA256 = require('crypto-js/sha256')
  2.  
  3. class Block {
  4. constructor(index, timestamp, data, previousHash) {
  5. this.index = index
  6. this.timestamp = timestamp
  7. this.data = data
  8. this.previousHash = previousHash
  9. this.hash = this.calculateHash()
  10. this.nonce = 0
  11. }
  12.  
  13. calculateHash() {
  14. return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data) + this.nonce).toString()
  15. }
  16.  
  17. mine(difficulty) {
  18.  
  19. console.log("mining block: " + this.index)
  20. console.log("... difficulty: " + difficulty)
  21.  
  22. while(this.hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")) {
  23. this.nonce++
  24. this.hash = this.calculateHash()
  25. }
  26.  
  27. console.log("... block mined: " + this.hash)
  28. console.log("... nonce: " + this.nonce)
  29. }
  30. }
Add Comment
Please, Sign In to add comment