|
| 1 | +import Domain |
| 2 | +import Foundation |
| 3 | + |
| 4 | +extension AnoncredsCredentialStack: ProvableCredential { |
| 5 | + func presentation( |
| 6 | + request: Message, |
| 7 | + options: [CredentialOperationsOptions] |
| 8 | + ) throws -> String { |
| 9 | + let requestStr: String |
| 10 | + guard let attachment = request.attachments.first else { |
| 11 | + throw PolluxError.messageDoesntProvideEnoughInformation |
| 12 | + } |
| 13 | + switch attachment.data { |
| 14 | + case let attachmentData as AttachmentJsonData: |
| 15 | + requestStr = try attachmentData.data.toString() |
| 16 | + case let attachmentData as AttachmentBase64: |
| 17 | + guard let data = Data(fromBase64URL: attachmentData.base64) else { |
| 18 | + throw PolluxError.messageDoesntProvideEnoughInformation |
| 19 | + } |
| 20 | + requestStr = try data.toString() |
| 21 | + default: |
| 22 | + throw PolluxError.messageDoesntProvideEnoughInformation |
| 23 | + } |
| 24 | + |
| 25 | + guard |
| 26 | + let linkSecretOption = options.first(where: { |
| 27 | + if case .linkSecret = $0 { return true } |
| 28 | + return false |
| 29 | + }), |
| 30 | + case let CredentialOperationsOptions.linkSecret(_, secret: linkSecret) = linkSecretOption |
| 31 | + else { |
| 32 | + throw PolluxError.missingAndIsRequiredForOperation(type: "LinkSecret") |
| 33 | + } |
| 34 | + |
| 35 | + if |
| 36 | + let zkpParameters = options.first(where: { |
| 37 | + if case .zkpPresentationParams = $0 { return true } |
| 38 | + return false |
| 39 | + }), |
| 40 | + case let CredentialOperationsOptions.zkpPresentationParams(attributes, predicates) = zkpParameters |
| 41 | + { |
| 42 | + return try AnoncredsPresentation().createPresentation( |
| 43 | + stack: self, |
| 44 | + request: requestStr, |
| 45 | + linkSecret: linkSecret, |
| 46 | + attributes: attributes, |
| 47 | + predicates: predicates |
| 48 | + ) |
| 49 | + } else { |
| 50 | + return try AnoncredsPresentation().createPresentation( |
| 51 | + stack: self, |
| 52 | + request: requestStr, |
| 53 | + linkSecret: linkSecret, |
| 54 | + attributes: try computeAttributes(requestJson: requestStr), |
| 55 | + predicates: try computePredicates(requestJson: requestStr) |
| 56 | + ) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +private func computeAttributes(requestJson: String) throws -> [String: Bool] { |
| 62 | + guard |
| 63 | + let json = try JSONSerialization.jsonObject(with: try requestJson.tryData(using: .utf8)) as? [String: Any] |
| 64 | + else { |
| 65 | + throw PolluxError.messageDoesntProvideEnoughInformation |
| 66 | + } |
| 67 | + let requestedAttributes = json["requested_attributes"] as? [String: Any] |
| 68 | + return requestedAttributes?.reduce([:]) { partialResult, row in |
| 69 | + var dic = partialResult |
| 70 | + dic[row.key] = true |
| 71 | + return dic |
| 72 | + } ?? [:] |
| 73 | +} |
| 74 | + |
| 75 | +private func computePredicates(requestJson: String) throws -> [String] { |
| 76 | + guard |
| 77 | + let json = try JSONSerialization.jsonObject(with: try requestJson.tryData(using: .utf8)) as? [String: Any] |
| 78 | + else { |
| 79 | + throw PolluxError.messageDoesntProvideEnoughInformation |
| 80 | + } |
| 81 | + let requestedPredicates = json["requested_predicates"] as? [String: Any] |
| 82 | + return requestedPredicates?.map { $0.key } ?? [] |
| 83 | +} |
0 commit comments