//
//  CredValueMaker.swift
//  TrustBuilderMfaSampleApp
//
//  Created by TB_COWORKER on 08/06/2023.
//

import Foundation
import TrustBuilderMfaLibSwift


class CredValueMake {
    static func make(tknAlias: String, credentialProvider: CredentialProvider, secretCode: String) async throws -> CredValue {
        let credValue: CredValue;
        if (credentialProvider.credType == CredTypeEnum.SECRET_CODE) {
            var bytes = Array(secretCode.utf8)
            defer { for i in 0..<bytes.count { bytes[i] = 0 } }
            credValue = CredValue(credType: credentialProvider.credType, value: bytes)
        } else {
            let privateKey = try await BioKeyStorage().get(tknAlias: tknAlias)
            let signedBioChallenge = GeneratorCryptoTools.signData(data: credentialProvider.credProperties!.bioChallenge!, privateKey: privateKey)
            credValue = CredValue(credType: credentialProvider.credType, value: [UInt8](signedBioChallenge))
        }
        return credValue;
    }
}
