Filter display for items in a blockchain (Javascript + node

Viewed 23

I'm currently learning to create a blockchain in Javascript (with node.js) following SimplyExplained on Youtube (https://www.youtube.com/watch?v=zVqczFZr124), and I would like to only display the blocks belong to the holder that the user asked for. I tried using the filter function for arrays but there's an error saying it's not a function.

It's basically the same as the one in the video mentioned but added "Issuer" and "Verifier":

const SHA256 = require("crypto-js/sha256")

var readline = require('readline-sync');
var a = 0;
var issuer = readline.question("Input your name as an issuer: ");
var holder = "default";

class Block{
    constructor(issuer, holder, timestamp, credential, previousHash = ""){
      this.issuer = issuer;
      this.holder = holder;
      this.timestamp = timestamp;
      this.credential = credential;
      this.previousHash = previousHash;
      this.hash = this.calculateHash();
    }
  
    calculateHash(){
      return SHA256(this.issuer + this.holder + this.timestamp + JSON.stringify(this.credential) + this.previousHash).toString();
    }

}

let mainchain = new Blockchain();

And then this part requires some simple user input, like asking if you're adding or viewing the credentials. There are only three "holders" for now to facilitate the debugging. (I allowed the display of the entire blockchain after adding so I can see what's wrong, usually I hide it)

while (a < 1){
    var userActionChoice = readline.question("Would you like to add or view? ");

if (userActionChoice.toLowerCase() == "add") {
    var holderID = readline.question("Input holder ID: ");
    var credential = readline.question("Input qualification/credential: "); 
    var timestamp = readline.question("Input date of qualification above: ");
    if (holderID.toLowerCase() == "s123456"){
        holder = "Beige Waves";
    }
    else if (holderID.toLowerCase() == "s987654"){
        holder = "Mickey Mouse";
    }
    else if (holderID.toLowerCase() == "s888888"){
        holder = "Donald Duck";
    } 
    mainchain.addBlock(new Block(issuer, holder, timestamp, credential));
        console.log(JSON.stringify(mainchain, null, 4));
        console.log("Added successfully to the Blockchain! It is " + mainchain.isChainValid() + " that the chain is valid.");
}
else {
    var holderID = readline.question("What is the ID of the holder whose qualifications you would like to check? ");
    if (holderID.toLowerCase() == "s123456"){
        holder = "Beige Waves";
    }
    else if (holderID.toLowerCase() == "s987654"){
        holder = "Mickey Mouse";
    }
    else if (holderID.toLowerCase() == "s888888"){
        holder = "Donald Duck";
    } 
    
var selected = mainchain.filter(function(selectedHolder){
    return selectedHolder.holder == holder;
});

console.log(JSON.stringify(selected))
}

    var userActionChoice = readline.question("Would you like to exit the program? (Y/N) ")

    if (userActionChoice.toLowerCase() == "y") {
        console.log("Goodbye.");
        a += 1;
    }
}

When I test it with node.js, the blockchain display was fine after adding something, but when I want to only show one holder's credentials, the error appeared:

macbookair% node /Users/(path)/blockchain.js 

Input your name as an issuer: disney
Would you like to add or view? add
Input holder ID: s123456
Input qualification/credential: driver's license
Input date of qualification above: today
{
    "chain": [
        {
            "issuer": "issuer",
            "holder": "default",
            "timestamp": "02/12/2021",
            "credential": "Genesis Block",
            "previousHash": "0",
            "hash": "6a2460df4316862c2bf2a5652e98b51a5adb1e46d6f175bf97ad5f638121bba6"
        },
        {
            "issuer": "disney",
            "holder": "Beige Waves",
            "timestamp": "today",
            "credential": "driver's license",
            "previousHash": "6a2460df4316862c2bf2a5652e98b51a5adb1e46d6f175bf97ad5f638121bba6",
            "hash": "b1fcbba67ccc78e3099d45b1996769a407b643d9bdc71f9c4bbc40b07f8ee12f"
        }
    ]
}
Added successfully to the Blockchain! It is true that the chain is valid.
Would you like to exit the program? (Y/N) n

Would you like to add or view? view
What is the ID of the holder whose qualifications you would like to check? s123456
/Users/(path)/blockchain.js:105
var selected = mainchain.filter(function(selectedHolder){
                         ^

TypeError: mainchain.filter is not a function
    at Object.<anonymous> (/Users/(path)/blockchain.js:105:26)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

The result I think I want to see should be something like this, only with the blocks with credentials belonging to Beige Waves:

 {
            "issuer": "disney",
            "holder": "Beige Waves",
            "timestamp": "today",
            "credential": "driver's license",
            "previousHash": "6a2460df4316862c2bf2a5652e98b51a5adb1e46d6f175bf97ad5f638121bba6",
            "hash": "b1fcbba67ccc78e3099d45b1996769a407b643d9bdc71f9c4bbc40b07f8ee12f"
        }

Is it not working because the Blockchain is not an array (it has the curly brackets at the beginning instead of the square brackets)? Is there any solution to this problem? Thank you very much.

(PS. This is my first time using Stack Overflow and I'm not very sure what code snippets to show so I put all my codes in here except the ones with double slashes. I'd very much appreciate the feedback, and thank you once again.)

0 Answers
Related