BlockController.js
/**
   * Web API POST endpoint with JSON response that submits the star information to be saved in the blockchain, url: "/block"
   */
Web API POST endpoint with JSON response that submits the star information to be saved in the blockchain, url: "/block"
Web API POST endpoint with JSON response that submits the star information to be saved in the blockchain url : " / block "
postBlock() {
    this.app.post("/block", async (req, res) => { 
      try {
        let address = req.body.address;
        if(!this.mempool.verifyAddressRequest(address)) {
          let errorMessage = "Wallet address is not in mempool or has timed out, use /requestValidation to request validation"
          console.log(errorMessage);
          res.status(400).send(errorMessage);
          res.end();
          return;
        }
        if(!this.mempool.verifyWalletValidationRequest(address)) {
          let errorMessage = "Wallet address has not been validated with the bitcoin wallet, use /message-signature/validate to request validation"
          console.log(errorMessage);
          res.status(400).send(errorMessage);
          res.end();
          return;
        }
        let star = req.body.star;
        if(!star["dec"] || !star["ra"] || !star["story"]) {
          let errorMessage = "dec, ra and story are mandatory"
          console.log(errorMessage);
          res.status(400).send(errorMessage);
          res.end();
          return;
        }
        let story = star.story;
        let storyBuffer = Buffer.from(story, "utf8");
        let hexEncodedStory = storyBuffer.toString("hex");
        let body = {
          "address" : address,
          "star" : {
            "ra" : star["ra"],
            "dec" : star["dec"],
            "mag" : star["mag"],
            "cen" : star["cen"],
            "story" : hexEncodedStory
          }
        }
        let newBlock = new Block.Block(body);
        newBlock = await this.blockchain.addBlock(newBlock);
        this.mempool.removeValidationRequest(address);
        res.json(newBlock);
      }
      catch(err) {
        let errorMessage = `Cannot add block to the chain: ${err}`;
        console.log(errorMessage);
        res.status(400).send(errorMessage);
      }
      res.end();
    });
  }
postBlock ( ) { this . app . post ( "/block" , async ( req , res ) => { try { let address = req . body . address ; if ( ! this . mempool . verifyAddressRequest ( address ) ) { let errorMessage = "Wallet address is not in mempool or has timed out, use /requestValidation to request validation" console . log ( errorMessage ) ; res . status ( 400 ) . send ( errorMessage ) ; res . end ( ) ; return ; } if ( ! this . mempool . verifyWalletValidationRequest ( address ) ) { let errorMessage = "Wallet address has not been validated with the bitcoin wallet, use /message-signature/validate to request validation" console . log ( errorMessage ) ; res . status ( 400 ) . send ( errorMessage ) ; res . end ( ) ; return ; } let star = req . body . star ; if ( ! star [ "dec" ] || ! star [ "ra" ] || ! star [ "story" ] ) { let errorMessage = "dec, ra and story are mandatory" console . log ( errorMessage ) ; res . status ( 400 ) . send ( errorMessage ) ; res . end ( ) ; return ; } let story = star . story ; let storyBuffer = Buffer . from ( story , "utf8" ) ; let hexEncodedStory = storyBuffer . toString ( "hex" ) ; let body = { "address" : address , "star" : { "ra" : star [ "ra" ] , "dec" : star [ "dec" ] , "mag" : star [ "mag" ] , "cen" : star [ "cen" ] , "story" : hexEncodedStory } } let newBlock = new Block . Block ( body ) ; newBlock = await this . blockchain . addBlock ( newBlock ) ; this . mempool . removeValidationRequest ( address ) ; res . json ( newBlock ) ; } catch ( err ) { let errorMessage = ` ${ err } ` ; console . log ( errorMessage ) ; res . status ( 400 ) . send ( errorMessage ) ; } res . end ( ) ; } ) ; }
Web API POST endpoint with JSON response that submits the star information to be saved in the blockchain, url: "/block"
Web API POST endpoint with JSON response that submits the star information to be saved in the blockchain url : " / block "
// Check if address is already in the mempool // Check if address is validated with the bitcoin wallet