Last active
January 8, 2019 18:10
-
-
Save harshvishu/d70da476259ee21f44f37d41cd380123 to your computer and use it in GitHub Desktop.
Swift-Blockchain-Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instance of blockchain | |
let blockchain = Blockchain() | |
// Create a new router | |
let router = Router() | |
router.all(middleware: BodyParser()) | |
// Handle HTTP GET requests to / | |
router.get("/") { | |
request, response, next in | |
defer {next()} | |
response.status(.OK) | |
response.send("Welcome to Swift Blockchain") | |
} | |
router.get("/mine") { | |
request, response, next in | |
defer {next()} | |
let last_block = blockchain.last_block | |
let last_proof = last_block.proof | |
let proof = blockchain.proofOfWork(last_proof: last_proof) | |
// We must receive a reward for finding the proof. | |
// The sender is "0" to signify that this node has mined a new coin. | |
_ = blockchain.newTransaction(sender: "0", recipient: blockchain.node_identifier, amount: 1) | |
// Forge the new Block by adding it to the chain | |
let previous_hash = blockchain.hash(block: last_block) | |
let block = blockchain.newBlock(previous_hash: previous_hash, proof: proof) | |
struct ResponseData: Encodable { | |
let message: String | |
let index: Int64 | |
let transactions: [Transaction] | |
let proof: Int64 | |
let previous_hash: String | |
} | |
let response_data = ResponseData(message: "New Block Foreged", | |
index: block.index, | |
transactions: block.transactions, | |
proof: proof, | |
previous_hash: previous_hash) | |
response.status(.OK) | |
response.send(response_data) | |
} | |
router.post("/transactions/new") { | |
request, response, next in | |
defer {next()} | |
guard let body = request.body?.asJSON, | |
let sender = body["sender"] as? String, | |
let recipient = body["recipient"] as? String, | |
let amount = body["amount"] as? Int64 | |
else { | |
response.status(.badRequest) | |
response.send("Missing values") | |
return | |
} | |
let index = blockchain.newTransaction(sender: sender, recipient: recipient, amount: amount) | |
struct ResponseData: Encodable { | |
let message: String | |
} | |
let response_data = ResponseData(message: "Transaction will be added to Block \(index)") | |
response.status(.created) | |
response.send(response_data) | |
} | |
router.get("/chain") { | |
request, response, next in | |
defer {next()} | |
struct ResponseData: Encodable { | |
let chain: [Block] | |
let length: Int | |
} | |
let response_data = ResponseData(chain: blockchain.chain, length: blockchain.chain.count) | |
response.status(.OK) | |
response.send(response_data) | |
} | |
router.post("/nodes/register") { | |
request, response, next in | |
defer {next()} | |
guard let body = request.body?.asJSON, | |
let nodes = body["nodes"] as? [String] | |
else { | |
response.status(.badRequest) | |
response.send("Error: Please supply a valid list of nodes") | |
return | |
} | |
for node in nodes { | |
_ = blockchain.registerNode(address: node) | |
} | |
struct ResponseData: Encodable { | |
let message: String | |
let total_nodes: [String] | |
} | |
let response_data = ResponseData(message: "New nodes have been added", total_nodes: Array(blockchain.nodes)) | |
response.status(.created) | |
response.send(response_data) | |
} | |
router.get("/nodes/resolve") { | |
request, response, next in | |
defer {next()} | |
let replaced = blockchain.resolveConflicts() | |
response.status(.OK) | |
if replaced { | |
struct ResponseData: Encodable { | |
let message: String | |
let new_chain: [Block] | |
} | |
let response_data = ResponseData(message: "Our chain was replaced", new_chain: blockchain.chain) | |
response.send(response_data) | |
} else { | |
struct ResponseData: Encodable { | |
let message: String | |
let chain: [Block] | |
} | |
let response_data = ResponseData(message: "Our chain is authoritative", chain: blockchain.chain) | |
response.send(response_data) | |
} | |
} | |
// Add an HTTP server and connect it to the router | |
Kitura.addHTTPServer(onPort: 5000, with: router) | |
// Start the Kitura runloop (this call never returns) | |
Kitura.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment