Nodejs How to pass user form data into defined class

0 votes

I am just trying build a blockchain after watching a youtube video.

app.js:

const SHA256 = require('crypto-js/sha256');

class Block {
    constructor(data, previousHash = ''){
        this.previousHash = previousHash;
        this.data = data;
        this.hash = this.calculateHash();
    }

    calculateHash(){
        return SHA256(this.previousHash + JSON.stringify(this.data)).toString();
    }
}

class Blockchain{
    constructor(){
        this.chain = [this.createGenesisBlock()];
    }

    createGenesisBlock(){
        return new Block("Genesis Block", "0");
    }

    getLatestBlock(){
        return this.chain[this.chain.length - 1];
    }

    addBlock(newBlock){
        newBlock.previousHash = this.getLatestBlock().hash;
        newBlock.hash = newBlock.calculateHash();
        this.chain.push(newBlock);
    }

    isChainValid(){
        for(let i = 1; i < this.chain.length; i++){
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];

            if(currentBlock.hash !== currentBlock.calculateHash()){
                return false;
            }

            if(currentBlock.previousHash !== previousBlock.hash){
                return false;
            }
        }
        return true;
    }
}

let xyzcOin = new Blockchain();

xyzcOin.addBlock(new Block({amount: 8}));
xyzcOin.addBlock(new Block({amount: 80}));

// console.log("Is blockchain is valid? " + xyzcOin.isChainValid());

// xyzcOin.chain[1].data = {amout: 100};

// console.log("Is blockchain is valid? " + xyzcOin.isChainValid());

console.log(JSON.stringify(xyzcOin, null, 4));

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>xyzcOin Blockchain</title>
    <script type="text/javascript" src="app.js"></script>
</head>
<body>
    <div class="container">
        <div class="input">
            <form method="post">
                <label for="data">Please enter data:</label>
                <input type="text" name="data" id="data">
                <button type="submit" onclick="addData()">Add Data</button>
            </form>
        </div>
        <div class="result">
            <h4>Blocks Added</h4>
            <ul onload="appendData()">
                <li>Block 1:</li>
                <li>Block 2:</li>
                <li>Block 3:</li>
            </ul>
        </div>
        <div class="validate">
            <button type="submit">Validate Chain</button>
            <p>Chain validated? true or false</p>
        </div>
    </div>
</body>
</html>

Query:

1) User entered data should be taken as data for a block(on submitting add data button) and it should append to listitem dynamically after data entered in above textbox.

2)To validate the blockchain for any tampering, I have created a button to validate the chain and result should be shown.

Sep 19, 2018 in Blockchain by slayer
• 29,370 points
946 views

1 answer to this question.

0 votes

This should do your work:

// select the form
const formData = document.querySelector('.form-data');

// attach an event listener
formData.addEventListener('submit', (evt) => {
  evt.preventDefault(); 

  const inputVal =  evt.target[0].value;

  // enter url as first arg and data as second arg
  postData('http://example.com/answer', { data: inputVal })
  .then(data => {
     // JSON from `response.json()` call
    // do something with the data
    console.log(data)
  })
  .catch(error => console.error(error))
})

// helper function for making posts with fetch api 
function postData(url, data) {
  return fetch(url, {
    body: JSON.stringify(data), 
    headers: {
      'content-type': 'application/json'
    },
    method: 'POST', 
  })
  .then(response => response.json()) // parses response to JSON
}
<form class="form-data">
    <label for="data">Please enter data:</label>
    <input type="text" name="data" id="data">
    <button type="submit">Add Data</button>
</form>
answered Sep 19, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

How to input realtime data from google sheets into urlfetch google scripts?

Based on your title I hope to ...READ MORE

answered Oct 12, 2018 in Blockchain by Perry
• 17,100 points
1,463 views
+1 vote
1 answer

How to store state data in Ethereum blockchain?

You won't have to overwrite the whole ...READ MORE

answered Apr 25, 2018 in Blockchain by Shashank
• 10,400 points
972 views
+1 vote
1 answer
0 votes
1 answer

How to check the data integrity logic in proof of work mining?

The proof of work is actually works ...READ MORE

answered May 8, 2018 in Blockchain by Johnathon
• 9,090 points
802 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,427 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

How i can use nodejs to watch transactions in bitcoin network?

you can use  const Socket = require('blockchain.info/Socket'); const mySocket ...READ MORE

answered Jul 9, 2018 in Blockchain by digger
• 26,740 points
1,282 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP