Skip to content

Commit

Permalink
Refactor issue request
Browse files Browse the repository at this point in the history
  • Loading branch information
phisakel committed Jan 27, 2024
1 parent 811d810 commit 6c79c57
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Sources/eudi-lib-ios-wallet-storage/IssueRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import MdocDataModel18013

/// Issue request structure
public struct IssueRequest {
public let id: String
public let docType: String?
public var keyData: Data?
public let privateKeyType: PrivateKeyType
public var id: String
public var docType: String?
public var keyData: Data
public var privateKeyType: PrivateKeyType

/// Initialize issue request with id
///
/// - Parameters:
/// - id: a key identifier (uuid)
public init(id: String = UUID().uuidString, docType: String? = nil, privateKeyType: PrivateKeyType = .x963EncodedP256, keyData: Data? = nil) throws {
public init(id: String = UUID().uuidString, docType: String? = nil, privateKeyType: PrivateKeyType = .secureEnclaveP256, keyData: Data? = nil) throws {
self.id = id
self.docType = docType
self.privateKeyType = privateKeyType
Expand All @@ -48,7 +48,7 @@ public struct IssueRequest {
let p256 = P256.KeyAgreement.PrivateKey()
self.keyData = p256.x963Representation
case .secureEnclaveP256:
let secureEnclaveKey = try SecureEnclave.P256.KeyAgreement.PrivateKey()
let secureEnclaveKey = try SecureEnclave.P256.KeyAgreement.PrivateKey()
self.keyData = secureEnclaveKey.dataRepresentation
}
}
Expand All @@ -59,13 +59,14 @@ public struct IssueRequest {
try storageService.saveDocument(docKey, allowOverwrite: true)
}

public mutating func loadFromStorage(_ storageService: any DataStorageService, id: String) throws {
guard let doc = try storageService.loadDocument(id: id) else { return }
keyData = doc.privateKey
public init?(_ storageService: any DataStorageService, id: String) throws {
guard let doc = try storageService.loadDocument(id: id), let pk = doc.privateKey, let pkt = doc.privateKeyType else { return nil }
self.id = id
keyData = pk
privateKeyType = pkt
}

public func toCoseKeyPrivate() throws -> CoseKeyPrivate {
guard let keyData else { fatalError("Key data not loaded") }
switch privateKeyType {
case .derEncodedP256:
let p256 = try P256.KeyAgreement.PrivateKey(derRepresentation: keyData)
Expand All @@ -83,7 +84,6 @@ public struct IssueRequest {
}

public func getPublicKeyPEM() throws -> String {
guard let keyData else { fatalError("Key data not loaded") }
switch privateKeyType {
case .derEncodedP256:
let p256 = try P256.KeyAgreement.PrivateKey(derRepresentation: keyData)
Expand Down

0 comments on commit 6c79c57

Please sign in to comment.