2 #import "keychain/ckks/CKKS.h"
3 #import "keychain/ckks/CKKSPeerProvider.h"
4 #import "keychain/categories/NSError+UsefulConstructors.h"
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
14 if((self = [super init])) {
15 _peerProviderID = providerID;
16 _essential = essential;
17 _currentSelfPeers = selfPeers;
18 _currentSelfPeersError = selfPeersError;
19 _currentTrustedPeers = currentTrustedPeers;
20 _currentTrustedPeersError = trustedPeersError;
22 if(_currentTrustedPeers) {
23 NSMutableSet<NSString*>* trustedPeerIDs = [NSMutableSet set];
24 for(id<CKKSPeer> peer in _currentTrustedPeers) {
25 [trustedPeerIDs addObject:peer.peerID];
27 _currentTrustedPeerIDs = trustedPeerIDs;
33 - (NSString*)description
35 return [NSString stringWithFormat:@"<CKKSPeerProviderState(%@): %@%@ %@%@>",
37 self.currentSelfPeers,
38 self.currentSelfPeersError ?: @"",
39 self.currentTrustedPeers,
40 self.currentTrustedPeersError ?: @""];
43 + (CKKSPeerProviderState*)noPeersState:(id<CKKSPeerProvider>)provider
45 return [[CKKSPeerProviderState alloc] initWithPeerProviderID:provider.providerID
46 essential:provider.essential
48 selfPeersError:[NSError errorWithDomain:CKKSErrorDomain
49 code:CKKSNoPeersAvailable
50 description:@"No current self peer available"]
52 trustedPeersError:[NSError errorWithDomain:CKKSErrorDomain
53 code:CKKSNoPeersAvailable
54 description:@"No current trusted peers available"]];
58 + (CKKSPeerProviderState*)createFromProvider:(id<CKKSPeerProvider>)provider
60 NSError* selfPeersError = nil;
61 CKKSSelves* currentSelfPeers = [provider fetchSelfPeers:&selfPeersError];
63 NSError* trustedPeersError = nil;
64 NSSet<id<CKKSRemotePeerProtocol>>* currentTrustedPeers = [provider fetchTrustedPeers:&trustedPeersError];
66 return [[CKKSPeerProviderState alloc] initWithPeerProviderID:provider.providerID
67 essential:provider.essential
68 selfPeers:currentSelfPeers
69 selfPeersError:selfPeersError
70 trustedPeers:currentTrustedPeers
71 trustedPeersError:trustedPeersError];