DEV Community

loizenai
loizenai

Posted on

How to explore Data inside of Blockchain Network in Javascript

https://grokonez.com/node-js/how-to-explore-data-blockchain-network-javascript-tutorial

How to explore Data inside of Blockchain Network in Javascript

In this tutorial, we're gonna build API that allows us to explore the data inside of our Blockchain Network. We will have ability to search the entire Blockchain for a specific Block by Hash, for a specific Transaction by Id. We can also fetch data from our Blockchain Network for a specific User/Address.

Previous Post: How to build Consensus Algorithm for Blockchain Network in Javascript

Explore Data Endpoints

We will make 3 endpoints:

  • GET /block/:hash: shows a specific Block by :hash.
  • GET /transaction/:id: shows a specific Transaction by Transaction's :id.
  • GET /address/:address: shows all Transactions that an :address is related to (address/user is a sender or a recipient).

These endpoints use 3 Blockchain class finder methods:


class Blockchain {

    findBlockByHash(hash) { ... }
    findTransactionById(id) { ... }
    findTransactionsByAddress(address) { ... }
}

Practice

Implement Blockchain class Finder Methods

Inside blockchain.js file, add 3 methods to Blockchain class:

Find specific Block by Hash


class Blockchain {
    ...
    findBlockByHash(hash) {
        let result = null;

        this.chain.forEach(block => {
            if (block.hash === hash) {
                result = block;
            }
        });

        return result;
    }
}

Find specific Transaction by Transaction's Id

More at:

https://grokonez.com/node-js/how-to-explore-data-blockchain-network-javascript-tutorial

How to explore Data inside of Blockchain Network in Javascript

Top comments (0)