From b9b3b5d82852d82797ddc77b65d3ce2fb75b7831 Mon Sep 17 00:00:00 2001 From: AZ Date: Fri, 6 Jan 2017 15:06:11 +0100 Subject: [PATCH] validate seed & if needed pad to 81 chars --- node_config.properties | 6 ++-- src/main/java/jota/IotaAPIProxy.java | 37 +++++++++++++++++--- src/main/java/jota/utils/InputValidator.java | 28 +++++++++------ 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/node_config.properties b/node_config.properties index e5e5a5a..fd80f02 100644 --- a/node_config.properties +++ b/node_config.properties @@ -1,5 +1,7 @@ iota.node.protocol=http -#iota.node.host=138.68.126.141 -iota.node.host=node.iotawallet.info +#iota.node.host=node.iotawallet.info +#iota.node.host=138.68.90.186 +#iota.node.host=192.168.11.2 +iota.node.host=138.68.90.186 iota.node.port=14265 diff --git a/src/main/java/jota/IotaAPIProxy.java b/src/main/java/jota/IotaAPIProxy.java index 6963577..b1b6ad8 100644 --- a/src/main/java/jota/IotaAPIProxy.java +++ b/src/main/java/jota/IotaAPIProxy.java @@ -13,6 +13,7 @@ import jota.utils.IotaAPIUtils; import jota.utils.Signing; import okhttp3.OkHttpClient; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.StopWatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import retrofit2.Call; @@ -259,6 +260,11 @@ public class IotaAPIProxy { * @returns {object} success **/ public GetTransferResponse getTransfers(String seed, Integer start, Integer end, Boolean inclusionStates) throws ArgumentException, InvalidBundleException, InvalidSignatureException { + // validate & if needed pad seed + if ( (seed = InputValidator.validateSeed(seed)) == null) { + throw new IllegalStateException("Invalid Seed"); + } + start = start != null ? 0 : start; end = end == null ? null : end; inclusionStates = inclusionStates != null ? inclusionStates : null; @@ -266,12 +272,19 @@ public class IotaAPIProxy { if (start > end || end > (start + 500)) { throw new ArgumentException(); } - + StopWatch sw = new StopWatch(); + sw.start(); + System.out.println("GetTransfer started"); GetNewAddressResponse gnr = getNewAddress(seed, start, false, end == null ? end - start : end, true); if (gnr != null && gnr.getAddresses() != null) { + System.out.println("GetTransfers after getNewAddresses " + sw.getTime() + " ms"); Bundle[] bundles = bundlesFromAddresses(gnr.getAddresses().toArray(new String[gnr.getAddresses().size()]), inclusionStates); + System.out.println("GetTransfers after bundlesFromAddresses " + sw.getTime() + " ms"); + sw.stop(); + return GetTransferResponse.create(bundles); } + sw.stop(); return null; } @@ -310,7 +323,11 @@ public class IotaAPIProxy { // of the tail transactions, and thus the bundles GetInclusionStateResponse gisr = null; if (inclusionStates) { - gisr = getLatestInclusion(tailTxArray); + try { + gisr = getLatestInclusion(tailTxArray); + } catch (IllegalAccessError e) { + + } if (gisr == null || gisr.getStates() == null || gisr.getStates().length == 0) return null; } for (String trx : tailTxArray) { @@ -473,13 +490,19 @@ public class IotaAPIProxy { * @property {string} address Remainder address * @returns {array} trytes Returns bundle trytes **/ - public List prepareTransfers(final String seed, final List transfers, String remainder, List inputs) { + public List prepareTransfers(String seed, final List transfers, String remainder, List inputs) { // Input validation of transfers object if (!InputValidator.isTransfersCollectionCorrect(transfers)) { throw new IllegalStateException("Invalid Transfer"); } + // validate & if needed pad seed + if ( (seed = InputValidator.validateSeed(seed)) == null) { + throw new IllegalStateException("Invalid Seed"); + } + + // Create a new bundle final Bundle bundle = new Bundle(); final List signatureFragments = new ArrayList<>(); @@ -619,13 +642,18 @@ public class IotaAPIProxy { * @property {int} end Ending key index * @property {int} threshold Min balance required **/ - public GetBalancesAndFormatResponse getInputs(final String seed, final List balances, int start, int end, int threshold) { + public GetBalancesAndFormatResponse getInputs(String seed, final List balances, int start, int end, int threshold) { // validate the seed if (!InputValidator.isTrytes(seed, 0)) { throw new IllegalStateException("Invalid Seed"); } + // validate & if needed pad seed + if ( (seed = InputValidator.validateSeed(seed)) == null) { + throw new IllegalStateException("Invalid Seed"); + } + // If start value bigger than end, return error // or if difference between end and start is bigger than 500 keys if (start > end || end > (start + 500)) { @@ -913,6 +941,7 @@ public class IotaAPIProxy { final long totalValue, final String remainderAddress, final List signatureFragments) { + for (int i = 0; i < inputs.size(); i++) { long thisBalance = inputs.get(i).getBalance(); long totalTransferValue = totalValue; diff --git a/src/main/java/jota/utils/InputValidator.java b/src/main/java/jota/utils/InputValidator.java index 90392e7..b636bb5 100644 --- a/src/main/java/jota/utils/InputValidator.java +++ b/src/main/java/jota/utils/InputValidator.java @@ -28,7 +28,7 @@ public class InputValidator { public static boolean isTrytes(final String trytes, final int length) { return trytes.matches("^[A-Z9]{" + (length == 0 ? "0," : length) + "}$"); } - + public static boolean isValue(final String value) { return NumberUtils.isNumber(value); } @@ -51,16 +51,16 @@ public class InputValidator { } return true; } - + /** - * checks if input is correct hash collections - * - * @method isTransfersArray - * @param {array} hash - * @returns {boolean} - **/ + * checks if input is correct hash collections + * + * @param {array} hash + * @method isTransfersArray + * @returns {boolean} + **/ public static boolean isTransfersCollectionCorrect(final List transfers) { - + for (final Transfer transfer : transfers) { if (!isTransfersArray(transfer)) { return false; @@ -68,9 +68,9 @@ public class InputValidator { } return true; } - + public static boolean isTransfersArray(final Transfer transfer) { - + if (!isAddress(transfer.getAddress())) { return false; } @@ -87,4 +87,10 @@ public class InputValidator { return true; } + + public static String validateSeed(String seed) { + if (seed.length() > 81) return null; + while (seed.length() < 81) seed += 9; + return seed; + } }