Merge branch 'mkirk/cache-local-number'

pull/1/head
Michael Kirk 8 years ago
commit aa5c441aed

@ -9,7 +9,6 @@
#import "Signal-Swift.h" #import "Signal-Swift.h"
#import "TSAttachmentStream.h" #import "TSAttachmentStream.h"
#import "TSMessagesManager.h" #import "TSMessagesManager.h"
#import "TSStorageManager+keyingMaterial.h"
#import "UIColor+JSQMessages.h" #import "UIColor+JSQMessages.h"
#import "UIColor+OWS.h" #import "UIColor+OWS.h"
#import "UIDevice+TSHardwareVersion.h" #import "UIDevice+TSHardwareVersion.h"

@ -86,7 +86,6 @@
#import <SignalServiceKit/TSSocketManager.h> #import <SignalServiceKit/TSSocketManager.h>
#import <SignalServiceKit/TSStorageManager+Calling.h> #import <SignalServiceKit/TSStorageManager+Calling.h>
#import <SignalServiceKit/TSStorageManager+SessionStore.h> #import <SignalServiceKit/TSStorageManager+SessionStore.h>
#import <SignalServiceKit/TSStorageManager+keyingMaterial.h>
#import <SignalServiceKit/TSThread.h> #import <SignalServiceKit/TSThread.h>
#import <WebRTC/RTCAudioSession.h> #import <WebRTC/RTCAudioSession.h>
#import <WebRTC/RTCCameraPreviewView.h> #import <WebRTC/RTCCameraPreviewView.h>

@ -13,7 +13,6 @@
#import <SignalServiceKit/OWSError.h> #import <SignalServiceKit/OWSError.h>
#import <SignalServiceKit/TSAccountManager.h> #import <SignalServiceKit/TSAccountManager.h>
#import <SignalServiceKit/TSNetworkManager.h> #import <SignalServiceKit/TSNetworkManager.h>
#import <SignalServiceKit/TSStorageManager+keyingMaterial.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN

@ -17,9 +17,9 @@
#import <SignalServiceKit/OWSFingerprint.h> #import <SignalServiceKit/OWSFingerprint.h>
#import <SignalServiceKit/OWSFingerprintBuilder.h> #import <SignalServiceKit/OWSFingerprintBuilder.h>
#import <SignalServiceKit/OWSIdentityManager.h> #import <SignalServiceKit/OWSIdentityManager.h>
#import <SignalServiceKit/TSAccountManager.h>
#import <SignalServiceKit/TSInfoMessage.h> #import <SignalServiceKit/TSInfoMessage.h>
#import <SignalServiceKit/TSStorageManager+SessionStore.h> #import <SignalServiceKit/TSStorageManager+SessionStore.h>
#import <SignalServiceKit/TSStorageManager+keyingMaterial.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -74,7 +74,7 @@ typedef void (^CustomLayoutBlock)();
@property (nonatomic) NSString *recipientId; @property (nonatomic) NSString *recipientId;
@property (nonatomic) NSData *identityKey; @property (nonatomic) NSData *identityKey;
@property (nonatomic) TSStorageManager *storageManager; @property (nonatomic) TSAccountManager *accountManager;
@property (nonatomic) OWSFingerprint *fingerprint; @property (nonatomic) OWSFingerprint *fingerprint;
@property (nonatomic) NSString *contactName; @property (nonatomic) NSString *contactName;
@ -118,6 +118,8 @@ typedef void (^CustomLayoutBlock)();
return self; return self;
} }
_accountManager = [TSAccountManager sharedInstance];
[self observeNotifications]; [self observeNotifications];
return self; return self;
@ -142,8 +144,6 @@ typedef void (^CustomLayoutBlock)();
self.recipientId = recipientId; self.recipientId = recipientId;
self.storageManager = [TSStorageManager sharedManager];
OWSContactsManager *contactsManager = [Environment getCurrent].contactsManager; OWSContactsManager *contactsManager = [Environment getCurrent].contactsManager;
self.contactName = [contactsManager displayNameForPhoneIdentifier:recipientId]; self.contactName = [contactsManager displayNameForPhoneIdentifier:recipientId];
@ -155,7 +155,7 @@ typedef void (^CustomLayoutBlock)();
self.identityKey = recipientIdentity.identityKey; self.identityKey = recipientIdentity.identityKey;
OWSFingerprintBuilder *builder = OWSFingerprintBuilder *builder =
[[OWSFingerprintBuilder alloc] initWithStorageManager:self.storageManager contactsManager:contactsManager]; [[OWSFingerprintBuilder alloc] initWithAccountManager:self.accountManager contactsManager:contactsManager];
self.fingerprint = self.fingerprint =
[builder fingerprintWithTheirSignalId:recipientId theirIdentityKey:recipientIdentity.identityKey]; [builder fingerprintWithTheirSignalId:recipientId theirIdentityKey:recipientIdentity.identityKey];
} }

