Add AccountsService

This commit is contained in:
Vadim Zakharenko 2021-06-12 19:47:44 +03:00
parent 36d47f1b8e
commit 479369f91d
2 changed files with 49 additions and 6 deletions

View File

@ -1,19 +1,62 @@
// Copyright © 2021 Encrypted Ink. All rights reserved. // Copyright © 2021 Encrypted Ink. All rights reserved.
import Foundation import Foundation
import Web3Swift
struct AccountsService { struct AccountsService {
private static let keychainKey = "EncryptedInkStorage"
static func addAccount(privateKey: String) -> Account? {
guard
let addressBytes = try? EthPrivateKey(hex: privateKey).address().value()
else {
return nil
}
let address = addressBytes.toPrefixedHexString()
let account = Account(privateKey: privateKey, address: address)
var accounts = getAccounts()
guard !accounts.contains(where: { $0.address == address }) else { return nil }
accounts.append(account)
saveInKeychain(accounts: accounts)
return account
}
static func removeAccount(_ account: Account) {
var accounts = getAccounts()
accounts.removeAll(where: {$0.address == account.address })
saveInKeychain(accounts: accounts)
}
static func getAccounts() -> [Account] { static func getAccounts() -> [Account] {
return [] return loadAccountsFromKeychain() ?? []
} }
func removeAccount(_ account: Account) { private static func saveInKeychain(accounts: [Account]) {
guard let data = try? JSONEncoder().encode(accounts) else { return }
let query = [kSecClass as String: kSecClassGenericPassword as String,
kSecAttrAccount as String: keychainKey,
kSecValueData as String: data] as [String: Any]
SecItemDelete(query as CFDictionary)
SecItemAdd(query as CFDictionary, nil)
} }
func addAccount(privateKey: String) -> Account? { private static func loadAccountsFromKeychain() -> [Account]? {
return nil guard let returnDataQueryValue = kCFBooleanTrue else { return nil }
} let query = [kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: keychainKey,
kSecReturnData as String: returnDataQueryValue,
kSecMatchLimit as String: kSecMatchLimitOne] as [String: Any]
var dataTypeRef: AnyObject?
let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)
if status == noErr, let data = dataTypeRef as? Data {
let accounts = try? JSONDecoder().decode([Account].self, from: data)
return accounts
} else {
return nil
}
}
} }

View File

@ -13,7 +13,7 @@ struct Transaction {
let contractCall: String let contractCall: String
} }
struct Account { struct Account: Codable {
let privateKey: String let privateKey: String
let address: String let address: String
} }