From 8258034f7c6895526c051f3c4fb370ef27bac6dd Mon Sep 17 00:00:00 2001 From: goncalo-frade-iohk Date: Thu, 22 Jun 2023 21:08:50 +0100 Subject: [PATCH] fix: typo in restoring keys this also fixes a bug found on creating JWTs and was creating an issue with issuing credential and presenting proof --- .../Apollo/Sources/Model/Ed25519Key.swift | 2 +- .../Apollo/Sources/Model/X25519Key.swift | 2 +- ...DDIDPrivateKeyDAO+DIDPrivateKeyStore.swift | 4 ++-- .../Sources/PrismAgent+Credentials.swift | 16 +++------------- .../PrismAgent/Sources/PrismAgent+Proof.swift | 19 ++++--------------- 5 files changed, 11 insertions(+), 32 deletions(-) diff --git a/AtalaPrismSDK/Apollo/Sources/Model/Ed25519Key.swift b/AtalaPrismSDK/Apollo/Sources/Model/Ed25519Key.swift index 40ba2f77..853732bd 100644 --- a/AtalaPrismSDK/Apollo/Sources/Model/Ed25519Key.swift +++ b/AtalaPrismSDK/Apollo/Sources/Model/Ed25519Key.swift @@ -34,7 +34,7 @@ extension Ed25519PrivateKey: SignableKey { extension Ed25519PrivateKey: StorableKey { var securityLevel: SecurityLevel { SecurityLevel.high } - var restorationIdentifier: String { "ed25519+prv" } + var restorationIdentifier: String { "ed25519+priv" } var storableData: Data { raw } } diff --git a/AtalaPrismSDK/Apollo/Sources/Model/X25519Key.swift b/AtalaPrismSDK/Apollo/Sources/Model/X25519Key.swift index 8f11a05b..2d9b0ff9 100644 --- a/AtalaPrismSDK/Apollo/Sources/Model/X25519Key.swift +++ b/AtalaPrismSDK/Apollo/Sources/Model/X25519Key.swift @@ -22,7 +22,7 @@ struct X25519PrivateKey: PrivateKey { extension X25519PrivateKey: StorableKey { var securityLevel: SecurityLevel { SecurityLevel.high } - var restorationIdentifier: String { "x25519+prv" } + var restorationIdentifier: String { "x25519+priv" } var storableData: Data { raw } } diff --git a/AtalaPrismSDK/Pluto/Sources/PersistentStorage/DAO/CDDIDPrivateKeyDAO+DIDPrivateKeyStore.swift b/AtalaPrismSDK/Pluto/Sources/PersistentStorage/DAO/CDDIDPrivateKeyDAO+DIDPrivateKeyStore.swift index e3675527..95677ced 100644 --- a/AtalaPrismSDK/Pluto/Sources/PersistentStorage/DAO/CDDIDPrivateKeyDAO+DIDPrivateKeyStore.swift +++ b/AtalaPrismSDK/Pluto/Sources/PersistentStorage/DAO/CDDIDPrivateKeyDAO+DIDPrivateKeyStore.swift @@ -33,8 +33,8 @@ private extension CDDIDPrivateKey { else { return } switch curve { case .x25519: - self.privateKeyKeyAgreement = $0.storableData - self.curveKeyAgreement = $0.restorationIdentifier + self.privateKeyAuthenticate = $0.storableData + self.curveAuthenticate = $0.restorationIdentifier case .ed25519: self.privateKeyAuthenticate = $0.storableData self.curveAuthenticate = $0.restorationIdentifier diff --git a/AtalaPrismSDK/PrismAgent/Sources/PrismAgent+Credentials.swift b/AtalaPrismSDK/PrismAgent/Sources/PrismAgent+Credentials.swift index 4ea63b9d..13d426e1 100644 --- a/AtalaPrismSDK/PrismAgent/Sources/PrismAgent+Credentials.swift +++ b/AtalaPrismSDK/PrismAgent/Sources/PrismAgent+Credentials.swift @@ -50,8 +50,6 @@ public extension PrismAgent { /// - Throws: PrismAgentError, if there is a problem creating the request credential. func prepareRequestCredentialWithIssuer(did: DID, offer: OfferCredential) async throws -> RequestCredential? { guard did.method == "prism" else { throw PolluxError.invalidPrismDID } - let apollo = self.apollo - let seed = self.seed let didInfo = try await pluto .getDIDInfo(did: did) .first() @@ -60,7 +58,8 @@ public extension PrismAgent { guard let privateKey = didInfo?.privateKeys.first else { throw PrismAgentError.cannotFindDIDKeyPairIndex } guard - let signing = privateKey.signing + let exporting = privateKey.exporting, + let pemData = exporting.pem.data(using: .utf8) else { throw PrismAgentError.cannotFindDIDKeyPairIndex } guard let offerData = offer @@ -93,16 +92,7 @@ public extension PrismAgent { ])) )) - let signer = JWTSigner.none - let withoutSignature = try JWTEncoder(jwtSigner: signer).encodeToString(jwt) - print(withoutSignature) - let removedHeader = withoutSignature.components(separatedBy: ".").last! - let headerBase64 = "{\"typ\": \"JWT\", \"alg\": \"ES256K\"}".data(using: .utf8)!.base64UrlEncodedString() - let body = headerBase64 + "." + removedHeader - let signature = try await signing.sign(data: body.data(using: .utf8)!) - let signatureBase64 = signature.raw.base64UrlEncodedString() - - let jwtString = body + "." + signatureBase64 + let jwtString = try JWTEncoder(jwtSigner: .es256k(privateKey: pemData)).encodeToString(jwt) guard let base64String = jwtString.data(using: .utf8)?.base64EncodedString() else { throw UnknownError.somethingWentWrongError() diff --git a/AtalaPrismSDK/PrismAgent/Sources/PrismAgent+Proof.swift b/AtalaPrismSDK/PrismAgent/Sources/PrismAgent+Proof.swift index 1a2a13b6..e9762fee 100644 --- a/AtalaPrismSDK/PrismAgent/Sources/PrismAgent+Proof.swift +++ b/AtalaPrismSDK/PrismAgent/Sources/PrismAgent+Proof.swift @@ -43,8 +43,6 @@ public extension PrismAgent { throw UnknownError.somethingWentWrongError() } - let apollo = apollo - let seed = seed let didInfo = try await pluto .getDIDInfo(did: subjectDID) .first() @@ -53,7 +51,8 @@ public extension PrismAgent { guard let privateKey = didInfo?.privateKeys.first else { throw PrismAgentError.cannotFindDIDKeyPairIndex } guard - let signing = privateKey.signing + let exporting = privateKey.exporting, + let pemData = exporting.pem.data(using: .utf8) else { throw PrismAgentError.cannotFindDIDKeyPairIndex } let jwt = JWT(claims: ClaimsProofPresentationJWT( @@ -66,18 +65,8 @@ public extension PrismAgent { verifiableCredential: [credential.id] ) )) - let signer = JWTSigner.none - let withoutSignature = try JWTEncoder(jwtSigner: signer).encodeToString(jwt) - print(withoutSignature) - let removedHeader = withoutSignature.components(separatedBy: ".").last! - let headerBase64 = "{\"typ\": \"JWT\", \"alg\": \"ES256K\"}".data(using: .utf8)!.base64UrlEncodedString() - let body = headerBase64 + "." + removedHeader - let signature = try await signing.sign(data: body.data(using: .utf8)!) - let signatureBase64 = signature.raw.base64UrlEncodedString() - - let jwtString = body + "." + signatureBase64 - print("JWTString: \(jwtString)") - //let jwtString = try JWTEncoder(jwtSigner: .es256k(privateKey: pemPrivateKey)).encodeToString(jwt) + let jwtString = try JWTEncoder(jwtSigner: .es256k(privateKey: pemData)).encodeToString(jwt) + guard let base64String = jwtString.data(using: .utf8)?.base64EncodedString() else { throw UnknownError.somethingWentWrongError() }