Adding type erasers

This commit is contained in:
Max Goedjen 2020-03-07 15:04:36 -08:00
parent fb752240b5
commit e87c181e86
No known key found for this signature in database
GPG Key ID: E58C21DD77B9B8E8
5 changed files with 84 additions and 2 deletions

View File

@ -1,6 +1,35 @@
public protocol Secret: Identifiable, Hashable { public protocol Secret: Identifiable {
var name: String { get } var name: String { get }
var publicKey: Data { get } var publicKey: Data { get }
} }
public struct AnySecret: Secret {
fileprivate let _id: () -> AnyHashable
fileprivate let _name: () -> String
fileprivate let _publicKey: () -> Data
public init<T>(_ secret: T) where T: Secret {
_id = { secret.id as AnyHashable }
_name = { secret.name }
_publicKey = { secret.publicKey }
}
public var id: AnyHashable {
return _id()
}
public var name: String {
return _name()
}
public var publicKey: Data {
return _publicKey()
}
}

View File

@ -16,3 +16,37 @@ extension NSNotification.Name {
static let secretStoreUpdated = NSNotification.Name("com.maxgoedjen.Secretive.secretStore.updated") static let secretStoreUpdated = NSNotification.Name("com.maxgoedjen.Secretive.secretStore.updated")
} }
public class AnySecretStore: SecretStore {
fileprivate let _name: () -> String
fileprivate let _secrets: () -> [AnySecret]
fileprivate let _sign: (Data, AnySecret) throws -> Data
fileprivate let _delete: (AnySecret) throws -> Void
public init<T>(_ secretStore: T) where T: SecretStore {
_name = { secretStore.name }
_secrets = { secretStore.secrets.map { AnySecret($0) } }
_sign = { try secretStore.sign(data: $0, with: $1 as! T.SecretType) }
_delete = { try secretStore.delete(secret: $0 as! T.SecretType) }
}
public var name: String {
return _name()
}
public var secrets: [AnySecret] {
return _secrets()
}
public func sign(data: Data, with secret: AnySecret) throws -> Data {
try _sign(data, secret)
}
public func delete(secret: AnySecret) throws {
try _delete(secret)
}
}

View File

@ -4,11 +4,19 @@ import Combine
extension SecureEnclave { extension SecureEnclave {
public struct Secret: SecretKit.Secret { public struct Secret: SecretKit.Secret {
public init(id: Data, name: String, publicKey: Data) {
self.id = id
self.name = name
self.publicKey = publicKey
}
public let id: Data public let id: Data
public let name: String public let name: String
public let publicKey: Data public let publicKey: Data
} }
} }

View File

@ -4,9 +4,17 @@ import Combine
extension SmartCard { extension SmartCard {
public struct Secret: SecretKit.Secret { public struct Secret: SecretKit.Secret {
public init(id: Data, name: String, publicKey: Data) {
self.id = id
// self.name = name
self.publicKey = publicKey
}
public let id: Data public let id: Data
public let name: String public var name: String {
UUID().uuidString
}
public let publicKey: Data public let publicKey: Data
} }

View File

@ -9,6 +9,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var toolbar: NSToolbar! @IBOutlet var toolbar: NSToolbar!
let secureEnclave = SecureEnclave.Store() let secureEnclave = SecureEnclave.Store()
let smartCard = SmartCard.Store() let smartCard = SmartCard.Store()
lazy var allStores: [AnySecretStore] = {
[AnySecretStore(secureEnclave), AnySecretStore(smartCard)]
}()
func applicationDidFinishLaunching(_ aNotification: Notification) { func applicationDidFinishLaunching(_ aNotification: Notification) {