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) {
BitmessageAddress chan = BitmessageAddress.chan(address, passphrase);
chan.setAlias(passphrase);
ctx.getAddressRepository().save(chan);
return chan;
}

View File

@ -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());

View File

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

View File

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

View File

@ -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<BitmessageAddress> 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");
}
}
}

View File

@ -70,6 +70,11 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
return find("private_key IS NOT NULL");
}
@Override
public List<BitmessageAddress> getChans() {
return find("chan = '1'");
}
@Override
public List<BitmessageAddress> getSubscriptions() {
return find("subscribed = '1'");
@ -86,7 +91,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
@Override
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) {
@ -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();
}
}

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");
List<BitmessageAddress> 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