-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(pollux): add anoncreds prooving implementation #106
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small comment or suggestion but is this if condition or else really needed?
computeAttributes and computePredicates will not process much if the arrays or object with attributes or predicates are empty right?
Could be wrong :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah so the way I envision this logic was.
If the user provides ZKP Parameters then we don't do anything, if they wish to provide ZKP parameters with no attributes or predicates, its their option, so we don't compute them.
If the user doesn't provide ZKP parameters then we compute a "default" version based on the presentation request.
We can change this logic but it was what I envisioned