Skip to content

Instantly share code, notes, and snippets.

View azamsharp's full-sized avatar

Mohammad Azam azamsharp

View GitHub Profile
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
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)
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)
}
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)
let json = """
{
"foo" : "hello",
"bar" : 123
}
""".data(using: .utf8)!
let dictionary = try! JSONDecoder().decode([String:Any].self, from: json)
public struct AnyDecodable : Decodable {
let value :Any
public init<T>(_ value :T?) {
self.value = value ?? ()
}
public init(from decoder :Decoder) throws {
let dictionary = try! JSONDecoder().decode([String:AnyDecodable].self, from: json)
dictionary["foo"]?.value // prints hello
# 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'
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
return true
}
//
// ViewController.swift
// FirebaseML
//
// Created by Mohammad Azam on 5/8/18.
// Copyright © 2018 Mohammad Azam. All rights reserved.
//
import UIKit
import Firebase