Skip to content

Commit

Permalink
feat(agent): implement proof of request protocols
Browse files Browse the repository at this point in the history
Fixes ATL-2500
  • Loading branch information
goncalo-frade-iohk committed Dec 1, 2022
1 parent 809c1f5 commit f065cff
Show file tree
Hide file tree
Showing 14 changed files with 549 additions and 31 deletions.
3 changes: 3 additions & 0 deletions PrismAgent/Sources/Error.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public enum PrismAgentError: Error {
case invalidURLError
}
3 changes: 3 additions & 0 deletions PrismAgent/Sources/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ public enum PrismAgentError: Error {
case invalidProposedCredentialMessageError
case invalidIssueCredentialMessageError
case invalidRequestCredentialMessageError
case invalidPresentationMessageError
case invalidRequestPresentationMessageError
case invalidProposePresentationMessageError
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public struct OfferCredential {
)
}

public static func makeOfferToProposedCredential(msg: Message) throws -> OfferCredential {
public static func makeOfferFromProposedCredential(msg: Message) throws -> OfferCredential {
let proposed = try ProposeCredential(fromMessage: msg)

return OfferCredential(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public struct RequestCredential {
)
}

public static func makeFromOffer(message: Message) throws -> RequestCredential {
public static func makeRequestFromOfferCredential(message: Message) throws -> RequestCredential {
let offer = try OfferCredential(fromMessage: message)
return RequestCredential(
body: .init(
Expand Down
111 changes: 111 additions & 0 deletions PrismAgent/Sources/Protocols/ProofPresentation/Presentation.swift
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
}
}
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
}
}
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
}
}
3 changes: 3 additions & 0 deletions PrismAgent/Sources/Protocols/ProtocolTypes.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Foundation

enum ProtocolTypes: String {
case didcommPresentation = "https://didcomm.org/present-proof/2.0/presentation"
case didcommRequestPresentation = "https://didcomm.org/present-proof/2.0/request-presentation"
case didcommProposePresentation = "https://didcomm.org/present-proof/2.0/propose-presentation"
case didcommCredentialPreview = "https://didcomm.org/issue-credential/2.0/credential-preview"
case didcommIssueCredential = "https://didcomm.org/issue-credential/2.0/issue-credential"
case didcommOfferCredential = "https://didcomm.org/issue-credential/2.0/offer-credential"
Expand Down
2 changes: 1 addition & 1 deletion PrismAgent/Tests/OfferCredentialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class OfferCredentialTests: XCTestCase {
)
let proposeMessage = try validProposeCredential.makeMessage()

let testOfferCredential = try OfferCredential.makeOfferToProposedCredential(msg: proposeMessage)
let testOfferCredential = try OfferCredential.makeOfferFromProposedCredential(msg: proposeMessage)
XCTAssertEqual(validProposeCredential.from, testOfferCredential.to)
XCTAssertEqual(validProposeCredential.to, testOfferCredential.from)
XCTAssertEqual(validProposeCredential.attachments, testOfferCredential.attachments)
Expand Down
Loading

0 comments on commit f065cff

Please sign in to comment.