Chans should now work.

Other deterministic addresses should be easy to implement, but aren't done yet
This commit is contained in:
Christian Basler 2016-04-07 17:24:56 +02:00
parent ead5341b2e
commit 32ea3517fe
8 changed files with 70 additions and 16 deletions

View File

@ -116,6 +116,7 @@ public class BitmessageContext {
public BitmessageAddress joinChan(String passphrase, String address) { public BitmessageAddress joinChan(String passphrase, String address) {
BitmessageAddress chan = BitmessageAddress.chan(address, passphrase); BitmessageAddress chan = BitmessageAddress.chan(address, passphrase);
chan.setAlias(passphrase);
ctx.getAddressRepository().save(chan); ctx.getAddressRepository().save(chan);
return chan; return chan;
} }

View File

@ -73,7 +73,7 @@ class DefaultMessageListener implements NetworkHandler.MessageListener {
protected void receive(ObjectMessage object, GetPubkey getPubkey) { protected void receive(ObjectMessage object, GetPubkey getPubkey) {
BitmessageAddress identity = ctx.getAddressRepository().findIdentity(getPubkey.getRipeTag()); 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); LOG.info("Got pubkey request for identity " + identity);
// FIXME: only send pubkey if it wasn't sent in the last 28 days // FIXME: only send pubkey if it wasn't sent in the last 28 days
ctx.sendPubkey(identity, object.getStream()); ctx.sendPubkey(identity, object.getStream());

View File

@ -247,4 +247,8 @@ public class BitmessageAddress implements Serializable {
public boolean isChan() { public boolean isChan() {
return chan; return chan;
} }
public void setChan(boolean chan) {
this.chan = chan;
}
} }

View File

@ -30,12 +30,17 @@ public interface AddressRepository {
*/ */
List<BitmessageAddress> getIdentities(); List<BitmessageAddress> getIdentities();
/**
* @return all subscribed chans.
*/
List<BitmessageAddress> getChans();
List<BitmessageAddress> getSubscriptions(); List<BitmessageAddress> getSubscriptions();
List<BitmessageAddress> getSubscriptions(long broadcastVersion); List<BitmessageAddress> 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<BitmessageAddress> getContacts(); List<BitmessageAddress> getContacts();

View File

@ -54,14 +54,11 @@ public class Application {
.networkHandler(new DefaultNetworkHandler()) .networkHandler(new DefaultNetworkHandler())
.cryptography(new BouncyCryptography()) .cryptography(new BouncyCryptography())
.port(48444) .port(48444)
.listener(new BitmessageContext.Listener() { .listener(plaintext -> {
@Override try {
public void receive(Plaintext plaintext) { System.out.println(new String(plaintext.getMessage(), "UTF-8"));
try { } catch (UnsupportedEncodingException e) {
System.out.println(new String(plaintext.getMessage(), "UTF-8")); LOG.error(e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
LOG.error(e.getMessage(), e);
}
} }
}) })
.build(); .build();
@ -136,6 +133,7 @@ public class Application {
System.out.println(); System.out.println();
commandLine.listAddresses(identities, "identities"); commandLine.listAddresses(identities, "identities");
System.out.println("a) create identity"); System.out.println("a) create identity");
System.out.println("c) join chan");
System.out.println(COMMAND_BACK); System.out.println(COMMAND_BACK);
command = commandLine.nextCommand(); command = commandLine.nextCommand();
@ -144,6 +142,10 @@ public class Application {
addIdentity(); addIdentity();
identities = ctx.addresses().getIdentities(); identities = ctx.addresses().getIdentities();
break; break;
case "c":
joinChan();
identities = ctx.addresses().getIdentities();
break;
case "b": case "b":
return; return;
default: default:
@ -168,6 +170,15 @@ public class Application {
ctx.addresses().save(identity); 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() { private void contacts() {
String command; String command;
List<BitmessageAddress> contacts = ctx.addresses().getContacts(); List<BitmessageAddress> contacts = ctx.addresses().getContacts();
@ -258,6 +269,12 @@ public class Application {
} else { } else {
System.out.println("Public key available"); System.out.println("Public key available");
} }
} else {
if (address.isChan()) {
System.out.println("Chan");
} else {
System.out.println("Identity");
}
} }
} }

View File

@ -70,6 +70,11 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
return find("private_key IS NOT NULL"); return find("private_key IS NOT NULL");
} }
@Override
public List<BitmessageAddress> getChans() {
return find("chan = '1'");
}
@Override @Override
public List<BitmessageAddress> getSubscriptions() { public List<BitmessageAddress> getSubscriptions() {
return find("subscribed = '1'"); return find("subscribed = '1'");
@ -86,7 +91,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
@Override @Override
public List<BitmessageAddress> getContacts() { public List<BitmessageAddress> getContacts() {
return find("private_key IS NULL"); return find("private_key IS NULL OR chan = '1'");
} }
private List<BitmessageAddress> find(String where) { private List<BitmessageAddress> find(String where) {
@ -94,7 +99,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
try ( try (
Connection connection = config.getConnection(); Connection connection = config.getConnection();
Statement stmt = connection.createStatement(); 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) "FROM Address WHERE " + where)
) { ) {
while (rs.next()) { while (rs.next()) {
@ -118,6 +123,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
} }
address.setAlias(rs.getString("alias")); address.setAlias(rs.getString("alias"));
address.setSubscribed(rs.getBoolean("subscribed")); address.setSubscribed(rs.getBoolean("subscribed"));
address.setChan(rs.getBoolean("chan"));
result.add(address); result.add(address);
} }
@ -164,7 +170,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
if (address.getPrivateKey() != null) { if (address.getPrivateKey() != null) {
statement.append(", private_key=?"); statement.append(", private_key=?");
} }
statement.append(", subscribed=? WHERE address=?"); statement.append(", subscribed=?, chan=? WHERE address=?");
try ( try (
Connection connection = config.getConnection(); Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement(statement.toString()) PreparedStatement ps = connection.prepareStatement(statement.toString())
@ -178,6 +184,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
writeBlob(ps, ++i, address.getPrivateKey()); writeBlob(ps, ++i, address.getPrivateKey());
} }
ps.setBoolean(++i, address.isSubscribed()); ps.setBoolean(++i, address.isSubscribed());
ps.setBoolean(++i, address.isChan());
ps.setString(++i, address.getAddress()); ps.setString(++i, address.getAddress());
ps.executeUpdate(); ps.executeUpdate();
} }
@ -187,8 +194,8 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
try ( try (
Connection connection = config.getConnection(); Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement( PreparedStatement ps = connection.prepareStatement(
"INSERT INTO Address (address, version, alias, public_key, private_key, subscribed) " + "INSERT INTO Address (address, version, alias, public_key, private_key, subscribed, chan) " +
"VALUES (?, ?, ?, ?, ?, ?)") "VALUES (?, ?, ?, ?, ?, ?, ?)")
) { ) {
ps.setString(1, address.getAddress()); ps.setString(1, address.getAddress());
ps.setLong(2, address.getVersion()); ps.setLong(2, address.getVersion());
@ -196,6 +203,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
writePubkey(ps, 4, address.getPubkey()); writePubkey(ps, 4, address.getPubkey());
writeBlob(ps, 5, address.getPrivateKey()); writeBlob(ps, 5, address.getPrivateKey());
ps.setBoolean(6, address.isSubscribed()); ps.setBoolean(6, address.isSubscribed());
ps.setBoolean(7, address.isChan());
ps.executeUpdate(); ps.executeUpdate();
} }
} }

View File

@ -0,0 +1 @@
ALTER TABLE Address ADD COLUMN chan BIT NOT NULL DEFAULT '0';

View File

@ -95,7 +95,7 @@ public class JdbcAddressRepositoryTest extends TestBase {
addSubscription("BM-2D9QKN4teYRvoq2fyzpiftPh9WP9qggtzh"); addSubscription("BM-2D9QKN4teYRvoq2fyzpiftPh9WP9qggtzh");
List<BitmessageAddress> subscriptions; List<BitmessageAddress> subscriptions;
subscriptions = repo.getSubscriptions(5); subscriptions = repo.getSubscriptions(5);
assertEquals(1, subscriptions.size()); assertEquals(1, subscriptions.size());
@ -137,6 +137,24 @@ public class JdbcAddressRepositoryTest extends TestBase {
assertNotNull(identityA.getPubkey()); assertNotNull(identityA.getPubkey());
assertNotNull(identityA.getPrivateKey()); assertNotNull(identityA.getPrivateKey());
assertEquals("Test", identityA.getAlias()); 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 @Test