+ accountManager.setPreKeys(identityKeyPair.getPublicKey(), signedPreKeyRecord, oneTimePreKeys);
+ }
+
+ private SignalServiceMessageReceiver createMessageReceiver() {
+ final ClientZkProfileOperations clientZkProfileOperations = capabilities.isGv2() ? ClientZkOperations.create(
+ serviceConfiguration).getProfileOperations() : null;
+ return new SignalServiceMessageReceiver(serviceConfiguration,
+ account.getUuid(),
+ account.getUsername(),
+ account.getPassword(),
+ account.getDeviceId(),
+ account.getSignalingKey(),
+ userAgent,
+ null,
+ timer,
+ clientZkProfileOperations);
+ }
+
+ private SignalServiceMessageReceiver getOrCreateMessageReceiver() {
+ if (messageReceiver == null) {
+ messageReceiver = createMessageReceiver();
+ }
+ return messageReceiver;
+ }
+
+ private SignalServiceMessagePipe getOrCreateMessagePipe() {
+ if (messagePipe == null) {
+ messagePipe = getOrCreateMessageReceiver().createMessagePipe();
+ }
+ return messagePipe;
+ }
+
+ private SignalServiceMessagePipe getOrCreateUnidentifiedMessagePipe() {
+ if (unidentifiedMessagePipe == null) {
+ unidentifiedMessagePipe = getOrCreateMessageReceiver().createUnidentifiedMessagePipe();
+ }
+ return unidentifiedMessagePipe;
+ }
+
+ private SignalServiceMessageSender createMessageSender() {
+ final ClientZkProfileOperations clientZkProfileOperations = capabilities.isGv2() ? ClientZkOperations.create(
+ serviceConfiguration).getProfileOperations() : null;
+ final ExecutorService executor = null;
+ return new SignalServiceMessageSender(serviceConfiguration,
+ account.getUuid(),
+ account.getUsername(),
+ account.getPassword(),
+ account.getDeviceId(),
+ account.getSignalProtocolStore(),
+ userAgent,
+ account.isMultiDevice(),
+ Optional.fromNullable(messagePipe),
+ Optional.fromNullable(unidentifiedMessagePipe),
+ Optional.absent(),
+ clientZkProfileOperations,
+ executor,
+ ServiceConfig.MAX_ENVELOPE_SIZE);
+ }
+
+ private SignalServiceProfile getEncryptedRecipientProfile(SignalServiceAddress address) throws IOException {
+ return profileHelper.retrieveProfileSync(address, SignalServiceProfile.RequestType.PROFILE).getProfile();
+ }
+
+ private SignalProfile getRecipientProfile(
+ SignalServiceAddress address
+ ) {
+ SignalProfileEntry profileEntry = account.getProfileStore().getProfileEntry(address);
+ if (profileEntry == null) {
+ return null;
+ }
+ long now = new Date().getTime();
+ // Profiles are cache for 24h before retrieving them again
+ if (!profileEntry.isRequestPending() && (
+ profileEntry.getProfile() == null || now - profileEntry.getLastUpdateTimestamp() > 24 * 60 * 60 * 1000
+ )) {
+ ProfileKey profileKey = profileEntry.getProfileKey();
+ profileEntry.setRequestPending(true);
+ SignalProfile profile;
+ try {
+ profile = retrieveRecipientProfile(address, profileKey);
+ } catch (IOException e) {
+ System.err.println("Failed to retrieve profile, ignoring: " + e.getMessage());
+ profileEntry.setRequestPending(false);
+ return null;
+ }
+ profileEntry.setRequestPending(false);
+ account.getProfileStore()
+ .updateProfile(address, profileKey, now, profile, profileEntry.getProfileKeyCredential());
+ return profile;
+ }
+ return profileEntry.getProfile();
+ }
+
+ private ProfileKeyCredential getRecipientProfileKeyCredential(SignalServiceAddress address) {
+ SignalProfileEntry profileEntry = account.getProfileStore().getProfileEntry(address);
+ if (profileEntry == null) {
+ return null;
+ }
+ if (profileEntry.getProfileKeyCredential() == null) {
+ ProfileAndCredential profileAndCredential;
+ try {
+ profileAndCredential = profileHelper.retrieveProfileSync(address,
+ SignalServiceProfile.RequestType.PROFILE_AND_CREDENTIAL);
+ } catch (IOException e) {
+ System.err.println("Failed to retrieve profile key credential, ignoring: " + e.getMessage());
+ return null;
+ }
+
+ long now = new Date().getTime();
+ final ProfileKeyCredential profileKeyCredential = profileAndCredential.getProfileKeyCredential().orNull();
+ final SignalProfile profile = decryptProfile(address,
+ profileEntry.getProfileKey(),
+ profileAndCredential.getProfile());
+ account.getProfileStore()
+ .updateProfile(address, profileEntry.getProfileKey(), now, profile, profileKeyCredential);
+ return profileKeyCredential;
+ }
+ return profileEntry.getProfileKeyCredential();