Skip to content

Commit

Permalink
Refactor data storage service to use async/await, actor isolated
Browse files Browse the repository at this point in the history
  • Loading branch information
phisakel committed Sep 20, 2024
1 parent 1cf7717 commit 57c2312
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
12 changes: 5 additions & 7 deletions Sources/WalletStorage/DataStorageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import Foundation

/// Data storage protocol
public protocol DataStorageService {
var serviceName: String { get set }
var accessGroup: String? { get set }
func loadDocument(id: String, status: DocumentStatus) throws -> Document?
func loadDocuments(status: DocumentStatus) throws -> [Document]?
func saveDocument(_ document: Document, allowOverwrite: Bool) throws
func deleteDocument(id: String, status: DocumentStatus) throws
func deleteDocuments(status: DocumentStatus) throws
func loadDocument(id: String, status: DocumentStatus) async throws -> Document?
func loadDocuments(status: DocumentStatus) async throws -> [Document]?
func saveDocument(_ document: Document, allowOverwrite: Bool) async throws
func deleteDocument(id: String, status: DocumentStatus) async throws
func deleteDocuments(status: DocumentStatus) async throws
}
8 changes: 4 additions & 4 deletions Sources/WalletStorage/IssueRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public struct IssueRequest: Sendable {
if let docType { logger.info(" and docType: \(docType)") }
}

public func saveToStorage(_ storageService: any DataStorageService, status: DocumentStatus) throws {
public func saveTo(storageService: any DataStorageService, status: DocumentStatus) async throws {
// save key data to storage with id
logger.info("Saving Issue request with id: \(id) and document status: \(status)")
let docKey = Document(id: id, docType: docType ?? "P256", docDataType: .cbor, data: Data(), privateKeyType: privateKeyType, privateKey: keyData, createdAt: Date(), displayName: nil, status: status)
try storageService.saveDocument(docKey, allowOverwrite: true)
try await storageService.saveDocument(docKey, allowOverwrite: true)
}

public init?(_ storageService: any DataStorageService, id: String, status: DocumentStatus) throws {
guard let doc = try storageService.loadDocument(id: id, status: status), let pk = doc.privateKey, let pkt = doc.privateKeyType else { return nil }
public mutating func loadFrom(storageService: any DataStorageService, id: String, status: DocumentStatus) async throws {
guard let doc = try await storageService.loadDocument(id: id, status: status), let pk = doc.privateKey, let pkt = doc.privateKeyType else { return }
self.id = id
keyData = pk
privateKeyType = pkt
Expand Down
2 changes: 1 addition & 1 deletion Sources/WalletStorage/KeyChainStorageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
/// Implements key-chain storage
/// Documents are saved as a pair of generic password items (document data and private key)
/// For implementation details see [Apple documentation](https://developer.apple.com/documentation/security/ksecclassgenericpassword)
public class KeyChainStorageService: DataStorageService {
public actor KeyChainStorageService: DataStorageService {

public init(serviceName: String, accessGroup: String? = nil) {
self.serviceName = serviceName
Expand Down

0 comments on commit 57c2312

Please sign in to comment.