Skip to content

Commit

Permalink
log important operations
Browse files Browse the repository at this point in the history
  • Loading branch information
phisakel committed Aug 2, 2024
1 parent bd4af0b commit 609c678
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Sources/WalletStorage/IssueRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public struct IssueRequest {
self.privateKeyType = privateKeyType
if let keyData {
self.keyData = keyData
// key-data already created, exit
return
}
switch privateKeyType {
Expand All @@ -51,10 +52,13 @@ public struct IssueRequest {
let secureEnclaveKey = try SecureEnclave.P256.KeyAgreement.PrivateKey()
self.keyData = secureEnclaveKey.dataRepresentation
}
logger.info("Created private key of type \(privateKeyType)")
if let docType { logger.info(" and docType: \(docType)") }
}

public func saveToStorage(_ storageService: any DataStorageService, status: DocumentStatus) 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)
}
Expand Down
16 changes: 13 additions & 3 deletions Sources/WalletStorage/KeyChainStorageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ public class KeyChainStorageService: DataStorageService {
/// - Parameter id: Document identifier
/// - Returns: The document if exists
public func loadDocument(id: String, status: DocumentStatus) throws -> Document? {
try loadDocuments(id: id, status: status)?.first
logger.info("Load document with status: \(status), id: \(id)")
return try loadDocuments(id: id, status: status)?.first
}
public func loadDocuments(status: DocumentStatus) throws -> [Document]? {
try loadDocuments(id: nil, status: status)
logger.info("Load documents with status: \(status)")
return try loadDocuments(id: nil, status: status)
}
// use is-negative to denote type of data
static func isDocumentDataRow(_ d: [String: Any]) -> Bool { if let b = d[kSecAttrIsNegative as String] as? Bool { !b } else { true } }
Expand All @@ -59,6 +61,7 @@ public class KeyChainStorageService: DataStorageService {
if status == errSecItemNotFound { return nil }
let statusMessage = SecCopyErrorMessageString(status, nil) as? String
guard status == errSecSuccess else {
logger.error("Error code: \(Int(status)), description: \(statusMessage ?? "")")
throw StorageError(description: statusMessage ?? "", code: Int(status))
}
var res = result as! [[String: Any]]
Expand Down Expand Up @@ -103,6 +106,7 @@ public class KeyChainStorageService: DataStorageService {
public func saveDocumentData(_ document: Document, dataToSaveType: SavedKeyChainDataType, dataType: String, allowOverwrite: Bool = true) throws {
// kSecAttrAccount is used to store the secret Id (we save the document ID)
// kSecAttrService is a key whose value is a string indicating the item's service.
logger.info("Save document for status: \(document.status), id: \(document.id), docType: \(document.docType), displayName: \(document.displayName ?? "")")
guard dataType.count == 4 else { throw StorageError(description: "Invalid type") }
if dataToSaveType == .key && document.privateKey == nil { throw StorageError(description: "Private key not available") }
var query: [String: Any] = makeQuery(id: document.id, bForSave: true, status: document.status, dataType: dataToSaveType)
Expand All @@ -124,6 +128,7 @@ public class KeyChainStorageService: DataStorageService {
}
let statusMessage = SecCopyErrorMessageString(status, nil) as? String
guard status == errSecSuccess else {
logger.error("Error code: \(Int(status)), description: \(statusMessage ?? "")")
throw StorageError(description: statusMessage ?? "", code: Int(status))
}
}
Expand All @@ -133,21 +138,26 @@ public class KeyChainStorageService: DataStorageService {
/// - Parameters:
/// - id: The Id of the secret
public func deleteDocument(id: String, status: DocumentStatus) throws {
logger.info("Delete document with status: \(status), id: \(id)")
try deleteDocumentData(id: id, docStatus: status)
}

public func deleteDocumentData(id: String?, docStatus: DocumentStatus, dataType: SavedKeyChainDataType = .doc) throws {
let query: [String: Any] = makeQuery(id: id, bForSave: true, status: docStatus, dataType: dataType)
let status = SecItemDelete(query as CFDictionary)
let statusMessage = SecCopyErrorMessageString(status, nil) as? String
guard status == errSecSuccess else { throw StorageError(description: statusMessage ?? "", code: Int(status)) }
guard status == errSecSuccess else {
logger.error("Error code: \(Int(status)), description: \(statusMessage ?? "")")
throw StorageError(description: statusMessage ?? "", code: Int(status))
}
if dataType == .doc { try deleteDocumentData(id: id, docStatus: docStatus, dataType: .key) }
}

/// Delete all documents from keychain
/// - Parameters:
/// - id: The Id of the secret
public func deleteDocuments(status: DocumentStatus) throws {
logger.info("Delete documents with status: \(status)")
try deleteDocumentData(id: nil, docStatus: status)
}

Expand Down

0 comments on commit 609c678

Please sign in to comment.