@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface FingerprintViewScanController () <OWSQRScannerDelegate> @interface FingerprintViewScanController () <OWSQRScannerDelegate>
@property (nonatomic) TSStorageManager *storageManager; @property (nonatomic) TSAccountManager *accountManager;
@property (nonatomic) NSString *recipientId; @property (nonatomic) NSString *recipientId;
@property (nonatomic) NSData *identityKey; @property (nonatomic) NSData *identityKey;
@property (nonatomic) OWSFingerprint *fingerprint; @property (nonatomic) OWSFingerprint *fingerprint;
@ -39,8 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssert(recipientId.length > 0); OWSAssert(recipientId.length > 0);
self.recipientId = recipientId; self.recipientId = recipientId;
self.accountManager = [TSAccountManager sharedInstance];
self.storageManager = [TSStorageManager sharedManager];
OWSContactsManager *contactsManager = [Environment getCurrent].contactsManager; OWSContactsManager *contactsManager = [Environment getCurrent].contactsManager;
self.contactName = [contactsManager displayNameForPhoneIdentifier:recipientId]; self.contactName = [contactsManager displayNameForPhoneIdentifier:recipientId];
@ -53,7 +52,7 @@ NS_ASSUME_NONNULL_BEGIN
self.identityKey = recipientIdentity.identityKey; self.identityKey = recipientIdentity.identityKey;
OWSFingerprintBuilder *builder = OWSFingerprintBuilder *builder =
[[OWSFingerprintBuilder alloc] initWithStorageManager:self.storageManager contactsManager:contactsManager]; [[OWSFingerprintBuilder alloc] initWithAccountManager:self.accountManager contactsManager:contactsManager];
self.fingerprint = self.fingerprint =
[builder fingerprintWithTheirSignalId:recipientId theirIdentityKey:recipientIdentity.identityKey]; [builder fingerprintWithTheirSignalId:recipientId theirIdentityKey:recipientIdentity.identityKey];
} }

@ -41,7 +41,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) NSArray<NSNumber *> *disappearingMessagesDurations; @property (nonatomic) NSArray<NSNumber *> *disappearingMessagesDurations;
@property (nonatomic) OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration; @property (nonatomic) OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration;
@property (nonatomic, readonly) TSStorageManager *storageManager; @property (nonatomic, readonly) TSAccountManager *accountManager;
@property (nonatomic, readonly) OWSContactsManager *contactsManager; @property (nonatomic, readonly) OWSContactsManager *contactsManager;
@property (nonatomic, readonly) OWSMessageSender *messageSender; @property (nonatomic, readonly) OWSMessageSender *messageSender;
@property (nonatomic, readonly) OWSBlockingManager *blockingManager; @property (nonatomic, readonly) OWSBlockingManager *blockingManager;
@ -93,7 +93,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)commonInit - (void)commonInit
{ {
_storageManager = [TSStorageManager sharedManager]; _accountManager = [TSAccountManager sharedInstance];
_contactsManager = [Environment getCurrent].contactsManager; _contactsManager = [Environment getCurrent].contactsManager;
_messageSender = [Environment getCurrent].messageSender; _messageSender = [Environment getCurrent].messageSender;
_blockingManager = [OWSBlockingManager sharedManager]; _blockingManager = [OWSBlockingManager sharedManager];
@ -925,7 +925,7 @@ NS_ASSUME_NONNULL_BEGIN
}]; }];
NSMutableArray *newGroupMemberIds = [NSMutableArray arrayWithArray:gThread.groupModel.groupMemberIds]; NSMutableArray *newGroupMemberIds = [NSMutableArray arrayWithArray:gThread.groupModel.groupMemberIds];
[newGroupMemberIds removeObject:[self.storageManager localNumber]]; [newGroupMemberIds removeObject:[self.accountManager localNumber]];
gThread.groupModel.groupMemberIds = newGroupMemberIds; gThread.groupModel.groupMemberIds = newGroupMemberIds;
[gThread save]; [gThread save];

