Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Simple Cryptocurrency Blockchain Using JavaScript

const sha256 = require('sha256');

function Blockchain() {
    this.chain = [];   //stores our blocks
    this.pendingTransactions = []; //this contains our pending transaction before create a new block
    this.createNewBlock(100, '0', '0');
}

Blockchain.prototype.createNewBlock = function(nonce, prevBlockHash, hash){
    const newBlock = {
        index : this.chain.length + 1,
        timestamp : Date.now(),
        transactions : this.pendingTransactions,
        nonce : nonce,
        prevBlockHash : prevBlockHash,
        hash : hash
    };

    this.pendingTransactions = [];
    this.chain.push(newBlock);

    return newBlock;
}

Blockchain.prototype.getLastBlock = function() {
    return this.chain[this.chain.length - 1];
}

Blockchain.prototype.createNewTransaction = function(amount, sender, recipient) {
    const NewTransaction = {
        amount : amount,
        sender : sender,
        recipient : recipient
    };

    this.pendingTransactions.push(NewTransaction);

    return this.getLastBlock()['index'] +1;
}

Blockchain.prototype.hashBlock = function(prevBlockHash, currentBlockData, nonce) {
    const dataString  = prevBlockHash + nonce.toString() + JSON.stringify(currentBlockData);
    const hash = sha256(dataString);

    return hash;
}

Blockchain.prototype.proofOfWork = function(prevBlockHash, currentBlockData) {
    let nonce = 0;
    let hash = this.hashBlock(prevBlockHash, currentBlockData, nonce);

    while (hash.substring(0,4) !== '0000') {
        nonce++;
        hash = this.hashBlock(prevBlockHash, currentBlockData, nonce);
    }

    return nonce;
}

module.exports = Blockchain;
Comment

PREVIOUS NEXT
Code Example
Javascript :: react native text input allow only numbers 
Javascript :: yoptascript 
Javascript :: Executing Code When Instance Is Created 
Javascript :: how to get content disposition from header jquery 
Javascript :: ms dyn crm associate n:m record js 
Javascript :: jquery ajax success function not executing 
Javascript :: javascript ls 
Javascript :: Fibonacci numbers for n terms 
Javascript :: adding amplify in index.js react native 
Javascript :: communicate between content script and bg 
Javascript :: js how to get random number (inclusive min max) and push it in an array 
Javascript :: save to text or html file very good 
Javascript :: Recursion In A Class Function 
Javascript :: how to get mempool transactions and decode with ethers js 
Javascript :: JavaScript HTMLCollection Object 
Javascript :: docker healthcheck express 
Javascript :: leetcode solution problem 66 plus one 
Javascript :: how to create existing nodes in godot 
Javascript :: supabase 
Javascript :: clear an array 
Javascript :: react native long form keyboard awaire 
Javascript :: get selected data items kendo grid 
Javascript :: promise object 
Javascript :: dom traversal jquery 
Javascript :: 2d arrays js 
Javascript :: prevent page scrolling when a modal is open 
Javascript :: chatbot js 
Javascript :: knockout subscribe 
Javascript :: javascript type 
Javascript :: javascript arrow function syntax 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =