2022-03-11 06:51:46 -08:00
|
|
|
//
|
2022-03-28 04:02:17 -07:00
|
|
|
// WalletStorage.swift
|
2022-03-11 06:51:46 -08:00
|
|
|
// secant-testnet
|
|
|
|
//
|
|
|
|
// Created by Lukáš Korba on 03/10/2022.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import MnemonicSwift
|
2022-04-13 06:52:31 -07:00
|
|
|
import ZcashLightClientKit
|
2022-03-11 06:51:46 -08:00
|
|
|
|
|
|
|
/// Zcash implementation of the keychain that is not universal but designed to deliver functionality needed by the wallet itself.
|
|
|
|
/// All the APIs should be thread safe according to official doc:
|
|
|
|
/// https://developer.apple.com/documentation/security/certificate_key_and_trust_services/working_with_concurrency?language=objc
|
2022-03-28 04:02:17 -07:00
|
|
|
struct WalletStorage {
|
2022-03-11 06:51:46 -08:00
|
|
|
enum Constants {
|
|
|
|
static let zcashStoredWallet = "zcashStoredWallet"
|
|
|
|
/// Versioning of the stored data
|
|
|
|
static let zcashKeychainVersion = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
enum KeychainError: Error, Equatable {
|
|
|
|
case decoding
|
|
|
|
case duplicate
|
|
|
|
case encoding
|
|
|
|
case noDataFound
|
|
|
|
case unknown(OSStatus)
|
2022-03-28 04:02:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
enum WalletStorageError: Error {
|
|
|
|
case alreadyImported
|
|
|
|
case uninitializedWallet
|
|
|
|
case storageError(Error)
|
2022-03-11 06:51:46 -08:00
|
|
|
case unsupportedVersion(Int)
|
|
|
|
case unsupportedLanguage(MnemonicLanguageType)
|
|
|
|
}
|
2022-03-28 04:02:17 -07:00
|
|
|
|
2022-11-17 03:25:55 -08:00
|
|
|
private let secItem: SecItemClient
|
2022-04-13 06:52:31 -07:00
|
|
|
var zcashStoredWalletPrefix = ""
|
2022-04-12 05:32:58 -07:00
|
|
|
|
2022-11-17 03:25:55 -08:00
|
|
|
init(secItem: SecItemClient) {
|
2022-04-12 05:32:58 -07:00
|
|
|
self.secItem = secItem
|
|
|
|
}
|
|
|
|
|
2022-03-28 04:02:17 -07:00
|
|
|
func importWallet(
|
|
|
|
bip39 phrase: String,
|
|
|
|
birthday: BlockHeight?,
|
|
|
|
language: MnemonicLanguageType = .english,
|
|
|
|
hasUserPassedPhraseBackupTest: Bool = false
|
|
|
|
) throws {
|
2023-01-02 07:18:58 -08:00
|
|
|
// Future-proof of the bundle to potentially avoid migration. We enforce english mnemonic.
|
2022-03-28 04:02:17 -07:00
|
|
|
guard language == .english else {
|
|
|
|
throw WalletStorageError.unsupportedLanguage(language)
|
|
|
|
}
|
|
|
|
|
|
|
|
let wallet = StoredWallet(
|
|
|
|
language: language,
|
2023-02-13 11:32:50 -08:00
|
|
|
seedPhrase: SeedPhrase(phrase),
|
2022-03-28 04:02:17 -07:00
|
|
|
version: Constants.zcashKeychainVersion,
|
2023-02-13 11:32:50 -08:00
|
|
|
birthday: Birthday(birthday),
|
2022-03-28 04:02:17 -07:00
|
|
|
hasUserPassedPhraseBackupTest: hasUserPassedPhraseBackupTest
|
|
|
|
)
|
|
|
|
|
|
|
|
do {
|
|
|
|
guard let data = try encode(object: wallet) else {
|
|
|
|
throw KeychainError.encoding
|
|
|
|
}
|
|
|
|
|
|
|
|
try setData(data, forKey: Constants.zcashStoredWallet)
|
|
|
|
} catch KeychainError.duplicate {
|
|
|
|
throw WalletStorageError.alreadyImported
|
|
|
|
} catch {
|
|
|
|
throw WalletStorageError.storageError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func exportWallet() throws -> StoredWallet {
|
|
|
|
guard let data = data(forKey: Constants.zcashStoredWallet) else {
|
|
|
|
throw WalletStorageError.uninitializedWallet
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let wallet = try decode(json: data, as: StoredWallet.self) else {
|
|
|
|
throw WalletStorageError.uninitializedWallet
|
|
|
|
}
|
|
|
|
|
|
|
|
guard wallet.version == Constants.zcashKeychainVersion else {
|
|
|
|
throw WalletStorageError.unsupportedVersion(wallet.version)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wallet
|
|
|
|
}
|
|
|
|
|
|
|
|
func areKeysPresent() throws -> Bool {
|
|
|
|
do {
|
|
|
|
_ = try exportWallet()
|
|
|
|
} catch {
|
2023-01-20 05:55:08 -08:00
|
|
|
// TODO: [#219] - report & log error.localizedDescription, https://github.com/zcash/secant-ios-wallet/issues/219]
|
2022-03-28 04:02:17 -07:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateBirthday(_ height: BlockHeight) throws {
|
|
|
|
do {
|
|
|
|
var wallet = try exportWallet()
|
2023-02-13 11:32:50 -08:00
|
|
|
wallet.birthday = Birthday(height)
|
2022-03-28 04:02:17 -07:00
|
|
|
|
|
|
|
guard let data = try encode(object: wallet) else {
|
|
|
|
throw KeychainError.encoding
|
|
|
|
}
|
|
|
|
|
|
|
|
try updateData(data, forKey: Constants.zcashStoredWallet)
|
|
|
|
} catch {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 06:51:46 -08:00
|
|
|
|
2023-03-01 10:33:58 -08:00
|
|
|
func markUserPassedPhraseBackupTest(_ flag: Bool = true) throws {
|
2022-03-28 04:02:17 -07:00
|
|
|
do {
|
|
|
|
var wallet = try exportWallet()
|
2023-03-01 10:33:58 -08:00
|
|
|
wallet.hasUserPassedPhraseBackupTest = flag
|
2022-03-28 04:02:17 -07:00
|
|
|
|
|
|
|
guard let data = try encode(object: wallet) else {
|
|
|
|
throw KeychainError.encoding
|
|
|
|
}
|
|
|
|
|
2022-04-13 06:52:31 -07:00
|
|
|
try updateData(data, forKey: Constants.zcashStoredWallet)
|
2022-03-28 04:02:17 -07:00
|
|
|
} catch {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nukeWallet() {
|
|
|
|
deleteData(forKey: Constants.zcashStoredWallet)
|
|
|
|
}
|
2022-03-11 06:51:46 -08:00
|
|
|
|
2022-03-28 04:02:17 -07:00
|
|
|
// MARK: - Wallet Storage Codable & Query helpers
|
|
|
|
|
|
|
|
func decode<T: Decodable>(json: Data, as clazz: T.Type) throws -> T? {
|
2022-03-11 06:51:46 -08:00
|
|
|
do {
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
let data = try decoder.decode(T.self, from: json)
|
|
|
|
return data
|
|
|
|
} catch {
|
|
|
|
throw KeychainError.decoding
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 04:02:17 -07:00
|
|
|
func encode<T: Codable>(object: T) throws -> Data? {
|
2022-03-11 06:51:46 -08:00
|
|
|
do {
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
encoder.outputFormatting = .prettyPrinted
|
|
|
|
return try encoder.encode(object)
|
|
|
|
} catch {
|
|
|
|
throw KeychainError.encoding
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 04:02:17 -07:00
|
|
|
func baseQuery(forAccount account: String = "", andKey forKey: String) -> [String: Any] {
|
2023-02-15 14:27:52 -08:00
|
|
|
let query: [String: AnyObject] = [
|
2022-03-11 06:51:46 -08:00
|
|
|
/// Uniquely identify this keychain accessor
|
2022-04-13 06:52:31 -07:00
|
|
|
kSecAttrService as String: (zcashStoredWalletPrefix + forKey) as AnyObject,
|
2022-03-11 06:51:46 -08:00
|
|
|
kSecAttrAccount as String: account as AnyObject,
|
|
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
|
|
/// The data in the keychain item can be accessed only while the device is unlocked by the user.
|
|
|
|
/// This is recommended for items that need to be accessible only while the application is in the foreground.
|
|
|
|
/// Items with this attribute do not migrate to a new device.
|
|
|
|
/// Thus, after restoring from a backup of a different device, these items will not be present.
|
|
|
|
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlockedThisDeviceOnly
|
|
|
|
]
|
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2022-03-28 04:02:17 -07:00
|
|
|
func restoreQuery(forAccount account: String = "", andKey forKey: String) -> [String: Any] {
|
2022-03-11 06:51:46 -08:00
|
|
|
var query = baseQuery(forAccount: account, andKey: forKey)
|
|
|
|
query[kSecMatchLimit as String] = kSecMatchLimitOne
|
|
|
|
query[kSecReturnData as String] = kCFBooleanTrue
|
|
|
|
query[kSecReturnRef as String] = kCFBooleanFalse
|
|
|
|
query[kSecReturnPersistentRef as String] = kCFBooleanFalse
|
|
|
|
query[kSecReturnAttributes as String] = kCFBooleanFalse
|
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Restore data for key
|
|
|
|
func data(
|
|
|
|
forKey: String,
|
|
|
|
account: String = ""
|
|
|
|
) -> Data? {
|
2022-03-28 04:02:17 -07:00
|
|
|
let query = restoreQuery(forAccount: account, andKey: forKey)
|
2022-03-11 06:51:46 -08:00
|
|
|
|
|
|
|
var result: AnyObject?
|
2022-04-12 05:32:58 -07:00
|
|
|
_ = secItem.copyMatching(query as CFDictionary, &result)
|
2022-03-11 06:51:46 -08:00
|
|
|
|
|
|
|
return result as? Data
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Use carefully: Deletes data for key
|
2022-03-28 04:02:17 -07:00
|
|
|
@discardableResult
|
2022-03-11 06:51:46 -08:00
|
|
|
func deleteData(
|
|
|
|
forKey: String,
|
|
|
|
account: String = ""
|
|
|
|
) -> Bool {
|
2022-03-28 04:02:17 -07:00
|
|
|
let query = baseQuery(forAccount: account, andKey: forKey)
|
2022-03-11 06:51:46 -08:00
|
|
|
|
2022-04-12 05:32:58 -07:00
|
|
|
let status = secItem.delete(query as CFDictionary)
|
2022-03-11 06:51:46 -08:00
|
|
|
|
|
|
|
return status == noErr
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Store data for key
|
|
|
|
func setData(
|
|
|
|
_ data: Data,
|
|
|
|
forKey: String,
|
|
|
|
account: String = ""
|
|
|
|
) throws {
|
2022-03-28 04:02:17 -07:00
|
|
|
var query = baseQuery(forAccount: account, andKey: forKey)
|
2022-03-11 06:51:46 -08:00
|
|
|
query[kSecValueData as String] = data as AnyObject
|
|
|
|
|
2022-04-12 05:32:58 -07:00
|
|
|
var result: AnyObject?
|
|
|
|
let status = secItem.add(query as CFDictionary, &result)
|
2022-03-11 06:51:46 -08:00
|
|
|
|
|
|
|
guard status != errSecDuplicateItem else {
|
|
|
|
throw KeychainError.duplicate
|
|
|
|
}
|
|
|
|
|
|
|
|
guard status == errSecSuccess else {
|
|
|
|
throw KeychainError.unknown(status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Use carefully: Update data for key
|
|
|
|
func updateData(
|
|
|
|
_ data: Data,
|
|
|
|
forKey: String,
|
|
|
|
account: String = ""
|
|
|
|
) throws {
|
2022-03-28 04:02:17 -07:00
|
|
|
let query = baseQuery(forAccount: account, andKey: forKey)
|
2022-03-11 06:51:46 -08:00
|
|
|
|
2023-02-15 14:27:52 -08:00
|
|
|
let attributes: [String: AnyObject] = [
|
2022-03-11 06:51:46 -08:00
|
|
|
kSecValueData as String: data as AnyObject
|
|
|
|
]
|
|
|
|
|
2022-04-12 05:32:58 -07:00
|
|
|
let status = secItem.update(query as CFDictionary, attributes as CFDictionary)
|
2022-03-11 06:51:46 -08:00
|
|
|
|
|
|
|
guard status != errSecItemNotFound else {
|
|
|
|
throw KeychainError.noDataFound
|
|
|
|
}
|
|
|
|
|
|
|
|
guard status == errSecSuccess else {
|
|
|
|
throw KeychainError.unknown(status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|