mirror of
https://github.com/gosticks/iota.lib.java.git
synced 2025-10-16 11:45:37 +00:00
refactored
This commit is contained in:
parent
e0dc12681f
commit
8d07e0ba60
@ -108,7 +108,7 @@ public class IotaAPI extends IotaAPICore {
|
|||||||
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
|
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
|
||||||
*/
|
*/
|
||||||
public GetTransferResponse getTransfers(String seed, int security, Integer start, Integer end, Boolean inclusionStates) throws ArgumentException, InvalidBundleException, InvalidSignatureException, NoNodeInfoException, NoInclusionStatesException, InvalidSecurityLevelException, InvalidAddressException {
|
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 seed
|
// validate seed
|
||||||
if ((!InputValidator.isValidSeed(seed))) {
|
if ((!InputValidator.isValidSeed(seed))) {
|
||||||
throw new IllegalStateException("Invalid Seed");
|
throw new IllegalStateException("Invalid Seed");
|
||||||
@ -118,12 +118,12 @@ public class IotaAPI extends IotaAPICore {
|
|||||||
throw new InvalidSecurityLevelException();
|
throw new InvalidSecurityLevelException();
|
||||||
}
|
}
|
||||||
|
|
||||||
start = start == null ? 0 : start;
|
|
||||||
|
|
||||||
if (start > end || end > (start + 500)) {
|
if (start > end || end > (start + 500)) {
|
||||||
throw new ArgumentException();
|
throw new ArgumentException("Invalid inputs provided");
|
||||||
}
|
}
|
||||||
StopWatch sw = new StopWatch();
|
|
||||||
|
StopWatch stopWatch = new StopWatch();
|
||||||
|
|
||||||
GetNewAddressResponse gnr = getNewAddress(seed, security, start, false, end, true);
|
GetNewAddressResponse gnr = getNewAddress(seed, security, start, false, end, true);
|
||||||
if (gnr != null && gnr.getAddresses() != null) {
|
if (gnr != null && gnr.getAddresses() != null) {
|
||||||
@ -510,7 +510,6 @@ public class IotaAPI extends IotaAPICore {
|
|||||||
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
|
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
|
||||||
**/
|
**/
|
||||||
public GetBalancesAndFormatResponse getInputs(String seed, int security, int start, int end, long threshold) throws InvalidSecurityLevelException, InvalidAddressException {
|
public GetBalancesAndFormatResponse getInputs(String seed, int security, int start, int end, long threshold) throws InvalidSecurityLevelException, InvalidAddressException {
|
||||||
StopWatch stopWatch = new StopWatch();
|
|
||||||
|
|
||||||
// validate the seed
|
// validate the seed
|
||||||
if ((!InputValidator.isValidSeed(seed))) {
|
if ((!InputValidator.isValidSeed(seed))) {
|
||||||
@ -527,6 +526,8 @@ public class IotaAPI extends IotaAPICore {
|
|||||||
throw new IllegalStateException("Invalid inputs provided");
|
throw new IllegalStateException("Invalid inputs provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StopWatch stopWatch = new StopWatch();
|
||||||
|
|
||||||
// Case 1: start and end
|
// Case 1: start and end
|
||||||
//
|
//
|
||||||
// If start and end is defined by the user, simply iterate through the keys
|
// If start and end is defined by the user, simply iterate through the keys
|
||||||
@ -541,7 +542,7 @@ public class IotaAPI extends IotaAPICore {
|
|||||||
allAddresses.add(address);
|
allAddresses.add(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
return getBalanceAndFormat(allAddresses, threshold, start, end, stopWatch, security);
|
return getBalanceAndFormat(allAddresses, threshold, start, stopWatch, security);
|
||||||
}
|
}
|
||||||
// Case 2: iterate till threshold || end
|
// Case 2: iterate till threshold || end
|
||||||
//
|
//
|
||||||
@ -550,7 +551,7 @@ public class IotaAPI extends IotaAPICore {
|
|||||||
// We then do getBalance, format the output and return it
|
// We then do getBalance, format the output and return it
|
||||||
else {
|
else {
|
||||||
final GetNewAddressResponse res = getNewAddress(seed, security, start, false, 0, true);
|
final GetNewAddressResponse res = getNewAddress(seed, security, start, false, 0, true);
|
||||||
return getBalanceAndFormat(res.getAddresses(), threshold, start, end, stopWatch, security);
|
return getBalanceAndFormat(res.getAddresses(), threshold, start, stopWatch, security);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -560,13 +561,12 @@ public class IotaAPI extends IotaAPICore {
|
|||||||
* @param addresses The addresses.
|
* @param addresses The addresses.
|
||||||
* @param threshold Min balance required.
|
* @param threshold Min balance required.
|
||||||
* @param start Starting key index.
|
* @param start Starting key index.
|
||||||
* @param end Ending key index.
|
|
||||||
* @param stopWatch the stopwatch.
|
* @param stopWatch the stopwatch.
|
||||||
* @param security The security level of private key / seed.
|
* @param security The security level of private key / seed.
|
||||||
* @return Inputs object.
|
* @return Inputs object.
|
||||||
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
|
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
|
||||||
**/
|
**/
|
||||||
public GetBalancesAndFormatResponse getBalanceAndFormat(final List<String> addresses, long threshold, int start, int end, StopWatch stopWatch, int security) throws InvalidSecurityLevelException {
|
public GetBalancesAndFormatResponse getBalanceAndFormat(final List<String> addresses, long threshold, int start, StopWatch stopWatch, int security) throws InvalidSecurityLevelException {
|
||||||
|
|
||||||
if (security < 1) {
|
if (security < 1) {
|
||||||
throw new InvalidSecurityLevelException();
|
throw new InvalidSecurityLevelException();
|
||||||
@ -618,13 +618,14 @@ public class IotaAPI extends IotaAPICore {
|
|||||||
* @throws InvalidSignatureException is thrown when an invalid signature is encountered.
|
* @throws InvalidSignatureException is thrown when an invalid signature is encountered.
|
||||||
*/
|
*/
|
||||||
public GetBundleResponse getBundle(String transaction) throws ArgumentException, InvalidBundleException, InvalidSignatureException {
|
public GetBundleResponse getBundle(String transaction) throws ArgumentException, InvalidBundleException, InvalidSignatureException {
|
||||||
StopWatch stopWatch = new StopWatch();
|
|
||||||
|
|
||||||
Bundle bundle = traverseBundle(transaction, null, new Bundle());
|
Bundle bundle = traverseBundle(transaction, null, new Bundle());
|
||||||
if (bundle == null) {
|
if (bundle == null) {
|
||||||
throw new ArgumentException("Unknown Bundle");
|
throw new ArgumentException("Unknown Bundle");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StopWatch stopWatch = new StopWatch();
|
||||||
|
|
||||||
long totalSum = 0;
|
long totalSum = 0;
|
||||||
String bundleHash = bundle.getTransactions().get(0).getBundle();
|
String bundleHash = bundle.getTransactions().get(0).getBundle();
|
||||||
|
|
||||||
@ -718,6 +719,10 @@ public class IotaAPI extends IotaAPICore {
|
|||||||
throws InvalidBundleException, ArgumentException, InvalidSignatureException,
|
throws InvalidBundleException, ArgumentException, InvalidSignatureException,
|
||||||
InvalidTrytesException, InvalidSecurityLevelException, InvalidAddressException, NoInclusionStatesException, NoNodeInfoException {
|
InvalidTrytesException, InvalidSecurityLevelException, InvalidAddressException, NoInclusionStatesException, NoNodeInfoException {
|
||||||
|
|
||||||
|
if (start > end || end > (start + 1000)) {
|
||||||
|
throw new ArgumentException("Invalid inputs provided");
|
||||||
|
}
|
||||||
|
|
||||||
StopWatch stopWatch = new StopWatch();
|
StopWatch stopWatch = new StopWatch();
|
||||||
|
|
||||||
GetNewAddressResponse gna = getNewAddress(seed, security, index, checksum, total, returnAll);
|
GetNewAddressResponse gna = getNewAddress(seed, security, index, checksum, total, returnAll);
|
||||||
@ -1044,29 +1049,7 @@ public class IotaAPI extends IotaAPICore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param hash The hash.
|
|
||||||
* @return A bundle.
|
|
||||||
* @throws ArgumentException is thrown when an invalid argument is provided.
|
|
||||||
*/
|
|
||||||
public String findTailTransactionHash(String hash) throws ArgumentException {
|
|
||||||
GetTrytesResponse gtr = getTrytes(hash);
|
|
||||||
|
|
||||||
if (gtr == null) throw new ArgumentException("Invalid hash");
|
|
||||||
|
|
||||||
if (gtr.getTrytes().length == 0) {
|
|
||||||
throw new ArgumentException("Bundle transactions not visible");
|
|
||||||
}
|
|
||||||
|
|
||||||
Transaction trx = new Transaction(gtr.getTrytes()[0], customCurl.clone());
|
|
||||||
if (trx.getBundle() == null) {
|
|
||||||
throw new ArgumentException("Invalid trytes, could not create object");
|
|
||||||
}
|
|
||||||
if (trx.getCurrentIndex() == 0) return trx.getHash();
|
|
||||||
else return findTailTransactionHash(trx.getBundle());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param seed Tryte-encoded seed.
|
* @param seed Tryte-encoded seed.
|
||||||
* @param security The security level of private key / seed.
|
* @param security The security level of private key / seed.
|
||||||
|
|||||||
@ -123,14 +123,14 @@ public class Bundle implements Comparable<Bundle> {
|
|||||||
normalizedBundleValue = normalizedBundle(hashInTrytes);
|
normalizedBundleValue = normalizedBundle(hashInTrytes);
|
||||||
|
|
||||||
boolean foundValue = false;
|
boolean foundValue = false;
|
||||||
for (int i = 0; i < normalizedBundleValue.length; i++) {
|
for (int aNormalizedBundleValue : normalizedBundleValue) {
|
||||||
if (normalizedBundleValue[i] == 13) {
|
if (aNormalizedBundleValue == 13) {
|
||||||
foundValue = true;
|
foundValue = true;
|
||||||
obsoleteTagTrits = Converter.trits(this.getTransactions().get(0).getObsoleteTag());
|
obsoleteTagTrits = Converter.trits(this.getTransactions().get(0).getObsoleteTag());
|
||||||
Converter.increment(obsoleteTagTrits, 81);
|
Converter.increment(obsoleteTagTrits, 81);
|
||||||
this.getTransactions().get(0).setObsoleteTag(Converter.trytes(obsoleteTagTrits));
|
this.getTransactions().get(0).setObsoleteTag(Converter.trytes(obsoleteTagTrits));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
valid = !foundValue;
|
valid = !foundValue;
|
||||||
|
|
||||||
} while (!valid);
|
} while (!valid);
|
||||||
@ -148,7 +148,7 @@ public class Bundle implements Comparable<Bundle> {
|
|||||||
public void addTrytes(List<String> signatureFragments) {
|
public void addTrytes(List<String> signatureFragments) {
|
||||||
String emptySignatureFragment = "";
|
String emptySignatureFragment = "";
|
||||||
String emptyHash = EMPTY_HASH;
|
String emptyHash = EMPTY_HASH;
|
||||||
long emptyTimestamp = 999999999l;
|
long emptyTimestamp = 999999999L;
|
||||||
|
|
||||||
emptySignatureFragment = StringUtils.rightPad(emptySignatureFragment, 2187, '9');
|
emptySignatureFragment = StringUtils.rightPad(emptySignatureFragment, 2187, '9');
|
||||||
|
|
||||||
|
|||||||
@ -423,7 +423,7 @@ public class Transaction {
|
|||||||
|
|
||||||
this.tag = this.tag != null && !this.tag.isEmpty() ? this.tag : this.obsoleteTag;
|
this.tag = this.tag != null && !this.tag.isEmpty() ? this.tag : this.obsoleteTag;
|
||||||
|
|
||||||
String trx = this.getSignatureFragments()
|
return this.getSignatureFragments()
|
||||||
+ this.getAddress()
|
+ this.getAddress()
|
||||||
+ Converter.trytes(valueTrits)
|
+ Converter.trytes(valueTrits)
|
||||||
+ this.getObsoleteTag()
|
+ this.getObsoleteTag()
|
||||||
@ -438,7 +438,6 @@ public class Transaction {
|
|||||||
+ Converter.trytes(attachmentTimestampLowerBoundTrits)
|
+ Converter.trytes(attachmentTimestampLowerBoundTrits)
|
||||||
+ Converter.trytes(attachmentTimestampUpperBoundTrits)
|
+ Converter.trytes(attachmentTimestampUpperBoundTrits)
|
||||||
+ this.getNonce();
|
+ this.getNonce();
|
||||||
return trx;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -40,8 +40,8 @@ public class Kerl extends JCurl {
|
|||||||
|
|
||||||
private static int sum(int[] toSum) {
|
private static int sum(int[] toSum) {
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (int i = 0; i < toSum.length; i++) {
|
for (int aToSum : toSum) {
|
||||||
sum += toSum[i];
|
sum += aToSum;
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -90,7 +90,6 @@ public class Checksum {
|
|||||||
int[] checksumTrits = new int[JCurl.HASH_LENGTH];
|
int[] checksumTrits = new int[JCurl.HASH_LENGTH];
|
||||||
curl.squeeze(checksumTrits);
|
curl.squeeze(checksumTrits);
|
||||||
String checksum = Converter.trytes(checksumTrits);
|
String checksum = Converter.trytes(checksumTrits);
|
||||||
String checksumPrt = checksum.substring(72, 81);
|
return checksum.substring(72, 81);
|
||||||
return checksumPrt;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user