|
| 1 | +import Core |
| 2 | +import Domain |
| 3 | +import Foundation |
| 4 | + |
| 5 | +// ALL parameters are DIDCOMMV2 format and naming conventions and follows the protocol |
| 6 | +// https://github.com/hyperledger/aries-rfcs/blob/main/features/0183-revocation-notification/README.md |
| 7 | +public struct RevocationNotification { |
| 8 | + public struct Body: Codable, Equatable { |
| 9 | + public let issueCredentialProtocolThreadId: String |
| 10 | + public let comment: String? |
| 11 | + |
| 12 | + public init( |
| 13 | + issueCredentialProtocolThreadId: String, |
| 14 | + comment: String? = nil |
| 15 | + ) { |
| 16 | + self.issueCredentialProtocolThreadId = issueCredentialProtocolThreadId |
| 17 | + self.comment = comment |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + public let id: String |
| 22 | + public let type: String |
| 23 | + public let body: Body |
| 24 | + public let attachments: [AttachmentDescriptor] |
| 25 | + public let thid: String? |
| 26 | + public let from: DID |
| 27 | + public let to: DID |
| 28 | + |
| 29 | + init( |
| 30 | + id: String = UUID().uuidString, |
| 31 | + body: Body, |
| 32 | + type: String, |
| 33 | + attachments: [AttachmentDescriptor], |
| 34 | + thid: String?, |
| 35 | + from: DID, |
| 36 | + to: DID |
| 37 | + ) { |
| 38 | + self.id = id |
| 39 | + self.body = body |
| 40 | + self.type = type |
| 41 | + self.attachments = attachments |
| 42 | + self.thid = thid |
| 43 | + self.from = from |
| 44 | + self.to = to |
| 45 | + } |
| 46 | + |
| 47 | + public init(fromMessage: Message) throws { |
| 48 | + guard |
| 49 | + let piuri = ProtocolTypes(rawValue: fromMessage.piuri), |
| 50 | + piuri == ProtocolTypes.didcommRevocationNotification, |
| 51 | + let fromDID = fromMessage.from, |
| 52 | + let toDID = fromMessage.to |
| 53 | + else { throw EdgeAgentError.invalidMessageType( |
| 54 | + type: fromMessage.piuri, |
| 55 | + shouldBe: [ProtocolTypes.didcommRevocationNotification.rawValue] |
| 56 | + ) } |
| 57 | + |
| 58 | + let body = try JSONDecoder.didComm().decode(Body.self, from: fromMessage.body) |
| 59 | + self.init( |
| 60 | + id: fromMessage.id, |
| 61 | + body: body, |
| 62 | + type: piuri.rawValue, |
| 63 | + attachments: fromMessage.attachments, |
| 64 | + thid: fromMessage.thid, |
| 65 | + from: fromDID, |
| 66 | + to: toDID |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + public func makeMessage() throws -> Message { |
| 71 | + .init( |
| 72 | + id: id, |
| 73 | + piuri: type, |
| 74 | + from: from, |
| 75 | + to: to, |
| 76 | + body: try JSONEncoder.didComm().encode(body), |
| 77 | + attachments: attachments, |
| 78 | + thid: thid, |
| 79 | + direction: .sent |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + public static func makeRequestFromOfferCredential(offer: OfferCredential) throws -> RequestCredential { |
| 84 | + guard |
| 85 | + let offerPiuri = ProtocolTypes(rawValue: offer.type) |
| 86 | + else { |
| 87 | + throw EdgeAgentError.invalidMessageType( |
| 88 | + type: offer.type, |
| 89 | + shouldBe: [ |
| 90 | + ProtocolTypes.didcommRevocationNotification.rawValue |
| 91 | + ] |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + return RequestCredential( |
| 96 | + body: .init( |
| 97 | + goalCode: offer.body.goalCode, |
| 98 | + comment: offer.body.comment, |
| 99 | + formats: offer.body.formats |
| 100 | + ), |
| 101 | + type: ProtocolTypes.didcommRevocationNotification.rawValue, |
| 102 | + attachments: offer.attachments, |
| 103 | + thid: offer.thid, // TODO: This needs to be changed in the pr |
| 104 | + from: offer.to, |
| 105 | + to: offer.from |
| 106 | + ) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +extension RevocationNotification: Equatable { |
| 111 | + public static func == (lhs: RevocationNotification, rhs: RevocationNotification) -> Bool { |
| 112 | + lhs.id == rhs.id && |
| 113 | + lhs.type == rhs.type && |
| 114 | + lhs.from == rhs.from && |
| 115 | + lhs.to == rhs.to && |
| 116 | + lhs.body == rhs.body |
| 117 | + } |
| 118 | +} |
0 commit comments