2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #import "TPPeerDynamicInfo.h"
27 static const NSString *kCircleID = @"circleID";
28 static const NSString *kClique = @"clique";
29 static const NSString *kRemovals = @"removals";
30 static const NSString *kClock = @"clock";
33 @interface TPPeerDynamicInfo ()
35 @property (nonatomic, strong) NSString *circleID;
36 @property (nonatomic, strong) NSString *clique;
37 @property (nonatomic, assign) TPCounter removals;
38 @property (nonatomic, assign) TPCounter clock;
39 @property (nonatomic, strong) NSData *dynamicInfoPList;
40 @property (nonatomic, strong) NSData *dynamicInfoSig;
45 @implementation TPPeerDynamicInfo
47 + (instancetype)dynamicInfoWithCircleID:(NSString *)circleID
48 clique:(NSString *)clique
49 removals:(TPCounter)removals
50 clock:(TPCounter)clock
51 trustSigningKey:(id<TPSigningKey>)trustSigningKey
52 error:(NSError **)error
54 NSDictionary *dict = @{
57 kRemovals: @(removals),
60 NSData *data = [TPUtils serializedPListWithDictionary:dict];
61 NSData *sig = [trustSigningKey signatureForData:data withError:error];
65 TPPeerDynamicInfo* info = [self dynamicInfoWithPListData:data dynamicInfoSig:sig];
70 + (instancetype)dynamicInfoWithPListData:(NSData *)dynamicInfoPList
71 dynamicInfoSig:(NSData *)dynamicInfoSig
73 id dict = [NSPropertyListSerialization propertyListWithData:dynamicInfoPList
74 options:NSPropertyListImmutable
77 if (![dict isKindOfClass:[NSDictionary class]]) {
81 TPPeerDynamicInfo* info = [[TPPeerDynamicInfo alloc] init];
83 if (![dict[kCircleID] isKindOfClass:[NSString class]]) {
86 info.circleID = dict[kCircleID];
88 if (![dict[kClique] isKindOfClass:[NSString class]]) {
91 info.clique = dict[kClique];
93 if (![dict[kRemovals] isKindOfClass:[NSNumber class]]) {
96 info.removals = [dict[kRemovals] unsignedLongLongValue];
98 if (![dict[kClock] isKindOfClass:[NSNumber class]]) {
101 info.clock = [dict[kClock] unsignedLongLongValue];
103 info.dynamicInfoPList = [dynamicInfoPList copy];
104 info.dynamicInfoSig = [dynamicInfoSig copy];
109 - (BOOL)isEqualToPeerDynamicInfo:(TPPeerDynamicInfo *)other
114 return [self.dynamicInfoPList isEqualToData:other.dynamicInfoPList]
115 && [self.dynamicInfoSig isEqualToData:other.dynamicInfoSig];
118 #pragma mark - NSObject
120 - (BOOL)isEqual:(id)object
122 if (self == object) {
125 if (![object isKindOfClass:[TPPeerDynamicInfo class]]) {
128 return [self isEqualToPeerDynamicInfo:object];