Building the /mine endpoint

Let's build the final endpoint for our blockchain API: the mine endpoint, this will mine and create a new block:

  1. To create a new block, we are going to use our createNewBlock method, which we already defined in our blockchain.js file. Let's go to our api.js file and create a new block inside the /mine endpoint: 
app.get('/mine', function(req, res) {    const newBlock = bitcoin.createNewBlock();});
  1. This createNewBlock method takes in three parameters: nonce, previousBlockHash, and hash:
Blockchain.prototype.createNewBlock = function(nonce, previousBlockHash, hash) {  const newBlock = {    index: this.chain.length + 1,    timestamp: Date.now(),    transactions: this.pendingTransactions,    nonce: nonce,    hash: hash, previousBlockHash: ...

Get Learn Blockchain Programming with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.