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
class Blockchain : Codable { | |
var blocks :[Block] = [Block]() | |
init() { | |
} | |
init(_ genesisBlock :Block) { | |
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
class BlockchainController { | |
private (set) var drop :Droplet | |
private (set) var blockchainService :BlockchainService! | |
init(drop :Droplet) { | |
self.drop = drop | |
self.blockchainService = BlockchainService() | |
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
// | |
// BlockchainService.swift | |
// Run | |
// | |
// Created by Mohammad Azam on 12/25/17. | |
// | |
import Foundation | |
import Vapor |
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
class BlockchainNode :Codable { | |
var address :String | |
init(address :String) { | |
self.address = address | |
} | |
init?(request :Request) { | |
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
self.drop.post("nodes/register") { request in | |
guard let blockchainNode = BlockchainNode(request :request) else { | |
return try JSONEncoder().encode(["message":"Error registering node"]) | |
} | |
self.blockchainService.registerNode(blockchainNode) | |
return try JSONEncoder().encode(blockchainNode) | |
} |
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
func getNodes() -> [BlockchainNode] { | |
return self.blockchain.nodes | |
} | |
func registerNode(_ blockchainNode :BlockchainNode) { | |
self.blockchain.addNode(blockchainNode) | |
} |
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
self.drop.get("nodes/resolve") { request in | |
return try Response.async { portal in | |
self.blockchainService.resolve { blockchain in | |
let blockchain = try! JSONEncoder().encode(blockchain) | |
portal.close(with: blockchain.makeResponse()) | |
} | |
} |
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
func resolve(completion :@escaping (Blockchain) -> ()) { | |
// get the nodes | |
let nodes = self.blockchain.nodes | |
for node in nodes { | |
let url = URL(string :"http://\(node.address)/blockchain")! | |
URLSession.shared.dataTask(with: url) { data, _, _ in | |
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
class Block { | |
var index :Int = 0 | |
var dateCreated :String | |
var previousHash :String = "" | |
var hash :String! | |
var nonce :Int | |
var key :String { | |
get { |
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
enum TranslationType : String, Codable { | |
case international | |
case domestic | |
} | |
class Transaction : Codable { | |
var from :String | |
var to :String |