Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Accessing Our CryptoCurrency blockchain through local server

const express = require('express')
const app = express()
const bodyParser = require('body-parser');
const Blockchain = require("./Blockchain");
const {v4: uuidv4} = require('uuid');
const bitcoin = new Blockchain();

const port = process.argv[2];

const nodeAddress = uuidv4().split("-").join('');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));

// respond with "hello world" when a GET request is made to the homepage
app.get('/blockchain', (req, res) => {
  res.send(bitcoin)
})

// app.post('/transaction', (req, res) => {
//   const blockIndex = bitcoin.createNewTransaction(req.body.amount, req.body.sender, req.body.recipient);
//   res.json({note: `this transaction will be added in block ${blockIndex}`})
// })

app.get('/mine', (req, res) => {
  const lastBlock = bitcoin.getLastBlock();
  const prevBlockHash = lastBlock['hash'];
  const currentBlockData = {
    transaction : bitcoin.pendingTransactions,
    index : lastBlock['index'] + 1
  }
  const nonce = bitcoin.proofOfWork(prevBlockHash, currentBlockData);
  const blockHash = bitcoin.hashBlock(prevBlockHash, currentBlockData, nonce);

  const newBlock = bitcoin.createNewBlock(nonce, prevBlockHash, blockHash);

  bitcoin.createNewTransaction(100, '0', nodeAddress);

  res.json({
    note : "New block mined Successfully",
    block : newBlock
  })
})

app.get('/wallet', (req, res) => {
  res.sendFile(__dirname + "/index.html");
})

app.post('/wallet', (req, res) => {
  const blockIndex = bitcoin.createNewTransaction(req.body.amount, req.body.senderAddress, req.body.recipientAddress);
  res.json({note: `this transaction will be added in block ${blockIndex}`})
})


app.listen(port, function(){
  console.log(`Server is Running on ${port}`);
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: string to date with ist javascript 
Javascript :: javascript cookies all together 
Javascript :: Declare Function In Class Constructor 
Javascript :: add defer in tag manager 
Javascript :: javascript check if a number starts with another number 
Javascript :: var a = x || y Variable Assignment In JavaScript 
Javascript :: ip scanner node 
Javascript :: palindrome short way 
Javascript :: confirming the end javascript 
Javascript :: array operations = map, filter, find, reduce, some, every, indexOf 
Javascript :: Viewing Your React App On Another Device 
Javascript :: The JavaScript call() Method 
Javascript :: An Array Of Functions With Parameter 
Javascript :: add text to each element in an array javascript 
Javascript :: Update A Request() Property 
Javascript :: check for overlapping time javascript 
Javascript :: destructuring array es6 
Javascript :: javascript variable scope in if statement 
Javascript :: how to render react quill the way it is without the html tags 
Javascript :: react private routes 
Javascript :: xor two hex strings js 
Javascript :: js onclick add table row 
Javascript :: javascript last value of array 
Javascript :: router.push 
Javascript :: react how to get checkbox value on click 
Javascript :: Remove uploaded file in jquery 
Javascript :: flatpickr current date set to text field 
Javascript :: assing multipe ids jquery to event 
Javascript :: javascript Implicit Conversion to String 
Javascript :: javascript Arrow Function as an Expressio 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =