From 4f7f80c12a9b1cb2717f1a536da554e918a352e0 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 8 Apr 2016 19:22:40 +0200 Subject: [PATCH] Added tests and code improvements --- .../dissem/bitmessage/BitmessageContext.java | 10 +++-- .../ch/dissem/bitmessage/InternalContext.java | 13 ++----- .../dissem/bitmessage/ProofOfWorkService.java | 6 ++- .../ch/dissem/bitmessage/entity/Addr.java | 4 +- .../bitmessage/entity/BitmessageAddress.java | 3 +- .../bitmessage/entity/CustomMessage.java | 2 + .../ch/dissem/bitmessage/entity/GetData.java | 2 + .../java/ch/dissem/bitmessage/entity/Inv.java | 2 + .../bitmessage/entity/NetworkMessage.java | 2 + .../bitmessage/entity/ObjectMessage.java | 2 + .../dissem/bitmessage/entity/Plaintext.java | 2 + .../ch/dissem/bitmessage/entity/VerAck.java | 2 + .../ch/dissem/bitmessage/entity/Version.java | 2 + .../bitmessage/entity/payload/Broadcast.java | 2 + .../bitmessage/entity/payload/CryptoBox.java | 1 + .../entity/payload/GenericPayload.java | 2 + .../bitmessage/entity/payload/GetPubkey.java | 2 + .../dissem/bitmessage/entity/payload/Msg.java | 2 + .../entity/payload/ObjectPayload.java | 3 +- .../bitmessage/entity/payload/Pubkey.java | 2 + .../bitmessage/entity/payload/V2Pubkey.java | 2 + .../bitmessage/entity/payload/V3Pubkey.java | 2 + .../entity/payload/V4Broadcast.java | 2 + .../bitmessage/entity/payload/V4Pubkey.java | 2 + .../entity/payload/V5Broadcast.java | 2 + .../entity/valueobject/PrivateKey.java | 4 +- .../exception/AddressFormatException.java | 2 + .../exception/ApplicationException.java | 2 + .../exception/DecryptionFailedException.java | 1 + .../InsufficientProofOfWorkException.java | 2 + .../bitmessage/exception/NodeException.java | 2 + .../ports/AbstractCryptography.java | 10 +++-- .../bitmessage/BitmessageContextTest.java | 37 +++++++++++++++++-- .../DefaultMessageListenerTest.java | 2 - .../bitmessage/ProofOfWorkServiceTest.java | 4 -- .../dissem/bitmessage/utils/EncodeTest.java | 2 +- .../bitmessage/utils/MessageMatchers.java | 1 - .../extensions/CryptoCustomMessage.java | 3 ++ .../extensions/pow/ProofOfWorkRequest.java | 2 + .../bitmessage/networking/Connection.java | 4 +- .../networking/NetworkHandlerTest.java | 1 - .../repository/JdbcNodeRegistryTest.java | 3 +- .../ch/dissem/bitmessage/wif/WifExporter.java | 3 ++ .../ch/dissem/bitmessage/wif/WifImporter.java | 3 ++ .../bitmessage/wif/WifExporterTest.java | 21 ++++++++++- .../bitmessage/wif/WifImporterTest.java | 21 +++++++++-- 46 files changed, 163 insertions(+), 43 deletions(-) diff --git a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java index 8377e22..51243ab 100644 --- a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java @@ -36,6 +36,8 @@ import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.*; +import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES; +import static ch.dissem.bitmessage.InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE; import static ch.dissem.bitmessage.entity.Plaintext.Status.*; import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST; import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; @@ -99,8 +101,8 @@ public class BitmessageContext { final BitmessageAddress identity = new BitmessageAddress(new PrivateKey( shorter, ctx.getStreams()[0], - ctx.getNetworkNonceTrialsPerByte(), - ctx.getNetworkExtraBytes(), + NETWORK_NONCE_TRIALS_PER_BYTE, + NETWORK_EXTRA_BYTES, features )); ctx.getAddressRepository().save(identity); @@ -130,12 +132,12 @@ public class BitmessageContext { } public List createDeterministicAddresses( - String passphrase, int numberOfAddresses, int version, int stream, boolean shorter) { + String passphrase, int numberOfAddresses, long version, long stream, boolean shorter) { List result = BitmessageAddress.deterministic( passphrase, numberOfAddresses, version, stream, shorter); for (int i = 0; i < result.size(); i++) { BitmessageAddress address = result.get(i); - address.setAlias(passphrase + " (" + (i + 1) + ")"); + address.setAlias("deterministic (" + (i + 1) + ")"); ctx.getAddressRepository().save(address); } return result; diff --git a/core/src/main/java/ch/dissem/bitmessage/InternalContext.java b/core/src/main/java/ch/dissem/bitmessage/InternalContext.java index 33ee15b..00ec40e 100644 --- a/core/src/main/java/ch/dissem/bitmessage/InternalContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/InternalContext.java @@ -43,6 +43,9 @@ import java.util.TreeSet; public class InternalContext { private final static Logger LOG = LoggerFactory.getLogger(InternalContext.class); + public final static long NETWORK_NONCE_TRIALS_PER_BYTE = 1000; + public final static long NETWORK_EXTRA_BYTES = 1000; + private final Cryptography cryptography; private final Inventory inventory; private final NodeRegistry nodeRegistry; @@ -58,8 +61,6 @@ public class InternalContext { private final TreeSet streams = new TreeSet<>(); private final int port; private final long clientNonce; - private final long networkNonceTrialsPerByte = 1000; - private final long networkExtraBytes = 1000; private long connectionTTL; private int connectionLimit; @@ -158,14 +159,6 @@ public class InternalContext { return port; } - public long getNetworkNonceTrialsPerByte() { - return networkNonceTrialsPerByte; - } - - public long getNetworkExtraBytes() { - return networkExtraBytes; - } - public void send(final BitmessageAddress from, BitmessageAddress to, final ObjectPayload payload, final long timeToLive) { try { diff --git a/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java b/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java index ad62dde..93e461f 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java +++ b/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java @@ -14,6 +14,8 @@ import org.slf4j.LoggerFactory; import java.util.List; +import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES; +import static ch.dissem.bitmessage.InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE; import static ch.dissem.bitmessage.utils.Singleton.security; /** @@ -45,8 +47,8 @@ public class ProofOfWorkService implements ProofOfWorkEngine.Callback, InternalC public void doProofOfWork(BitmessageAddress recipient, ObjectMessage object) { Pubkey pubkey = recipient == null ? null : recipient.getPubkey(); - long nonceTrialsPerByte = pubkey == null ? ctx.getNetworkNonceTrialsPerByte() : pubkey.getNonceTrialsPerByte(); - long extraBytes = pubkey == null ? ctx.getNetworkExtraBytes() : pubkey.getExtraBytes(); + long nonceTrialsPerByte = pubkey == null ? NETWORK_NONCE_TRIALS_PER_BYTE : pubkey.getNonceTrialsPerByte(); + long extraBytes = pubkey == null ? NETWORK_EXTRA_BYTES : pubkey.getExtraBytes(); powRepo.putObject(object, nonceTrialsPerByte, extraBytes); if (object.getPayload() instanceof PlaintextHolder) { diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/Addr.java b/core/src/main/java/ch/dissem/bitmessage/entity/Addr.java index 2c0eb3e..6125910 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/Addr.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/Addr.java @@ -29,6 +29,8 @@ import java.util.List; * The 'addr' command holds a list of known active Bitmessage nodes. */ public class Addr implements MessagePayload { + private static final long serialVersionUID = -5117688017050138720L; + private final List addresses; private Addr(Builder builder) { @@ -53,7 +55,7 @@ public class Addr implements MessagePayload { } public static final class Builder { - private List addresses = new ArrayList(); + private List addresses = new ArrayList<>(); public Builder addresses(Collection addresses){ this.addresses.addAll(addresses); diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/BitmessageAddress.java b/core/src/main/java/ch/dissem/bitmessage/entity/BitmessageAddress.java index 2458854..7868efd 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/BitmessageAddress.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/BitmessageAddress.java @@ -101,6 +101,7 @@ public class BitmessageAddress implements Serializable { public BitmessageAddress(String address, String passphrase) { this(address); this.privateKey = new PrivateKey(this, passphrase); + this.pubkey = this.privateKey.getPubkey(); if (!Arrays.equals(ripe, privateKey.getPubkey().getRipe())) { throw new IllegalArgumentException("Wrong address or passphrase"); } @@ -120,7 +121,7 @@ public class BitmessageAddress implements Serializable { } public static List deterministic(String passphrase, int numberOfAddresses, - int version, int stream, boolean shorter) { + long version, long stream, boolean shorter) { List result = new ArrayList<>(numberOfAddresses); List privateKeys = PrivateKey.deterministic(passphrase, numberOfAddresses, version, stream, shorter); for (PrivateKey pk : privateKeys) { diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/CustomMessage.java b/core/src/main/java/ch/dissem/bitmessage/entity/CustomMessage.java index ffdb9b2..e43f56d 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/CustomMessage.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/CustomMessage.java @@ -29,6 +29,8 @@ import static ch.dissem.bitmessage.utils.Decode.varString; * @author Christian Basler */ public class CustomMessage implements MessagePayload { + private static final long serialVersionUID = -8932056829480326011L; + public static final String COMMAND_ERROR = "ERROR"; private final String command; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/GetData.java b/core/src/main/java/ch/dissem/bitmessage/entity/GetData.java index 9569929..44fab5b 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/GetData.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/GetData.java @@ -28,6 +28,8 @@ import java.util.List; * The 'getdata' command is used to request objects from a node. */ public class GetData implements MessagePayload { + private static final long serialVersionUID = 1433878785969631061L; + public static final int MAX_INVENTORY_SIZE = 50_000; List inventory; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/Inv.java b/core/src/main/java/ch/dissem/bitmessage/entity/Inv.java index 7130fd7..fd2d40d 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/Inv.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/Inv.java @@ -28,6 +28,8 @@ import java.util.List; * The 'inv' command holds up to 50000 inventory vectors, i.e. hashes of inventory items. */ public class Inv implements MessagePayload { + private static final long serialVersionUID = 3662992522956947145L; + private List inventory; private Inv(Builder builder) { diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/NetworkMessage.java b/core/src/main/java/ch/dissem/bitmessage/entity/NetworkMessage.java index 5e8552e..347af6c 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/NetworkMessage.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/NetworkMessage.java @@ -33,6 +33,8 @@ import static ch.dissem.bitmessage.utils.Singleton.security; * A network message is exchanged between two nodes. */ public class NetworkMessage implements Streamable { + private static final long serialVersionUID = 702708857104464809L; + /** * Magic value indicating message origin network, and used to seek to next message when stream state is unknown */ diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java b/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java index 89ddddb..68aa5ac 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java @@ -36,6 +36,8 @@ import static ch.dissem.bitmessage.utils.Singleton.security; * The 'object' command sends an object that is shared throughout the network. */ public class ObjectMessage implements MessagePayload { + private static final long serialVersionUID = 2495752480120659139L; + private byte[] nonce; private long expiresTime; private long objectType; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java b/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java index fe8b1d3..034f2da 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java @@ -31,6 +31,8 @@ import java.util.*; * The unencrypted message to be sent by 'msg' or 'broadcast'. */ public class Plaintext implements Streamable { + private static final long serialVersionUID = -5325729856394951079L; + private final Type type; private final BitmessageAddress from; private final long encoding; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/VerAck.java b/core/src/main/java/ch/dissem/bitmessage/entity/VerAck.java index 1aad501..815a20f 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/VerAck.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/VerAck.java @@ -23,6 +23,8 @@ import java.io.OutputStream; * The 'verack' command answers a 'version' command, accepting the other node's version. */ public class VerAck implements MessagePayload { + private static final long serialVersionUID = -4302074845199181687L; + @Override public Command getCommand() { return Command.VERACK; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/Version.java b/core/src/main/java/ch/dissem/bitmessage/entity/Version.java index 6d24d92..3722528 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/Version.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/Version.java @@ -29,6 +29,8 @@ import java.util.Random; * The 'version' command advertises this node's latest supported protocol version upon initiation. */ public class Version implements MessagePayload { + private static final long serialVersionUID = 7219240857343176567L; + /** * Identifies protocol version being used by the node. Should equal 3. Nodes should disconnect if the remote node's * version is lower but continue with the connection if it is higher. diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/Broadcast.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/Broadcast.java index 47bf539..7e19e8f 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/Broadcast.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/Broadcast.java @@ -32,6 +32,8 @@ import static ch.dissem.bitmessage.utils.Singleton.security; * Broadcasts are version 4 or 5. */ public abstract class Broadcast extends ObjectPayload implements Encrypted, PlaintextHolder { + private static final long serialVersionUID = 4064521827582239069L; + protected final long stream; protected CryptoBox encrypted; protected Plaintext plaintext; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/CryptoBox.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/CryptoBox.java index 8887fea..6609070 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/CryptoBox.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/CryptoBox.java @@ -31,6 +31,7 @@ import static ch.dissem.bitmessage.utils.Singleton.security; public class CryptoBox implements Streamable { + private static final long serialVersionUID = 7217659539975573852L; private static final Logger LOG = LoggerFactory.getLogger(CryptoBox.class); private final byte[] initializationVector; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/GenericPayload.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/GenericPayload.java index 7051814..66cc296 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/GenericPayload.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/GenericPayload.java @@ -28,6 +28,8 @@ import java.util.Arrays; * have to know what it is. */ public class GenericPayload extends ObjectPayload { + private static final long serialVersionUID = -912314085064185940L; + private long stream; private byte[] data; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/GetPubkey.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/GetPubkey.java index e31dbe3..06e623a 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/GetPubkey.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/GetPubkey.java @@ -27,6 +27,8 @@ import java.io.OutputStream; * Request for a public key. */ public class GetPubkey extends ObjectPayload { + private static final long serialVersionUID = -3634516646972610180L; + private long stream; private byte[] ripeTag; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/Msg.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/Msg.java index 52c36e7..8974ce3 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/Msg.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/Msg.java @@ -31,6 +31,8 @@ import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; * Used for person-to-person messages. */ public class Msg extends ObjectPayload implements Encrypted, PlaintextHolder { + private static final long serialVersionUID = 4327495048296365733L; + private long stream; private CryptoBox encrypted; private Plaintext plaintext; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/ObjectPayload.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/ObjectPayload.java index 0ca45cd..33da28d 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/ObjectPayload.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/ObjectPayload.java @@ -21,12 +21,13 @@ import ch.dissem.bitmessage.entity.Streamable; import java.io.IOException; import java.io.OutputStream; -import java.io.Serializable; /** * The payload of an 'object' command. This is shared by the network. */ public abstract class ObjectPayload implements Streamable { + private static final long serialVersionUID = -5034977402902364482L; + private final long version; protected ObjectPayload(long version) { diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/Pubkey.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/Pubkey.java index 1243b32..dec339d 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/Pubkey.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/Pubkey.java @@ -26,6 +26,8 @@ import static ch.dissem.bitmessage.utils.Singleton.security; * Public keys for signing and encryption, the answer to a 'getpubkey' request. */ public abstract class Pubkey extends ObjectPayload { + private static final long serialVersionUID = -6634533361454999619L; + public final static long LATEST_VERSION = 4; protected Pubkey(long version) { diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/V2Pubkey.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/V2Pubkey.java index d915941..0eceb91 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/V2Pubkey.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/V2Pubkey.java @@ -27,6 +27,8 @@ import java.io.OutputStream; * A version 2 public key. */ public class V2Pubkey extends Pubkey { + private static final long serialVersionUID = -257598690676510460L; + protected long stream; protected int behaviorBitfield; protected byte[] publicSigningKey; // 64 Bytes diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/V3Pubkey.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/V3Pubkey.java index a3b4da2..72358d0 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/V3Pubkey.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/V3Pubkey.java @@ -29,6 +29,8 @@ import java.util.Objects; * A version 3 public key. */ public class V3Pubkey extends V2Pubkey { + private static final long serialVersionUID = 6958853116648528319L; + long nonceTrialsPerByte; long extraBytes; byte[] signature; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/V4Broadcast.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/V4Broadcast.java index 39127a8..7781455 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/V4Broadcast.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/V4Broadcast.java @@ -28,6 +28,8 @@ import java.io.OutputStream; * Broadcasts are version 4 or 5. */ public class V4Broadcast extends Broadcast { + private static final long serialVersionUID = 195663108282762711L; + protected V4Broadcast(long version, long stream, CryptoBox encrypted, Plaintext plaintext) { super(version, stream, encrypted, plaintext); } diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/V4Pubkey.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/V4Pubkey.java index 048e7a6..8aa0ee4 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/V4Pubkey.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/V4Pubkey.java @@ -33,6 +33,8 @@ import java.util.Arrays; * to create messages to be used in spam or in flooding attacks. */ public class V4Pubkey extends Pubkey implements Encrypted { + private static final long serialVersionUID = 1556710353694033093L; + private long stream; private byte[] tag; private CryptoBox encrypted; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/payload/V5Broadcast.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/V5Broadcast.java index c12aae0..8f07a30 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/payload/V5Broadcast.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/payload/V5Broadcast.java @@ -28,6 +28,8 @@ import java.io.OutputStream; * Users who are subscribed to the sending address will see the message appear in their inbox. */ public class V5Broadcast extends V4Broadcast { + private static final long serialVersionUID = 920649721626968644L; + private byte[] tag; private V5Broadcast(long stream, byte[] tag, CryptoBox encrypted) { diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/PrivateKey.java b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/PrivateKey.java index 73594ff..b9c3afb 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/PrivateKey.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/PrivateKey.java @@ -16,6 +16,7 @@ package ch.dissem.bitmessage.entity.valueobject; +import ch.dissem.bitmessage.InternalContext; import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.Streamable; import ch.dissem.bitmessage.entity.payload.Pubkey; @@ -81,7 +82,8 @@ public class PrivateKey implements Streamable { private PrivateKey(Builder builder) { this.privateSigningKey = builder.privSK; this.privateEncryptionKey = builder.privEK; - this.pubkey = Factory.createPubkey(builder.version, builder.stream, builder.pubSK, builder.pubEK, 0, 0); + this.pubkey = Factory.createPubkey(builder.version, builder.stream, builder.pubSK, builder.pubEK, + InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE, InternalContext.NETWORK_EXTRA_BYTES); } private static class Builder { diff --git a/core/src/main/java/ch/dissem/bitmessage/exception/AddressFormatException.java b/core/src/main/java/ch/dissem/bitmessage/exception/AddressFormatException.java index 7da990a..9f6674d 100644 --- a/core/src/main/java/ch/dissem/bitmessage/exception/AddressFormatException.java +++ b/core/src/main/java/ch/dissem/bitmessage/exception/AddressFormatException.java @@ -20,6 +20,8 @@ package ch.dissem.bitmessage.exception; * Indicates an illegal Bitmessage address */ public class AddressFormatException extends RuntimeException { + private static final long serialVersionUID = 6943764578672021573L; + public AddressFormatException(String message) { super(message); } diff --git a/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java b/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java index c381410..e688cb1 100644 --- a/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java +++ b/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java @@ -20,6 +20,8 @@ package ch.dissem.bitmessage.exception; * @author Christian Basler */ public class ApplicationException extends RuntimeException { + private static final long serialVersionUID = 1796776684126759324L; + public ApplicationException(Throwable cause) { super(cause); } diff --git a/core/src/main/java/ch/dissem/bitmessage/exception/DecryptionFailedException.java b/core/src/main/java/ch/dissem/bitmessage/exception/DecryptionFailedException.java index c68a050..c9e3efd 100644 --- a/core/src/main/java/ch/dissem/bitmessage/exception/DecryptionFailedException.java +++ b/core/src/main/java/ch/dissem/bitmessage/exception/DecryptionFailedException.java @@ -17,4 +17,5 @@ package ch.dissem.bitmessage.exception; public class DecryptionFailedException extends Exception { + private static final long serialVersionUID = 3241116253113872731L; } diff --git a/core/src/main/java/ch/dissem/bitmessage/exception/InsufficientProofOfWorkException.java b/core/src/main/java/ch/dissem/bitmessage/exception/InsufficientProofOfWorkException.java index 685e710..94a5d84 100644 --- a/core/src/main/java/ch/dissem/bitmessage/exception/InsufficientProofOfWorkException.java +++ b/core/src/main/java/ch/dissem/bitmessage/exception/InsufficientProofOfWorkException.java @@ -22,6 +22,8 @@ import java.io.IOException; import java.util.Arrays; public class InsufficientProofOfWorkException extends IOException { + private static final long serialVersionUID = 9105580366564571318L; + public InsufficientProofOfWorkException(byte[] target, byte[] hash) { super("Insufficient proof of work: " + Strings.hex(target) + " required, " + Strings.hex(Arrays.copyOfRange(hash, 0, 8)) + " achieved."); } diff --git a/core/src/main/java/ch/dissem/bitmessage/exception/NodeException.java b/core/src/main/java/ch/dissem/bitmessage/exception/NodeException.java index 9ab2b7f..cecd950 100644 --- a/core/src/main/java/ch/dissem/bitmessage/exception/NodeException.java +++ b/core/src/main/java/ch/dissem/bitmessage/exception/NodeException.java @@ -22,6 +22,8 @@ package ch.dissem.bitmessage.exception; * @author Ch. Basler */ public class NodeException extends RuntimeException { + private static final long serialVersionUID = 2965325796118227802L; + public NodeException(String message) { super(message); } diff --git a/core/src/main/java/ch/dissem/bitmessage/ports/AbstractCryptography.java b/core/src/main/java/ch/dissem/bitmessage/ports/AbstractCryptography.java index 81252d5..3b08377 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ports/AbstractCryptography.java +++ b/core/src/main/java/ch/dissem/bitmessage/ports/AbstractCryptography.java @@ -35,6 +35,8 @@ import java.security.GeneralSecurityException; import java.security.MessageDigest; import java.security.SecureRandom; +import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES; +import static ch.dissem.bitmessage.InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE; import static ch.dissem.bitmessage.utils.Numbers.max; /** @@ -99,8 +101,8 @@ public abstract class AbstractCryptography implements Cryptography, InternalCont public void doProofOfWork(ObjectMessage object, long nonceTrialsPerByte, long extraBytes, ProofOfWorkEngine.Callback callback) { - nonceTrialsPerByte = max(nonceTrialsPerByte, context.getNetworkNonceTrialsPerByte()); - extraBytes = max(extraBytes, context.getNetworkExtraBytes()); + nonceTrialsPerByte = max(nonceTrialsPerByte, NETWORK_NONCE_TRIALS_PER_BYTE); + extraBytes = max(extraBytes, NETWORK_EXTRA_BYTES); byte[] initialHash = getInitialHash(object); @@ -125,8 +127,8 @@ public abstract class AbstractCryptography implements Cryptography, InternalCont @Override public byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) { - if (nonceTrialsPerByte == 0) nonceTrialsPerByte = context.getNetworkNonceTrialsPerByte(); - if (extraBytes == 0) extraBytes = context.getNetworkExtraBytes(); + if (nonceTrialsPerByte == 0) nonceTrialsPerByte = NETWORK_NONCE_TRIALS_PER_BYTE; + if (extraBytes == 0) extraBytes = NETWORK_EXTRA_BYTES; BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - UnixTime.now()); BigInteger numerator = TWO_POW_64; diff --git a/core/src/test/java/ch/dissem/bitmessage/BitmessageContextTest.java b/core/src/test/java/ch/dissem/bitmessage/BitmessageContextTest.java index a1991f8..530ece0 100644 --- a/core/src/test/java/ch/dissem/bitmessage/BitmessageContextTest.java +++ b/core/src/test/java/ch/dissem/bitmessage/BitmessageContextTest.java @@ -31,16 +31,13 @@ import org.hamcrest.Description; import org.junit.Before; import org.junit.Test; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import static ch.dissem.bitmessage.entity.payload.ObjectType.*; import static ch.dissem.bitmessage.utils.MessageMatchers.object; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.*; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; /** @@ -216,6 +213,38 @@ public class BitmessageContextTest { assertTrue(chan.isChan()); } + @Test + public void ensureDeterministicAddressesAreCreated() { + final int expected_size = 8; + List addresses = ctx.createDeterministicAddresses("test", expected_size, 4, 1, false); + assertEquals(expected_size, addresses.size()); + Set expected = new HashSet<>(expected_size); + expected.add("BM-2cWFkyuXXFw6d393RGnin2RpSXj8wxtt6F"); + expected.add("BM-2cX8TF9vuQZEWvT7UrEeq1HN9dgiSUPLEN"); + expected.add("BM-2cUzX8f9CKUU7L8NeB8GExZvf54PrcXq1S"); + expected.add("BM-2cU7MAoQd7KE8SPF7AKFPpoEZKjk86KRqE"); + expected.add("BM-2cVm8ByVBacc2DVhdTNs6rmy5ZQK6DUsrt"); + expected.add("BM-2cW2af1vB6kWon2WkygDHqGwfcpfAFm2Jk"); + expected.add("BM-2cWdWD7UtUN4gWChgNX9pvyvNPjUZvU8BT"); + expected.add("BM-2cXkYgYcUrv4fGxSHzyEScW955Cc8sDteo"); + for (BitmessageAddress a : addresses) { + assertTrue(expected.contains(a.getAddress())); + expected.remove(a.getAddress()); + } + } + @Test + public void ensureShortDeterministicAddressesAreCreated() { + final int expected_size = 1; + List addresses = ctx.createDeterministicAddresses("test", expected_size, 4, 1, true); + assertEquals(expected_size, addresses.size()); + Set expected = new HashSet<>(expected_size); + expected.add("BM-NBGyBAEp6VnBkFWKpzUSgxuTqVdWPi78"); + for (BitmessageAddress a : addresses) { + assertTrue(expected.contains(a.getAddress())); + expected.remove(a.getAddress()); + } + } + @Test public void ensureChanIsCreated() { BitmessageAddress chan = ctx.createChan("test"); diff --git a/core/src/test/java/ch/dissem/bitmessage/DefaultMessageListenerTest.java b/core/src/test/java/ch/dissem/bitmessage/DefaultMessageListenerTest.java index a6e6c53..976e464 100644 --- a/core/src/test/java/ch/dissem/bitmessage/DefaultMessageListenerTest.java +++ b/core/src/test/java/ch/dissem/bitmessage/DefaultMessageListenerTest.java @@ -40,8 +40,6 @@ import static ch.dissem.bitmessage.entity.Plaintext.Status.PUBKEY_REQUESTED; import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST; import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; import static ch.dissem.bitmessage.utils.MessageMatchers.plaintext; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; import static org.mockito.Mockito.*; /** diff --git a/core/src/test/java/ch/dissem/bitmessage/ProofOfWorkServiceTest.java b/core/src/test/java/ch/dissem/bitmessage/ProofOfWorkServiceTest.java index e489e35..e66beb1 100644 --- a/core/src/test/java/ch/dissem/bitmessage/ProofOfWorkServiceTest.java +++ b/core/src/test/java/ch/dissem/bitmessage/ProofOfWorkServiceTest.java @@ -34,10 +34,6 @@ import java.util.Arrays; import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.isNull; -import static org.mockito.Mockito.anyLong; import static org.mockito.Mockito.*; /** diff --git a/core/src/test/java/ch/dissem/bitmessage/utils/EncodeTest.java b/core/src/test/java/ch/dissem/bitmessage/utils/EncodeTest.java index aaba5bf..23d602c 100644 --- a/core/src/test/java/ch/dissem/bitmessage/utils/EncodeTest.java +++ b/core/src/test/java/ch/dissem/bitmessage/utils/EncodeTest.java @@ -57,7 +57,7 @@ public class EncodeTest { checkBytes(stream, 4, 3, 2, 1); stream = new ByteArrayOutputStream(); - Encode.int32(3355443201l, stream); + Encode.int32(3355443201L, stream); checkBytes(stream, 200, 0, 0, 1); } diff --git a/core/src/test/java/ch/dissem/bitmessage/utils/MessageMatchers.java b/core/src/test/java/ch/dissem/bitmessage/utils/MessageMatchers.java index 8bb1e36..e423d02 100644 --- a/core/src/test/java/ch/dissem/bitmessage/utils/MessageMatchers.java +++ b/core/src/test/java/ch/dissem/bitmessage/utils/MessageMatchers.java @@ -21,7 +21,6 @@ import ch.dissem.bitmessage.entity.Plaintext; import ch.dissem.bitmessage.entity.payload.ObjectType; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; -import org.hamcrest.Matcher; import org.mockito.Matchers; /** diff --git a/extensions/src/main/java/ch/dissem/bitmessage/extensions/CryptoCustomMessage.java b/extensions/src/main/java/ch/dissem/bitmessage/extensions/CryptoCustomMessage.java index 33c7524..0b5b142 100644 --- a/extensions/src/main/java/ch/dissem/bitmessage/extensions/CryptoCustomMessage.java +++ b/extensions/src/main/java/ch/dissem/bitmessage/extensions/CryptoCustomMessage.java @@ -36,7 +36,10 @@ import static ch.dissem.bitmessage.utils.Singleton.security; * @author Christian Basler */ public class CryptoCustomMessage extends CustomMessage { + private static final long serialVersionUID = 7395193565986284426L; + public static final String COMMAND = "ENCRYPTED"; + private final Reader dataReader; private CryptoBox container; private BitmessageAddress sender; diff --git a/extensions/src/main/java/ch/dissem/bitmessage/extensions/pow/ProofOfWorkRequest.java b/extensions/src/main/java/ch/dissem/bitmessage/extensions/pow/ProofOfWorkRequest.java index 0024aaa..d661c50 100644 --- a/extensions/src/main/java/ch/dissem/bitmessage/extensions/pow/ProofOfWorkRequest.java +++ b/extensions/src/main/java/ch/dissem/bitmessage/extensions/pow/ProofOfWorkRequest.java @@ -32,6 +32,8 @@ import static ch.dissem.bitmessage.utils.Decode.*; * @author Christian Basler */ public class ProofOfWorkRequest implements Streamable { + private static final long serialVersionUID = 4729003751499662713L; + private final BitmessageAddress sender; private final byte[] initialHash; private final Request request; diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java index 5781bc3..54c8acf 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java @@ -41,6 +41,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentMap; +import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES; +import static ch.dissem.bitmessage.InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE; import static ch.dissem.bitmessage.networking.Connection.Mode.CLIENT; import static ch.dissem.bitmessage.networking.Connection.Mode.SYNC; import static ch.dissem.bitmessage.networking.Connection.State.*; @@ -247,7 +249,7 @@ class Connection { } try { listener.receive(objectMessage); - security().checkProofOfWork(objectMessage, ctx.getNetworkNonceTrialsPerByte(), ctx.getNetworkExtraBytes()); + security().checkProofOfWork(objectMessage, NETWORK_NONCE_TRIALS_PER_BYTE, NETWORK_EXTRA_BYTES); ctx.getInventory().storeObject(objectMessage); // offer object to some random nodes so it gets distributed throughout the network: networkHandler.offer(objectMessage.getInventoryVector()); diff --git a/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java b/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java index 2e63e3a..accb4c5 100644 --- a/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java +++ b/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java @@ -27,7 +27,6 @@ import ch.dissem.bitmessage.utils.Property; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import org.mockito.Mockito; import java.net.InetAddress; import java.util.concurrent.Future; diff --git a/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.java b/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.java index 044c44c..f56d973 100644 --- a/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.java +++ b/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.java @@ -23,6 +23,7 @@ import org.junit.Before; import org.junit.Test; import java.util.Arrays; +import java.util.Collections; import java.util.List; import static ch.dissem.bitmessage.utils.UnixTime.now; @@ -73,7 +74,7 @@ public class JdbcNodeRegistryTest extends TestBase { List knownAddresses = registry.getKnownAddresses(1000, 1); assertEquals(5, knownAddresses.size()); - registry.offerAddresses(Arrays.asList( + registry.offerAddresses(Collections.singletonList( createAddress(1, 8445, 1, now()) )); diff --git a/wif/src/main/java/ch/dissem/bitmessage/wif/WifExporter.java b/wif/src/main/java/ch/dissem/bitmessage/wif/WifExporter.java index 00f2790..ad6098e 100644 --- a/wif/src/main/java/ch/dissem/bitmessage/wif/WifExporter.java +++ b/wif/src/main/java/ch/dissem/bitmessage/wif/WifExporter.java @@ -60,6 +60,9 @@ public class WifExporter { section.add("label", identity.getAlias()); section.add("enabled", true); section.add("decoy", false); + if (identity.isChan()) { + section.add("chan", identity.isChan()); + } section.add("noncetrialsperbyte", identity.getPubkey().getNonceTrialsPerByte()); section.add("payloadlengthextrabytes", identity.getPubkey().getExtraBytes()); section.add("privsigningkey", exportSecret(identity.getPrivateKey().getPrivateSigningKey())); diff --git a/wif/src/main/java/ch/dissem/bitmessage/wif/WifImporter.java b/wif/src/main/java/ch/dissem/bitmessage/wif/WifImporter.java index 3a45b44..1bd6bcd 100644 --- a/wif/src/main/java/ch/dissem/bitmessage/wif/WifImporter.java +++ b/wif/src/main/java/ch/dissem/bitmessage/wif/WifImporter.java @@ -70,6 +70,9 @@ public class WifImporter { section.get("payloadlengthextrabytes", long.class), Pubkey.Feature.bitfield(features) ); + if (section.containsKey("chan")) { + address.setChan(section.get("chan", boolean.class)); + } address.setAlias(section.get("label")); identities.add(address); } diff --git a/wif/src/test/java/ch/dissem/bitmessage/wif/WifExporterTest.java b/wif/src/test/java/ch/dissem/bitmessage/wif/WifExporterTest.java index b930c0a..225f2f9 100644 --- a/wif/src/test/java/ch/dissem/bitmessage/wif/WifExporterTest.java +++ b/wif/src/test/java/ch/dissem/bitmessage/wif/WifExporterTest.java @@ -17,6 +17,7 @@ package ch.dissem.bitmessage.wif; import ch.dissem.bitmessage.BitmessageContext; +import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography; import org.junit.Before; @@ -80,9 +81,27 @@ public class WifExporterTest { "noncetrialsperbyte = 320" + System.lineSeparator() + "payloadlengthextrabytes = 14000" + System.lineSeparator() + "privsigningkey = 5KU2gbe9u4rKJ8PHYb1rvwMnZnAJj4gtV5GLwoYckeYzygWUzB9" + System.lineSeparator() + - "privencryptionkey = 5KHd4c6cavd8xv4kzo3PwnVaYuBgEfg7voPQ5V97aZKgpYBXGck" + System.lineSeparator() + System.lineSeparator(); + "privencryptionkey = 5KHd4c6cavd8xv4kzo3PwnVaYuBgEfg7voPQ5V97aZKgpYBXGck" + System.lineSeparator() + + System.lineSeparator(); importer = new WifImporter(ctx, expected); exporter.addIdentity(importer.getIdentities().get(0)); assertEquals(expected, exporter.toString()); } + + @Test + public void ensureChanIsAdded() throws Exception { + String expected = "[BM-2cW67GEKkHGonXKZLCzouLLxnLym3azS8r]" + System.lineSeparator() + + "label = general" + System.lineSeparator() + + "enabled = true" + System.lineSeparator() + + "decoy = false" + System.lineSeparator() + + "chan = true" + System.lineSeparator() + + "noncetrialsperbyte = 1000" + System.lineSeparator() + + "payloadlengthextrabytes = 1000" + System.lineSeparator() + + "privsigningkey = 5Jnbdwc4u4DG9ipJxYLznXSvemkRFueQJNHujAQamtDDoX3N1eQ" + System.lineSeparator() + + "privencryptionkey = 5JrDcFtQDv5ydcHRW6dfGUEvThoxCCLNEUaxQfy8LXXgTJzVAcq" + System.lineSeparator() + + System.lineSeparator(); + BitmessageAddress chan = ctx.joinChan("general", "BM-2cW67GEKkHGonXKZLCzouLLxnLym3azS8r"); + exporter.addIdentity(chan); + assertEquals(expected, exporter.toString()); + } } \ No newline at end of file diff --git a/wif/src/test/java/ch/dissem/bitmessage/wif/WifImporterTest.java b/wif/src/test/java/ch/dissem/bitmessage/wif/WifImporterTest.java index fe8e15c..398ff20 100644 --- a/wif/src/test/java/ch/dissem/bitmessage/wif/WifImporterTest.java +++ b/wif/src/test/java/ch/dissem/bitmessage/wif/WifImporterTest.java @@ -25,9 +25,7 @@ import org.junit.Test; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Matchers.any; +import static org.junit.Assert.*; import static org.mockito.Mockito.*; public class WifImporterTest { @@ -69,6 +67,7 @@ public class WifImporterTest { assertNotNull("Private key", identity.getPrivateKey()); assertEquals(32, identity.getPrivateKey().getPrivateEncryptionKey().length); assertEquals(32, identity.getPrivateKey().getPrivateSigningKey().length); + assertFalse(identity.isChan()); } @Test @@ -98,4 +97,20 @@ public class WifImporterTest { importer.importIdentity(identities.get(0)); verify(repo, times(1)).save(identities.get(0)); } + + @Test + public void ensureChanIsImported() throws Exception { + importer = new WifImporter(ctx, "[BM-2cW67GEKkHGonXKZLCzouLLxnLym3azS8r]\n" + + "label = [chan] general\n" + + "enabled = true\n" + + "decoy = false\n" + + "chan = true\n" + + "noncetrialsperbyte = 1000\n" + + "payloadlengthextrabytes = 1000\n" + + "privsigningkey = 5Jnbdwc4u4DG9ipJxYLznXSvemkRFueQJNHujAQamtDDoX3N1eQ\n" + + "privencryptionkey = 5JrDcFtQDv5ydcHRW6dfGUEvThoxCCLNEUaxQfy8LXXgTJzVAcq\n"); + assertEquals(1, importer.getIdentities().size()); + BitmessageAddress chan = importer.getIdentities().get(0); + assertTrue(chan.isChan()); + } } \ No newline at end of file