+ private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) throws IOException {
+ ContactInfo contact = account.getContactStore().getContact(recipient.getNumber().get());
+ if (contact == null || contact.profileKey == null) {
+ return null;
+ }
+ ProfileKey theirProfileKey;
+ try {
+ theirProfileKey = new ProfileKey(Base64.decode(contact.profileKey));
+ } catch (InvalidInputException e) {
+ throw new AssertionError(e);
+ }
+ SignalProfile targetProfile = decryptProfile(getRecipientProfile(recipient, Optional.absent()), theirProfileKey);
+
+ if (targetProfile == null || targetProfile.getUnidentifiedAccess() == null) {
+ return null;
+ }
+
+ if (targetProfile.isUnrestrictedUnidentifiedAccess()) {
+ return KeyUtils.createUnrestrictedUnidentifiedAccess();
+ }
+
+ return UnidentifiedAccess.deriveAccessKeyFrom(theirProfileKey);
+ }
+
+ private Optional<UnidentifiedAccessPair> getAccessForSync() throws IOException {
+ byte[] selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey();
+ byte[] selfUnidentifiedAccessCertificate = getSenderCertificate();
+
+ if (selfUnidentifiedAccessKey == null || selfUnidentifiedAccessCertificate == null) {
+ return Optional.absent();
+ }
+
+ try {
+ return Optional.of(new UnidentifiedAccessPair(
+ new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate),
+ new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate)
+ ));
+ } catch (InvalidCertificateException e) {
+ return Optional.absent();
+ }