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

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

25 Sep 2024 02:40PM UTC coverage: 40.842% (-3.1%) from 43.981%
11035384347

Pull #159

github

web-flow
Merge 65ff99d66 into d3597e19d
Pull Request #159: feat(agent): agent separation of concerns

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

187 existing lines in 14 files now uncovered.

5202 of 12737 relevant lines covered (40.84%)

97.32 hits per line

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

7.0
/EdgeAgentSDK/EdgeAgent/Sources/DIDCommAgent/DIDCommAgent+DIDs.swift
1
import Combine
2
import Domain
3
import Foundation
4

5
// MARK: DID High Level functionalities
6
public extension DIDCommAgent {
7

8
    /// This method create a new Prism DID, that can be used to identify the agent and interact with other agents.
9
    /// - Parameters:
10
    ///   - keyPathIndex: key path index used to identify the DID
11
    ///   - alias: An alias that can be used to identify the DID
12
    ///   - services: an array of services associated to the DID
13
    /// - Returns: The new created DID
14
    func createNewPrismDID(
15
        keyPathIndex: Int? = nil,
16
        alias: String? = nil,
17
        services: [DIDDocument.Service] = []
18
    ) async throws -> DID {
12✔
19
        try await edgeAgent.createNewPrismDID(
12✔
20
            keyPathIndex: keyPathIndex,
12✔
21
            alias: alias,
12✔
22
            services: services
12✔
23
        )
12✔
24
    }
12✔
25

26
    /// This method registers a Prism DID, that can be used to identify the agent and interact with other agents.
27
    /// - Parameters:
28
    ///   - did: the DID which will be registered.
29
    ///   - keyPathIndex: key path index used to identify the DID
30
    ///   - alias: An alias that can be used to identify the DID
31
    /// - Returns: The new created DID
32
    func registerPrismDID(
33
        did: DID,
34
        privateKey: PrivateKey,
35
        alias: String? = nil
NEW
36
    ) async throws {
×
NEW
37
        try await edgeAgent.registerPrismDID(
×
NEW
38
            did: did,
×
NEW
39
            privateKey: privateKey,
×
NEW
40
            alias: alias
×
NEW
41
        )
×
NEW
42
    }
×
43
    /// This function creates a new Peer DID, stores it in pluto database and updates the mediator if requested.
44
    ///
45
    /// - Parameters:
46
    ///   - services: The services associated to the new DID.
47
    ///   - updateMediator: Indicates if the new DID should be added to the mediator's list. It will as well add the mediator service.
48
    /// - Returns: A new DID
49
    /// - Throws: EdgeAgentError, if updateMediator is true and there is no mediator available or if storing the new DID failed
50
    func createNewPeerDID(
51
        services: [DIDDocument.Service] = [],
52
        alias: String? = "",
53
        updateMediator: Bool
NEW
54
    ) async throws -> DID {
×
NEW
55
        var keyAgreementPrivateKey = try apollo.createPrivateKey(parameters: [
×
NEW
56
            KeyProperties.type.rawValue: "EC",
×
NEW
57
            KeyProperties.curve.rawValue: KnownKeyCurves.x25519.rawValue
×
NEW
58
        ])
×
NEW
59

×
NEW
60
        var authenticationPrivateKey = try apollo.createPrivateKey(parameters: [
×
NEW
61
            KeyProperties.type.rawValue: "EC",
×
NEW
62
            KeyProperties.curve.rawValue: KnownKeyCurves.ed25519.rawValue
×
NEW
63
        ])
×
NEW
64

×
NEW
65
        let withServices: [DIDDocument.Service]
×
NEW
66
        if updateMediator, let routingDID = mediatorRoutingDID?.string {
×
NEW
67
            withServices = services + [.init(
×
NEW
68
                id: "#didcomm-1",
×
NEW
69
                type: ["DIDCommMessaging"],
×
NEW
70
                serviceEndpoint: [.init(
×
NEW
71
                    uri: routingDID
×
NEW
72
                )])]
×
NEW
73
        } else {
×
NEW
74
            withServices = services
×
NEW
75
        }
×
NEW
76

×
NEW
77
        let newDID = try castor.createPeerDID(
×
NEW
78
            keyAgreementPublicKey: keyAgreementPrivateKey.publicKey(),
×
NEW
79
            authenticationPublicKey: authenticationPrivateKey.publicKey(),
×
NEW
80
            services: withServices
×
NEW
81
        )
×
NEW
82

×
NEW
83
        let didDocument = try await castor.resolveDID(did: newDID)
×
NEW
84
        didDocument.authenticate.first.map { authenticationPrivateKey.identifier = $0.id.string }
×
NEW
85
        didDocument.keyAgreement.first.map { keyAgreementPrivateKey.identifier = $0.id.string }
×
NEW
86

×
NEW
87
        logger.debug(message: "Created new Peer DID", metadata: [
×
NEW
88
            .maskedMetadataByLevel(key: "DID", value: newDID.string, level: .debug)
×
NEW
89
        ])
×
NEW
90

×
NEW
91
        try await registerPeerDID(
×
NEW
92
            did: newDID,
×
NEW
93
            privateKeys: [
×
NEW
94
                keyAgreementPrivateKey,
×
NEW
95
                authenticationPrivateKey
×
NEW
96
            ],
×
NEW
97
            alias: alias,
×
NEW
98
            updateMediator: updateMediator
×
NEW
99
        )
×
NEW
100

×
NEW
101
        return newDID
×
NEW
102
    }
×
103

104
    /// This function registers a Peer DID, stores it and his private key in pluto database and updates the mediator if requested.
105
    ///
106
    /// - Parameters:
107
    ///   - services: The services associated to the new DID.
108
    ///   - updateMediator: Indicates if the new DID should be added to the mediator's list.
109
    /// - Returns: A new DID
110
    /// - Throws: EdgeAgentError, if updateMediator is true and there is no mediator available or if storing the new DID failed
111
    func registerPeerDID(
112
        did: DID,
113
        privateKeys: [PrivateKey],
114
        alias: String?,
115
        updateMediator: Bool
NEW
116
    ) async throws {
×
NEW
117
        if updateMediator {
×
NEW
118
            try await updateMediatorWithDID(did: did)
×
NEW
119
        }
×
NEW
120
        logger.debug(message: "Register of DID in storage", metadata: [
×
NEW
121
            .maskedMetadataByLevel(key: "DID", value: did.string, level: .debug)
×
NEW
122
        ])
×
NEW
123

×
NEW
124
        let storablePrivateKeys = try privateKeys
×
NEW
125
            .map {
×
NEW
126
                guard let storablePrivateKey = $0 as? (PrivateKey & StorableKey) else {
×
NEW
127
                    throw KeyError.keyRequiresConformation(conformations: ["PrivateKey", "StorableKey"])
×
NEW
128
                }
×
NEW
129
                return storablePrivateKey
×
NEW
130
            }
×
NEW
131

×
NEW
132
        try await pluto
×
NEW
133
            .storePeerDID(
×
NEW
134
                did: did,
×
NEW
135
                privateKeys: storablePrivateKeys,
×
NEW
136
                alias: alias
×
NEW
137
            )
×
NEW
138
            .first()
×
NEW
139
            .await()
×
NEW
140
    }
×
141

142
    /// This function updates the mediator key list with a new DID.
143
    ///
144
    /// - Parameters:
145
    ///   - services: The services associated to the new DID.
146
    ///   - updateMediator: Indicates if the new DID should be added to the mediator's list.
147
    /// - Returns: A new DID
148
    /// - Throws: EdgeAgentError, if updateMediator is true and there is no mediator available
NEW
149
    func updateMediatorWithDID(did: DID) async throws {
×
NEW
150
        logger.debug(message: "Update mediator key list with DID", metadata: [
×
NEW
151
            .maskedMetadataByLevel(key: "DID", value: did.string, level: .debug)
×
NEW
152
        ])
×
NEW
153

×
NEW
154
        try await mediationHandler?.updateKeyListWithDIDs(dids: [did])
×
NEW
155
    }
×
156

157
    /// This function gets all the DID peers from the `pluto` store
158
    ///
159
    /// - Returns: A publisher that emits an array of tuples (`DID`, `String?`) objects, or an error if there was a problem getting the dids
NEW
160
    func getAllRegisteredPeerDIDs() -> AnyPublisher<[(did: DID, alias: String?)], Error> {
×
NEW
161
        pluto.getAllPeerDIDs()
×
NEW
162
            .map { $0.map { ($0.did, $0.alias) } }
×
NEW
163
            .eraseToAnyPublisher()
×
NEW
164
    }
×
165
}
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

© 2026 Coveralls, Inc