Some changes needed for POW server and some general improvements
This commit is contained in:
		| @@ -54,8 +54,8 @@ public class CryptoCustomMessage<T extends Streamable> extends CustomMessage { | ||||
|         this.dataReader = dataReader; | ||||
|     } | ||||
|  | ||||
|     public static <T extends Streamable> CryptoCustomMessage<T> read(byte[] data, Reader<T> dataReader) throws IOException { | ||||
|         CryptoBox cryptoBox = CryptoBox.read(new ByteArrayInputStream(data), data.length); | ||||
|     public static <T extends Streamable> CryptoCustomMessage<T> read(CustomMessage data, Reader<T> dataReader) throws IOException { | ||||
|         CryptoBox cryptoBox = CryptoBox.read(new ByteArrayInputStream(data.getData()), data.getData().length); | ||||
|         return new CryptoCustomMessage<>(cryptoBox, dataReader); | ||||
|     } | ||||
|  | ||||
| @@ -111,6 +111,7 @@ public class CryptoCustomMessage<T extends Streamable> extends CustomMessage { | ||||
|  | ||||
|     @Override | ||||
|     public void write(OutputStream out) throws IOException { | ||||
|         Encode.varString(COMMAND, out); | ||||
|         container.write(out); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -24,6 +24,7 @@ import ch.dissem.bitmessage.utils.Encode; | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.io.OutputStream; | ||||
| import java.util.Arrays; | ||||
|  | ||||
| import static ch.dissem.bitmessage.utils.Decode.*; | ||||
|  | ||||
| @@ -80,6 +81,28 @@ public class ProofOfWorkRequest implements Streamable { | ||||
|         Encode.varBytes(data, out); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean equals(Object o) { | ||||
|         if (this == o) return true; | ||||
|         if (o == null || getClass() != o.getClass()) return false; | ||||
|  | ||||
|         ProofOfWorkRequest other = (ProofOfWorkRequest) o; | ||||
|  | ||||
|         if (!sender.equals(other.sender)) return false; | ||||
|         if (!Arrays.equals(initialHash, other.initialHash)) return false; | ||||
|         if (request != other.request) return false; | ||||
|         return Arrays.equals(data, other.data); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int hashCode() { | ||||
|         int result = sender.hashCode(); | ||||
|         result = 31 * result + Arrays.hashCode(initialHash); | ||||
|         result = 31 * result + request.hashCode(); | ||||
|         result = 31 * result + Arrays.hashCode(data); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     public static class Reader implements CryptoCustomMessage.Reader<ProofOfWorkRequest> { | ||||
|         private final BitmessageAddress identity; | ||||
|  | ||||
| @@ -93,7 +116,6 @@ public class ProofOfWorkRequest implements Streamable { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public enum Request { | ||||
|         CALCULATE, | ||||
|         CALCULATING, | ||||
|   | ||||
| @@ -17,8 +17,10 @@ | ||||
| package ch.dissem.bitmessage.extensions; | ||||
|  | ||||
| import ch.dissem.bitmessage.entity.BitmessageAddress; | ||||
| import ch.dissem.bitmessage.entity.CustomMessage; | ||||
| import ch.dissem.bitmessage.entity.payload.GenericPayload; | ||||
| import ch.dissem.bitmessage.entity.valueobject.PrivateKey; | ||||
| import ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest; | ||||
| import ch.dissem.bitmessage.utils.TestBase; | ||||
| import ch.dissem.bitmessage.utils.TestUtils; | ||||
| import org.junit.Test; | ||||
| @@ -33,7 +35,7 @@ import static org.junit.Assert.assertEquals; | ||||
|  | ||||
| public class CryptoCustomMessageTest extends TestBase { | ||||
|     @Test | ||||
|     public void testEncryptThenDecrypt() throws Exception { | ||||
|     public void ensureEncryptThenDecryptYieldsSameObject() throws Exception { | ||||
|         PrivateKey privateKey = PrivateKey.read(TestUtils.getResource("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8.privkey")); | ||||
|         BitmessageAddress sendingIdentity = new BitmessageAddress(privateKey); | ||||
|  | ||||
| @@ -45,14 +47,40 @@ public class CryptoCustomMessageTest extends TestBase { | ||||
|         messageBefore.write(out); | ||||
|         ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
|  | ||||
|         CryptoCustomMessage<GenericPayload> messageAfter = CryptoCustomMessage.read(out.toByteArray(), new CryptoCustomMessage.Reader<GenericPayload>() { | ||||
|             @Override | ||||
|             public GenericPayload read(BitmessageAddress ignore, InputStream in) throws IOException { | ||||
|                 return GenericPayload.read(0, in, 1, 100); | ||||
|             } | ||||
|         }); | ||||
|         CustomMessage customMessage = CustomMessage.read(in, out.size()); | ||||
|         CryptoCustomMessage<GenericPayload> messageAfter = CryptoCustomMessage.read(customMessage, | ||||
|                 new CryptoCustomMessage.Reader<GenericPayload>() { | ||||
|                     @Override | ||||
|                     public GenericPayload read(BitmessageAddress ignore, InputStream in) throws IOException { | ||||
|                         return GenericPayload.read(0, in, 1, 100); | ||||
|                     } | ||||
|                 }); | ||||
|         GenericPayload payloadAfter = messageAfter.decrypt(sendingIdentity.getPublicDecryptionKey()); | ||||
|  | ||||
|         assertEquals(payloadBefore, payloadAfter); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testWithActualRequest() throws Exception { | ||||
|         PrivateKey privateKey = PrivateKey.read(TestUtils.getResource("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8.privkey")); | ||||
|         final BitmessageAddress sendingIdentity = new BitmessageAddress(privateKey); | ||||
|  | ||||
|         ProofOfWorkRequest requestBefore = new ProofOfWorkRequest(sendingIdentity, security().randomBytes(64), | ||||
|                 ProofOfWorkRequest.Request.CALCULATE); | ||||
|  | ||||
|         CryptoCustomMessage<ProofOfWorkRequest> messageBefore = new CryptoCustomMessage<>(requestBefore); | ||||
|         messageBefore.signAndEncrypt(sendingIdentity, security().createPublicKey(sendingIdentity.getPublicDecryptionKey())); | ||||
|  | ||||
|  | ||||
|         ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
|         messageBefore.write(out); | ||||
|         ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
|  | ||||
|         CustomMessage customMessage = CustomMessage.read(in, out.size()); | ||||
|         CryptoCustomMessage<ProofOfWorkRequest> messageAfter = CryptoCustomMessage.read(customMessage, | ||||
|                 new ProofOfWorkRequest.Reader(sendingIdentity)); | ||||
|         ProofOfWorkRequest requestAfter = messageAfter.decrypt(sendingIdentity.getPublicDecryptionKey()); | ||||
|  | ||||
|         assertEquals(requestBefore, requestAfter); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user