Implement contact manager methods for pending contacts.

This commit is contained in:
akwizgran
2019-04-17 17:00:59 +01:00
parent fc8ca872a8
commit fa562b40bc
9 changed files with 158 additions and 37 deletions

View File

@@ -59,17 +59,18 @@ public interface ContactManager {
throws DbException;
/**
* Returns the static link that needs to be sent to the contact to be added.
* Returns the handshake link that needs to be sent to a contact we want
* to add.
*/
String getHandshakeLink() throws DbException;
/**
* Requests a new contact to be added via the given {@code link}.
* Adds a new pending contact identified by the given handshake link.
*
* @param link The link received from the contact we want to add.
* @param link The handshake link received from the contact we want to add.
* @param alias The alias the user has given this contact.
*/
void addPendingContact(String link, String alias)
PendingContact addPendingContact(String link, String alias)
throws DbException, FormatException;
/**
@@ -78,10 +79,9 @@ public interface ContactManager {
Collection<PendingContact> getPendingContacts() throws DbException;
/**
* Removes a {@link PendingContact} that is in state
* {@link PendingContactState FAILED}.
* Removes a {@link PendingContact}.
*/
void removePendingContact(PendingContactId pendingContact) throws DbException;
void removePendingContact(PendingContactId p) throws DbException;
/**
* Returns the contact with the given ID.

View File

@@ -0,0 +1,34 @@
package org.briarproject.bramble.api.contact;
import java.util.regex.Pattern;
public interface HandshakeLinkConstants {
/**
* The current version of the handshake link format.
*/
int FORMAT_VERSION = 0;
/**
* The length of a base32-encoded handshake link in bytes, excluding the
* 'briar://' prefix.
*/
int BASE32_LINK_BYTES = 53;
/**
* The length of a raw handshake link in bytes, before base32 encoding.
*/
int RAW_LINK_BYTES = 33;
/**
* Regular expression for matching handshake links, including or excluding
* the 'briar://' prefix.
*/
Pattern LINK_REGEX =
Pattern.compile("(briar://)?([a-z2-7]{" + BASE32_LINK_BYTES + "})");
/**
* Label for hashing handshake public keys to calculate their identifiers.
*/
String ID_LABEL = "org.briarproject.bramble/HANDSHAKE_KEY_ID";
}

View File

@@ -37,7 +37,7 @@ public class Base32 {
return s.toString();
}
public static byte[] decode(String s) {
public static byte[] decode(String s, boolean strict) {
ByteArrayOutputStream b = new ByteArrayOutputStream();
int digitIndex = 0, digitCount = s.length(), currentByte = 0x00;
int byteMask = 0x80, codeMask = 0x10;
@@ -61,7 +61,7 @@ public class Base32 {
}
}
// If any extra bits were used for encoding, they should all be zero
if (byteMask != 0x80 && currentByte != 0x00)
if (strict && byteMask != 0x80 && currentByte != 0x00)
throw new IllegalArgumentException();
return b.toByteArray();
}

View File

@@ -153,4 +153,13 @@ public class StringUtils {
return new String(c);
}
public static String getRandomBase32String(int length) {
char[] c = new char[length];
for (int i = 0; i < length; i++) {
int character = random.nextInt(32);
if (character < 26) c[i] = (char) ('a' + character);
else c[i] = (char) ('2' + (character - 26));
}
return new String(c);
}
}