diff --git a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java index 2b68f57..6348bcc 100644 --- a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java @@ -116,6 +116,7 @@ public class BitmessageContext { public BitmessageAddress joinChan(String passphrase, String address) { BitmessageAddress chan = BitmessageAddress.chan(address, passphrase); + chan.setAlias(passphrase); ctx.getAddressRepository().save(chan); return chan; } diff --git a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java index d209974..c2c2f64 100644 --- a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java +++ b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java @@ -73,7 +73,7 @@ class DefaultMessageListener implements NetworkHandler.MessageListener { protected void receive(ObjectMessage object, GetPubkey getPubkey) { BitmessageAddress identity = ctx.getAddressRepository().findIdentity(getPubkey.getRipeTag()); - if (identity != null && identity.getPrivateKey() != null) { + if (identity != null && identity.getPrivateKey() != null && !identity.isChan()) { LOG.info("Got pubkey request for identity " + identity); // FIXME: only send pubkey if it wasn't sent in the last 28 days ctx.sendPubkey(identity, object.getStream()); diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/BitmessageAddress.java b/core/src/main/java/ch/dissem/bitmessage/entity/BitmessageAddress.java index ff44d0f..229da14 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/BitmessageAddress.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/BitmessageAddress.java @@ -247,4 +247,8 @@ public class BitmessageAddress implements Serializable { public boolean isChan() { return chan; } + + public void setChan(boolean chan) { + this.chan = chan; + } } diff --git a/core/src/main/java/ch/dissem/bitmessage/ports/AddressRepository.java b/core/src/main/java/ch/dissem/bitmessage/ports/AddressRepository.java index 2770997..ff397ba 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ports/AddressRepository.java +++ b/core/src/main/java/ch/dissem/bitmessage/ports/AddressRepository.java @@ -30,12 +30,17 @@ public interface AddressRepository { */ List getIdentities(); + /** + * @return all subscribed chans. + */ + List getChans(); + List getSubscriptions(); List getSubscriptions(long broadcastVersion); /** - * @return all Bitmessage addresses that have no private key. + * @return all Bitmessage addresses that have no private key or are chans. */ List getContacts(); diff --git a/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java b/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java index a6a6e1f..f480785 100644 --- a/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java +++ b/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java @@ -54,14 +54,11 @@ public class Application { .networkHandler(new DefaultNetworkHandler()) .cryptography(new BouncyCryptography()) .port(48444) - .listener(new BitmessageContext.Listener() { - @Override - public void receive(Plaintext plaintext) { - try { - System.out.println(new String(plaintext.getMessage(), "UTF-8")); - } catch (UnsupportedEncodingException e) { - LOG.error(e.getMessage(), e); - } + .listener(plaintext -> { + try { + System.out.println(new String(plaintext.getMessage(), "UTF-8")); + } catch (UnsupportedEncodingException e) { + LOG.error(e.getMessage(), e); } }) .build(); @@ -136,6 +133,7 @@ public class Application { System.out.println(); commandLine.listAddresses(identities, "identities"); System.out.println("a) create identity"); + System.out.println("c) join chan"); System.out.println(COMMAND_BACK); command = commandLine.nextCommand(); @@ -144,6 +142,10 @@ public class Application { addIdentity(); identities = ctx.addresses().getIdentities(); break; + case "c": + joinChan(); + identities = ctx.addresses().getIdentities(); + break; case "b": return; default: @@ -168,6 +170,15 @@ public class Application { ctx.addresses().save(identity); } + private void joinChan() { + System.out.println(); + System.out.print("Passphrase: "); + String passphrase = commandLine.nextLine(); + System.out.print("Address: "); + String address = commandLine.nextLineTrimmed(); + ctx.joinChan(passphrase, address); + } + private void contacts() { String command; List contacts = ctx.addresses().getContacts(); @@ -258,6 +269,12 @@ public class Application { } else { System.out.println("Public key available"); } + } else { + if (address.isChan()) { + System.out.println("Chan"); + } else { + System.out.println("Identity"); + } } } diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcAddressRepository.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcAddressRepository.java index 2cbd8c7..5422997 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcAddressRepository.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcAddressRepository.java @@ -70,6 +70,11 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito return find("private_key IS NOT NULL"); } + @Override + public List getChans() { + return find("chan = '1'"); + } + @Override public List getSubscriptions() { return find("subscribed = '1'"); @@ -86,7 +91,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito @Override public List getContacts() { - return find("private_key IS NULL"); + return find("private_key IS NULL OR chan = '1'"); } private List find(String where) { @@ -94,7 +99,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito try ( Connection connection = config.getConnection(); Statement stmt = connection.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT address, alias, public_key, private_key, subscribed " + + ResultSet rs = stmt.executeQuery("SELECT address, alias, public_key, private_key, subscribed, chan " + "FROM Address WHERE " + where) ) { while (rs.next()) { @@ -118,6 +123,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito } address.setAlias(rs.getString("alias")); address.setSubscribed(rs.getBoolean("subscribed")); + address.setChan(rs.getBoolean("chan")); result.add(address); } @@ -164,7 +170,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito if (address.getPrivateKey() != null) { statement.append(", private_key=?"); } - statement.append(", subscribed=? WHERE address=?"); + statement.append(", subscribed=?, chan=? WHERE address=?"); try ( Connection connection = config.getConnection(); PreparedStatement ps = connection.prepareStatement(statement.toString()) @@ -178,6 +184,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito writeBlob(ps, ++i, address.getPrivateKey()); } ps.setBoolean(++i, address.isSubscribed()); + ps.setBoolean(++i, address.isChan()); ps.setString(++i, address.getAddress()); ps.executeUpdate(); } @@ -187,8 +194,8 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito try ( Connection connection = config.getConnection(); PreparedStatement ps = connection.prepareStatement( - "INSERT INTO Address (address, version, alias, public_key, private_key, subscribed) " + - "VALUES (?, ?, ?, ?, ?, ?)") + "INSERT INTO Address (address, version, alias, public_key, private_key, subscribed, chan) " + + "VALUES (?, ?, ?, ?, ?, ?, ?)") ) { ps.setString(1, address.getAddress()); ps.setLong(2, address.getVersion()); @@ -196,6 +203,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito writePubkey(ps, 4, address.getPubkey()); writeBlob(ps, 5, address.getPrivateKey()); ps.setBoolean(6, address.isSubscribed()); + ps.setBoolean(7, address.isChan()); ps.executeUpdate(); } } diff --git a/repositories/src/main/resources/db/migration/V3.0__Update_table_address.sql b/repositories/src/main/resources/db/migration/V3.0__Update_table_address.sql new file mode 100644 index 0000000..01e036b --- /dev/null +++ b/repositories/src/main/resources/db/migration/V3.0__Update_table_address.sql @@ -0,0 +1 @@ +ALTER TABLE Address ADD COLUMN chan BIT NOT NULL DEFAULT '0'; diff --git a/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcAddressRepositoryTest.java b/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcAddressRepositoryTest.java index 18e70b2..bd91329 100644 --- a/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcAddressRepositoryTest.java +++ b/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcAddressRepositoryTest.java @@ -95,7 +95,7 @@ public class JdbcAddressRepositoryTest extends TestBase { addSubscription("BM-2D9QKN4teYRvoq2fyzpiftPh9WP9qggtzh"); List subscriptions; - + subscriptions = repo.getSubscriptions(5); assertEquals(1, subscriptions.size()); @@ -137,6 +137,24 @@ public class JdbcAddressRepositoryTest extends TestBase { assertNotNull(identityA.getPubkey()); assertNotNull(identityA.getPrivateKey()); assertEquals("Test", identityA.getAlias()); + assertFalse(identityA.isChan()); + } + + @Test + public void ensureNewChanIsSavedAndUpdated() { + BitmessageAddress chan = BitmessageAddress.chan(1, "test"); + repo.save(chan); + BitmessageAddress address = repo.getAddress(chan.getAddress()); + assertNotNull(address); + assertTrue(address.isChan()); + + address.setAlias("Test"); + repo.save(address); + + address = repo.getAddress(chan.getAddress()); + assertNotNull(address); + assertTrue(address.isChan()); + assertEquals("Test", address.getAlias()); } @Test