Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
phisakel committed Nov 30, 2023
2 parents 4e37a38 + f701027 commit 12943c9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
23 changes: 13 additions & 10 deletions Sources/eudi-lib-ios-wallet-kit/EudiWallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ public final class EudiWallet: ObservableObject {
/// - Parameters:
/// - id: Document identifier
/// - issuer: Issuer function
public func issueDocument(id: String, issuer: (_ send: IssueRequest) async throws -> WalletStorage.Document) async throws {
let request = try IssueRequest()
let document = try await issuer(request)
try storage.storageService.saveDocument(document)
public func beginIssueDocument(id: String) async throws -> IssueRequest {
let request = try IssueRequest(id: id)
try request.saveToStorage(storage.storageService)
return request
}

public func endIssueDocument(_ issued: WalletStorage.Document) throws {
try storage.storageService.saveDocumentData(issued, dataToSaveType: .doc, dataType: issued.docDataType.rawValue, allowOverwrite: true)
}

/// Load documents from storage
Expand All @@ -69,8 +73,8 @@ public final class EudiWallet: ObservableObject {
try? storageService.deleteDocuments()
let docSamples = (sampleDataFiles ?? ["EUDI_sample_data"]).compactMap { Data(name:$0) }
.compactMap(SignUpResponse.decomposeCBORSignupResponse(data:)).flatMap {$0}
.map { Document(docType: $0.docType, data: $0.jsonData, createdAt: Date.distantPast, modifiedAt: nil) }
for docSample in docSamples { try storageService.saveDocument(docSample) }
.map { Document(docType: $0.docType, docDataType: .cbor, data: $0.drData, privateKeyType: .x963EncodedP256, privateKey: $0.pkData, createdAt: Date.distantPast, modifiedAt: nil) }
for docSample in docSamples { try storageService.saveDocument(docSample, allowOverwrite: true) }
storage.loadDocuments()
}

Expand All @@ -88,10 +92,9 @@ public final class EudiWallet: ObservableObject {
guard var docs = try storageService.loadDocuments(), docs.count > 0 else { throw WalletError(description: "No documents found") }
if let docType { docs = docs.filter { $0.docType == docType} }
if let docType { guard docs.count > 0 else { throw WalletError(description: "No documents of type \(docType) found") } }
let srs = docs.compactMap {$0.data.decodeJSON(type: SignUpResponse.self)}; let drs = srs.compactMap(\.deviceResponse)
guard drs.count > 0 else { throw WalletError(description: "Documents decode error") }
guard let sr = srs.first, let dpk = sr.devicePrivateKey else { throw WalletError(description: "Error: No private key found") }
parameters = [InitializeKeys.document_signup_response_data.rawValue: drs, InitializeKeys.device_private_key.rawValue: dpk]
let cborsWithKeys = docs.compactMap { $0.getCborData() }
guard cborsWithKeys.count > 0 else { throw WalletError(description: "Documents decode error") }
parameters = [InitializeKeys.document_signup_response_obj.rawValue: cborsWithKeys.map(\.dr), InitializeKeys.device_private_key_obj.rawValue: cborsWithKeys.first!.dpk]
if let trustedReaderCertificates { parameters[InitializeKeys.trusted_certificates.rawValue] = trustedReaderCertificates }
default:
fatalError("jwt format not implemented")
Expand Down
2 changes: 2 additions & 0 deletions Sources/eudi-lib-ios-wallet-kit/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ extension String {





6 changes: 3 additions & 3 deletions Sources/eudi-lib-ios-wallet-kit/Services/Enumerations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public enum FlowType: Codable, Hashable {
}

/// Data format of the exchanged data
public enum DataFormat {
case cbor
case jwt
public enum DataFormat: String {
case cbor = "cbor"
case sjwt = "sjwt"
}

public enum StorageType {
Expand Down
13 changes: 8 additions & 5 deletions Sources/eudi-lib-ios-wallet-kit/Services/StorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import SwiftCBOR
import MdocDataModel18013
import WalletStorage
import Logging
import CryptoKit

/// Storage manager. Provides services and view models
public class StorageManager: ObservableObject {
Expand Down Expand Up @@ -50,9 +51,9 @@ public class StorageManager: ObservableObject {
}

fileprivate func refreshPublishedVars() {
hasWellKnownData = !Set(docTypes.compactMap {$0}).isDisjoint(with: Self.knownDocTypes)
hasData = documentIds.compactMap { $0 }.count > 0
docCount = documentIds.compactMap { $0 }.count
hasData = mdocModels.compactMap { $0 }.count > 0
hasWellKnownData = hasData && !Set(docTypes.compactMap {$0}).isDisjoint(with: Self.knownDocTypes)
docCount = mdocModels.compactMap { $0 }.count
mdlModel = getTypedDoc()
pidModel = getTypedDoc()
otherModels = getTypedDocs()
Expand All @@ -68,7 +69,7 @@ public class StorageManager: ObservableObject {
mdocModels = docs.map { _ in nil }
documentIds = docs.map(\.id)
for (i, doc) in docs.enumerated() {
guard let sr = doc.data.decodeJSON(type: SignUpResponse.self), let dr = sr.deviceResponse, let dpk = sr.devicePrivateKey else { continue }
guard let (dr,dpk) = doc.getCborData() else { continue }
mdocModels[i] = switch doc.docType {
case EuPidModel.EuPidDocType: EuPidModel(response: dr, devicePrivateKey: dpk)
case IsoMdlModel.isoDocType: IsoMdlModel(response: dr, devicePrivateKey: dpk)
Expand Down Expand Up @@ -118,6 +119,8 @@ public class StorageManager: ObservableObject {
documentIds[index] = nil; mdocModels[index] = nil; docTypes[index] = nil
refreshPublishedVars()
}

}



}

0 comments on commit 12943c9

Please sign in to comment.