Created
February 17, 2022 12:35
-
-
Save javedbaloch4/ec713ed05913cf787ff7febde5dc9dfb to your computer and use it in GitHub Desktop.
Hashing with Asynchronous programming
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
/* --------- 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