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

View File

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

View File

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