Commit 64aa121c authored by akwizgran's avatar akwizgran

Reuse UnsupportedVersionException for handshake links.

parent cc3486df
package org.briarproject.bramble.api.contact; package org.briarproject.bramble.api.contact;
import org.briarproject.bramble.api.FormatException; import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.UnsupportedVersionException;
import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.NoSuchContactException;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.identity.Author; import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorId; import org.briarproject.bramble.api.identity.AuthorId;
...@@ -65,10 +67,15 @@ public interface ContactManager { ...@@ -65,10 +67,15 @@ public interface ContactManager {
String getHandshakeLink() throws DbException; String getHandshakeLink() throws DbException;
/** /**
* Adds a new pending contact identified by the given handshake link. * Creates a {@link PendingContact} from the given handshake link and
* alias, adds it to the database and returns it.
* *
* @param link The handshake 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. * @param alias The alias the user has given this contact
* @return A PendingContact representing the contact to be added
* @throws UnsupportedVersionException If the link uses a format version
* that is not supported
* @throws FormatException If the link is invalid
*/ */
PendingContact addPendingContact(String link, String alias) PendingContact addPendingContact(String link, String alias)
throws DbException, FormatException; throws DbException, FormatException;
...@@ -92,7 +99,7 @@ public interface ContactManager { ...@@ -92,7 +99,7 @@ public interface ContactManager {
* Returns the contact with the given remoteAuthorId * Returns the contact with the given remoteAuthorId
* that was added by the LocalAuthor with the given localAuthorId * that was added by the LocalAuthor with the given localAuthorId
* *
* @throws org.briarproject.bramble.api.db.NoSuchContactException * @throws NoSuchContactException If the contact is not in the database
*/ */
Contact getContact(AuthorId remoteAuthorId, AuthorId localAuthorId) Contact getContact(AuthorId remoteAuthorId, AuthorId localAuthorId)
throws DbException; throws DbException;
...@@ -101,7 +108,7 @@ public interface ContactManager { ...@@ -101,7 +108,7 @@ public interface ContactManager {
* Returns the contact with the given remoteAuthorId * Returns the contact with the given remoteAuthorId
* that was added by the LocalAuthor with the given localAuthorId * that was added by the LocalAuthor with the given localAuthorId
* *
* @throws org.briarproject.bramble.api.db.NoSuchContactException * @throws NoSuchContactException If the contact is not in the database
*/ */
Contact getContact(Transaction txn, AuthorId remoteAuthorId, Contact getContact(Transaction txn, AuthorId remoteAuthorId,
AuthorId localAuthorId) throws DbException; AuthorId localAuthorId) throws DbException;
......
package org.briarproject.bramble.contact; package org.briarproject.bramble.contact;
import org.briarproject.bramble.api.FormatException; import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.UnsupportedVersionException;
import org.briarproject.bramble.api.contact.PendingContact; import org.briarproject.bramble.api.contact.PendingContact;
interface PendingContactFactory { interface PendingContactFactory {
/**
* Creates a {@link PendingContact} from the given handshake link and alias.
*
* @throws UnsupportedVersionException If the link uses a format version
* that is not supported
* @throws FormatException If the link is invalid
*/
PendingContact createPendingContact(String link, String alias) PendingContact createPendingContact(String link, String alias)
throws FormatException; throws FormatException;
} }
package org.briarproject.bramble.contact; package org.briarproject.bramble.contact;
import org.briarproject.bramble.api.FormatException; import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.UnsupportedVersionException;
import org.briarproject.bramble.api.contact.PendingContact; import org.briarproject.bramble.api.contact.PendingContact;
import org.briarproject.bramble.api.contact.PendingContactId; import org.briarproject.bramble.api.contact.PendingContactId;
import org.briarproject.bramble.api.contact.PendingContactState; import org.briarproject.bramble.api.contact.PendingContactState;
...@@ -49,7 +50,9 @@ class PendingContactFactoryImpl implements PendingContactFactory { ...@@ -49,7 +50,9 @@ class PendingContactFactoryImpl implements PendingContactFactory {
if (link.startsWith("briar://")) link = link.substring(8); if (link.startsWith("briar://")) link = link.substring(8);
byte[] base32 = Base32.decode(link, false); byte[] base32 = Base32.decode(link, false);
if (base32.length != RAW_LINK_BYTES) throw new AssertionError(); if (base32.length != RAW_LINK_BYTES) throw new AssertionError();
if (base32[0] != FORMAT_VERSION) throw new FormatException(); byte version = base32[0];
if (version != FORMAT_VERSION)
throw new UnsupportedVersionException(version < FORMAT_VERSION);
byte[] publicKeyBytes = new byte[base32.length - 1]; byte[] publicKeyBytes = new byte[base32.length - 1];
arraycopy(base32, 1, publicKeyBytes, 0, publicKeyBytes.length); arraycopy(base32, 1, publicKeyBytes, 0, publicKeyBytes.length);
try { try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment