mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-26 00:14:49 +01:00
70 lines
2 KiB
Swift
70 lines
2 KiB
Swift
//
|
|
// KeychainHelper.swift
|
|
// BlueWallet
|
|
//
|
|
// Created by Marcos Rodriguez on 11/20/24.
|
|
// Copyright © 2024 BlueWallet. All rights reserved.
|
|
//
|
|
|
|
|
|
import Foundation
|
|
import Security
|
|
|
|
class KeychainHelper {
|
|
|
|
static let shared = KeychainHelper()
|
|
|
|
private init() {}
|
|
|
|
/// Save data to Keychain
|
|
func save(_ data: Data, service: String, account: String) -> Bool {
|
|
// Create query
|
|
let query: [String: Any] = [
|
|
kSecClass as String : kSecClassGenericPassword,
|
|
kSecAttrService as String : service,
|
|
kSecAttrAccount as String : account,
|
|
kSecValueData as String : data
|
|
]
|
|
|
|
// Delete any existing item
|
|
SecItemDelete(query as CFDictionary)
|
|
|
|
// Add new item
|
|
let status = SecItemAdd(query as CFDictionary, nil)
|
|
return status == errSecSuccess
|
|
}
|
|
|
|
/// Retrieve data from Keychain
|
|
func retrieve(service: String, account: String) -> Data? {
|
|
// Create query
|
|
let query: [String: Any] = [
|
|
kSecClass as String : kSecClassGenericPassword,
|
|
kSecAttrService as String : service,
|
|
kSecAttrAccount as String : account,
|
|
kSecReturnData as String : true,
|
|
kSecMatchLimit as String : kSecMatchLimitOne
|
|
]
|
|
|
|
var dataTypeRef: AnyObject?
|
|
let status = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)
|
|
|
|
if status == errSecSuccess {
|
|
return dataTypeRef as? Data
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
/// Delete data from Keychain
|
|
func delete(service: String, account: String) -> Bool {
|
|
// Create query
|
|
let query: [String: Any] = [
|
|
kSecClass as String : kSecClassGenericPassword,
|
|
kSecAttrService as String : service,
|
|
kSecAttrAccount as String : account
|
|
]
|
|
|
|
let status = SecItemDelete(query as CFDictionary)
|
|
return status == errSecSuccess
|
|
}
|
|
}
|