Giter Site home page Giter Site logo

project-3-connect-private-blockchain-to-front-end-client-via-apis's Introduction

REST API for a private blockchain implementation using JavaScript, NodeJS, hapi.js and levelDB

Blockchain has the potential to change the way that the world approaches data. Develop Blockchain skills by understanding the data model behind Blockchain by developing your own simplified private blockchain. This projects provides a easy way to communicate with the private NodeJS blockchain, allowing clients to request block data for a given height and to create new blocks.

NodeJS

This project depends on NodeJS to work, you can download it and install it for your platform from here

Install node packages

npm install

Install dependencies

All the necessary packages are automatically installed by npm based on the package.json file

Dependencies

  1. boom v7.3.0 for easy hapi.js exception handling
  2. crypto-js v3.1.9-1 to generate SHA256 hashes and crypto related functionality
  3. hapi v18.1.0 NodeJS framework to create REST API
  4. level v4.0.0 Fast & simple storage. A Node.js-style LevelDB wrapper.

Running the server

The server runs on localhost, port 8000. To execute the NodeJS app run the following

node BlockAPI.js

The server and port are showed in the console, as well as the current Blockheight

Server running at: http://localhost:8000
Current Blockchain Height 0

API

Currently, the API supports two methods:

Response example

{
"hash": "8b43a6453a8e26cd28bc186e7adf76221005f20e50d0ee9f741faf7bb088d54e",
"height": 2,
"body": "datos2",
"time": "1551744127",
"previousBlockHash": "9e626346fe4f6024e79bf859864837d62df6814e6b7c48249f1feef824a6811d"
}

If an out-of-bounds height is passed, server responds with a 404 HTTP code

{
"statusCode": 404,
"error": "Not Found",
"message": "Block not found."
}
  • http://localhost:8000/api/block/ Creates a new block in the chain - POST - Creates a new block with the data passed in the body property of JSON Object sent in the request and returns the new block as response with 201 Create HTTP Code

Example of JSON sent in the body request

{"body":"data for the block"}

It is important to indicate the content-type header as application/json

New block method response example

{  
   "hash":"e32c7fdf49cdf878e690bcfa9dc568f71795e56d55aece0945067e8ba7496bd6",
   "height":7,
   "body":"data for the block",
   "time":"1551829104",
   "previousBlockHash":"cdbe67d80e9f865875eecf58cd5960d2967473bd8665a817d741c35e3a86964d"
}

If there's no a valid JSON object sent in the request, the server responds with a 400 HTTP code

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "You have to specify a valid json object with a body property."
}

Authors

License

This project is licensed under the MIT License, for more information refer to the LICENSE.md file

Acknowledgements

I would like to thank to all of the Udacity team

Specially to the instructors in the following courses that helped me to make this project possible

project-3-connect-private-blockchain-to-front-end-client-via-apis's People

Contributors

irq20xdfr avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.