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.
		
		
		
		
		
			
		
			
	
	
		
			81 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			81 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Swift
		
	
| 
											5 years ago
										 | import SessionProtocolKit | ||
| 
											5 years ago
										 | import SessionUtilitiesKit | ||
| 
											5 years ago
										 | 
 | ||
|  | @objc(SNSessionRequest) | ||
|  | public final class SessionRequest : ControlMessage { | ||
| 
											5 years ago
										 |     public var preKeyBundle: PreKeyBundle? | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 |     // MARK: Initialization | ||
| 
											5 years ago
										 |     public override init() { super.init() } | ||
|  | 
 | ||
| 
											5 years ago
										 |     internal init(preKeyBundle: PreKeyBundle) { | ||
| 
											5 years ago
										 |         super.init() | ||
|  |         self.preKeyBundle = preKeyBundle | ||
|  |     } | ||
| 
											5 years ago
										 | 
 | ||
|  |     // MARK: Validation | ||
| 
											5 years ago
										 |     public override var isValid: Bool { | ||
|  |         guard super.isValid else { return false } | ||
|  |         return preKeyBundle != nil | ||
|  |     } | ||
| 
											5 years ago
										 | 
 | ||
|  |     // MARK: Coding | ||
|  |     public required init?(coder: NSCoder) { | ||
|  |         super.init(coder: coder) | ||
|  |         if let preKeyBundle = coder.decodeObject(forKey: "preKeyBundle") as! PreKeyBundle? { self.preKeyBundle = preKeyBundle } | ||
|  |     } | ||
|  | 
 | ||
|  |     public override func encode(with coder: NSCoder) { | ||
|  |         super.encode(with: coder) | ||
|  |         coder.encode(preKeyBundle, forKey: "preKeyBundle") | ||
|  |     } | ||
|  | 
 | ||
|  |     // MARK: Proto Conversion | ||
|  |     public override class func fromProto(_ proto: SNProtoContent) -> SessionRequest? { | ||
|  |         guard proto.nullMessage != nil, let preKeyBundleProto = proto.prekeyBundleMessage else { return nil } | ||
|  |         var registrationID: UInt32 = 0 | ||
|  |         Configuration.shared.storage.with { transaction in | ||
|  |             registrationID = Configuration.shared.storage.getOrGenerateRegistrationID(using: transaction) | ||
|  |         } | ||
| 
											5 years ago
										 |         guard let preKeyBundle = PreKeyBundle( | ||
|  |             registrationId: Int32(registrationID), | ||
|  |             deviceId: 1, | ||
|  |             preKeyId: Int32(preKeyBundleProto.prekeyID), | ||
|  |             preKeyPublic: preKeyBundleProto.prekey, | ||
|  |             signedPreKeyPublic: preKeyBundleProto.signedKey, | ||
|  |             signedPreKeyId: Int32(preKeyBundleProto.signedKeyID), | ||
|  |             signedPreKeySignature: preKeyBundleProto.signature, | ||
|  |             identityKey: preKeyBundleProto.identityKey | ||
|  |         ) else { return nil } | ||
| 
											5 years ago
										 |         return SessionRequest(preKeyBundle: preKeyBundle) | ||
|  |     } | ||
|  | 
 | ||
|  |     public override func toProto() -> SNProtoContent? { | ||
|  |         guard let preKeyBundle = preKeyBundle else { | ||
|  |             SNLog("Couldn't construct session request proto from: \(self).") | ||
|  |             return nil | ||
|  |         } | ||
|  |         let nullMessageProto = SNProtoNullMessage.builder() | ||
|  |         let paddingSize = UInt.random(in: 0..<512) // random(in:) uses the system's default random generator, which is cryptographically secure | ||
|  |         let padding = Data.getSecureRandomData(ofSize: paddingSize)! | ||
|  |         nullMessageProto.setPadding(padding) | ||
|  |         let preKeyBundleProto = SNProtoPrekeyBundleMessage.builder() | ||
|  |         preKeyBundleProto.setIdentityKey(preKeyBundle.identityKey) | ||
|  |         preKeyBundleProto.setDeviceID(UInt32(preKeyBundle.deviceId)) | ||
|  |         preKeyBundleProto.setPrekeyID(UInt32(preKeyBundle.preKeyId)) | ||
|  |         preKeyBundleProto.setPrekey(preKeyBundle.preKeyPublic) | ||
|  |         preKeyBundleProto.setSignedKeyID(UInt32(preKeyBundle.signedPreKeyId)) | ||
|  |         preKeyBundleProto.setSignedKey(preKeyBundle.signedPreKeyPublic) | ||
|  |         preKeyBundleProto.setSignature(preKeyBundle.signedPreKeySignature) | ||
|  |         let contentProto = SNProtoContent.builder() | ||
|  |         do { | ||
|  |             contentProto.setNullMessage(try nullMessageProto.build()) | ||
|  |             contentProto.setPrekeyBundleMessage(try preKeyBundleProto.build()) | ||
|  |             return try contentProto.build() | ||
|  |         } catch { | ||
|  |             SNLog("Couldn't construct session request proto from: \(self).") | ||
|  |             return nil | ||
|  |         } | ||
|  |     } | ||
| 
											5 years ago
										 | } |