-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update CredentialPack to use ParsedCredential (#39)
* Update CredentialPack to use ParsedCredential * Make CredentialStore and MDoc compatible to ParsedCredential --------- Co-authored-by: Juliano Cezar Chagas Tavares <[email protected]>
- Loading branch information
1 parent
daedd38
commit 76d2b77
Showing
9 changed files
with
167 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,78 @@ | ||
import Foundation | ||
import SpruceIDMobileSdkRs | ||
import CryptoKit | ||
|
||
public class CredentialPack { | ||
|
||
private var credentials: [Credential] | ||
private var credentials: [ParsedCredential] | ||
|
||
/// Initialize an empty CredentialPack. | ||
public init() { | ||
self.credentials = [] | ||
} | ||
|
||
public init(credentials: [Credential]) { | ||
/// Initialize a CredentialPack from existing credentials. | ||
public init(credentials: [ParsedCredential]) { | ||
self.credentials = credentials | ||
} | ||
|
||
public func addW3CVC(credentialString: String) throws -> [Credential]? { | ||
do { | ||
let credential = try W3CVC(credentialString: credentialString) | ||
self.credentials.append(credential) | ||
return self.credentials | ||
} catch { | ||
throw error | ||
} | ||
/// Add a JwtVc to the CredentialPack. | ||
public func addJwtVc(jwtVc: JwtVc) -> [ParsedCredential] { | ||
self.credentials.append(ParsedCredential.newJwtVcJson(jwtVc: jwtVc)) | ||
return self.credentials | ||
} | ||
|
||
public func addMDoc(mdocBase64: String, keyAlias: String = UUID().uuidString) throws -> [Credential]? { | ||
let mdocData = Data(base64Encoded: mdocBase64)! | ||
let credential = MDoc(fromMDoc: mdocData, namespaces: [:], keyAlias: keyAlias)! | ||
self.credentials.append(credential) | ||
/// Add a JsonVc to the CredentialPack. | ||
public func addJsonVc(jsonVc: JsonVc) -> [ParsedCredential] { | ||
self.credentials.append(ParsedCredential.newLdpVc(jsonVc: jsonVc)) | ||
return self.credentials | ||
} | ||
|
||
public func get(keys: [String]) -> [String: [String: GenericJSON]] { | ||
var values: [String: [String: GenericJSON]] = [:] | ||
for cred in self.credentials { | ||
values[cred.id] = cred.get(keys: keys) | ||
} | ||
|
||
return values | ||
/// Add an Mdoc to the CredentialPack. | ||
public func addMDoc(mdoc: Mdoc) -> [ParsedCredential] { | ||
self.credentials.append(ParsedCredential.newMsoMdoc(mdoc: mdoc)) | ||
return self.credentials | ||
} | ||
|
||
public func get(credentialsIds: [String]) -> [Credential] { | ||
return self.credentials.filter { credentialsIds.contains($0.id) } | ||
/// Find credential claims from all credentials in this CredentialPack. | ||
public func findCredentialClaims(claimNames: [String]) -> [Uuid: [String: GenericJSON]] { | ||
Dictionary(uniqueKeysWithValues: self.list() | ||
.map { credential in | ||
var claims: [String: GenericJSON] | ||
if let mdoc = credential.asMsoMdoc() { | ||
claims = mdoc.jsonEncodedDetails(containing: claimNames) | ||
} else if let jwtVc = credential.asJwtVc() { | ||
claims = jwtVc.credentialClaims(containing: claimNames) | ||
} else if let jsonVc = credential.asJsonVc() { | ||
claims = jsonVc.credentialClaims(containing: claimNames) | ||
} else { | ||
var type: String | ||
do { | ||
type = try credential.intoGenericForm().type | ||
} catch { | ||
type = "unknown" | ||
} | ||
print("unsupported credential type: \(type)") | ||
claims = [:] | ||
} | ||
return (credential.id(), claims) | ||
}) | ||
} | ||
|
||
public func get(credentialId: String) -> Credential? { | ||
if let credential = self.credentials.first(where: { $0.id == credentialId }) { | ||
return credential | ||
} else { | ||
return nil | ||
/// Get credentials by id. | ||
public func get(credentialsIds: [Uuid]) -> [ParsedCredential] { | ||
return self.credentials.filter { | ||
credentialsIds.contains($0.id()) | ||
} | ||
} | ||
|
||
/// Get a credential by id. | ||
public func get(credentialId: Uuid) -> ParsedCredential? { | ||
return self.credentials.first(where: { $0.id() == credentialId }) | ||
} | ||
|
||
/// List all of the credentials in the CredentialPack. | ||
public func list() -> [ParsedCredential] { | ||
return self.credentials | ||
} | ||
} |
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 |
---|---|---|
@@ -1,26 +1,26 @@ | ||
import Foundation | ||
import SpruceIDMobileSdkRs | ||
|
||
public class CredentialStore { | ||
public var credentials: [Credential] | ||
public var credentials: [ParsedCredential] | ||
|
||
public init(credentials: [Credential]) { | ||
public init(credentials: [ParsedCredential]) { | ||
self.credentials = credentials | ||
} | ||
|
||
// swiftlint:disable force_cast | ||
public func presentMdocBLE(deviceEngagement: DeviceEngagement, | ||
callback: BLESessionStateDelegate, | ||
useL2CAP: Bool = true | ||
// , trustedReaders: TrustedReaders | ||
) async -> IsoMdlPresentation? { | ||
if let firstMdoc = self.credentials.first(where: {$0 is MDoc}) { | ||
return await IsoMdlPresentation(mdoc: firstMdoc as! MDoc, | ||
if let firstMdoc = self.credentials.first(where: { $0.asMsoMdoc() != nil }) { | ||
let mdoc = firstMdoc.asMsoMdoc()! | ||
return await IsoMdlPresentation(mdoc: MDoc(Mdoc: mdoc), | ||
engagement: DeviceEngagement.QRCode, | ||
callback: callback, | ||
useL2CAP: useL2CAP) | ||
} else { | ||
return nil | ||
} | ||
} | ||
// swiftlint:enable force_cast | ||
} |
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