]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSPeerProvider.m
Security-59306.41.2.tar.gz
[apple/security.git] / keychain / ckks / CKKSPeerProvider.m
1 #if OCTAGON
2 #import "keychain/ckks/CKKS.h"
3 #import "keychain/ckks/CKKSPeerProvider.h"
4 #import "keychain/categories/NSError+UsefulConstructors.h"
5
6 @implementation CKKSPeerProviderState
7 - (instancetype)initWithPeerProviderID:(NSString*)providerID
8 essential:(BOOL)essential
9 selfPeers:(CKKSSelves* _Nullable)selfPeers
10 selfPeersError:(NSError* _Nullable)selfPeersError
11 trustedPeers:(NSSet<id<CKKSRemotePeerProtocol>>* _Nullable)currentTrustedPeers
12 trustedPeersError:(NSError* _Nullable)trustedPeersError
13 {
14 if((self = [super init])) {
15 _peerProviderID = providerID;
16 _essential = essential;
17 _currentSelfPeers = selfPeers;
18 _currentSelfPeersError = selfPeersError;
19 _currentTrustedPeers = currentTrustedPeers;
20 _currentTrustedPeersError = trustedPeersError;
21
22 if(_currentTrustedPeers) {
23 NSMutableSet<NSString*>* trustedPeerIDs = [NSMutableSet set];
24 for(id<CKKSPeer> peer in _currentTrustedPeers) {
25 [trustedPeerIDs addObject:peer.peerID];
26 }
27 _currentTrustedPeerIDs = trustedPeerIDs;
28 }
29 }
30 return self;
31 }
32
33 - (NSString*)description
34 {
35 return [NSString stringWithFormat:@"<CKKSPeerProviderState(%@): %@%@ %@%@>",
36 self.peerProviderID,
37 self.currentSelfPeers,
38 self.currentSelfPeersError ?: @"",
39 self.currentTrustedPeers,
40 self.currentTrustedPeersError ?: @""];
41 }
42
43 + (CKKSPeerProviderState*)noPeersState:(id<CKKSPeerProvider>)provider
44 {
45 return [[CKKSPeerProviderState alloc] initWithPeerProviderID:provider.providerID
46 essential:provider.essential
47 selfPeers:nil
48 selfPeersError:[NSError errorWithDomain:CKKSErrorDomain
49 code:CKKSNoPeersAvailable
50 description:@"No current self peer available"]
51 trustedPeers:nil
52 trustedPeersError:[NSError errorWithDomain:CKKSErrorDomain
53 code:CKKSNoPeersAvailable
54 description:@"No current trusted peers available"]];
55 }
56
57
58 + (CKKSPeerProviderState*)createFromProvider:(id<CKKSPeerProvider>)provider
59 {
60 NSError* selfPeersError = nil;
61 CKKSSelves* currentSelfPeers = [provider fetchSelfPeers:&selfPeersError];
62
63 NSError* trustedPeersError = nil;
64 NSSet<id<CKKSRemotePeerProtocol>>* currentTrustedPeers = [provider fetchTrustedPeers:&trustedPeersError];
65
66 return [[CKKSPeerProviderState alloc] initWithPeerProviderID:provider.providerID
67 essential:provider.essential
68 selfPeers:currentSelfPeers
69 selfPeersError:selfPeersError
70 trustedPeers:currentTrustedPeers
71 trustedPeersError:trustedPeersError];
72 }
73 @end
74
75 #endif
76