You are viewing a single comment's thread from:

RE: Blockchain Development from Scratch with Typescript #3: Resolve Type error and Setup Express API Server

in #utopian-io6 years ago

Great tutorial, was easy to understand for a novice TypeScript coder.

You can use nodejs built in hashing library for making the sha256 hash so no need to import js-sha256 libraries ;)

var crypto = require('crypto');
var hash = crypto.createHash('sha256').update(block.key).digest("hex");

here is my complete generateHash method:

public generateHash(block: BlockData): string {
    let hash = crypto.createHash('sha256').digest("hex");
    
    // mining
    while(!hash.startsWith(Array(this.difficulty + 1).join('0'))) {
        block.nonce += 1;
        hash = crypto.createHash('sha256').update(block.key).digest("hex");
        console.log(block.nonce, hash);
    }

    return hash;
}
Sort:  

I din know that, was finding a library which supporr types haha. Thanks for sharing about this.