mirror of
https://github.com/gosticks/iota.lib.java.git
synced 2026-10-04 05:26:59 +00:00
validate seed & if needed pad to 81 chars
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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<String> prepareTransfers(final String seed, final List<Transfer> transfers, String remainder, List<Input> inputs) {
|
||||
public List<String> prepareTransfers(String seed, final List<Transfer> transfers, String remainder, List<Input> 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<String> 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<String> balances, int start, int end, int threshold) {
|
||||
public GetBalancesAndFormatResponse getInputs(String seed, final List<String> 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<String> signatureFragments) {
|
||||
|
||||
for (int i = 0; i < inputs.size(); i++) {
|
||||
long thisBalance = inputs.get(i).getBalance();
|
||||
long totalTransferValue = totalValue;
|
||||
|
||||
@@ -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<Transfer> 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user