mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Co-authored-by: npub102wg7q285p64ch2fjvstmf2ntn2sz3c4u5hmwatalc76mhsuauysftjtfj <7a9c8f0147a0755c5d499320bda5535cd5014715e52fb7757dfe3dadde1cef09@buzz.block.builderlab.xyz> Signed-off-by: npub102wg7q285p64ch2fjvstmf2ntn2sz3c4u5hmwatalc76mhsuauysftjtfj <7a9c8f0147a0755c5d499320bda5535cd5014715e52fb7757dfe3dadde1cef09@buzz.block.builderlab.xyz>
39 lines
1.4 KiB
Swift
39 lines
1.4 KiB
Swift
import Foundation
|
|
import Security
|
|
|
|
enum BuzzPushKeychain {
|
|
static let service = "buzz.push.nse.signing"
|
|
|
|
static func replace(signingKeys: [String: String], accessGroup: String?) throws {
|
|
var query = baseQuery(accessGroup: accessGroup)
|
|
SecItemDelete(query as CFDictionary)
|
|
for (communityID, privateKeyHex) in signingKeys {
|
|
query[kSecAttrAccount as String] = communityID
|
|
query[kSecValueData as String] = Data(privateKeyHex.utf8)
|
|
query[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
|
|
let status = SecItemAdd(query as CFDictionary, nil)
|
|
guard status == errSecSuccess else {
|
|
SecItemDelete(baseQuery(accessGroup: accessGroup) as CFDictionary)
|
|
throw NSError(
|
|
domain: NSOSStatusErrorDomain, code: Int(status),
|
|
userInfo: [NSLocalizedDescriptionKey: SecCopyErrorMessageString(status, nil) ?? "Keychain write failed" as CFString]
|
|
)
|
|
}
|
|
query.removeValue(forKey: kSecValueData as String)
|
|
query.removeValue(forKey: kSecAttrAccessible as String)
|
|
query.removeValue(forKey: kSecAttrAccount as String)
|
|
}
|
|
}
|
|
|
|
private static func baseQuery(accessGroup: String?) -> [String: Any] {
|
|
var query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
]
|
|
if let accessGroup, !accessGroup.isEmpty {
|
|
query[kSecAttrAccessGroup as String] = accessGroup
|
|
}
|
|
return query
|
|
}
|
|
}
|