From e362cb12511b4ccb04aa82b8d746c98839666dae Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sat, 8 Jul 2017 19:48:14 +0200 Subject: [PATCH] Some fixes for the new Kotlin based version of Jabit. --- .../server/ProofOfWorkRequestHandler.java | 72 +++++++++---------- .../ch/dissem/bitmessage/server/Utils.java | 31 ++++---- .../ServerProofOfWorkRepository.java | 2 +- 3 files changed, 48 insertions(+), 57 deletions(-) diff --git a/src/main/java/ch/dissem/bitmessage/server/ProofOfWorkRequestHandler.java b/src/main/java/ch/dissem/bitmessage/server/ProofOfWorkRequestHandler.java index c953e3b..79ff004 100644 --- a/src/main/java/ch/dissem/bitmessage/server/ProofOfWorkRequestHandler.java +++ b/src/main/java/ch/dissem/bitmessage/server/ProofOfWorkRequestHandler.java @@ -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 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 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 response; + if (nonce != null) { + response = new CryptoCustomMessage<>( + new ProofOfWorkRequest(getIdentity(), request.getInitialHash(), COMPLETE, nonce) ); } else { - byte[] nonce = repo.getNonce(request); - CryptoCustomMessage 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; diff --git a/src/main/java/ch/dissem/bitmessage/server/Utils.java b/src/main/java/ch/dissem/bitmessage/server/Utils.java index fedc24f..8b9e0cd 100644 --- a/src/main/java/ch/dissem/bitmessage/server/Utils.java +++ b/src/main/java/ch/dissem/bitmessage/server/Utils.java @@ -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