Skip to content

Commit

Permalink
Feature/payload method cbor (#103)
Browse files Browse the repository at this point in the history
* added debug to CountryModel

* Fixed CountryModel

* CBOR payload method added

Co-authored-by: Alexandr Chernyy <[email protected]>
Co-authored-by: Illia Vlasov <[email protected]>
  • Loading branch information
3 people authored Sep 13, 2021
1 parent d31c610 commit 3c3f26a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 83 deletions.
21 changes: 17 additions & 4 deletions Sources/Models/CountryModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@ import Foundation

public class CountryModel: Codable {
public var code: String
public var debugModeEnabled: Bool

public var name: String {
get { l10n("country.\(code.uppercased())")}
}

public init(code: String) {
public init(code: String, debugModeEnabled: Bool = false) {
self.code = code
self.debugModeEnabled = debugModeEnabled
}

enum CodingKeys: String, CodingKey {
case code = "code"
case code = "code", debugModeEnabled
}

// Init Rule from JSON Data
required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
code = try container.decode(String.self, forKey: .code)
debugModeEnabled = try container.decodeIfPresent(Bool.self, forKey: .debugModeEnabled) ?? false
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(code, forKey: .code)
try container.encode(debugModeEnabled, forKey: .debugModeEnabled)
}


}
164 changes: 86 additions & 78 deletions Sources/Services/CBOR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import Foundation
import SwiftCBOR

struct UnwrappedCBOR {
let payload: SwiftCBOR.CBOR
let protected: SwiftCBOR.CBOR
let unprotected: [SwiftCBOR.CBOR: SwiftCBOR.CBOR]
let protectedBytes: [UInt8]
let payloadBytes: [UInt8]
let signatureBytes: [UInt8]
let payload: SwiftCBOR.CBOR
let protected: SwiftCBOR.CBOR
let unprotected: [SwiftCBOR.CBOR: SwiftCBOR.CBOR]
let protectedBytes: [UInt8]
let payloadBytes: [UInt8]
let signatureBytes: [UInt8]
}

enum CborType: UInt8 {
Expand All @@ -57,22 +57,22 @@ public struct CBOR {

let type = CborType.from(data: data);
let decoder = SwiftCBOR.CBORDecoder(input: data.uint)

switch type {
case .tag:
guard
let cbor = try? decoder.decodeItem(),
case let SwiftCBOR.CBOR.tagged(_, cborElement) = cbor,
case let SwiftCBOR.CBOR.array(array) = cborElement,
case let SwiftCBOR.CBOR.byteString(protectedBytes) = array[0],
let protected = try? SwiftCBOR.CBOR.decode(protectedBytes),
case let SwiftCBOR.CBOR.map(unprotectedMap) = array[1],
case let SwiftCBOR.CBOR.byteString(payloadBytes) = array[2],
case let SwiftCBOR.CBOR.byteString(signatureBytes) = array[3],
let payload = try? SwiftCBOR.CBOR.decode(payloadBytes)
else {
return nil
}
let cbor = try? decoder.decodeItem(),
case let SwiftCBOR.CBOR.tagged(_, cborElement) = cbor,
case let SwiftCBOR.CBOR.array(array) = cborElement,
case let SwiftCBOR.CBOR.byteString(protectedBytes) = array[0],
let protected = try? SwiftCBOR.CBOR.decode(protectedBytes),
case let SwiftCBOR.CBOR.map(unprotectedMap) = array[1],
case let SwiftCBOR.CBOR.byteString(payloadBytes) = array[2],
case let SwiftCBOR.CBOR.byteString(signatureBytes) = array[3],
let payload = try? SwiftCBOR.CBOR.decode(payloadBytes)
else {
return nil
}
return .init(payload: payload,
protected: protected,
unprotected: unprotectedMap,
Expand All @@ -81,17 +81,17 @@ public struct CBOR {
signatureBytes: signatureBytes)
case .list:
guard
let cbor=try? SwiftCBOR.CBOR.decode(data.uint),
case let SwiftCBOR.CBOR.array(array) = cbor,
case let SwiftCBOR.CBOR.byteString(protectedBytes) = array[0],
let protected = try? SwiftCBOR.CBOR.decode(protectedBytes),
case let SwiftCBOR.CBOR.map(unprotectedMap) = array[1],
case let SwiftCBOR.CBOR.byteString(payloadBytes) = array[2],
case let SwiftCBOR.CBOR.byteString(signatureBytes) = array[3],
let payload = try? SwiftCBOR.CBOR.decode(payloadBytes)
else {
return nil
}
let cbor=try? SwiftCBOR.CBOR.decode(data.uint),
case let SwiftCBOR.CBOR.array(array) = cbor,
case let SwiftCBOR.CBOR.byteString(protectedBytes) = array[0],
let protected = try? SwiftCBOR.CBOR.decode(protectedBytes),
case let SwiftCBOR.CBOR.map(unprotectedMap) = array[1],
case let SwiftCBOR.CBOR.byteString(payloadBytes) = array[2],
case let SwiftCBOR.CBOR.byteString(signatureBytes) = array[3],
let payload = try? SwiftCBOR.CBOR.decode(payloadBytes)
else {
return nil
}
return .init(payload: payload,
protected: protected,
unprotected: unprotectedMap,
Expand All @@ -100,19 +100,19 @@ public struct CBOR {
signatureBytes: signatureBytes)
case .cwt:
guard
let cbor = try? decoder.decodeItem(),
case let SwiftCBOR.CBOR.tagged(_, cborElement) = cbor,
case let SwiftCBOR.CBOR.tagged(_, childElement) = cborElement,
case let SwiftCBOR.CBOR.array(array) = childElement,
case let SwiftCBOR.CBOR.byteString(protectedBytes) = array[0],
let protected = try? SwiftCBOR.CBOR.decode(protectedBytes),
case let SwiftCBOR.CBOR.map(unprotectedMap) = array[1],
case let SwiftCBOR.CBOR.byteString(payloadBytes) = array[2],
case let SwiftCBOR.CBOR.byteString(signatureBytes) = array[3],
let payload = try? SwiftCBOR.CBOR.decode(payloadBytes)
else {
return nil
}
let cbor = try? decoder.decodeItem(),
case let SwiftCBOR.CBOR.tagged(_, cborElement) = cbor,
case let SwiftCBOR.CBOR.tagged(_, childElement) = cborElement,
case let SwiftCBOR.CBOR.array(array) = childElement,
case let SwiftCBOR.CBOR.byteString(protectedBytes) = array[0],
let protected = try? SwiftCBOR.CBOR.decode(protectedBytes),
case let SwiftCBOR.CBOR.map(unprotectedMap) = array[1],
case let SwiftCBOR.CBOR.byteString(payloadBytes) = array[2],
case let SwiftCBOR.CBOR.byteString(signatureBytes) = array[3],
let payload = try? SwiftCBOR.CBOR.decode(payloadBytes)
else {
return nil
}
return .init(payload: payload,
protected: protected,
unprotected: unprotectedMap,
Expand All @@ -122,42 +122,50 @@ public struct CBOR {
case .unknown:
return nil;
}
}

public static func payload(from data: Data) -> SwiftCBOR.CBOR? {
return unwrap(data: data)?.payload
}

public static func header(from data: Data) -> SwiftCBOR.CBOR? {
return unwrap(data: data)?.protected
}

public static func kid(from data: Data) -> [UInt8]? {
let cosePhdrKid = SwiftCBOR.CBOR.unsignedInt(4)

let unwrap = unwrap(data: data)
guard
let protected = unwrap?.protected,
case let SwiftCBOR.CBOR.map(protectedMap) = protected,
let unprotected = unwrap?.unprotected
else {
return nil
}
let kid = protectedMap[cosePhdrKid] ?? (unprotected[cosePhdrKid] ?? .null)
switch kid {
case let .byteString(uint):
return uint
default:
return nil

public static func payload(from data: Data) -> SwiftCBOR.CBOR? {
return unwrap(data: data)?.payload
}
}

public static func hash(from cborData: Data) -> String {
guard
let data = COSE.signedPayloadBytes(from: cborData)
else {
return ""

public static func payloadBytes(from data: Data) -> [UInt8]? {
return unwrap(data: data)?.payloadBytes
}

public static func protectedBytes(from data: Data) -> [UInt8]? {
return unwrap(data: data)?.protectedBytes
}

public static func header(from data: Data) -> SwiftCBOR.CBOR? {
return unwrap(data: data)?.protected
}

public static func kid(from data: Data) -> [UInt8]? {
let cosePhdrKid = SwiftCBOR.CBOR.unsignedInt(4)

let unwrap = unwrap(data: data)
guard
let protected = unwrap?.protected,
case let SwiftCBOR.CBOR.map(protectedMap) = protected,
let unprotected = unwrap?.unprotected
else {
return nil
}
let kid = protectedMap[cosePhdrKid] ?? (unprotected[cosePhdrKid] ?? .null)
switch kid {
case let .byteString(uint):
return uint
default:
return nil
}
}

public static func hash(from cborData: Data) -> String {
guard
let data = COSE.signedPayloadBytes(from: cborData)
else {
return ""
}
return SHA256.digest(input: data as NSData).base64EncodedString()
}
return SHA256.digest(input: data as NSData).base64EncodedString()
}
}
3 changes: 2 additions & 1 deletion Sources/ViewControllers/Scan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ open class ScanVC: UIViewController {
#if targetEnvironment(simulator)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
// swiftlint:disable:next line_length
self.observationHandler(payloadS: "HC1:NCF/Y43088D0000MIU%LJJKDO51FY0TZGD7FU5WG72 73*ZKHJPMH2FTF-6FOZ31:911K-441526+6UNAB1J48K%7TORRP018O3K32IF8H7R7ZV4MS FR2SPQ-DI7P%B7E6U$/76OATWJ%QAJ5LE.IF240213*JC/EP6C98IJ9HZ QX-53IGJ8KQR3 THF%B5 5JB7/HVIZ5XZ7JXABM1ZP1JM0BJQXZUG2EI782X9GU6OKNQS8GQCNRCQA4WGAL35TCL5R41C57W46+E J4KJDU4R 00XZPPNP0QMAVG0.TYQGBKOF1G%TKFB62.O/Y807UI%A4/EHS8K%O9SS017J47V5WKXQKEJEWTU8SLMIDU7RR19XK54RV$9ELJQTAFP1858EC65QH5EQB:N8ARAQA23EG7T% NI-TVZH:$5/GH+PC0-DKIT2F6.2OK:U%9T$UKMCLU4DGYT3TNBZMN1WLORN:UOQBI05 9ME8PQBORKDM4")
self.observationHandler(payloadS: "HC1:6BFA70$90T9WTWGSLKC 4X7923S%CA.48Y+6/AB3XK5F3 026003F3RD6Z*B1JC X8Y50.FK8ZKO/EZKEZ967L6C56..DX%DZJC2/D:+9 QE5$CLPCG/D0.CHY8ITAUIAI3DG8DXFF 8DXEDU3EI3DAWE1Z9CN9IB85T9JPCT3E5JDOA73467463W5-A67:EDOL9WEQDD+Q6TW6FA7C466KCK9E2H9G:6V6BEM6Q$D.UDRYA 96NF6L/5QW6307B$D% D3IA4W5646946%96X47XJC$+D3KC.SCXJCCWENF6OF63W5CA7746WJCT3E0ZA%JCIQEAZAWJC0FD6A5AIA%G7X+AQB9F+ALG7$X85+8+*81IA3H87+8/R8/A8+M986APH9$59/Y9WA627B873 3K9UD5M3JFG.BOO3L-GE828UE0T46/*JSTLE4MEJRX797NEXF5I$2+.LGOJXF24D2WR9 W8WQT:HHJ$7:TKP2RT+J:G4V5GT7E")
// HC1:NCF/Y43088D0000MIU%LJJKDO51FY0TZGD7FU5WG72 73*ZKHJPMH2FTF-6FOZ31:911K-441526+6UNAB1J48K%7TORRP018O3K32IF8H7R7ZV4MS FR2SPQ-DI7P%B7E6U$/76OATWJ%QAJ5LE.IF240213*JC/EP6C98IJ9HZ QX-53IGJ8KQR3 THF%B5 5JB7/HVIZ5XZ7JXABM1ZP1JM0BJQXZUG2EI782X9GU6OKNQS8GQCNRCQA4WGAL35TCL5R41C57W46+E J4KJDU4R 00XZPPNP0QMAVG0.TYQGBKOF1G%TKFB62.O/Y807UI%A4/EHS8K%O9SS017J47V5WKXQKEJEWTU8SLMIDU7RR19XK54RV$9ELJQTAFP1858EC65QH5EQB:N8ARAQA23EG7T% NI-TVZH:$5/GH+PC0-DKIT2F6.2OK:U%9T$UKMCLU4DGYT3TNBZMN1WLORN:UOQBI05 9ME8PQBORKDM4
}
#else
captureSession = AVCaptureSession()
Expand Down

0 comments on commit 3c3f26a

Please sign in to comment.