-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agent): implement proof of request protocols
Fixes ATL-2500
- Loading branch information
1 parent
809c1f5
commit f065cff
Showing
14 changed files
with
549 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public enum PrismAgentError: Error { | ||
case invalidURLError | ||
} |
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
111 changes: 111 additions & 0 deletions
111
PrismAgent/Sources/Protocols/ProofPresentation/Presentation.swift
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import Domain | ||
import Foundation | ||
|
||
struct PresentationFormat: Codable, Equatable { | ||
let attachId: String | ||
let format: String | ||
} | ||
|
||
public struct Presentation { | ||
struct Body: Codable, Equatable { | ||
let goalCode: String? | ||
let comment: String? | ||
let lastPresentation: Bool? | ||
let formats: [PresentationFormat] | ||
|
||
init( | ||
goalCode: String? = nil, | ||
comment: String? = nil, | ||
lastPresentation: Bool? = true, | ||
formats: [PresentationFormat] | ||
) { | ||
self.goalCode = goalCode | ||
self.comment = comment | ||
self.lastPresentation = lastPresentation | ||
self.formats = formats | ||
} | ||
} | ||
public let id: String | ||
public let type = ProtocolTypes.didcommPresentation.rawValue | ||
let body: Body | ||
let attachments: [AttachmentDescriptor] | ||
public let thid: String? | ||
public let from: DID | ||
// swiftlint:disable identifier_name | ||
public let to: DID | ||
// swiftlint:enable identifier_name | ||
|
||
init( | ||
id: String = UUID().uuidString, | ||
body: Body, | ||
attachments: [AttachmentDescriptor], | ||
thid: String?, | ||
from: DID, | ||
// swiftlint:disable identifier_name | ||
to: DID | ||
// swiftlint:enable identifier_name | ||
) { | ||
self.id = id | ||
self.body = body | ||
self.attachments = attachments | ||
self.thid = thid | ||
self.from = from | ||
self.to = to | ||
} | ||
|
||
public init(fromMessage: Message) throws { | ||
guard | ||
fromMessage.piuri == ProtocolTypes.didcommPresentation.rawValue, | ||
let fromDID = fromMessage.from, | ||
let toDID = fromMessage.to | ||
else { throw PrismAgentError.invalidPresentationMessageError } | ||
|
||
let body = try JSONDecoder().decode(Body.self, from: fromMessage.body) | ||
self.init( | ||
id: fromMessage.id, | ||
body: body, | ||
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().encode(body), | ||
attachments: attachments, | ||
thid: thid | ||
) | ||
} | ||
|
||
public static func makePresentationFromRequest(msg: Message) throws -> Presentation { | ||
let request = try RequestPresentation(fromMessage: msg) | ||
|
||
return Presentation( | ||
body: Body( | ||
goalCode: request.body.goalCode, | ||
comment: request.body.comment, | ||
lastPresentation: true, | ||
formats: request.body.formats | ||
), | ||
attachments: request.attachments, | ||
thid: msg.id, | ||
from: request.to, | ||
to: request.from) | ||
} | ||
} | ||
|
||
extension Presentation: Equatable { | ||
public static func == (lhs: Presentation, rhs: Presentation) -> Bool { | ||
lhs.id == rhs.id && | ||
lhs.type == rhs.type && | ||
lhs.from == rhs.from && | ||
lhs.to == rhs.to && | ||
lhs.body == rhs.body | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
PrismAgent/Sources/Protocols/ProofPresentation/ProposePresentation.swift
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import Domain | ||
import Foundation | ||
|
||
public struct ProposePresentation { | ||
struct Body: Codable, Equatable { | ||
let goalCode: String? | ||
let comment: String? | ||
let formats: [PresentationFormat] | ||
|
||
init( | ||
goalCode: String? = nil, | ||
comment: String? = nil, | ||
formats: [PresentationFormat] | ||
) { | ||
self.goalCode = goalCode | ||
self.comment = comment | ||
self.formats = formats | ||
} | ||
} | ||
public let id: String | ||
public let type = ProtocolTypes.didcommProposePresentation.rawValue | ||
let body: Body | ||
let attachments: [AttachmentDescriptor] | ||
public let thid: String? | ||
public let from: DID | ||
// swiftlint:disable identifier_name | ||
public let to: DID | ||
// swiftlint:enable identifier_name | ||
|
||
init( | ||
id: String = UUID().uuidString, | ||
body: Body, | ||
attachments: [AttachmentDescriptor], | ||
thid: String?, | ||
from: DID, | ||
// swiftlint:disable identifier_name | ||
to: DID | ||
// swiftlint:enable identifier_name | ||
) { | ||
self.id = id | ||
self.body = body | ||
self.attachments = attachments | ||
self.thid = thid | ||
self.from = from | ||
self.to = to | ||
} | ||
|
||
public init(fromMessage: Message) throws { | ||
guard | ||
fromMessage.piuri == ProtocolTypes.didcommProposePresentation.rawValue, | ||
let fromDID = fromMessage.from, | ||
let toDID = fromMessage.to | ||
else { throw PrismAgentError.invalidProposePresentationMessageError } | ||
|
||
let body = try JSONDecoder().decode(Body.self, from: fromMessage.body) | ||
self.init( | ||
id: fromMessage.id, | ||
body: body, | ||
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().encode(body), | ||
attachments: attachments, | ||
thid: thid | ||
) | ||
} | ||
|
||
public static func makeProposalFromRequest(msg: Message) throws -> ProposePresentation { | ||
let request = try RequestPresentation(fromMessage: msg) | ||
|
||
return ProposePresentation( | ||
body: Body( | ||
goalCode: request.body.goalCode, | ||
comment: request.body.comment, | ||
formats: request.body.formats | ||
), | ||
attachments: request.attachments, | ||
thid: msg.id, | ||
from: request.to, | ||
to: request.from) | ||
} | ||
} | ||
|
||
extension ProposePresentation: Equatable { | ||
public static func == (lhs: ProposePresentation, rhs: ProposePresentation) -> Bool { | ||
lhs.id == rhs.id && | ||
lhs.type == rhs.type && | ||
lhs.from == rhs.from && | ||
lhs.to == rhs.to && | ||
lhs.body == rhs.body | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
PrismAgent/Sources/Protocols/ProofPresentation/RequestPresentation.swift
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import Domain | ||
import Foundation | ||
|
||
public struct RequestPresentation { | ||
struct Body: Codable, Equatable { | ||
let goalCode: String? | ||
let comment: String? | ||
let willConfirm: Bool? | ||
let presentMultiple: Bool? | ||
let formats: [PresentationFormat] | ||
|
||
init( | ||
goalCode: String? = nil, | ||
comment: String? = nil, | ||
willConfirm: Bool? = false, | ||
presentMultiple: Bool? = false, | ||
formats: [PresentationFormat] | ||
) { | ||
self.goalCode = goalCode | ||
self.comment = comment | ||
self.willConfirm = willConfirm | ||
self.presentMultiple = presentMultiple | ||
self.formats = formats | ||
} | ||
} | ||
public let id: String | ||
public let type = ProtocolTypes.didcommRequestCredential.rawValue | ||
let body: Body | ||
let attachments: [AttachmentDescriptor] | ||
public let thid: String? | ||
public let from: DID | ||
// swiftlint:disable identifier_name | ||
public let to: DID | ||
// swiftlint:enable identifier_name | ||
|
||
init( | ||
id: String = UUID().uuidString, | ||
body: Body, | ||
attachments: [AttachmentDescriptor], | ||
thid: String?, | ||
from: DID, | ||
// swiftlint:disable identifier_name | ||
to: DID | ||
// swiftlint:enable identifier_name | ||
) { | ||
self.id = id | ||
self.body = body | ||
self.attachments = attachments | ||
self.thid = thid | ||
self.from = from | ||
self.to = to | ||
} | ||
|
||
public init(fromMessage: Message) throws { | ||
guard | ||
fromMessage.piuri == ProtocolTypes.didcommRequestCredential.rawValue, | ||
let fromDID = fromMessage.from, | ||
let toDID = fromMessage.to | ||
else { throw PrismAgentError.invalidRequestPresentationMessageError } | ||
|
||
let body = try JSONDecoder().decode(Body.self, from: fromMessage.body) | ||
self.init( | ||
id: fromMessage.id, | ||
body: body, | ||
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().encode(body), | ||
attachments: attachments, | ||
thid: thid | ||
) | ||
} | ||
|
||
public static func makeRequestFromProposal(msg: Message) throws -> RequestPresentation { | ||
let request = try ProposePresentation(fromMessage: msg) | ||
|
||
return RequestPresentation( | ||
body: Body( | ||
goalCode: request.body.goalCode, | ||
comment: request.body.comment, | ||
willConfirm: false, | ||
presentMultiple: false, | ||
formats: request.body.formats | ||
), | ||
attachments: request.attachments, | ||
thid: msg.id, | ||
from: request.to, | ||
to: request.from) | ||
} | ||
} | ||
|
||
extension RequestPresentation: Equatable { | ||
public static func == (lhs: RequestPresentation, rhs: RequestPresentation) -> Bool { | ||
lhs.id == rhs.id && | ||
lhs.type == rhs.type && | ||
lhs.from == rhs.from && | ||
lhs.to == rhs.to && | ||
lhs.body == rhs.body | ||
} | ||
} |
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
Oops, something went wrong.