Add a method to add recovered contacts to db

This commit is contained in:
ameba23
2021-04-22 17:42:33 +02:00
parent b1c6c602a6
commit 07141b688a
2 changed files with 34 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
package org.briarproject.briar.api.socialbackup.recovery; package org.briarproject.briar.api.socialbackup.recovery;
import org.briarproject.bramble.api.FormatException; import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.briar.api.socialbackup.ReturnShardPayload; import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
import org.briarproject.briar.api.socialbackup.SocialBackup; import org.briarproject.briar.api.socialbackup.SocialBackup;
@@ -17,4 +18,6 @@ public interface RestoreAccount {
int recover() throws FormatException, GeneralSecurityException; int recover() throws FormatException, GeneralSecurityException;
SocialBackup getSocialBackup(); SocialBackup getSocialBackup();
void addContactsToDb() throws InterruptedException, DbException;
} }

View File

@@ -1,14 +1,19 @@
package org.briarproject.briar.socialbackup.recovery; package org.briarproject.briar.socialbackup.recovery;
import org.briarproject.bramble.api.FormatException; import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.contact.Contact;
import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.briar.api.socialbackup.BackupPayload; import org.briarproject.briar.api.socialbackup.BackupPayload;
import org.briarproject.briar.api.socialbackup.ContactData;
import org.briarproject.briar.api.socialbackup.DarkCrystal; import org.briarproject.briar.api.socialbackup.DarkCrystal;
import org.briarproject.briar.api.socialbackup.ReturnShardPayload; import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
import org.briarproject.briar.api.socialbackup.Shard; import org.briarproject.briar.api.socialbackup.Shard;
import org.briarproject.briar.api.socialbackup.SocialBackup;
import org.briarproject.briar.api.socialbackup.recovery.RestoreAccount; import org.briarproject.briar.api.socialbackup.recovery.RestoreAccount;
import org.briarproject.briar.socialbackup.BackupPayloadDecoder; import org.briarproject.briar.socialbackup.BackupPayloadDecoder;
import org.briarproject.briar.api.socialbackup.SocialBackup;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.util.ArrayList; import java.util.ArrayList;
@@ -18,17 +23,25 @@ import javax.inject.Inject;
public class RestoreAccountImpl implements RestoreAccount { public class RestoreAccountImpl implements RestoreAccount {
private ArrayList<ReturnShardPayload> recoveredShards = new ArrayList<>(); private ArrayList<ReturnShardPayload> recoveredShards = new ArrayList<>();
private final DarkCrystal darkCrystal; private final DarkCrystal darkCrystal;
private final DatabaseComponent db;
private final LifecycleManager lifecycleManager;
private SecretKey secretKey; private SecretKey secretKey;
private final BackupPayloadDecoder backupPayloadDecoder; private final BackupPayloadDecoder backupPayloadDecoder;
private SocialBackup socialBackup; private SocialBackup socialBackup;
@Inject @Inject
RestoreAccountImpl(DarkCrystal darkCrystal, BackupPayloadDecoder backupPayloadDecoder) { RestoreAccountImpl(DarkCrystal darkCrystal,
BackupPayloadDecoder backupPayloadDecoder, DatabaseComponent db,
LifecycleManager lifecycleManager) {
this.darkCrystal = darkCrystal; this.darkCrystal = darkCrystal;
this.backupPayloadDecoder = backupPayloadDecoder; this.backupPayloadDecoder = backupPayloadDecoder;
this.db = db;
this.lifecycleManager = lifecycleManager;
} }
public int getNumberOfShards() { return recoveredShards.size(); } public int getNumberOfShards() {
return recoveredShards.size();
}
// TODO figure out how to actually use a hash set for these objects // TODO figure out how to actually use a hash set for these objects
public boolean addReturnShardPayload(ReturnShardPayload toAdd) { public boolean addReturnShardPayload(ReturnShardPayload toAdd) {
@@ -64,7 +77,8 @@ public class RestoreAccountImpl implements RestoreAccount {
int highestVersion = -1; int highestVersion = -1;
for (ReturnShardPayload returnShardPayload : recoveredShards) { for (ReturnShardPayload returnShardPayload : recoveredShards) {
BackupPayload backupPayload = returnShardPayload.getBackupPayload(); BackupPayload backupPayload = returnShardPayload.getBackupPayload();
SocialBackup s = backupPayloadDecoder.decodeBackupPayload(secretKey, backupPayload); SocialBackup s = backupPayloadDecoder
.decodeBackupPayload(secretKey, backupPayload);
if (s.getVersion() > highestVersion) { if (s.getVersion() > highestVersion) {
socialBackup = s; socialBackup = s;
highestVersion = s.getVersion(); highestVersion = s.getVersion();
@@ -76,4 +90,17 @@ public class RestoreAccountImpl implements RestoreAccount {
public SocialBackup getSocialBackup() { public SocialBackup getSocialBackup() {
return socialBackup; return socialBackup;
} }
public void addContactsToDb() throws InterruptedException, DbException {
if (socialBackup == null) throw new DbException();
// TODO maybe waitForDatabase should be in another thread
lifecycleManager.waitForDatabase();
db.transaction(false, txn -> {
for (ContactData contactData : socialBackup.getContacts()) {
Contact c = contactData.getContact();
db.addContact(txn, c.getAuthor(), c.getLocalAuthorId(),
c.getHandshakePublicKey(), c.isVerified());
}
});
}
} }