@ -8,7 +8,7 @@
#import <SignalServiceKit/ECKeyPair+OWSPrivateKey.h> #import <SignalServiceKit/ECKeyPair+OWSPrivateKey.h>
#import <SignalServiceKit/OWSDeviceProvisioner.h> #import <SignalServiceKit/OWSDeviceProvisioner.h>
#import <SignalServiceKit/OWSIdentityManager.h> #import <SignalServiceKit/OWSIdentityManager.h>
#import <SignalServiceKit/TSStorageManager+keyingMaterial.h> #import <SignalServiceKit/TSAccountManager.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -131,7 +131,7 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssert(identityKeyPair); OWSAssert(identityKeyPair);
NSData *myPublicKey = identityKeyPair.publicKey; NSData *myPublicKey = identityKeyPair.publicKey;
NSData *myPrivateKey = identityKeyPair.ows_privateKey; NSData *myPrivateKey = identityKeyPair.ows_privateKey;
NSString *accountIdentifier = [TSStorageManager localNumber]; NSString *accountIdentifier = [TSAccountManager localNumber];
OWSDeviceProvisioner *provisioner = [[OWSDeviceProvisioner alloc] initWithMyPublicKey:myPublicKey OWSDeviceProvisioner *provisioner = [[OWSDeviceProvisioner alloc] initWithMyPublicKey:myPublicKey
myPrivateKey:myPrivateKey myPrivateKey:myPrivateKey

@ -113,7 +113,7 @@ EXTERNAL SOURCES:
AxolotlKit: AxolotlKit:
:git: https://github.com/WhisperSystems/SignalProtocolKit.git :git: https://github.com/WhisperSystems/SignalProtocolKit.git
SignalServiceKit: SignalServiceKit:
:path: "../../../SignalServiceKit.podspec" :path: ../../../SignalServiceKit.podspec
SocketRocket: SocketRocket:
:git: https://github.com/facebook/SocketRocket.git :git: https://github.com/facebook/SocketRocket.git

@ -22,7 +22,7 @@ extern NSString *const kNSNotificationName_LocalNumberDidChange;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithNetworkManager:(TSNetworkManager *)networkManager - (instancetype)initWithNetworkManager:(TSNetworkManager *)networkManager
storageManager:(TSStorageManager *)storageManager; storageManager:(TSStorageManager *)storageManager NS_DESIGNATED_INITIALIZER;
+ (instancetype)sharedInstance; + (instancetype)sharedInstance;
@ -43,6 +43,7 @@ extern NSString *const kNSNotificationName_LocalNumberDidChange;
* @return E164 formatted phone number * @return E164 formatted phone number
*/ */
+ (nullable NSString *)localNumber; + (nullable NSString *)localNumber;
- (nullable NSString *)localNumber;
/** /**
* The registration ID is unique to an installation of TextSecure, it allows to know if the app was reinstalled * The registration ID is unique to an installation of TextSecure, it allows to know if the app was reinstalled

@ -20,10 +20,15 @@ NSString *const TSRegistrationErrorUserInfoHTTPStatus = @"TSHTTPStatus";
NSString *const kNSNotificationName_RegistrationStateDidChange = @"kNSNotificationName_RegistrationStateDidChange"; NSString *const kNSNotificationName_RegistrationStateDidChange = @"kNSNotificationName_RegistrationStateDidChange";
NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName_LocalNumberDidChange"; NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName_LocalNumberDidChange";
NSString *const TSAccountManager_RegisteredNumberKey = @"TSStorageRegisteredNumberKey";
NSString *const TSAccountManager_LocalRegistrationIdKey = @"TSStorageLocalRegistrationId";
@interface TSAccountManager () @interface TSAccountManager ()
@property (nonatomic, readonly) BOOL isRegistered;
@property (nonatomic, nullable) NSString *phoneNumberAwaitingVerification; @property (nonatomic, nullable) NSString *phoneNumberAwaitingVerification;
@property (nonatomic, readonly) TSStorageManager *storageManager; @property (nonatomic, nullable) NSString *cachedLocalNumber;
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
@end @end
@ -31,6 +36,8 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName
@implementation TSAccountManager @implementation TSAccountManager
@synthesize isRegistered = _isRegistered;
- (instancetype)initWithNetworkManager:(TSNetworkManager *)networkManager - (instancetype)initWithNetworkManager:(TSNetworkManager *)networkManager
storageManager:(TSStorageManager *)storageManager storageManager:(TSStorageManager *)storageManager
{ {
@ -40,7 +47,7 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName
} }
_networkManager = networkManager; _networkManager = networkManager;
_storageManager = storageManager; _dbConnection = [storageManager newDatabaseConnection];
OWSSingletonAssert(); OWSSingletonAssert();
@ -68,13 +75,42 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName
userInfo:nil]; userInfo:nil];
} }
+ (BOOL)isRegistered { + (BOOL)isRegistered
return [TSStorageManager localNumber] ? YES : NO; {
return [[self sharedInstance] isRegistered];
} }
- (void)ifRegistered:(BOOL)isRegistered runAsync:(void (^)())block - (BOOL)isRegistered
{ {
[self.storageManager ifLocalNumberPresent:isRegistered runAsync:block]; if (_isRegistered) {
return YES;
} else {
@synchronized (self) {
// Cache this once it's true since it's called alot, involves a dbLookup, and once set - it doesn't change.
_isRegistered = [self storedLocalNumber] != nil;
}
}
return _isRegistered;
}
- (void)ifRegistered:(BOOL)runIfRegistered runAsync:(void (^)())block
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if ([self isRegistered] == runIfRegistered) {
if (runIfRegistered) {
DDLogDebug(@"%@ Running existing-user block", self.tag);
} else {
DDLogDebug(@"%@ Running new-user block", self.tag);
}
block();
} else {
if (runIfRegistered) {
DDLogDebug(@"%@ Skipping existing-user block for new-user", self.tag);
} else {
DDLogDebug(@"%@ Skipping new-user block for existing-user", self.tag);
}
}
});
} }
- (void)didRegister - (void)didRegister
@ -86,7 +122,7 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName
@throw [NSException exceptionWithName:@"RegistrationFail" reason:@"Internal Corrupted State" userInfo:nil]; @throw [NSException exceptionWithName:@"RegistrationFail" reason:@"Internal Corrupted State" userInfo:nil];
} }
[self.storageManager storePhoneNumber:phoneNumber]; [self storeLocalNumber:phoneNumber];
[[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_RegistrationStateDidChange [[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_RegistrationStateDidChange
object:nil object:nil
@ -95,32 +131,61 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName
+ (nullable NSString *)localNumber + (nullable NSString *)localNumber
{ {
TSAccountManager *sharedManager = [self sharedInstance]; return [[self sharedInstance] localNumber];
NSString *awaitingVerif = sharedManager.phoneNumberAwaitingVerification; }
- (nullable NSString *)localNumber
{
NSString *awaitingVerif = self.phoneNumberAwaitingVerification;
if (awaitingVerif) { if (awaitingVerif) {
return awaitingVerif; return awaitingVerif;
} }
return [TSStorageManager localNumber]; // Cache this since we access this a lot, and once set it will not change.
@synchronized(self)
{
if (self.cachedLocalNumber == nil) {
self.cachedLocalNumber = self.storedLocalNumber;
}
}
return self.cachedLocalNumber;
}
- (nullable NSString *)storedLocalNumber
{
@synchronized (self) {
return [self.dbConnection stringForKey:TSAccountManager_RegisteredNumberKey
inCollection:TSStorageUserAccountCollection];
}
}
- (void)storeLocalNumber:(NSString *)localNumber
{
@synchronized (self) {
[self.dbConnection setObject:localNumber
forKey:TSAccountManager_RegisteredNumberKey
inCollection:TSStorageUserAccountCollection];
}
} }
+ (uint32_t)getOrGenerateRegistrationId { + (uint32_t)getOrGenerateRegistrationId
YapDatabaseConnection *dbConn = [[TSStorageManager sharedManager] newDatabaseConnection]; {
__block uint32_t registrationID = 0; return [[self sharedInstance] getOrGenerateRegistrationId];
}
[dbConn readWithBlock:^(YapDatabaseReadTransaction *transaction) { - (uint32_t)getOrGenerateRegistrationId
registrationID = [[transaction objectForKey:TSStorageLocalRegistrationId {
uint32_t registrationID = [[self.dbConnection objectForKey:TSAccountManager_LocalRegistrationIdKey
inCollection:TSStorageUserAccountCollection] unsignedIntValue]; inCollection:TSStorageUserAccountCollection] unsignedIntValue];
}];
if (registrationID == 0) { if (registrationID == 0) {
registrationID = (uint32_t)arc4random_uniform(16380) + 1; registrationID = (uint32_t)arc4random_uniform(16380) + 1;
DDLogWarn(@"%@ Generated a new registrationID: %u", self.tag, registrationID);
[dbConn readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbConnection setObject:[NSNumber numberWithUnsignedInteger:registrationID]
[transaction setObject:[NSNumber numberWithUnsignedInteger:registrationID] forKey:TSAccountManager_LocalRegistrationIdKey
forKey:TSStorageLocalRegistrationId
inCollection:TSStorageUserAccountCollection]; inCollection:TSStorageUserAccountCollection];
}];
} }
return registrationID; return registrationID;

@ -3,9 +3,8 @@
// //
#import "TSAttributes.h" #import "TSAttributes.h"
#import "TSAccountManager.h" #import "TSAccountManager.h"
#import "TSStorageHeaders.h" #import "TSStorageManager+keyingMaterial.h"
@implementation TSAttributes @implementation TSAttributes

@ -4,7 +4,8 @@
#import "SignalRecipient.h" #import "SignalRecipient.h"
#import "OWSIdentityManager.h" #import "OWSIdentityManager.h"
#import "TSStorageHeaders.h" #import "TSAccountManager.h"
#import <YapDatabase/YapDatabaseConnection.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -43,12 +44,12 @@ NS_ASSUME_NONNULL_BEGIN
return recipient; return recipient;
} }
// TODO This method should probably live on the TSAccountManager rather than grabbing a global singleton.
+ (instancetype)selfRecipient + (instancetype)selfRecipient
{ {
SignalRecipient *myself = [self recipientWithTextSecureIdentifier:[TSStorageManager localNumber]]; SignalRecipient *myself = [self recipientWithTextSecureIdentifier:[TSAccountManager localNumber]];
if (!myself) { if (!myself) {
myself = [[self alloc] initWithTextSecureIdentifier:[TSStorageManager localNumber] myself = [[self alloc] initWithTextSecureIdentifier:[TSAccountManager localNumber] relay:nil];
relay:nil];
} }
return myself; return myself;
} }

@ -15,7 +15,6 @@
#import "TSContactThread.h" #import "TSContactThread.h"
#import "TSErrorMessage.h" #import "TSErrorMessage.h"
#import "TSGroupThread.h" #import "TSGroupThread.h"
#import "TSStorageManager+keyingMaterial.h"
#import "TSStorageManager+sessionStore.h" #import "TSStorageManager+sessionStore.h"
#import "TSStorageManager.h" #import "TSStorageManager.h"
#import "TextSecureKitEnv.h" #import "TextSecureKitEnv.h"
@ -326,7 +325,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
@synchronized(self) @synchronized(self)
{ {
if ([[self.storageManager localNumber] isEqualToString:recipientId]) { if ([[TSAccountManager localNumber] isEqualToString:recipientId]) {
if ([[self identityKeyPair].publicKey isEqualToData:identityKey]) { if ([[self identityKeyPair].publicKey isEqualToData:identityKey]) {
return YES; return YES;
} else { } else {

@ -30,7 +30,6 @@
#import "TSPreKeyManager.h" #import "TSPreKeyManager.h"
#import "TSStorageManager+PreKeyStore.h" #import "TSStorageManager+PreKeyStore.h"
#import "TSStorageManager+SignedPreKeyStore.h" #import "TSStorageManager+SignedPreKeyStore.h"
#import "TSStorageManager+keyingMaterial.h"
#import "TSStorageManager+sessionStore.h" #import "TSStorageManager+sessionStore.h"
#import "TSStorageManager.h" #import "TSStorageManager.h"
#import "TSThread.h" #import "TSThread.h"
@ -623,7 +622,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
|| [message isKindOfClass:[OWSOutgoingSyncMessage class]]) { || [message isKindOfClass:[OWSOutgoingSyncMessage class]]) {
TSContactThread *contactThread = (TSContactThread *)thread; TSContactThread *contactThread = (TSContactThread *)thread;
if ([contactThread.contactIdentifier isEqualToString:self.storageManager.localNumber] if ([contactThread.contactIdentifier isEqualToString:[TSAccountManager localNumber]]
&& ![message isKindOfClass:[OWSOutgoingSyncMessage class]]) { && ![message isKindOfClass:[OWSOutgoingSyncMessage class]]) {
[self handleSendToMyself:message]; [self handleSendToMyself:message];
@ -632,7 +631,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
NSString *recipientContactId = [message isKindOfClass:[OWSOutgoingSyncMessage class]] NSString *recipientContactId = [message isKindOfClass:[OWSOutgoingSyncMessage class]]
? self.storageManager.localNumber ? [TSAccountManager localNumber]
: contactThread.contactIdentifier; : contactThread.contactIdentifier;
// If we block a user, don't send 1:1 messages to them. The UI // If we block a user, don't send 1:1 messages to them. The UI
@ -734,7 +733,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
NSString *recipientId = recipient.recipientId; NSString *recipientId = recipient.recipientId;
// We don't need to send the message to ourselves... // We don't need to send the message to ourselves...
if ([recipientId isEqualToString:[TSStorageManager localNumber]]) { if ([recipientId isEqualToString:[TSAccountManager localNumber]]) {
continue; continue;
} }
// We don't need to sent the message to all group members if // We don't need to sent the message to all group members if

@ -476,9 +476,8 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
} }
- (NSString *)webSocketAuthenticationString { - (NSString *)webSocketAuthenticationString {
return [NSString return [NSString stringWithFormat:@"?login=%@&password=%@",
stringWithFormat:@"?login=%@&password=%@", [[TSAccountManager localNumber] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"],
[[TSStorageManager localNumber] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"],
[TSStorageManager serverAuthToken]]; [TSStorageManager serverAuthToken]];
} }

@ -4,14 +4,14 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@class TSStorageManager; @class TSAccountManager;
@class OWSFingerprint; @class OWSFingerprint;
@protocol ContactsManagerProtocol; @protocol ContactsManagerProtocol;
@interface OWSFingerprintBuilder : NSObject @interface OWSFingerprintBuilder : NSObject
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithStorageManager:(TSStorageManager *)storageManager - (instancetype)initWithAccountManager:(TSAccountManager *)accountManager
contactsManager:(id<ContactsManagerProtocol>)contactsManager NS_DESIGNATED_INITIALIZER; contactsManager:(id<ContactsManagerProtocol>)contactsManager NS_DESIGNATED_INITIALIZER;
/** /**

@ -6,21 +6,21 @@
#import "ContactsManagerProtocol.h" #import "ContactsManagerProtocol.h"
#import "OWSFingerprint.h" #import "OWSFingerprint.h"
#import "OWSIdentityManager.h" #import "OWSIdentityManager.h"
#import "TSStorageManager+keyingMaterial.h" #import "TSAccountManager.h"
#import <25519/Curve25519.h> #import <25519/Curve25519.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@interface OWSFingerprintBuilder () @interface OWSFingerprintBuilder ()
@property (nonatomic, readonly) TSStorageManager *storageManager; @property (nonatomic, readonly) TSAccountManager *accountManager;
@property (nonatomic, readonly) id<ContactsManagerProtocol> contactsManager; @property (nonatomic, readonly) id<ContactsManagerProtocol> contactsManager;
@end @end
@implementation OWSFingerprintBuilder @implementation OWSFingerprintBuilder
- (instancetype)initWithStorageManager:(TSStorageManager *)storageManager - (instancetype)initWithAccountManager:(TSAccountManager *)accountManager
contactsManager:(id<ContactsManagerProtocol>)contactsManager contactsManager:(id<ContactsManagerProtocol>)contactsManager
{ {
self = [super init]; self = [super init];
@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN
return self; return self;
} }
_storageManager = storageManager; _accountManager = accountManager;
_contactsManager = contactsManager; _contactsManager = contactsManager;
return self; return self;
@ -50,7 +50,7 @@ NS_ASSUME_NONNULL_BEGIN
{ {
NSString *theirName = [self.contactsManager displayNameForPhoneIdentifier:theirSignalId]; NSString *theirName = [self.contactsManager displayNameForPhoneIdentifier:theirSignalId];
NSString *mySignalId = [self.storageManager localNumber]; NSString *mySignalId = [self.accountManager localNumber];
NSData *myIdentityKey = [[OWSIdentityManager sharedManager] identityKeyPair].publicKey; NSData *myIdentityKey = [[OWSIdentityManager sharedManager] identityKeyPair].publicKey;
return [OWSFingerprint fingerprintWithMyStableId:mySignalId return [OWSFingerprint fingerprintWithMyStableId:mySignalId

@ -12,7 +12,6 @@
#import "TSStorageManager+SessionStore.h" #import "TSStorageManager+SessionStore.h"
#import "TSStorageManager+SignedPreKeyStore.h" #import "TSStorageManager+SignedPreKeyStore.h"
#import "TSStorageManager+keyFromIntLong.h" #import "TSStorageManager+keyFromIntLong.h"
#import "TSStorageManager+keyingMaterial.h"
#import "TSStorageManager+messageIDs.h" #import "TSStorageManager+messageIDs.h"
#endif #endif

@ -1,9 +1,5 @@
// //
// TSStorageKeys.h // Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// TextSecureKit
//
// Created by Frederic Jacobs on 28/10/14.
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
// //
#ifndef TextSecureKit_TSStorageKeys_h #ifndef TextSecureKit_TSStorageKeys_h
@ -13,10 +9,8 @@
#define TSStorageUserAccountCollection @"TSStorageUserAccountCollection" #define TSStorageUserAccountCollection @"TSStorageUserAccountCollection"
#define TSStorageRegisteredNumberKey @"TSStorageRegisteredNumberKey"
#define TSStorageServerAuthToken @"TSStorageServerAuthToken" #define TSStorageServerAuthToken @"TSStorageServerAuthToken"
#define TSStorageServerSignalingKey @"TSStorageServerSignalingKey" #define TSStorageServerSignalingKey @"TSStorageServerSignalingKey"
#define TSStorageLocalRegistrationId @"TSStorageLocalRegistrationId"
/** /**
* Preferences exposed to the user * Preferences exposed to the user

@ -1,9 +1,5 @@
// //
// TSStorageManager+keyingMaterial.h // Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// TextSecureKit
//
// Created by Frederic Jacobs on 06/11/14.
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
// //
#import "TSStorageManager.h" #import "TSStorageManager.h"
@ -28,18 +24,6 @@
+ (NSString *)serverAuthToken; + (NSString *)serverAuthToken;
/**
* Registered phone number
*
* @return E164 string of the registered phone number
*/
- (NSString *)localNumber;
+ (NSString *)localNumber;
- (void)ifLocalNumberPresent:(BOOL)isPresent runAsync:(void (^)())block;
+ (void)storeServerToken:(NSString *)authToken signalingKey:(NSString *)signalingKey; + (void)storeServerToken:(NSString *)authToken signalingKey:(NSString *)signalingKey;
- (void)storePhoneNumber:(NSString *)phoneNumber;
@end @end

@ -4,45 +4,9 @@
#import "TSStorageManager+keyingMaterial.h" #import "TSStorageManager+keyingMaterial.h"
// TODO merge this category extension's functionality into TSAccountManager
@implementation TSStorageManager (keyingMaterial) @implementation TSStorageManager (keyingMaterial)
+ (NSString *)localNumber
{
return [[self sharedManager] localNumber];
}
- (NSString *)localNumber
{
// TODO cache this? It only changes once, ever, and otherwise causes "surprising" transactions to occur.
return [self stringForKey:TSStorageRegisteredNumberKey inCollection:TSStorageUserAccountCollection];
}
- (void)ifLocalNumberPresent:(BOOL)runIfPresent runAsync:(void (^)())block;
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__block BOOL isPresent;
[self.newDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
isPresent = [[transaction objectForKey:TSStorageRegisteredNumberKey
inCollection:TSStorageUserAccountCollection] boolValue];
}];
if (isPresent == runIfPresent) {
if (runIfPresent) {
DDLogDebug(@"%@ Running existing-user block", self.logTag);
} else {
DDLogDebug(@"%@ Running new-user block", self.logTag);
}
block();
} else {
if (runIfPresent) {
DDLogDebug(@"%@ Skipping existing-user block for new-user", self.logTag);
} else {
DDLogDebug(@"%@ Skipping new-user block for existing-user", self.logTag);
}
}
});
}
+ (NSString *)signalingKey { + (NSString *)signalingKey {
return [[self sharedManager] stringForKey:TSStorageServerSignalingKey inCollection:TSStorageUserAccountCollection]; return [[self sharedManager] stringForKey:TSStorageServerSignalingKey inCollection:TSStorageUserAccountCollection];
} }
@ -51,15 +15,6 @@
return [[self sharedManager] stringForKey:TSStorageServerAuthToken inCollection:TSStorageUserAccountCollection]; return [[self sharedManager] stringForKey:TSStorageServerAuthToken inCollection:TSStorageUserAccountCollection];
} }
- (void)storePhoneNumber:(NSString *)phoneNumber
{
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction setObject:phoneNumber
forKey:TSStorageRegisteredNumberKey
inCollection:TSStorageUserAccountCollection];
}];
}
+ (void)storeServerToken:(NSString *)authToken signalingKey:(NSString *)signalingKey { + (void)storeServerToken:(NSString *)authToken signalingKey:(NSString *)signalingKey {
TSStorageManager *sharedManager = self.sharedManager; TSStorageManager *sharedManager = self.sharedManager;
[sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {

@ -3,10 +3,16 @@
// //
#import "SignalRecipient.h" #import "SignalRecipient.h"
#import "TSAccountManager.h"
#import "TSStorageManager+keyingMaterial.h" #import "TSStorageManager+keyingMaterial.h"
#import "TSStorageManager.h"
#import <XCTest/XCTest.h> #import <XCTest/XCTest.h>
@interface TSAccountManager (Testing)
- (void)storeLocalNumber:(NSString *)localNumber;
@end
@interface SignalRecipientTest : XCTestCase @interface SignalRecipientTest : XCTestCase
@property (nonatomic) NSString *localNumber; @property (nonatomic) NSString *localNumber;
@ -19,7 +25,7 @@
{ {
[super setUp]; [super setUp];
self.localNumber = @"+13231231234"; self.localNumber = @"+13231231234";
[[TSStorageManager sharedManager] storePhoneNumber:self.localNumber]; [[TSAccountManager sharedInstance] storeLocalNumber:self.localNumber];
} }
- (void)testSelfRecipientWithExistingRecord - (void)testSelfRecipientWithExistingRecord

@ -10,13 +10,13 @@
#import "OWSFakeNetworkManager.h" #import "OWSFakeNetworkManager.h"
#import "OWSMessageSender.h" #import "OWSMessageSender.h"
#import "OWSUploadingService.h" #import "OWSUploadingService.h"
#import "TSAccountManager.h"
#import "TSContactThread.h" #import "TSContactThread.h"
#import "TSGroupModel.h" #import "TSGroupModel.h"
#import "TSGroupThread.h" #import "TSGroupThread.h"
#import "TSMessagesManager.h" #import "TSMessagesManager.h"
#import "TSNetworkManager.h" #import "TSNetworkManager.h"
#import "TSOutgoingMessage.h" #import "TSOutgoingMessage.h"
#import "TSStorageManager+keyingMaterial.h"
#import "TSStorageManager.h" #import "TSStorageManager.h"
#import <AxolotlKit/AxolotlExceptions.h> #import <AxolotlKit/AxolotlExceptions.h>
#import <AxolotlKit/SessionBuilder.h> #import <AxolotlKit/SessionBuilder.h>
@ -174,6 +174,12 @@ NS_ASSUME_NONNULL_BEGIN
@end @end
@interface TSAccountManager (Testing)
- (void)storeLocalNumber:(NSString *)localNumber;
@end
@interface OWSMessageSenderTest : XCTestCase @interface OWSMessageSenderTest : XCTestCase
@property (nonatomic) TSThread *thread; @property (nonatomic) TSThread *thread;
@ -192,7 +198,7 @@ NS_ASSUME_NONNULL_BEGIN
[super setUp]; [super setUp];
// Hack to make sure we don't explode when sending sync message. // Hack to make sure we don't explode when sending sync message.
[[TSStorageManager sharedManager] storePhoneNumber:@"+13231231234"]; [[TSAccountManager sharedInstance] storeLocalNumber:@"+13231231234"];
self.thread = [[TSContactThread alloc] initWithUniqueId:@"fake-thread-id"]; self.thread = [[TSContactThread alloc] initWithUniqueId:@"fake-thread-id"];
[self.thread save]; [self.thread save];

Loading…
Cancel
Save