mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Include creation time in LocalAuthor.
This allows the oldest LocalAuthor to be used as the default.
This commit is contained in:
@@ -4,15 +4,21 @@ package org.briarproject.api;
|
|||||||
public class LocalAuthor extends Author {
|
public class LocalAuthor extends Author {
|
||||||
|
|
||||||
private final byte[] privateKey;
|
private final byte[] privateKey;
|
||||||
|
private final long created;
|
||||||
|
|
||||||
public LocalAuthor(AuthorId id, String name, byte[] publicKey,
|
public LocalAuthor(AuthorId id, String name, byte[] publicKey,
|
||||||
byte[] privateKey) {
|
byte[] privateKey, long created) {
|
||||||
super(id, name, publicKey);
|
super(id, name, publicKey);
|
||||||
this.privateKey = privateKey;
|
this.privateKey = privateKey;
|
||||||
|
this.created = created;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the private key used to generate the pseudonym's signatures. */
|
/** Returns the private key used to generate the pseudonym's signatures. */
|
||||||
public byte[] getPrivateKey() {
|
public byte[] getPrivateKey() {
|
||||||
return privateKey;
|
return privateKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getTimeCreated() {
|
||||||
|
return created;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,8 +60,8 @@ import org.briarproject.api.transport.TemporarySecret;
|
|||||||
*/
|
*/
|
||||||
abstract class JdbcDatabase implements Database<Connection> {
|
abstract class JdbcDatabase implements Database<Connection> {
|
||||||
|
|
||||||
private static final int SCHEMA_VERSION = 4;
|
private static final int SCHEMA_VERSION = 5;
|
||||||
private static final int MIN_SCHEMA_VERSION = 4;
|
private static final int MIN_SCHEMA_VERSION = 5;
|
||||||
|
|
||||||
private static final String CREATE_SETTINGS =
|
private static final String CREATE_SETTINGS =
|
||||||
"CREATE TABLE settings"
|
"CREATE TABLE settings"
|
||||||
@@ -77,6 +77,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
+ " name VARCHAR NOT NULL,"
|
+ " name VARCHAR NOT NULL,"
|
||||||
+ " publicKey BINARY NOT NULL,"
|
+ " publicKey BINARY NOT NULL,"
|
||||||
+ " privateKey BINARY NOT NULL,"
|
+ " privateKey BINARY NOT NULL,"
|
||||||
|
+ " created BIGINT NOT NULL,"
|
||||||
+ " PRIMARY KEY (authorId))";
|
+ " PRIMARY KEY (authorId))";
|
||||||
|
|
||||||
// Locking: contact
|
// Locking: contact
|
||||||
@@ -789,13 +790,14 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
try {
|
try {
|
||||||
String sql = "INSERT INTO localAuthors"
|
String sql = "INSERT INTO localAuthors"
|
||||||
+ " (authorId, name, publicKey, privateKey)"
|
+ " (authorId, name, publicKey, privateKey, created)"
|
||||||
+ " VALUES (?, ?, ?, ?)";
|
+ " VALUES (?, ?, ?, ?, ?)";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setBytes(1, a.getId().getBytes());
|
ps.setBytes(1, a.getId().getBytes());
|
||||||
ps.setString(2, a.getName());
|
ps.setString(2, a.getName());
|
||||||
ps.setBytes(3, a.getPublicKey());
|
ps.setBytes(3, a.getPublicKey());
|
||||||
ps.setBytes(4, a.getPrivateKey());
|
ps.setBytes(4, a.getPrivateKey());
|
||||||
|
ps.setLong(5, a.getTimeCreated());
|
||||||
int affected = ps.executeUpdate();
|
int affected = ps.executeUpdate();
|
||||||
if(affected != 1) throw new DbStateException();
|
if(affected != 1) throw new DbStateException();
|
||||||
ps.close();
|
ps.close();
|
||||||
@@ -1584,14 +1586,19 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
String sql = "SELECT name, publicKey, privateKey FROM localAuthors"
|
String sql = "SELECT name, publicKey, privateKey, created"
|
||||||
|
+ " FROM localAuthors"
|
||||||
+ " WHERE authorId = ?";
|
+ " WHERE authorId = ?";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setBytes(1, a.getBytes());
|
ps.setBytes(1, a.getBytes());
|
||||||
rs = ps.executeQuery();
|
rs = ps.executeQuery();
|
||||||
if(!rs.next()) throw new DbStateException();
|
if(!rs.next()) throw new DbStateException();
|
||||||
LocalAuthor localAuthor = new LocalAuthor(a, rs.getString(1),
|
String name = rs.getString(1);
|
||||||
rs.getBytes(2), rs.getBytes(3));
|
byte[] publicKey = rs.getBytes(2);
|
||||||
|
byte[] privateKey = rs.getBytes(3);
|
||||||
|
long created = rs.getLong(4);
|
||||||
|
LocalAuthor localAuthor = new LocalAuthor(a, name, publicKey,
|
||||||
|
privateKey, created);
|
||||||
if(rs.next()) throw new DbStateException();
|
if(rs.next()) throw new DbStateException();
|
||||||
rs.close();
|
rs.close();
|
||||||
ps.close();
|
ps.close();
|
||||||
@@ -1608,7 +1615,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
String sql = "SELECT authorId, name, publicKey, privateKey"
|
String sql = "SELECT authorId, name, publicKey, privateKey, created"
|
||||||
+ " FROM localAuthors";
|
+ " FROM localAuthors";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
rs = ps.executeQuery();
|
rs = ps.executeQuery();
|
||||||
@@ -1618,8 +1625,9 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
String name = rs.getString(2);
|
String name = rs.getString(2);
|
||||||
byte[] publicKey = rs.getBytes(3);
|
byte[] publicKey = rs.getBytes(3);
|
||||||
byte[] privateKey = rs.getBytes(4);
|
byte[] privateKey = rs.getBytes(4);
|
||||||
|
long created = rs.getLong(5);
|
||||||
authors.add(new LocalAuthor(authorId, name, publicKey,
|
authors.add(new LocalAuthor(authorId, name, publicKey,
|
||||||
privateKey));
|
privateKey, created));
|
||||||
}
|
}
|
||||||
rs.close();
|
rs.close();
|
||||||
ps.close();
|
ps.close();
|
||||||
|
|||||||
@@ -15,16 +15,20 @@ import org.briarproject.api.crypto.CryptoComponent;
|
|||||||
import org.briarproject.api.crypto.MessageDigest;
|
import org.briarproject.api.crypto.MessageDigest;
|
||||||
import org.briarproject.api.serial.Writer;
|
import org.briarproject.api.serial.Writer;
|
||||||
import org.briarproject.api.serial.WriterFactory;
|
import org.briarproject.api.serial.WriterFactory;
|
||||||
|
import org.briarproject.api.system.Clock;
|
||||||
|
|
||||||
class AuthorFactoryImpl implements AuthorFactory {
|
class AuthorFactoryImpl implements AuthorFactory {
|
||||||
|
|
||||||
private final CryptoComponent crypto;
|
private final CryptoComponent crypto;
|
||||||
private final WriterFactory writerFactory;
|
private final WriterFactory writerFactory;
|
||||||
|
private final Clock clock;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
AuthorFactoryImpl(CryptoComponent crypto, WriterFactory writerFactory) {
|
AuthorFactoryImpl(CryptoComponent crypto, WriterFactory writerFactory,
|
||||||
|
Clock clock) {
|
||||||
this.crypto = crypto;
|
this.crypto = crypto;
|
||||||
this.writerFactory = writerFactory;
|
this.writerFactory = writerFactory;
|
||||||
|
this.clock = clock;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Author createAuthor(String name, byte[] publicKey) {
|
public Author createAuthor(String name, byte[] publicKey) {
|
||||||
@@ -34,7 +38,7 @@ class AuthorFactoryImpl implements AuthorFactory {
|
|||||||
public LocalAuthor createLocalAuthor(String name, byte[] publicKey,
|
public LocalAuthor createLocalAuthor(String name, byte[] publicKey,
|
||||||
byte[] privateKey) {
|
byte[] privateKey) {
|
||||||
return new LocalAuthor(getId(name, publicKey), name, publicKey,
|
return new LocalAuthor(getId(name, publicKey), name, publicKey,
|
||||||
privateKey);
|
privateKey, clock.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
private AuthorId getId(String name, byte[] publicKey) {
|
private AuthorId getId(String name, byte[] publicKey) {
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
|
|||||||
author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
|
author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
|
||||||
localAuthorId = new AuthorId(TestUtils.getRandomId());
|
localAuthorId = new AuthorId(TestUtils.getRandomId());
|
||||||
localAuthor = new LocalAuthor(localAuthorId, "Bob",
|
localAuthor = new LocalAuthor(localAuthorId, "Bob",
|
||||||
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
|
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
|
||||||
messageId = new MessageId(TestUtils.getRandomId());
|
messageId = new MessageId(TestUtils.getRandomId());
|
||||||
messageId1 = new MessageId(TestUtils.getRandomId());
|
messageId1 = new MessageId(TestUtils.getRandomId());
|
||||||
contentType = "text/plain";
|
contentType = "text/plain";
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ public class H2DatabaseTest extends BriarTestCase {
|
|||||||
author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
|
author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
|
||||||
localAuthorId = new AuthorId(TestUtils.getRandomId());
|
localAuthorId = new AuthorId(TestUtils.getRandomId());
|
||||||
localAuthor = new LocalAuthor(localAuthorId, "Bob",
|
localAuthor = new LocalAuthor(localAuthorId, "Bob",
|
||||||
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
|
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
|
||||||
messageId = new MessageId(TestUtils.getRandomId());
|
messageId = new MessageId(TestUtils.getRandomId());
|
||||||
contentType = "text/plain";
|
contentType = "text/plain";
|
||||||
subject = "Foo";
|
subject = "Foo";
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
|||||||
// Add a local pseudonym for Alice
|
// Add a local pseudonym for Alice
|
||||||
AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
|
AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
|
||||||
LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice",
|
LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice",
|
||||||
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
|
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
|
||||||
db.addLocalAuthor(aliceAuthor);
|
db.addLocalAuthor(aliceAuthor);
|
||||||
// Add Bob as a contact
|
// Add Bob as a contact
|
||||||
AuthorId bobId = new AuthorId(TestUtils.getRandomId());
|
AuthorId bobId = new AuthorId(TestUtils.getRandomId());
|
||||||
@@ -173,7 +173,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
|||||||
// Add a local pseudonym for Bob
|
// Add a local pseudonym for Bob
|
||||||
AuthorId bobId = new AuthorId(TestUtils.getRandomId());
|
AuthorId bobId = new AuthorId(TestUtils.getRandomId());
|
||||||
LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob",
|
LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob",
|
||||||
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
|
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
|
||||||
db.addLocalAuthor(bobAuthor);
|
db.addLocalAuthor(bobAuthor);
|
||||||
// Add Alice as a contact
|
// Add Alice as a contact
|
||||||
AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
|
AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
|
||||||
|
|||||||
Reference in New Issue
Block a user