From d2b33f73bda7dab7afd167e1dcfa325799bbc8df Mon Sep 17 00:00:00 2001 From: pinpong Date: Sun, 13 Aug 2017 07:27:28 +0200 Subject: [PATCH] refactored --- dependency-reduced-pom.xml | 261 +++++++++--------- pom.xml | 2 +- src/main/java/jota/IotaAPI.java | 31 +-- .../request/IotaAttachToTangleRequest.java | 2 +- .../IotaBroadcastTransactionRequest.java | 2 +- .../request/IotaStoreTransactionsRequest.java | 2 +- src/main/java/jota/pow/JCurl.java | 1 + src/main/java/jota/utils/Converter.java | 8 +- src/main/java/jota/utils/InputValidator.java | 16 +- src/main/java/jota/utils/Multisig.java | 6 +- 10 files changed, 158 insertions(+), 173 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 92d17ba..9f66667 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -1,135 +1,136 @@ - - 4.0.0 - org.iota - jota - JOTA - 0.9.2 - JOTA library is a simple Java wrapper around IOTA Node's JSON-REST HTTP interface. - - - Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - - - - - maven-compiler-plugin - 3.3 - - ${java-version} - ${java-version} - ${java-version} - ${java-version} - - - - maven-shade-plugin - 2.4.3 - - - package - - shade - - - - - - - *:* - - META-INF/*.SF - META-INF/*.DSA - META-INF/*.RSA - - - - - - - maven-surefire-plugin - 2.12.4 - - ${skipTests} - - - - - - - build-extras - + + 4.0.0 + org.iota + jota + JOTA + 0.9.2 + JOTA library is a simple Java wrapper around IOTA Node's JSON-REST HTTP interface. + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + - - maven-source-plugin - - - attach-sources - - jar - - - - - - maven-javadoc-plugin - 2.10.4 - - - attach-javadocs - - jar - - - - - private - true - -Xdoclint:none - - - - maven-gpg-plugin - 1.6 - - - sign-artifacts - package - - sign - - - - + + maven-compiler-plugin + 3.3 + + ${java-version} + ${java-version} + ${java-version} + ${java-version} + + + + maven-shade-plugin + 2.4.3 + + + package + + shade + + + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + maven-surefire-plugin + 2.12.4 + + ${skipTests} + + - - - ${env.GPG_KEYNAME} - ${env.GPG_PASSPHRASE} - - - - - - junit - junit - 4.12 - test - - - hamcrest-core - org.hamcrest - - - - - - true - 1.7 - UTF-8 - + + + + build-extras + + + + maven-source-plugin + + + attach-sources + + jar + + + + + + maven-javadoc-plugin + 2.10.4 + + + attach-javadocs + + jar + + + + + private + true + -Xdoclint:none + + + + maven-gpg-plugin + 1.6 + + + sign-artifacts + package + + sign + + + + + + + + ${env.GPG_KEYNAME} + ${env.GPG_PASSPHRASE} + + + + + + junit + junit + 4.12 + test + + + hamcrest-core + org.hamcrest + + + + + + true + 1.7 + UTF-8 + diff --git a/pom.xml b/pom.xml index b5724d0..48bc214 100644 --- a/pom.xml +++ b/pom.xml @@ -126,7 +126,7 @@ maven-surefire-plugin 2.12.4 - ${skipTests} + ${skipTests} diff --git a/src/main/java/jota/IotaAPI.java b/src/main/java/jota/IotaAPI.java index 7a73930..feecdd9 100644 --- a/src/main/java/jota/IotaAPI.java +++ b/src/main/java/jota/IotaAPI.java @@ -109,8 +109,8 @@ public class IotaAPI extends IotaAPICore { */ public GetTransferResponse getTransfers(String seed, int security, Integer start, Integer end, Boolean inclusionStates) throws ArgumentException, InvalidBundleException, InvalidSignatureException, NoNodeInfoException, NoInclusionStatesException, InvalidSecurityLevelException, InvalidAddressException { StopWatch stopWatch = new StopWatch(); - // validate & if needed pad seed - if ((seed = InputValidator.validateSeed(seed)) == null) { + // validate seed + if ((!InputValidator.isValidSeed(seed))) { throw new IllegalStateException("Invalid Seed"); } @@ -344,14 +344,15 @@ public class IotaAPI extends IotaAPICore { public List prepareTransfers(String seed, int security, final List transfers, String remainder, List inputs) throws NotEnoughBalanceException, InvalidSecurityLevelException, InvalidAddressException, InvalidTransferException { return prepareTransfers(seed, security, transfers, remainder, inputs, true); } + /** * Prepares transfer by generating bundle, finding and signing inputs. * - * @param seed 81-tryte encoded address of recipient. - * @param security The security level of private key / seed. - * @param transfers Array of transfer objects. - * @param remainder If defined, this address will be used for sending the remainder value (of the inputs) to. - * @param inputs The inputs. + * @param seed 81-tryte encoded address of recipient. + * @param security The security level of private key / seed. + * @param transfers Array of transfer objects. + * @param remainder If defined, this address will be used for sending the remainder value (of the inputs) to. + * @param inputs The inputs. * @param validateInputs whether or not to validate the balances of the provided inputs * @return Returns bundle trytes. * @throws InvalidAddressException is thrown when the specified address is not an valid address. @@ -366,8 +367,8 @@ public class IotaAPI extends IotaAPICore { throw new InvalidTransferException(); } - // validate & if needed pad seed - if ((seed = InputValidator.validateSeed(seed)) == null) { + // validate seed + if ((!InputValidator.isValidSeed(seed))) { throw new IllegalStateException("Invalid Seed"); } @@ -438,11 +439,11 @@ public class IotaAPI extends IotaAPICore { // Get inputs if we are sending tokens if (totalValue != 0) { - if(!validateInputs) + if (!validateInputs) return addRemainder(seed, security, inputs, bundle, tag, totalValue, remainder, signatureFragments); // Case 1: user provided inputs // Validate the inputs by calling getBalances - if(!validateInputs) + if (!validateInputs) return addRemainder(seed, security, inputs, bundle, tag, totalValue, remainder, signatureFragments); if (inputs != null && !inputs.isEmpty()) { @@ -524,13 +525,9 @@ public class IotaAPI extends IotaAPICore { **/ public GetBalancesAndFormatResponse getInputs(String seed, int security, int start, int end, long threshold) throws InvalidSecurityLevelException, InvalidAddressException { StopWatch stopWatch = new StopWatch(); - // validate the seed - if (!InputValidator.isTrytes(seed, 0)) { - throw new IllegalStateException("Invalid Seed"); - } - // validate & if needed pad seed - if ((seed = InputValidator.validateSeed(seed)) == null) { + // validate the seed + if ((!InputValidator.isValidSeed(seed))) { throw new IllegalStateException("Invalid Seed"); } diff --git a/src/main/java/jota/dto/request/IotaAttachToTangleRequest.java b/src/main/java/jota/dto/request/IotaAttachToTangleRequest.java index 6e7fe01..5dba481 100644 --- a/src/main/java/jota/dto/request/IotaAttachToTangleRequest.java +++ b/src/main/java/jota/dto/request/IotaAttachToTangleRequest.java @@ -4,7 +4,7 @@ import jota.IotaAPICommands; /** * This class represents the core API request 'attachToTangle'. - * + *

* It is used to attach trytes to the tangle. **/ public class IotaAttachToTangleRequest extends IotaCommandRequest { diff --git a/src/main/java/jota/dto/request/IotaBroadcastTransactionRequest.java b/src/main/java/jota/dto/request/IotaBroadcastTransactionRequest.java index 61d27cd..e09afc0 100644 --- a/src/main/java/jota/dto/request/IotaBroadcastTransactionRequest.java +++ b/src/main/java/jota/dto/request/IotaBroadcastTransactionRequest.java @@ -4,7 +4,7 @@ import jota.IotaAPICommands; /** * This class represents the core API request 'broadcastTransaction'. - * + *

* Broadcast a list of transactions to all neighbors. The input trytes for this call are provided by attachToTangle **/ public class IotaBroadcastTransactionRequest extends IotaCommandRequest { diff --git a/src/main/java/jota/dto/request/IotaStoreTransactionsRequest.java b/src/main/java/jota/dto/request/IotaStoreTransactionsRequest.java index 1662d9a..7e774b7 100644 --- a/src/main/java/jota/dto/request/IotaStoreTransactionsRequest.java +++ b/src/main/java/jota/dto/request/IotaStoreTransactionsRequest.java @@ -4,7 +4,7 @@ import jota.IotaAPICommands; /** * This class represents the core API request 'getTransactionsToApprove'. - * + *

* It stores transactions into the local storage. The trytes to be used for this call are returned by attachToTangle. **/ public class IotaStoreTransactionsRequest extends IotaCommandRequest { diff --git a/src/main/java/jota/pow/JCurl.java b/src/main/java/jota/pow/JCurl.java index ea7640c..e242fed 100644 --- a/src/main/java/jota/pow/JCurl.java +++ b/src/main/java/jota/pow/JCurl.java @@ -43,6 +43,7 @@ public class JCurl implements ICurl { stateLow = null; } } + /** * Absorbs the specified trits. * diff --git a/src/main/java/jota/utils/Converter.java b/src/main/java/jota/utils/Converter.java index 69f4a33..f24fb51 100644 --- a/src/main/java/jota/utils/Converter.java +++ b/src/main/java/jota/utils/Converter.java @@ -9,21 +9,20 @@ import java.util.List; */ public class Converter { + public static final int HIGH_INTEGER_BITS = 0xFFFFFFFF; + public static final long HIGH_LONG_BITS = 0xFFFFFFFFFFFFFFFFL; /** * The radix */ private static final int RADIX = 3; - /** * The maximum trit value */ private static final int MAX_TRIT_VALUE = (RADIX - 1) / 2, MIN_TRIT_VALUE = -MAX_TRIT_VALUE; - /** * The number of trits in a byte */ private static final int NUMBER_OF_TRITS_IN_A_BYTE = 5; - /** * The number of trits in a tryte */ @@ -31,9 +30,6 @@ public class Converter { private static final int[][] BYTE_TO_TRITS_MAPPINGS = new int[243][]; private static final int[][] TRYTE_TO_TRITS_MAPPINGS = new int[27][]; - public static final int HIGH_INTEGER_BITS = 0xFFFFFFFF; - public static final long HIGH_LONG_BITS = 0xFFFFFFFFFFFFFFFFL; - static { final int[] trits = new int[NUMBER_OF_TRITS_IN_A_BYTE]; diff --git a/src/main/java/jota/utils/InputValidator.java b/src/main/java/jota/utils/InputValidator.java index 7d1686e..8badaf8 100644 --- a/src/main/java/jota/utils/InputValidator.java +++ b/src/main/java/jota/utils/InputValidator.java @@ -2,7 +2,6 @@ package jota.utils; import jota.error.InvalidAddressException; import jota.model.Transfer; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import java.util.List; @@ -153,18 +152,9 @@ public class InputValidator { * Checks if the seed is valid. If not, an exception is thrown. * * @param seed The seed to validate. - * @return The validated seed. - * @throws IllegalStateException Format not in trytes or Invalid Seed: Seed too long. + * @return true if the specified seed is valid; otherwise, false. **/ - public static String validateSeed(String seed) { - if (seed.length() > 81) - throw new IllegalStateException("Invalid Seed: Seed too long"); - - if (!isTrytes(seed, seed.length())) - throw new IllegalStateException("Invalid Seed: Format not in trytes"); - - seed = StringUtils.rightPad(seed, 81, '9'); - - return seed; + public static boolean isValidSeed(String seed) { + return isTrytes(seed, seed.length()); } } diff --git a/src/main/java/jota/utils/Multisig.java b/src/main/java/jota/utils/Multisig.java index 35a8cf2..7a185d2 100644 --- a/src/main/java/jota/utils/Multisig.java +++ b/src/main/java/jota/utils/Multisig.java @@ -31,9 +31,9 @@ public class Multisig { } /** - * @param seed Tryte-encoded seed. It should be noted that this seed is not transferred. + * @param seed Tryte-encoded seed. It should be noted that this seed is not transferred. * @param security Secuirty level of private key / seed. - * @param index Key index to start search from. If the index is provided, the generation of the address is not deterministic. + * @param index Key index to start search from. If the index is provided, the generation of the address is not deterministic. * @return trytes **/ public String getDigest(String seed, int security, int index) { @@ -70,7 +70,7 @@ public class Multisig { /** * Gets the key value of a seed * - * @param seed Tryte-encoded seed. It should be noted that this seed is not transferred + * @param seed Tryte-encoded seed. It should be noted that this seed is not transferred * @param index Key index to start search from. If the index is provided, the generation of the address is not deterministic. * @return trytes. **/