Let's build the final endpoint for our blockchain API: the mine endpoint, this will mine and create a new block:
- 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();
});
- 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: previousBlockHash
};
this.pendingTransactions = [];
this.chain.push(newBlock);
return...