Giter VIP home page Giter VIP logo

swiftyblockchain's Introduction

License

Swifty Blockchain

A Blockchain developer course project. Simple blockchain implementation. Based on Vapor 4.

Requirements

  • Swift 5.6
  • Linux or macOS, tested on Ubuntu 20.04 and macOS 13.3.1

Install Swift toolchain

Build

git clone https://github.com/DimaRU/SwiftyBlockchain
cd SwiftyBlockchain
swift build

Run

Start first node:

swift run Run serve --hostname 127.0.0.1 --port 8080

Start second node

swift run Run serve --hostname 127.0.0.1 --port 8081

Documentation:

API Endpoints


Method Route Comment
GET / Check work
GET /chain Fetch the blockchain
GET /nodes/resolve Syncronise blockchain
GET /mine Mining new block
POST /transactions/add Add new transaction
POST /nodes/register Register new node in the network

Fetching the blockchain

curl -X GET http://localhost:8080/chain
{
  "chain": [
    {
      "nonce": 100,
      "timestamp": 1651744662.377732,
      "previous_hash": "1",
      "transactions": [
      ],
      "index": 1
    }
  ],
  "length": 1
}

Adding a new transaction

curl -X POST http://localhost:8080/transactions/add \
  -H 'content-type: application/json' \
  -d '{
 "sender": "Alice",
 "recipient": "Bob",
 "amount": 5
}'

Transaction added to Block 2

Mining a new Block

curl -X GET http://localhost:8080/mine
{
  "message": "New Block Mined",
  "nonce": 33575,
  "previous_hash": "49b04ba15e4c658069d77dd22f8d21fb847a3bba92b92e4a5cdf0cff905e26cb",
  "transactions": [
    {
      "amount": 5,
      "recipient": "Bob",
      "sender": "Alice"
    },
    {
      "amount": 2,
      "recipient": "127.0.0.1:8080",
      "sender": ""
    }
  ],
  "index": 2
}

Registering a new Node in the Network

curl -X "POST" "http://127.0.0.1:8080/nodes/register" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "address": "http://127.0.0.1:8081"
}'
{
  "message": "New node added",
  "nodes": [
    "http://127.0.0.1:8081"
  ]
}

Syncronise blockchain and resolve conflicts

curl -X GET "http://127.0.0.1:8080/nodes/resolve"
{
  "message": "Our chain was replaced",
  "chain": [
    {
      "nonce": 100,
      "timestamp": 1651697093.6841831,
      "previous_hash": "1",
      "transactions": [],
      "index": 1
    },
    {
      "nonce": 33575,
      "timestamp": 1651697144.188246,
      "previous_hash": "e790a22b1acd2f7a06efb941c8a6167468dcc5a39d25d72ace8360156f48033d",
      "transactions": [
        {
          "amount": 10,
          "recipient": "Bob",
          "sender": "Alice"
        },
        {
          "amount": 1,
          "recipient": "127.0.0.1:8081",
          "sender": "0"
        }
      ],
      "index": 2
    }
  ]
}

License

MIT

swiftyblockchain's People

Contributors

dimaru avatar

Watchers

 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.