From 33d700b590ad120f5939d106fdb5dca4e5316661 Mon Sep 17 00:00:00 2001 From: goncalo-frade-iohk Date: Mon, 20 May 2024 13:41:24 +0100 Subject: [PATCH] feat(agent): revocation notification Signed-off-by: goncalo-frade-iohk --- .../RevocationNotification.swift | 118 ++++++++++++++++++ .../Sources/Protocols/ProtocolTypes.swift | 1 + 2 files changed, 119 insertions(+) create mode 100644 EdgeAgentSDK/EdgeAgent/Sources/Protocols/IssueCredential/RevocationNotification.swift diff --git a/EdgeAgentSDK/EdgeAgent/Sources/Protocols/IssueCredential/RevocationNotification.swift b/EdgeAgentSDK/EdgeAgent/Sources/Protocols/IssueCredential/RevocationNotification.swift new file mode 100644 index 00000000..00921b46 --- /dev/null +++ b/EdgeAgentSDK/EdgeAgent/Sources/Protocols/IssueCredential/RevocationNotification.swift @@ -0,0 +1,118 @@ +import Core +import Domain +import Foundation + +// ALL parameters are DIDCOMMV2 format and naming conventions and follows the protocol +// https://github.com/hyperledger/aries-rfcs/blob/main/features/0183-revocation-notification/README.md +public struct RevocationNotification { + public struct Body: Codable, Equatable { + public let issueCredentialProtocolThreadId: String + public let comment: String? + + public init( + issueCredentialProtocolThreadId: String, + comment: String? = nil + ) { + self.issueCredentialProtocolThreadId = issueCredentialProtocolThreadId + self.comment = comment + } + } + + public let id: String + public let type: String + public let body: Body + public let attachments: [AttachmentDescriptor] + public let thid: String? + public let from: DID + public let to: DID + + init( + id: String = UUID().uuidString, + body: Body, + type: String, + attachments: [AttachmentDescriptor], + thid: String?, + from: DID, + to: DID + ) { + self.id = id + self.body = body + self.type = type + self.attachments = attachments + self.thid = thid + self.from = from + self.to = to + } + + public init(fromMessage: Message) throws { + guard + let piuri = ProtocolTypes(rawValue: fromMessage.piuri), + piuri == ProtocolTypes.didcommRevocationNotification, + let fromDID = fromMessage.from, + let toDID = fromMessage.to + else { throw EdgeAgentError.invalidMessageType( + type: fromMessage.piuri, + shouldBe: [ProtocolTypes.didcommRevocationNotification.rawValue] + ) } + + let body = try JSONDecoder.didComm().decode(Body.self, from: fromMessage.body) + self.init( + id: fromMessage.id, + body: body, + type: piuri.rawValue, + attachments: fromMessage.attachments, + thid: fromMessage.thid, + from: fromDID, + to: toDID + ) + } + + public func makeMessage() throws -> Message { + .init( + id: id, + piuri: type, + from: from, + to: to, + body: try JSONEncoder.didComm().encode(body), + attachments: attachments, + thid: thid, + direction: .sent + ) + } + + public static func makeRequestFromOfferCredential(offer: OfferCredential) throws -> RequestCredential { + guard + let offerPiuri = ProtocolTypes(rawValue: offer.type) + else { + throw EdgeAgentError.invalidMessageType( + type: offer.type, + shouldBe: [ + ProtocolTypes.didcommRevocationNotification.rawValue + ] + ) + } + + return RequestCredential( + body: .init( + goalCode: offer.body.goalCode, + comment: offer.body.comment, + formats: offer.body.formats + ), + type: ProtocolTypes.didcommRevocationNotification.rawValue, + attachments: offer.attachments, + thid: offer.thid, // TODO: This needs to be changed in the pr + from: offer.to, + to: offer.from + ) + } +} + +extension RevocationNotification: Equatable { + public static func == (lhs: RevocationNotification, rhs: RevocationNotification) -> Bool { + lhs.id == rhs.id && + lhs.type == rhs.type && + lhs.from == rhs.from && + lhs.to == rhs.to && + lhs.body == rhs.body + } +} diff --git a/EdgeAgentSDK/EdgeAgent/Sources/Protocols/ProtocolTypes.swift b/EdgeAgentSDK/EdgeAgent/Sources/Protocols/ProtocolTypes.swift index 319462ab..b431e66d 100644 --- a/EdgeAgentSDK/EdgeAgent/Sources/Protocols/ProtocolTypes.swift +++ b/EdgeAgentSDK/EdgeAgent/Sources/Protocols/ProtocolTypes.swift @@ -22,6 +22,7 @@ public enum ProtocolTypes: String { case didcommRequestCredential3_0 = "https://didcomm.org/issue-credential/3.0/request-credential" case didcommconnectionRequest = "https://atalaprism.io/mercury/connections/1.0/request" case didcommconnectionResponse = "https://atalaprism.io/mercury/connections/1.0/response" + case didcommRevocationNotification = "https://atalaprism.io/revocation_notification/1.0/revoke" case didcomminvitation = "https://didcomm.org/out-of-band/2.0/invitation" case prismOnboarding = "https://atalaprism.io/did-request" case pickupRequest = "https://didcomm.org/messagepickup/3.0/delivery-request"