95 lines
3.2 KiB
Swift
95 lines
3.2 KiB
Swift
//
|
|
// AccountModel.swift
|
|
// List
|
|
//
|
|
// Created by Igor Danich on 24.07.2020.
|
|
// Copyright © 2020 List. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import WalletKit
|
|
|
|
extension String {
|
|
fileprivate static let privateKey = "privateKey"
|
|
}
|
|
|
|
extension Account {
|
|
struct Model: Codable {
|
|
var username: String
|
|
var publicKey: String
|
|
var keyType: KeyType
|
|
var state: WalletState
|
|
|
|
init(username: String, publicKey: String, keyType: KeyType, state: WalletState) {
|
|
self.username = username
|
|
self.publicKey = publicKey
|
|
self.keyType = keyType
|
|
self.state = state
|
|
}
|
|
|
|
// MARK: - Codable
|
|
|
|
private enum Common: String, CodingKey {
|
|
case username
|
|
case publicKey
|
|
case keyType
|
|
case state
|
|
}
|
|
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: Common.self)
|
|
self.username = try container.decode(String.self, forKey: .username)
|
|
self.publicKey = try container.decode(String.self, forKey: .publicKey)
|
|
self.keyType = try container.decode(KeyType.self, forKey: .keyType)
|
|
// this required for migrate
|
|
let state = try container.decodeIfPresent(WalletState.self, forKey: .state)
|
|
self.state = state ?? .accepted
|
|
}
|
|
}
|
|
|
|
struct OldModel: Codable {
|
|
let username: String
|
|
let publicKey: String
|
|
}
|
|
|
|
enum KeyType: String, Codable {
|
|
case active
|
|
case owner
|
|
}
|
|
}
|
|
|
|
extension Account.Model {
|
|
init(username: String, publicKey: String, privateKey: String, keyType: Account.KeyType, password: String, state: WalletState) {
|
|
self.username = username
|
|
self.publicKey = publicKey
|
|
self.keyType = keyType
|
|
self.state = state
|
|
updatePrivateKey(privateKey: privateKey, password: password)
|
|
}
|
|
|
|
func privateKey(password: String?) -> String? {
|
|
if let password = password {
|
|
return WalletKeychain.instance[.key("\(username)@\(keyType.rawValue)", suffix: .privateKey), password: password]
|
|
} else {
|
|
return WalletKeychain.instance[biometric: .key("\(username)@\(keyType.rawValue)", suffix: .privateKey)]
|
|
}
|
|
}
|
|
|
|
func updatePrivateKey(privateKey: String, password: String) {
|
|
WalletKeychain.instance[.key("\(username)@\(keyType.rawValue)", suffix: .privateKey), password: password] = privateKey
|
|
}
|
|
|
|
func migrate(password: String) {
|
|
let privateKey = WalletKeychain.instance[.key(username, suffix: .privateKey), password: password]
|
|
WalletKeychain.instance[.key(username, suffix: .privateKey), password: ""] = nil
|
|
WalletKeychain.instance[.key("\(username)@\(keyType.rawValue)", suffix: .privateKey), password: password] = privateKey
|
|
}
|
|
|
|
func update(password: String, old: String) {
|
|
WalletKeychain.instance[.key("\(username)@\(keyType.rawValue)", suffix: .privateKey), password: password] = privateKey(password: old)
|
|
}
|
|
func clear() {
|
|
WalletKeychain.instance[.key("\(username)@\(keyType.rawValue)", suffix: .privateKey), password: ""] = nil
|
|
}
|
|
}
|