Some fixes for the new Kotlin based version of Jabit.

This commit is contained in:
Christian Basler 2017-07-08 19:48:14 +02:00
parent 1a52af880d
commit e362cb1251
3 changed files with 48 additions and 57 deletions

View File

@ -27,11 +27,9 @@ import ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest;
import ch.dissem.bitmessage.ports.CustomCommandHandler; import ch.dissem.bitmessage.ports.CustomCommandHandler;
import ch.dissem.bitmessage.ports.ProofOfWorkEngine; import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
import ch.dissem.bitmessage.server.repository.ServerProofOfWorkRepository; import ch.dissem.bitmessage.server.repository.ServerProofOfWorkRepository;
import ch.dissem.bitmessage.utils.UnixTime;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Timer; import java.util.Timer;
@ -81,46 +79,42 @@ public class ProofOfWorkRequestHandler implements CustomCommandHandler, Internal
@Override @Override
public MessagePayload handle(CustomMessage message) { public MessagePayload handle(CustomMessage message) {
try { CryptoCustomMessage<ProofOfWorkRequest> cryptoMessage = CryptoCustomMessage.read(message,
CryptoCustomMessage<ProofOfWorkRequest> cryptoMessage = CryptoCustomMessage.read(message, ProofOfWorkRequest::read);
ProofOfWorkRequest::read); ProofOfWorkRequest request = decrypt(cryptoMessage);
ProofOfWorkRequest request = decrypt(cryptoMessage); if (request == null) {
if (request == null) { return CustomMessage.error(
return CustomMessage.error( "Unknown sender. Please ask the server's administrator to add you as a client. " +
"Unknown sender. Please ask the server's administrator to add you as a client. " + "For this he'll need your identity."
"For this he'll need your identity." );
); }
} switch (request.getRequest()) {
switch (request.getRequest()) { case CALCULATE:
case CALCULATE: if (!repo.hasTask(request.getInitialHash())) {
if (!repo.hasTask(request.getInitialHash())) { repo.storeTask(request);
repo.storeTask(request); // TODO: This is probably the place to do some book-keeping
// TODO: This is probably the place to do some book-keeping // if we want to bill our customers.
// if we want to bill our customers. engine.calculateNonce(request.getInitialHash(), request.getData(), repo::updateTask);
engine.calculateNonce(request.getInitialHash(), request.getData(), repo::updateTask); return new CryptoCustomMessage<>(
return new CryptoCustomMessage<>( new ProofOfWorkRequest(getIdentity(), request.getInitialHash(), CALCULATING, new byte[0])
new ProofOfWorkRequest(getIdentity(), request.getInitialHash(), CALCULATING, new byte[0]) );
} else {
byte[] nonce = repo.getNonce(request);
CryptoCustomMessage<ProofOfWorkRequest> response;
if (nonce != null) {
response = new CryptoCustomMessage<>(
new ProofOfWorkRequest(getIdentity(), request.getInitialHash(), COMPLETE, nonce)
); );
} else { } else {
byte[] nonce = repo.getNonce(request); response = new CryptoCustomMessage<>(
CryptoCustomMessage<ProofOfWorkRequest> response; new ProofOfWorkRequest(getIdentity(), request.getInitialHash(), CALCULATING, new byte[0])
if (nonce != null) { );
response = new CryptoCustomMessage<>(
new ProofOfWorkRequest(getIdentity(), request.getInitialHash(), COMPLETE, nonce)
);
} else {
response = new CryptoCustomMessage<>(
new ProofOfWorkRequest(getIdentity(), request.getInitialHash(), CALCULATING, new byte[0])
);
}
response.signAndEncrypt(serverIdentity, request.getSender().getPubkey().getEncryptionKey());
return response;
} }
} response.signAndEncrypt(serverIdentity, request.getSender().getPubkey().getEncryptionKey());
return null; return response;
} catch (IOException e) { }
return CustomMessage.error(e.getMessage());
} }
return null;
} }
private BitmessageAddress getIdentity() { private BitmessageAddress getIdentity() {
@ -148,8 +142,6 @@ public class ProofOfWorkRequestHandler implements CustomCommandHandler, Internal
try { try {
return cryptoMessage.decrypt(key); return cryptoMessage.decrypt(key);
} catch (DecryptionFailedException ignore) { } catch (DecryptionFailedException ignore) {
} catch (IOException e) {
throw new RuntimeException(e);
} }
} }
return null; return null;

View File

@ -25,7 +25,10 @@ import com.google.zxing.qrcode.encoder.QRCode;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.*; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Scanner; import java.util.Scanner;
import java.util.Set; import java.util.Set;
@ -91,17 +94,13 @@ public class Utils {
if (address.getAlias() != null) { if (address.getAlias() != null) {
link.append("?label=").append(address.getAlias()); link.append("?label=").append(address.getAlias());
} }
if (address.getPubkey() != null) { // This makes the QR code quite big, so it's not active. But sometimes it might be useful:
link.append(address.getAlias() == null ? '?' : '&'); // if (address.getPubkey() != null) {
ByteArrayOutputStream pubkey = new ByteArrayOutputStream(); // link.append(address.getAlias() == null ? '?' : '&');
try { // ByteArrayOutputStream pubkey = new ByteArrayOutputStream();
address.getPubkey().writeUnencrypted(pubkey); // address.getPubkey().writeUnencrypted(pubkey);
} catch (IOException e) { // link.append("pubkey=").append(Base64.getUrlEncoder().encodeToString(pubkey.toByteArray()));
throw new RuntimeException(e); // }
}
// This makes the QR code quite big, so it's not active. But sometimes it might be useful:
// link.append("pubkey=").append(Base64.getUrlEncoder().encodeToString(pubkey.toByteArray()));
}
QRCode code; QRCode code;
try { try {
code = Encoder.encode(link.toString(), ErrorCorrectionLevel.L, null); code = Encoder.encode(link.toString(), ErrorCorrectionLevel.L, null);
@ -111,8 +110,8 @@ public class Utils {
} }
ByteMatrix matrix = code.getMatrix(); ByteMatrix matrix = code.getMatrix();
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
for (int i=0; i<2; i++){ for (int i = 0; i < 2; i++) {
for (int j=0;j<matrix.getWidth()+8; j++){ for (int j = 0; j < matrix.getWidth() + 8; j++) {
result.append('█'); result.append('█');
} }
result.append('\n'); result.append('\n');
@ -136,8 +135,8 @@ public class Utils {
} }
result.append("████\n"); result.append("████\n");
} }
for (int i=0; i<2; i++){ for (int i = 0; i < 2; i++) {
for (int j=0;j<matrix.getWidth()+8; j++){ for (int j = 0; j < matrix.getWidth() + 8; j++) {
result.append('█'); result.append('█');
} }
result.append('\n'); result.append('\n');

View File

@ -118,7 +118,7 @@ public class ServerProofOfWorkRepository extends JdbcHelper {
try (Connection connection = config.getConnection()) { try (Connection connection = config.getConnection()) {
PreparedStatement ps = connection.prepareStatement( PreparedStatement ps = connection.prepareStatement(
"DELETE FROM ProofOfWorkTask WHERE timestamp < ?"); "DELETE FROM ProofOfWorkTask WHERE timestamp < ?");
ps.setLong(1, UnixTime.now(-ageInSeconds)); ps.setLong(1, UnixTime.now() - ageInSeconds);
ps.executeUpdate(); ps.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);