Skip to content

Instantly share code, notes, and snippets.

@javedbaloch4
Created February 17, 2022 12:35
Show Gist options
  • Save javedbaloch4/ec713ed05913cf787ff7febde5dc9dfb to your computer and use it in GitHub Desktop.
Save javedbaloch4/ec713ed05913cf787ff7febde5dc9dfb to your computer and use it in GitHub Desktop.
Hashing with Asynchronous programming
/* --------- app.js ----------*/
// Import block class
const BlockClass = require('./block')
// Create a block object
const block = new BlockClass.Block("Test Block")
// Generate the block hash
block.generateHash().then((result) => {
console.log(`Block Hash: ${result.hash}`)
console.log(`Block: ${JSON.stringify(result)}`)
}).catch((error) => {
console.log(error)
})
/* --------- block.js ----------- */
const SHA256 = require('crypto-js/sha256')
class Block {
constructor(data) {
this.id = 0
this.nonce = 14444
this.body = data
this.hash = ""
}
generateHash() {
let self = this;
return new Promise(function(resolve, reject) {
self.hash = SHA256(JSON.stringify(self)).toString()
resolve(self)
})
}
}
module.exports.Block = Block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment