Draft for key operations

This commit is contained in:
2022-08-28 12:45:20 +03:00
committed by y.shikin
parent 0aea752053
commit e083211591
4 changed files with 100 additions and 10 deletions
@@ -10,6 +10,11 @@ import WalletNetwork
final class VillageHouse: Bank {
private enum Constants {
static let oldKey = "Account.Service.collection"
static let key = "Wallet.bank.service"
}
// MARK: - Properties
private let password: String
@@ -24,12 +29,31 @@ final class VillageHouse: Bank {
self.password = password
self.device = device
self.environment = environment
self.migrate(using: self.password)
}
// MARK: - Private
private func restore() {
let data = UserDefaults.standard.value(forKey: "Account.Service.collection") as? Data
}
private func migrate(using password: String) {
let migrateWallets: [Villager]
if let data = UserDefaults.standard.value(forKey: Constants.oldKey) as? Data,
let collection = try? JSONDecoder().decode([Villager.OldVillager].self, from: data) {
migrateWallets = collection.compactMap { $0.covert(password: password) }
} else {
migrateWallets = []
}
print(migrateWallets)
// Delete previous key
// UserDefaults.standard.set(nil, forKey: Constants.oldKey)
}
@@ -40,10 +64,12 @@ final class VillageHouse: Bank {
func remove(wallet: Wallet) -> Bool { false }
func add(using walletCase: WalletCase) async throws -> Wallet {
// TODO: - Add logic
try await Villager.create(walletCase: walletCase, on: self.device, using: self.environment)
}
func add(using key: WalletKey) async throws -> Wallet {
// TODO: - Add logic
try await Villager.create(walletKey: key, using: self.environment)
}
@@ -55,12 +81,6 @@ final class VillageHouse: Bank {
}
/*
let collection = try? JSONDecoder().decode([Account.Model].self, from: data!)
print(collection)
*/
/*
Optional([Malinka.Account.Model(username: "juraldinio42", publicKey: "EOS5tevDFkqE5oKTnEd5NtehNGNfvyKMLSZjCmVxttvfVfARWdn5q", keyType: Malinka.Account.KeyType.owner), Malinka.Account.Model(username: "juraldinio44", publicKey: "EOS5C9wVQowph1XGJUp8iqs94rh3FuZr9gmV6PHWhRenycgctP9fH", keyType: Malinka.Account.KeyType.owner), Malinka.Account.Model(username: "juraldinio45", publicKey: "EOS8kGoX3w37t88RnBDSA7ztHSkCvUy3iu2bX8s5KobK93E4ekxSU", keyType: Malinka.Account.KeyType.owner)])
*/
@@ -9,7 +9,29 @@ import Foundation
import Combine
import WalletNetwork
final class Villager: Wallet {
final class Villager: Wallet, Codable, CustomStringConvertible {
struct OldVillager: Codable {
var username: String
var publicKey: String
var keyType: String
func covert(password: String) -> Villager? {
guard let keyType = WalletKeyType(rawValue: self.keyType),
let key = try? WalletKey.restore(using: self.username, type: keyType, publicKey: self.publicKey, password: password) else {
return nil
}
return Villager(name: self.username, key: key, keyType: keyType, state: .pending)
}
}
private enum CodingKeys: String, CodingKey {
case name
case key
case keyType
case state
}
private let stateSubject: CurrentValueSubject<WalletState, Never>
@@ -22,6 +44,33 @@ final class Villager: Wallet {
self.stateSubject = CurrentValueSubject<WalletState, Never>(state)
}
private init(name: String, key: WalletKey, keyType: WalletKeyType, state: WalletState) {
self.name = name
self.key = key
self.keyType = keyType
self.stateSubject = CurrentValueSubject<WalletState, Never>(state)
}
// MARK: - Codable
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.keyType = try container.decode(WalletKeyType.self, forKey: .keyType)
let publicKey = try container.decode(String.self, forKey: .key)
// TODO: - NEED COMPLETETASK
self.key = try WalletKey.restore(using: self.name, type: self.keyType, publicKey: publicKey, password: "")
let state = try container.decode(WalletState.self, forKey: .state)
self.stateSubject = CurrentValueSubject<WalletState, Never>(state)
}
func encode(to encoder: Encoder) throws {
// TODO: - NEED COMPLETE
}
// MARK: - Wallet
let name: String
@@ -38,6 +87,10 @@ final class Villager: Wallet {
return self.key
}
// MARK: - CustomStringConvertible
var description: String { "(name: \(name), keyType: \(keyType), state: \(state), public: \(key.publicKey)" }
// MARK: - Static
static func create(walletCase: WalletCase, on device: Device, using environment: NetworkEnvironment) async throws -> Villager {
@@ -73,6 +126,7 @@ final class Villager: Wallet {
}
static func create(walletKey: WalletKey, using environment: NetworkEnvironment) async throws -> Villager {
// TODO: - Add logic
throw WalletError.unavailable
}
@@ -29,7 +29,7 @@ public enum WalletError: Error {
}
/// Wallet state
public enum WalletState {
public enum WalletState: String {
/// Creation in progress
case pending
/// Created
@@ -38,6 +38,8 @@ public enum WalletState {
case denied
}
extension WalletState: Codable { }
public enum WalletKeyType: String, Codable, CustomStringConvertible {
case owner
case active
@@ -18,6 +18,8 @@ public enum WalletKeyError: Error {
case invalidPrivate
/// Invalid pair public and private keys
case invalidPair
///
case unlock
}
/// Represents Wallet key.
@@ -28,6 +30,12 @@ public struct WalletKey {
/// Private key
public let privateKey: String
func save() {
}
// MARK: - Public static
public static func create(public: String, private: String) throws -> WalletKey {
let privateKey: EOSPrivateKey
@@ -49,7 +57,6 @@ public struct WalletKey {
}
return WalletKey(publicKey: publicKey.base58, privateKey: privateKey.base58)
}
public static func create(private: String) throws -> WalletKey {
@@ -69,4 +76,11 @@ public struct WalletKey {
guard let key = try? EOSPrivateKey() else { throw WalletKeyError.system }
return WalletKey(publicKey: key.publicKey.base58, privateKey: key.base58)
}
// MARK: - Internal static
static func restore(using name: String, type: WalletKeyType, publicKey: String, password: String) throws -> WalletKey {
throw WalletKeyError.unlock
}
}