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 DrivingRecordSmartContract : Codable { | |
func apply(transaction :Transaction, allBlocks :[Block]) { | |
allBlocks.forEach { block in | |
block.transactions.forEach { trans in | |
if trans.driverLicenseNumber == transaction.driverLicenseNumber { | |
transaction.noOfVoilations += 1 |
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
private var drivingRecordSmartContract :DrivingRecordSmartContract = DrivingRecordSmartContract() | |
func getNextBlock(transactions :[Transaction]) -> Block { | |
let block = Block() | |
transactions.forEach { transaction in | |
// applying smart contract | |
self.drivingRecordSmartContract.apply(transaction: transaction, allBlocks: self.blocks) | |
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("/driving-records/:drivingLicenseNumber") { request in | |
guard let drivingLicenseNumber = request.parameters["drivingLicenseNumber"]?.string else { | |
return try JSONEncoder().encode(["message":"drivingLicenseNumber parameter not found!"]) | |
} | |
let blockchain = self.blockchainService.blockchain | |
let transactions = blockchain?.transactionsBy(drivingLicenseNumber: drivingLicenseNumber) | |
return try JSONEncoder().encode(transactions) | |
} |
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 transactionsBy(drivingLicenseNumber :String) -> [Transaction] { | |
var transactions = [Transaction]() | |
self.blocks.forEach { block in | |
block.transactions.forEach { transaction in | |
if transaction.driverLicenseNumber == drivingLicenseNumber.sha1Hash() { | |
transactions.append(transaction) |
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
let json = """ | |
{ | |
"foo" : "hello", | |
"bar" : 123 | |
} | |
""".data(using: .utf8)! | |
let dictionary = try! JSONDecoder().decode([String:Any].self, from: json) |
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
public struct AnyDecodable : Decodable { | |
let value :Any | |
public init<T>(_ value :T?) { | |
self.value = value ?? () | |
} | |
public init(from decoder :Decoder) throws { | |
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
let dictionary = try! JSONDecoder().decode([String:AnyDecodable].self, from: json) | |
dictionary["foo"]?.value // prints hello |
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
# Uncomment the next line to define a global platform for your project | |
# platform :ios, '9.0' | |
target 'FirebaseML' do | |
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks | |
use_frameworks! | |
# Pods for FirebaseML | |
pod 'Firebase/Core' |
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 application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
// Override point for customization after application launch. | |
FirebaseApp.configure() | |
return true | |
} |
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
// | |
// ViewController.swift | |
// FirebaseML | |
// | |
// Created by Mohammad Azam on 5/8/18. | |
// Copyright © 2018 Mohammad Azam. All rights reserved. | |
// | |
import UIKit | |
import Firebase |