Add public key to pending contacts.

This commit is contained in:
akwizgran
2019-04-17 15:30:15 +01:00
parent 296ce080e2
commit 3aadcc17dd
6 changed files with 32 additions and 18 deletions

View File

@@ -77,7 +77,8 @@ public interface ContactManager {
Collection<PendingContact> getPendingContacts(); Collection<PendingContact> getPendingContacts();
/** /**
* Removes a {@link PendingContact} that is in state {@link FAILED}. * Removes a {@link PendingContact} that is in state
* {@link PendingContactState FAILED}.
*/ */
void removePendingContact(PendingContact pendingContact); void removePendingContact(PendingContact pendingContact);

View File

@@ -9,13 +9,15 @@ import javax.annotation.concurrent.Immutable;
public class PendingContact { public class PendingContact {
private final PendingContactId id; private final PendingContactId id;
private final byte[] publicKey;
private final String alias; private final String alias;
private final PendingContactState state; private final PendingContactState state;
private final long timestamp; private final long timestamp;
public PendingContact(PendingContactId id, String alias, public PendingContact(PendingContactId id, byte[] publicKey,
PendingContactState state, long timestamp) { String alias, PendingContactState state, long timestamp) {
this.id = id; this.id = id;
this.publicKey = publicKey;
this.alias = alias; this.alias = alias;
this.state = state; this.state = state;
this.timestamp = timestamp; this.timestamp = timestamp;
@@ -25,6 +27,10 @@ public class PendingContact {
return id; return id;
} }
public byte[] getPublicKey() {
return publicKey;
}
public String getAlias() { public String getAlias() {
return alias; return alias;
} }
@@ -47,5 +53,4 @@ public class PendingContact {
return o instanceof PendingContact && return o instanceof PendingContact &&
id.equals(((PendingContact) o).id); id.equals(((PendingContact) o).id);
} }
} }

View File

@@ -149,8 +149,10 @@ public class TestUtils {
public static PendingContact getPendingContact(int nameLength) { public static PendingContact getPendingContact(int nameLength) {
PendingContactId id = new PendingContactId(getRandomId()); PendingContactId id = new PendingContactId(getRandomId());
byte[] publicKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
String alias = getRandomString(nameLength); String alias = getRandomString(nameLength);
return new PendingContact(id, alias, WAITING_FOR_CONNECTION, timestamp); return new PendingContact(id, publicKey, alias, WAITING_FOR_CONNECTION,
timestamp);
} }
public static double getMedian(Collection<? extends Number> samples) { public static double getMedian(Collection<? extends Number> samples) {

View File

@@ -32,6 +32,7 @@ import javax.inject.Inject;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static org.briarproject.bramble.api.contact.PendingContactState.WAITING_FOR_CONNECTION; import static org.briarproject.bramble.api.contact.PendingContactState.WAITING_FOR_CONNECTION;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH; import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorInfo.Status.OURSELVES; import static org.briarproject.bramble.api.identity.AuthorInfo.Status.OURSELVES;
import static org.briarproject.bramble.api.identity.AuthorInfo.Status.UNKNOWN; import static org.briarproject.bramble.api.identity.AuthorInfo.Status.UNKNOWN;
import static org.briarproject.bramble.api.identity.AuthorInfo.Status.UNVERIFIED; import static org.briarproject.bramble.api.identity.AuthorInfo.Status.UNVERIFIED;
@@ -123,8 +124,8 @@ class ContactManagerImpl implements ContactManager {
public PendingContact addRemoteContactRequest(String link, String alias) { public PendingContact addRemoteContactRequest(String link, String alias) {
// TODO replace with real implementation // TODO replace with real implementation
PendingContactId id = new PendingContactId(link.getBytes()); PendingContactId id = new PendingContactId(link.getBytes());
return new PendingContact(id, alias, WAITING_FOR_CONNECTION, return new PendingContact(id, new byte[MAX_PUBLIC_KEY_LENGTH], alias,
System.currentTimeMillis()); WAITING_FOR_CONNECTION, System.currentTimeMillis());
} }
@Override @Override

View File

@@ -291,6 +291,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_PENDING_CONTACTS = private static final String CREATE_PENDING_CONTACTS =
"CREATE TABLE pendingContacts" "CREATE TABLE pendingContacts"
+ " (pendingContactId _HASH NOT NULL," + " (pendingContactId _HASH NOT NULL,"
+ " publicKey _BINARY NOT NULL,"
+ " alias _STRING NOT NULL," + " alias _STRING NOT NULL,"
+ " state INT NOT NULL," + " state INT NOT NULL,"
+ " timestamp BIGINT NOT NULL," + " timestamp BIGINT NOT NULL,"
@@ -1057,13 +1058,14 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
String sql = "INSERT INTO pendingContacts (pendingContactId," String sql = "INSERT INTO pendingContacts (pendingContactId,"
+ " alias, state, timestamp)" + " publicKey, alias, state, timestamp)"
+ " VALUES (?, ?, ?, ?)"; + " VALUES (?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, p.getId().getBytes()); ps.setBytes(1, p.getId().getBytes());
ps.setString(2, p.getAlias()); ps.setBytes(2, p.getPublicKey());
ps.setInt(3, p.getState().getValue()); ps.setString(3, p.getAlias());
ps.setLong(4, p.getTimestamp()); ps.setInt(4, p.getState().getValue());
ps.setLong(5, p.getTimestamp());
int affected = ps.executeUpdate(); int affected = ps.executeUpdate();
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
@@ -2372,19 +2374,21 @@ abstract class JdbcDatabase implements Database<Connection> {
Statement s = null; Statement s = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
String sql = "SELECT pendingContactId, alias, state, timestamp" String sql = "SELECT pendingContactId, publicKey, alias, state,"
+ " timestamp"
+ " FROM pendingContacts"; + " FROM pendingContacts";
s = txn.createStatement(); s = txn.createStatement();
rs = s.executeQuery(sql); rs = s.executeQuery(sql);
List<PendingContact> pendingContacts = new ArrayList<>(); List<PendingContact> pendingContacts = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
PendingContactId id = new PendingContactId(rs.getBytes(1)); PendingContactId id = new PendingContactId(rs.getBytes(1));
String alias = rs.getString(2); byte[] publicKey = rs.getBytes(2);
String alias = rs.getString(3);
PendingContactState state = PendingContactState state =
PendingContactState.fromValue(rs.getInt(3)); PendingContactState.fromValue(rs.getInt(4));
long timestamp = rs.getLong(4); long timestamp = rs.getLong(5);
pendingContacts.add(new PendingContact(id, alias, state, pendingContacts.add(new PendingContact(id, publicKey, alias,
timestamp)); state, timestamp));
} }
rs.close(); rs.close();
s.close(); s.close();

View File

@@ -46,6 +46,7 @@ class Migration41_42 implements Migration<Connection> {
+ " DROP COLUMN contactId"); + " DROP COLUMN contactId");
s.execute(dbTypes.replaceTypes("CREATE TABLE pendingContacts" s.execute(dbTypes.replaceTypes("CREATE TABLE pendingContacts"
+ " (pendingContactId _HASH NOT NULL," + " (pendingContactId _HASH NOT NULL,"
+ " publicKey _BINARY NOT NULL,"
+ " alias _STRING NOT NULL," + " alias _STRING NOT NULL,"
+ " state INT NOT NULL," + " state INT NOT NULL,"
+ " timestamp BIGINT NOT NULL," + " timestamp BIGINT NOT NULL,"