mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			68 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			68 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Matlab
		
	
| 
											9 years ago
										 | // | ||
|  | //  Copyright (c) 2017 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
|  | 
 | ||
|  | #import "OWS104CreateRecipientIdentities.h" | ||
| 
											9 years ago
										 | #import <SignalServiceKit/OWSIdentityManager.h> | ||
| 
											9 years ago
										 | #import <SignalServiceKit/OWSRecipientIdentity.h> | ||
|  | #import <YapDatabase/YapDatabaseConnection.h> | ||
|  | #import <YapDatabase/YapDatabaseTransaction.h> | ||
|  | 
 | ||
|  | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
|  | // Increment a similar constant for every future DBMigration | ||
|  | static NSString *const OWS104CreateRecipientIdentitiesMigrationId = @"104"; | ||
|  | 
 | ||
|  | /** | ||
| 
											9 years ago
										 |  * New SN behavior requires tracking additional state - not just the identity key data. | ||
| 
											9 years ago
										 |  * So we wrap the key, along with the new meta-data in an OWSRecipientIdentity. | ||
|  |  */ | ||
|  | @implementation OWS104CreateRecipientIdentities | ||
|  | 
 | ||
|  | + (NSString *)migrationId | ||
|  | { | ||
|  |     return OWS104CreateRecipientIdentitiesMigrationId; | ||
|  | } | ||
|  | 
 | ||
|  | // Overriding runUp instead of runUpWithTransaction in order to implement a blocking migration. | ||
|  | - (void)runUp | ||
|  | { | ||
| 
											8 years ago
										 |     [[OWSRecipientIdentity dbReadWriteConnection] readWriteWithBlock:^( | ||
|  |         YapDatabaseReadWriteTransaction *_Nonnull transaction) { | ||
| 
											9 years ago
										 |         NSMutableDictionary<NSString *, NSData *> *identityKeys = [NSMutableDictionary new]; | ||
|  | 
 | ||
|  |         [transaction | ||
|  |             enumerateKeysAndObjectsInCollection:TSStorageManagerTrustedKeysCollection | ||
|  |                                      usingBlock:^( | ||
|  |                                          NSString *_Nonnull recipientId, id _Nonnull object, BOOL *_Nonnull stop) { | ||
|  |                                          if (![object isKindOfClass:[NSData class]]) { | ||
| 
											8 years ago
										 |                                              OWSFail( | ||
| 
											9 years ago
										 |                                                  @"%@ Unexpected object in trusted keys collection key: %@ object: %@", | ||
| 
											8 years ago
										 |                                                  self.logTag, | ||
| 
											9 years ago
										 |                                                  recipientId, | ||
|  |                                                  object); | ||
|  |                                              return; | ||
|  |                                          } | ||
|  |                                          NSData *identityKey = (NSData *)object; | ||
|  |                                          [identityKeys setObject:identityKey forKey:recipientId]; | ||
|  |                                      }]; | ||
|  | 
 | ||
|  |         [identityKeys enumerateKeysAndObjectsUsingBlock:^( | ||
|  |             NSString *_Nonnull recipientId, NSData *_Nonnull identityKey, BOOL *_Nonnull stop) { | ||
| 
											8 years ago
										 |             DDLogInfo(@"%@ Migrating identity key for recipient: %@", self.logTag, recipientId); | ||
| 
											9 years ago
										 |             [[[OWSRecipientIdentity alloc] initWithRecipientId:recipientId | ||
|  |                                                    identityKey:identityKey | ||
|  |                                                isFirstKnownKey:NO | ||
|  |                                                      createdAt:[NSDate dateWithTimeIntervalSince1970:0] | ||
| 
											9 years ago
										 |                                              verificationState:OWSVerificationStateDefault] | ||
|  |                 saveWithTransaction:transaction]; | ||
| 
											9 years ago
										 |         }]; | ||
| 
											9 years ago
										 | 
 | ||
|  |         [self saveWithTransaction:transaction]; | ||
| 
											9 years ago
										 |     }]; | ||
|  | } | ||
|  | 
 | ||
|  | @end | ||
|  | 
 | ||
|  | NS_ASSUME_NONNULL_END |