• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperledger / identus-edge-agent-sdk-swift / 11049785480

25 Sep 2024 02:40PM UTC coverage: 40.771% (-3.2%) from 43.981%
11049785480

push

github

goncalo-frade-iohk
feat!(agent): agent separation of concerns

BREAKING CHANGE: This is a refactor, from now on the EdgeAgent will not have any reference with DIDComm and a DIDCommAgent will replace this.
EdgeAgent now will scope all the logic that is inherent to it removing any transport layer association, and new agents like DIDCommAgent will scope the EdgeAgent functionalities for a transport layer.

With this Pollux also has some significant changes so it is not aggregated to the DIDComm Message.

OIDCAgent will take part of OIDC transport layer communication.

Signed-off-by: goncalo-frade-iohk <goncalo.frade@iohk.io>

232 of 1063 new or added lines in 19 files covered. (21.83%)

187 existing lines in 14 files now uncovered.

5193 of 12737 relevant lines covered (40.77%)

97.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.15
/EdgeAgentSDK/EdgeAgent/Sources/DIDCommAgent/DIDCommAgent+Invitations.swift
1
import Domain
2
import Foundation
3

4
public extension DIDCommAgent {
5
    /// Enumeration representing the type of invitation
6
    enum InvitationType {
7
        /// Struct representing a Prism Onboarding invitation
8
        public struct PrismOnboarding {
9
            /// Sender of the invitation
10
            public let from: String
11
            /// Onboarding endpoint
12
            public let endpoint: URL
13
            /// The own DID of the user
14
            public let ownDID: DID
15
        }
16

17
        /// Case representing a Prism Onboarding invitation
18
        case onboardingPrism(PrismOnboarding)
19
        /// Case representing a DIDComm Out-of-Band invitation
20
        case onboardingDIDComm(OutOfBandInvitation)
21
    }
22

23
    /// Parses the given string as an Out-of-Band invitation
24
    /// - Parameter url: The string to parse
25
    /// - Returns: The parsed Out-of-Band invitation
26
    /// - Throws: `EdgeAgentError` if the string is not a valid URL
27
    func parseOOBInvitation(url: String) throws -> OutOfBandInvitation {
2✔
28
        guard let url = URL(string: url) else { throw CommonError.invalidURLError(url: url) }
2✔
29
        return try parseOOBInvitation(url: url)
2✔
30
    }
2✔
31

32
    /// Parses the given URL as an Out-of-Band invitation
33
    /// - Parameter url: The URL to parse
34
    /// - Returns: The parsed Out-of-Band invitation
35
    /// - Throws: `EdgeAgentError` if the URL is not a valid Out-of-Band invitation
36
    func parseOOBInvitation(url: URL) throws -> OutOfBandInvitation {
2✔
37
        return try DIDCommInvitationRunner(url: url).run()
2✔
38
    }
2✔
39

40
    /// Accepts a Prism Onboarding invitation and performs the onboarding process
41
    /// - Parameter invitation: The Prism Onboarding invitation to accept
42
    /// - Throws: `EdgeAgentError` if the onboarding process fails
NEW
43
    func acceptPrismInvitation(invitation: InvitationType.PrismOnboarding) async throws {
×
NEW
44
        struct SendDID: Encodable {
×
NEW
45
            let did: String
×
NEW
46
        }
×
NEW
47
        var request = URLRequest(url: invitation.endpoint)
×
NEW
48
        request.httpMethod = "POST"
×
NEW
49
        request.httpBody = try JSONEncoder().encode(SendDID(did: invitation.ownDID.string))
×
NEW
50
        request.setValue("application/json", forHTTPHeaderField: "content-type")
×
NEW
51
        do {
×
NEW
52
            let response = try await URLSession.shared.data(for: request)
×
NEW
53
            guard let urlResponse = response.1 as? HTTPURLResponse else {
×
NEW
54
                throw CommonError.invalidCoding(
×
NEW
55
                    message: "This should not happen cannot convert URLResponse to HTTPURLResponse"
×
NEW
56
                )
×
NEW
57
            }
×
NEW
58
            guard urlResponse.statusCode == 200 else {
×
NEW
59
                throw CommonError.httpError(
×
NEW
60
                    code: urlResponse.statusCode,
×
NEW
61
                    message: String(data: response.0, encoding: .utf8) ?? ""
×
NEW
62
                )
×
NEW
63
            }
×
NEW
64
        }
×
NEW
65
    }
×
66

67
    /// Parses the given string as an invitation
68
    /// - Parameter str: The string to parse
69
    /// - Returns: The parsed invitation
70
    /// - Throws: `EdgeAgentError` if the invitation is not a valid Prism or OOB type
71
    func parseInvitation(str: String) async throws -> InvitationType {
2✔
72
        if let prismOnboarding = try? await parsePrismInvitation(str: str) {
2✔
73
            return .onboardingPrism(prismOnboarding)
×
74
        } else if let message = try? parseOOBInvitation(url: str) {
2✔
75
            return .onboardingDIDComm(message)
2✔
76
        }
2✔
77
        throw EdgeAgentError.unknownInvitationTypeError
×
78
    }
2✔
79

80
    /// Parses the given string as a Prism Onboarding invitation
81
    /// - Parameter str: The string to parse
82
    /// - Returns: The parsed Prism Onboarding invitation
83
    /// - Throws: `EdgeAgentError` if the string is not a valid Prism Onboarding invitation
84
    func parsePrismInvitation(
85
        str: String
86
    ) async throws -> InvitationType.PrismOnboarding {
2✔
87
        let prismOnboarding = try PrismOnboardingInvitation(jsonString: str)
2✔
88
        guard
2✔
89
            let url = URL(string: prismOnboarding.body.onboardEndpoint)
2✔
90
        else { throw CommonError.invalidURLError(url: prismOnboarding.body.onboardEndpoint) }
2✔
91

2✔
92
        let ownDID = try await createNewPeerDID(
2✔
93
            services: [.init(
2✔
94
                id: "#didcomm-1",
2✔
95
                type: ["DIDCommMessaging"],
2✔
96
                serviceEndpoint: [.init(
2✔
97
                    uri: "https://localhost:8080/didcomm"
2✔
98
                )]
2✔
99
            )],
2✔
100
            updateMediator: false
2✔
101
        )
2✔
102

2✔
103
        return .init(
2✔
104
            from: prismOnboarding.body.from,
2✔
105
            endpoint: url,
2✔
106
            ownDID: ownDID
2✔
107
        )
2✔
108
    }
2✔
109

110
    /// Accepts an Out-of-Band (DIDComm) invitation and establishes a new connection
111
    /// - Parameter invitation: The Out-of-Band invitation to accept
112
    /// - Throws: `EdgeAgentError` if there is no mediator available or other errors occur during the acceptance process
113
    func acceptDIDCommInvitation(invitation: OutOfBandInvitation) async throws {
×
114
        guard
×
115
            let connectionManager
×
116
        else { throw EdgeAgentError.noMediatorAvailableError }
×
117
        logger.info(message: "Start accept DIDComm invitation")
×
118
        let ownDID = try await createNewPeerDID(updateMediator: true)
×
119

×
120
        logger.info(message: "Sending DIDComm Connection message")
×
121

×
122
        let pair = try await DIDCommConnectionRunner(
×
123
            invitationMessage: invitation,
×
124
            pluto: pluto,
×
125
            ownDID: ownDID,
×
126
            connection: connectionManager
×
127
        ).run()
×
128
        try await connectionManager.addConnection(pair)
×
129
    }
×
130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc