Files
raspberry/iOS/Wallet/Sources/Flow/OnBoarding/Service/AttestateWalletService.swift
T
2022-08-31 14:21:25 +03:00

55 lines
1.6 KiB
Swift

//
// AttestateWalletService.swift
// Malinka
//
// Created by Juraldinio on 25.08.2022.
// Copyright © 2022 NUT.Tech. All rights reserved.
//
import Foundation
import UIKit
import WalletKit
final class AttestateWalletService {
enum ServiceError: Error {
case url
case checks
}
static func createController() async throws -> UIViewController {
DispatchQueue.main.async { Loader.show() }
defer {
DispatchQueue.main.async { Loader.hide() }
}
do {
guard let url = URL(string: Network.servers.current.malinkaGraphQL) else { throw ServiceError.url }
let village = Village.create(city: url, street: Bundle.main.userAgent)
var certified: CertifiedDevice
do {
certified = try await AttestateWalletService.attestate(on: village)
} catch {
certified = try await AttestateWalletService.attestate(on: village, forced: true)
}
let service = CaseWalletService(village: village, certified: certified)
let controller = await CreateWalletViewController(with: service)
return controller
} catch {
throw ServiceError.checks
}
}
private static func attestate(on village: Village, forced: Bool = false) async throws -> CertifiedDevice {
let device = try await village.device()
let certified = try await village.attestate(device: device)
if certified.status == .invalid { throw ServiceError.checks }
return certified
}
}