Skip to content

Commit f482b20

Browse files
authored
fix: jwt vc and vp (#224)
Signed-off-by: Allain Magyar <allain.magyar@iohk.io>
1 parent 8fa112b commit f482b20

File tree

11 files changed

+72
-19
lines changed

11 files changed

+72
-19
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
3+
public extension String {
4+
func parseIfUrl() throws -> String {
5+
guard self.contains("data:application/"), let url = URL(string: self) else {
6+
return self
7+
}
8+
let data = try Data(contentsOf: url)
9+
return String(data: data, encoding: .utf8) ?? self
10+
}
11+
}

E2E/Tests/Source/Resolvers/PrismShortFormResolver.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class PrismShortFormResolver: DIDResolverDomain {
3636
}
3737
return DIDDocument.Service(
3838
id: service.id,
39-
type: type,
40-
serviceEndpoint: try convertServiceEndpoints(from: service.serviceEndpoint)
39+
type: .many(type),
40+
serviceEndpoint: .many(try convertServiceEndpoints(from: service.serviceEndpoint))
4141
)
4242
} ?? []
4343
)

E2E/Tests/Source/Workflows/EdgeAgentWorkflow.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class EdgeAgentWorkflow {
143143
) { sdk in
144144
let message = sdk.issueCredentialStack.removeFirst()
145145
let issuedCredential = try IssueCredential3_0(fromMessage: message)
146-
_ = try await sdk.didcommAgent.processIssuedCredentialMessage(message: issuedCredential)
146+
let credential = try await sdk.didcommAgent.processIssuedCredentialMessage(message: issuedCredential)
147147
try await edgeAgent.remember(key: recordId, value: message.id)
148148
}
149149
}
@@ -164,11 +164,16 @@ class EdgeAgentWorkflow {
164164
) { sdk in
165165
let credentials = sdk.didcommAgent.edgeAgent.verifiableCredentials()
166166
let credential = try await credentials.map { $0.first }.first().await()
167+
168+
guard let credential else {
169+
throw ValidationError.error(message: "No credential available to present")
170+
}
171+
167172
let message = sdk.proofOfRequestStack.removeFirst()
168173
let requestPresentationMessage = try RequestPresentation(fromMessage: message)
169174
let sendProofMessage = try await sdk.didcommAgent.createPresentationForRequestProof(
170175
request: requestPresentationMessage,
171-
credential: credential!,
176+
credential: credential,
172177
options: [.disclosingClaims(claims: ["automation-required"])]
173178
).makeMessage()
174179
_ = try await sdk.didcommAgent.sendMessage(message: sendProofMessage)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Domain
2+
import Foundation
3+
4+
extension JWTCredential: ExportableCredential {
5+
public var exporting: Data {
6+
(try? jwtString.tryToData()) ?? Data()
7+
}
8+
9+
public var restorationType: String { "jwt" }
10+
}

EdgeAgentSDK/Pollux/Sources/Models/JWT/JWTCredential+StorableCredential.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extension JWTCredential: StorableCredential {
77
}
88

99
public var recoveryId: String {
10-
"jwt+vc"
10+
"jwt+credential"
1111
}
1212

1313
public var credentialData: Data {

EdgeAgentSDK/Pollux/Sources/Models/JWT/JWTEnvelopedVerfiablePresentation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public struct EnvelopedVerfiablePresentation: RawCodable {
2626
enum CodingKeys: String, CodingKey {
2727
case context = "@context"
2828
case type
29-
case verifiableCredential
29+
case id
3030
}
3131

3232
/// The JSON‑LD `@context` values for the enveloped presentation entry (one‑or‑many).
@@ -62,7 +62,7 @@ public struct EnvelopedVerfiablePresentation: RawCodable {
6262
public init(from decoder: any Decoder) throws {
6363
let container = try decoder.container(keyedBy: CodingKeys.self)
6464
self.context = try container.decodeIfPresent(OneOrMany<String>.self, forKey: .context) ?? .many([])
65-
self.id = try container.decode(String.self, forKey: .context)
65+
self.id = try container.decode(String.self, forKey: .id)
6666
self.type = try container.decodeIfPresent(OneOrMany<String>.self, forKey: .type) ?? .many([])
6767
self.raw = try AnyCodable(from: decoder)
6868
}
@@ -71,7 +71,7 @@ public struct EnvelopedVerfiablePresentation: RawCodable {
7171
guard let raw else {
7272
var container = encoder.container(keyedBy: CodingKeys.self)
7373
try container.encode(context, forKey: .context)
74-
try container.encode(id, forKey: .context)
74+
try container.encode(id, forKey: .id)
7575
try container.encode(type, forKey: .type)
7676
return
7777
}

EdgeAgentSDK/Pollux/Sources/Models/JWT/JWTEnvelopedVerifiableCredential.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import Core
12
import Domain
23
import Foundation
34

4-
public struct JWTEnvelopedVerifiableCredential<Credential: Codable>: Codable {
5+
public struct JWTEnvelopedVerifiableCredential<Credential: Codable>: RawCodable {
56
public let iss: String?
67
public let sub: String?
78
public let nbf: Date?
@@ -10,6 +11,7 @@ public struct JWTEnvelopedVerifiableCredential<Credential: Codable>: Codable {
1011
public let jti: String?
1112
public let aud: [String]?
1213
public let vc: Credential
14+
public let raw: AnyCodable?
1315

1416
init(
1517
iss: String? = nil,
@@ -19,7 +21,8 @@ public struct JWTEnvelopedVerifiableCredential<Credential: Codable>: Codable {
1921
iat: Date? = nil,
2022
jti: String? = nil,
2123
aud: [String]? = nil,
22-
vc: Credential
24+
vc: Credential,
25+
raw: AnyCodable? = nil
2326
) {
2427
self.iss = iss
2528
self.sub = sub
@@ -29,6 +32,7 @@ public struct JWTEnvelopedVerifiableCredential<Credential: Codable>: Codable {
2932
self.jti = jti
3033
self.aud = aud
3134
self.vc = vc
35+
self.raw = raw
3236
}
3337

3438
enum CodingKeys: CodingKey {
@@ -63,5 +67,22 @@ public struct JWTEnvelopedVerifiableCredential<Credential: Codable>: Codable {
6367
} else {
6468
self.vc = try Credential(from: decoder)
6569
}
70+
self.raw = try AnyCodable(from: decoder)
71+
}
72+
73+
public func encode(to encoder: any Encoder) throws {
74+
guard let raw else {
75+
var container = encoder.container(keyedBy: JWTEnvelopedVerifiableCredential<Credential>.CodingKeys.self)
76+
try container.encodeIfPresent(self.iss, forKey: .iss)
77+
try container.encodeIfPresent(self.sub, forKey: .sub)
78+
try container.encodeIfPresent(self.nbf, forKey: .nbf)
79+
try container.encodeIfPresent(self.exp, forKey: .exp)
80+
try container.encodeIfPresent(self.iat, forKey: .iat)
81+
try container.encodeIfPresent(self.jti, forKey: .jti)
82+
try container.encodeIfPresent(self.aud, forKey: .aud)
83+
try container.encode(self.vc, forKey: .vc)
84+
return
85+
}
86+
try raw.encode(to: encoder)
6687
}
6788
}

EdgeAgentSDK/Pollux/Sources/Models/AnonCreds/JWTCredential+ExportableCredential.swift renamed to EdgeAgentSDK/Pollux/Sources/Models/JWT/Legacy/LegacyJWTCredential+ExportableCredential.swift

File renamed without changes.

EdgeAgentSDK/Pollux/Sources/Models/JWT/Presentation/JWTCreatePresentation.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct JWTCreatePresentation {
7676
throw PolluxError.credentialIsNotOfPresentationDefinitionRequiredAlgorithm
7777
}
7878

79-
let credentialSubject = try JSONEncoder().encode(credential.defaultEnvelop.vc)
79+
let credentialSubject = try JSONEncoder().encode(credential.defaultEnvelop)
8080

8181
try presentationRequest.presentationDefinition.inputDescriptors.forEach {
8282
try $0.constraints.fields.forEach {
@@ -92,7 +92,7 @@ struct JWTCreatePresentation {
9292
format: "jwt",
9393
pathNested: .init(
9494
id: $0.id,
95-
path: "$.vp.verifiableCredential[0]",
95+
path: "$.vp.verifiableCredential[0].id",
9696
format: "jwt"
9797
)
9898
)
@@ -126,7 +126,7 @@ struct JWTCreatePresentation {
126126
credential: JWTCredential,
127127
request: Data,
128128
did: DID
129-
) throws -> JWTEnvelopedVerifiablePresentation<VerifiablePresentation<EnvelopedVerfiablePresentation>> {
129+
) throws -> JWTEnvelopedVerifiablePresentation<VerifiablePresentation<OneOrMany<EnvelopedVerfiablePresentation>>> {
130130
let jsonObject = try JSONSerialization.jsonObject(with: request)
131131
guard
132132
let domain = findValue(forKey: "domain", in: jsonObject),
@@ -140,17 +140,17 @@ struct JWTCreatePresentation {
140140
vp: VerifiablePresentation(
141141
context: .one(W3CRegisteredConstants.verifiableCredential2_0Context),
142142
type: .one(W3CRegisteredConstants.verifiablePresentationType),
143-
verifiableCredential: EnvelopedVerfiablePresentation(
143+
verifiableCredential: .many([EnvelopedVerfiablePresentation(
144144
context: .one(W3CRegisteredConstants.verifiableCredential2_0Context),
145145
id: "data:application/vc+jwt,\(credential.jwtString)",
146146
type: .one(W3CRegisteredConstants.envelopedVerifiableCredentialType)
147-
)
147+
)])
148148
)
149149
)
150150
}
151151

152-
private func vcPresentationJWTString(
153-
payload: JWTEnvelopedVerifiablePresentation<VerifiablePresentation<EnvelopedVerfiablePresentation>>,
152+
private func vcPresentationJWTString<Payload: Codable>(
153+
payload: Payload,
154154
exportableKey: ExportableKey
155155
) throws -> String {
156156
let keyJWK = exportableKey.jwk

EdgeAgentSDK/Pollux/Sources/Operation/JWT/VerifyJWT.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Core
12
import Domain
23
import Foundation
34
import JSONWebAlgorithms
@@ -7,6 +8,7 @@ struct VerifyJWT {
78
let castor: Castor
89

910
func verifyJWT(jwtString: String) async throws -> Bool {
11+
let jwtString = try jwtString.parseIfUrl()
1012
try await verifyJWTCredentialRevocation(jwtString: jwtString)
1113
let payload: DefaultJWTClaimsImpl = try JWT.getPayload(jwtString: jwtString)
1214
guard let issuer = payload.iss else {

0 commit comments

Comments
 (0)