Extract PreKeyHelper from Manager

This commit is contained in:
AsamK 2021-09-12 11:59:20 +02:00
parent 74e576c907
commit 2196ac6975
2 changed files with 68 additions and 35 deletions

View file

@ -40,6 +40,7 @@ import org.asamk.signal.manager.helper.GroupHelper;
import org.asamk.signal.manager.helper.GroupV2Helper; import org.asamk.signal.manager.helper.GroupV2Helper;
import org.asamk.signal.manager.helper.IncomingMessageHandler; import org.asamk.signal.manager.helper.IncomingMessageHandler;
import org.asamk.signal.manager.helper.PinHelper; import org.asamk.signal.manager.helper.PinHelper;
import org.asamk.signal.manager.helper.PreKeyHelper;
import org.asamk.signal.manager.helper.ProfileHelper; import org.asamk.signal.manager.helper.ProfileHelper;
import org.asamk.signal.manager.helper.SendHelper; import org.asamk.signal.manager.helper.SendHelper;
import org.asamk.signal.manager.helper.StorageHelper; import org.asamk.signal.manager.helper.StorageHelper;
@ -62,14 +63,11 @@ import org.asamk.signal.manager.util.Utils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.libsignal.IdentityKey; import org.whispersystems.libsignal.IdentityKey;
import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.libsignal.InvalidKeyException; import org.whispersystems.libsignal.InvalidKeyException;
import org.whispersystems.libsignal.ecc.ECPublicKey; import org.whispersystems.libsignal.ecc.ECPublicKey;
import org.whispersystems.libsignal.fingerprint.Fingerprint; import org.whispersystems.libsignal.fingerprint.Fingerprint;
import org.whispersystems.libsignal.fingerprint.FingerprintParsingException; import org.whispersystems.libsignal.fingerprint.FingerprintParsingException;
import org.whispersystems.libsignal.fingerprint.FingerprintVersionMismatchException; import org.whispersystems.libsignal.fingerprint.FingerprintVersionMismatchException;
import org.whispersystems.libsignal.state.PreKeyRecord;
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
import org.whispersystems.libsignal.util.Pair; import org.whispersystems.libsignal.util.Pair;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.SignalSessionLock; import org.whispersystems.signalservice.api.SignalSessionLock;
@ -141,6 +139,7 @@ public class Manager implements Closeable {
private final GroupHelper groupHelper; private final GroupHelper groupHelper;
private final ContactHelper contactHelper; private final ContactHelper contactHelper;
private final IncomingMessageHandler incomingMessageHandler; private final IncomingMessageHandler incomingMessageHandler;
private final PreKeyHelper preKeyHelper;
private final Context context; private final Context context;
private boolean hasCaughtUpWithOldMessages = false; private boolean hasCaughtUpWithOldMessages = false;
@ -219,6 +218,7 @@ public class Manager implements Closeable {
groupHelper, groupHelper,
avatarStore, avatarStore,
this::resolveSignalServiceAddress); this::resolveSignalServiceAddress);
preKeyHelper = new PreKeyHelper(account, dependencies);
this.context = new Context(account, this.context = new Context(account,
dependencies, dependencies,
@ -249,10 +249,6 @@ public class Manager implements Closeable {
return account.getSelfRecipientId(); return account.getSelfRecipientId();
} }
private IdentityKeyPair getIdentityKeyPair() {
return account.getIdentityKeyPair();
}
public int getDeviceId() { public int getDeviceId() {
return account.getDeviceId(); return account.getDeviceId();
} }
@ -309,9 +305,7 @@ public class Manager implements Closeable {
days); days);
} }
} }
if (dependencies.getAccountManager().getPreKeysCount() < ServiceConfig.PREKEY_MINIMUM_COUNT) { preKeyHelper.refreshPreKeysIfNecessary();
refreshPreKeys();
}
if (account.getUuid() == null) { if (account.getUuid() == null) {
account.setUuid(dependencies.getAccountManager().getOwnUuid()); account.setUuid(dependencies.getAccountManager().getOwnUuid());
} }
@ -439,7 +433,7 @@ public class Manager implements Closeable {
} }
private void addDevice(String deviceIdentifier, ECPublicKey deviceKey) throws IOException, InvalidKeyException { private void addDevice(String deviceIdentifier, ECPublicKey deviceKey) throws IOException, InvalidKeyException {
var identityKeyPair = getIdentityKeyPair(); var identityKeyPair = account.getIdentityKeyPair();
var verificationCode = dependencies.getAccountManager().getNewDeviceVerificationCode(); var verificationCode = dependencies.getAccountManager().getNewDeviceVerificationCode();
dependencies.getAccountManager() dependencies.getAccountManager()
@ -472,29 +466,7 @@ public class Manager implements Closeable {
} }
void refreshPreKeys() throws IOException { void refreshPreKeys() throws IOException {
var oneTimePreKeys = generatePreKeys(); preKeyHelper.refreshPreKeys();
final var identityKeyPair = getIdentityKeyPair();
var signedPreKeyRecord = generateSignedPreKey(identityKeyPair);
dependencies.getAccountManager().setPreKeys(identityKeyPair.getPublicKey(), signedPreKeyRecord, oneTimePreKeys);
}
private List<PreKeyRecord> generatePreKeys() {
final var offset = account.getPreKeyIdOffset();
var records = KeyUtils.generatePreKeyRecords(offset, ServiceConfig.PREKEY_BATCH_SIZE);
account.addPreKeys(records);
return records;
}
private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) {
final var signedPreKeyId = account.getNextSignedPreKeyId();
var record = KeyUtils.generateSignedPreKeyRecord(identityKeyPair, signedPreKeyId);
account.addSignedPreKey(record);
return record;
} }
public Profile getRecipientProfile(RecipientId recipientId) { public Profile getRecipientProfile(RecipientId recipientId) {
@ -1175,7 +1147,7 @@ public class Manager implements Closeable {
) { ) {
return Utils.computeSafetyNumber(capabilities.isUuid(), return Utils.computeSafetyNumber(capabilities.isUuid(),
account.getSelfAddress(), account.getSelfAddress(),
getIdentityKeyPair().getPublicKey(), account.getIdentityKeyPair().getPublicKey(),
theirAddress, theirAddress,
theirIdentityKey); theirIdentityKey);
} }

View file

@ -0,0 +1,61 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.util.KeyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.libsignal.state.PreKeyRecord;
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
import java.io.IOException;
import java.util.List;
public class PreKeyHelper {
private final static Logger logger = LoggerFactory.getLogger(PreKeyHelper.class);
private final SignalAccount account;
private final SignalDependencies dependencies;
public PreKeyHelper(
final SignalAccount account, final SignalDependencies dependencies
) {
this.account = account;
this.dependencies = dependencies;
}
public void refreshPreKeysIfNecessary() throws IOException {
if (dependencies.getAccountManager().getPreKeysCount() < ServiceConfig.PREKEY_MINIMUM_COUNT) {
refreshPreKeys();
}
}
public void refreshPreKeys() throws IOException {
var oneTimePreKeys = generatePreKeys();
final var identityKeyPair = account.getIdentityKeyPair();
var signedPreKeyRecord = generateSignedPreKey(identityKeyPair);
dependencies.getAccountManager().setPreKeys(identityKeyPair.getPublicKey(), signedPreKeyRecord, oneTimePreKeys);
}
private List<PreKeyRecord> generatePreKeys() {
final var offset = account.getPreKeyIdOffset();
var records = KeyUtils.generatePreKeyRecords(offset, ServiceConfig.PREKEY_BATCH_SIZE);
account.addPreKeys(records);
return records;
}
private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) {
final var signedPreKeyId = account.getNextSignedPreKeyId();
var record = KeyUtils.generateSignedPreKeyRecord(identityKeyPair, signedPreKeyId);
account.addSignedPreKey(record);
return record;
}
}