-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pollux): add anoncreds prooving implementation
- Loading branch information
1 parent
ae14bb9
commit 80377b1
Showing
18 changed files
with
550 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
AtalaPrismSDK/Pollux/Sources/Models/AnonCreds/AnonCredentialSchema.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
|
||
struct AnonCredentialSchema: Codable { | ||
let name: String | ||
let version: String | ||
let attrNames: [String] | ||
let issuerId: String | ||
} |
83 changes: 83 additions & 0 deletions
83
...rismSDK/Pollux/Sources/Models/AnonCreds/AnoncredsCredentialStack+ProvableCredential.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import Domain | ||
import Foundation | ||
|
||
extension AnoncredsCredentialStack: ProvableCredential { | ||
func presentation( | ||
request: Message, | ||
options: [CredentialOperationsOptions] | ||
) throws -> String { | ||
let requestStr: String | ||
guard let attachment = request.attachments.first else { | ||
throw PolluxError.messageDoesntProvideEnoughInformation | ||
} | ||
switch attachment.data { | ||
case let attachmentData as AttachmentJsonData: | ||
requestStr = try attachmentData.data.toString() | ||
case let attachmentData as AttachmentBase64: | ||
guard let data = Data(fromBase64URL: attachmentData.base64) else { | ||
throw PolluxError.messageDoesntProvideEnoughInformation | ||
} | ||
requestStr = try data.toString() | ||
default: | ||
throw PolluxError.messageDoesntProvideEnoughInformation | ||
} | ||
|
||
guard | ||
let linkSecretOption = options.first(where: { | ||
if case .linkSecret = $0 { return true } | ||
return false | ||
}), | ||
case let CredentialOperationsOptions.linkSecret(_, secret: linkSecret) = linkSecretOption | ||
else { | ||
throw PolluxError.missingAndIsRequiredForOperation(type: "LinkSecret") | ||
} | ||
|
||
if | ||
let zkpParameters = options.first(where: { | ||
if case .zkpPresentationParams = $0 { return true } | ||
return false | ||
}), | ||
case let CredentialOperationsOptions.zkpPresentationParams(attributes, predicates) = zkpParameters | ||
{ | ||
return try AnoncredsPresentation().createPresentation( | ||
stack: self, | ||
request: requestStr, | ||
linkSecret: linkSecret, | ||
attributes: attributes, | ||
predicates: predicates | ||
) | ||
} else { | ||
return try AnoncredsPresentation().createPresentation( | ||
stack: self, | ||
request: requestStr, | ||
linkSecret: linkSecret, | ||
attributes: try computeAttributes(requestJson: requestStr), | ||
predicates: try computePredicates(requestJson: requestStr) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private func computeAttributes(requestJson: String) throws -> [String: Bool] { | ||
guard | ||
let json = try JSONSerialization.jsonObject(with: try requestJson.tryData(using: .utf8)) as? [String: Any] | ||
else { | ||
throw PolluxError.messageDoesntProvideEnoughInformation | ||
} | ||
let requestedAttributes = json["requested_attributes"] as? [String: Any] | ||
return requestedAttributes?.reduce([:]) { partialResult, row in | ||
var dic = partialResult | ||
dic[row.key] = true | ||
return dic | ||
} ?? [:] | ||
} | ||
|
||
private func computePredicates(requestJson: String) throws -> [String] { | ||
guard | ||
let json = try JSONSerialization.jsonObject(with: try requestJson.tryData(using: .utf8)) as? [String: Any] | ||
else { | ||
throw PolluxError.messageDoesntProvideEnoughInformation | ||
} | ||
let requestedPredicates = json["requested_predicates"] as? [String: Any] | ||
return requestedPredicates?.map { $0.key } ?? [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
AtalaPrismSDK/Pollux/Sources/Models/AnonCreds/AnoncredsPresentation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import AnoncredsSwift | ||
import Domain | ||
import Foundation | ||
|
||
struct AnoncredsPresentation { | ||
func createPresentation( | ||
stack: AnoncredsCredentialStack, | ||
request: String, | ||
linkSecret: String, | ||
attributes: [String: Bool], | ||
predicates: [String] | ||
) throws -> String { | ||
let linkSecret = try LinkSecret.newFromValue(valueString: linkSecret) | ||
let request = try PresentationRequest(jsonString: request) | ||
let credentialRequest = CredentialRequests( | ||
credential: try stack.credential.getAnoncred(), | ||
requestedAttribute: attributes.map { | ||
.init(referent: $0.key, revealed: $0.value) | ||
}, | ||
requestedPredicate: predicates.map { .init(referent: $0) } | ||
) | ||
|
||
let credential = stack.credential | ||
let schema = Schema.init( | ||
name: stack.schema.name, | ||
version: stack.schema.version, | ||
attrNames: AttributeNames(stack.schema.attrNames), | ||
issuerId: stack.schema.issuerId | ||
) | ||
|
||
let credentialDefinition = try stack.definition.getAnoncred() | ||
return try Prover().createPresentation( | ||
presentationRequest: request, | ||
credentials: [credentialRequest], | ||
selfAttested: [:], | ||
linkSecret: linkSecret, | ||
schemas: [credential.schemaId: schema], | ||
credentialDefinitions: [credential.credentialDefinitionId: credentialDefinition] | ||
).getJson() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.