Skip to content

Commit 422ad40

Browse files
committed
restructured
1 parent 0ff9b44 commit 422ad40

26 files changed

Lines changed: 328 additions & 408 deletions

‎jota/src/main/java/jota/IotaAPI.java‎

Lines changed: 113 additions & 103 deletions
Large diffs are not rendered by default.

‎jota/src/main/java/jota/IotaAPICore.java‎

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import jota.dto.request.*;
44
import jota.dto.response.*;
5-
import jota.error.InvalidApiVersionException;
6-
import jota.error.InvalidTrytesException;
5+
import jota.error.ArgumentException;
76
import jota.model.Transaction;
7+
import jota.utils.Checksum;
88
import jota.utils.InputValidator;
99
import okhttp3.Interceptor;
1010
import okhttp3.OkHttpClient;
@@ -19,6 +19,7 @@
1919
import java.io.BufferedReader;
2020
import java.io.FileReader;
2121
import java.io.IOException;
22+
import java.util.ArrayList;
2223
import java.util.List;
2324
import java.util.Properties;
2425
import java.util.concurrent.TimeUnit;
@@ -57,20 +58,23 @@ protected static <T> Response<T> wrapCheckedException(final Call<T> call) {
5758
try {
5859
final Response<T> res = call.execute();
5960

61+
String error = "";
62+
63+
if (res.errorBody() != null) {
64+
error = res.errorBody().string();
65+
}
66+
6067
if (res.code() == 400) {
61-
if (res.errorBody().string().contains("Invalid API Version")) {
62-
try {
63-
throw new InvalidApiVersionException();
64-
} catch (InvalidApiVersionException e) {
65-
e.printStackTrace();
66-
}
67-
} else {
68-
throw new IllegalAccessError("400 " + res.errorBody().string());
68+
try {
69+
throw new ArgumentException(error);
70+
} catch (ArgumentException e) {
71+
e.printStackTrace();
6972
}
73+
7074
} else if (res.code() == 401) {
71-
throw new IllegalAccessError("401 " + res.errorBody().string());
75+
throw new IllegalAccessError("401 " + error);
7276
} else if (res.code() == 500) {
73-
throw new IllegalAccessError("500 " + res.errorBody().string());
77+
throw new IllegalAccessError("500 " + error);
7478
}
7579
return res;
7680
} catch (IOException e) {
@@ -203,8 +207,15 @@ public FindTransactionResponse findTransactions(String[] addresses, String[] tag
203207
* @param addresses A List of addresses.
204208
* @return The transaction hashes which are returned depend on the input.
205209
*/
206-
public FindTransactionResponse findTransactionsByAddresses(final String... addresses) {
207-
return findTransactions(addresses, null, null, null);
210+
public FindTransactionResponse findTransactionsByAddresses(final String... addresses) throws ArgumentException {
211+
List<String> addressesWithoutChecksum = new ArrayList<>();
212+
213+
for (String address : addresses) {
214+
String addressO = Checksum.removeChecksum(address);
215+
addressesWithoutChecksum.add(addressO);
216+
}
217+
218+
return findTransactions(addressesWithoutChecksum.toArray(new String[addressesWithoutChecksum.size()]), null, null, null);
208219
}
209220

210221
/**
@@ -247,7 +258,17 @@ public FindTransactionResponse findTransactionsByDigests(final String... digests
247258
* @param tips ThelList of tips (including milestones) you want to search for the inclusion state.
248259
* @return The inclusion states of a set of transactions.
249260
*/
250-
public GetInclusionStateResponse getInclusionStates(String[] transactions, String[] tips) {
261+
public GetInclusionStateResponse getInclusionStates(String[] transactions, String[] tips) throws ArgumentException {
262+
263+
if (!InputValidator.isArrayOfHashes(transactions)) {
264+
throw new ArgumentException("Invalid hash provided");
265+
}
266+
267+
if (!InputValidator.isArrayOfHashes(tips)) {
268+
throw new ArgumentException("Invalid hash provided");
269+
}
270+
271+
251272
final Call<GetInclusionStateResponse> res = service.getInclusionStates(IotaGetInclusionStateRequest
252273
.createGetInclusionStateRequest(transactions, tips));
253274
return wrapCheckedException(res).body();
@@ -259,12 +280,16 @@ public GetInclusionStateResponse getInclusionStates(String[] transactions, Strin
259280
* @param hashes The of transaction hashes of which you want to get trytes from.
260281
* @return The the raw transaction data (trytes) of a specific transaction.
261282
*/
262-
public GetTrytesResponse getTrytes(String... hashes) {
283+
public GetTrytesResponse getTrytes(String... hashes) throws ArgumentException {
284+
285+
if (!InputValidator.isArrayOfHashes(hashes)) {
286+
throw new ArgumentException("Invalid hash provided");
287+
}
288+
263289
final Call<GetTrytesResponse> res = service.getTrytes(IotaGetTrytesRequest.createGetTrytesRequest(hashes));
264290
return wrapCheckedException(res).body();
265291
}
266292

267-
268293
/**
269294
* Tip selection which returns trunkTransaction and branchTransaction. The input value is the latest coordinator milestone, as provided through the getNodeInfo API call.
270295
*
@@ -283,7 +308,7 @@ public GetTransactionsToApproveResponse getTransactionsToApprove(Integer depth)
283308
* @param addresses The array list of addresses you want to get the confirmed balance from.
284309
* @return The confirmed balance which a list of addresses have at the latest confirmed milestone.
285310
*/
286-
public GetBalancesResponse getBalances(Integer threshold, String[] addresses) {
311+
private GetBalancesResponse getBalances(Integer threshold, String[] addresses) {
287312
final Call<GetBalancesResponse> res = service.getBalances(IotaGetBalancesRequest.createIotaGetBalancesRequest(threshold, addresses));
288313
return wrapCheckedException(res).body();
289314
}
@@ -295,8 +320,15 @@ public GetBalancesResponse getBalances(Integer threshold, String[] addresses) {
295320
* @param addresses The list of addresses you want to get the confirmed balance from.
296321
* @return The confirmed balance which a list of addresses have at the latest confirmed milestone.
297322
*/
298-
public GetBalancesResponse getBalances(Integer threshold, List<String> addresses) {
299-
return getBalances(threshold, addresses.toArray(new String[]{}));
323+
public GetBalancesResponse getBalances(Integer threshold, List<String> addresses) throws ArgumentException {
324+
325+
List<String> addressesWithoutChecksum = new ArrayList<>();
326+
327+
for (String address : addresses) {
328+
String addressO = Checksum.removeChecksum(address);
329+
addressesWithoutChecksum.add(addressO);
330+
}
331+
return getBalances(threshold, addressesWithoutChecksum.toArray(new String[]{}));
300332
}
301333

302334
/**
@@ -307,9 +339,18 @@ public GetBalancesResponse getBalances(Integer threshold, List<String> addresses
307339
* @param minWeightMagnitude The Proof of Work intensity.
308340
* @param trytes A List of trytes (raw transaction data) to attach to the tangle.
309341
*/
310-
public GetAttachToTangleResponse attachToTangle(String trunkTransaction, String branchTransaction, Integer minWeightMagnitude, String... trytes) throws InvalidTrytesException {
342+
public GetAttachToTangleResponse attachToTangle(String trunkTransaction, String branchTransaction, Integer minWeightMagnitude, String... trytes) throws ArgumentException {
343+
344+
if (!InputValidator.isHash(trunkTransaction)) {
345+
throw new ArgumentException("Invalid hash provided");
346+
}
347+
348+
if (!InputValidator.isHash(branchTransaction)) {
349+
throw new ArgumentException("Invalid hash provided");
350+
}
351+
311352
if (!InputValidator.isArrayOfTrytes(trytes)) {
312-
throw new InvalidTrytesException();
353+
throw new ArgumentException("Invalid trytes provided");
313354
}
314355

315356
if (localPoW != null) {
@@ -347,7 +388,12 @@ public InterruptAttachingToTangleResponse interruptAttachingToTangle() {
347388
*
348389
* @param trytes The list of raw data of transactions to be rebroadcast.
349390
*/
350-
public BroadcastTransactionsResponse broadcastTransactions(String... trytes) {
391+
public BroadcastTransactionsResponse broadcastTransactions(String... trytes) throws ArgumentException {
392+
393+
if (!InputValidator.isArrayOfAttachedTrytes(trytes)) {
394+
throw new ArgumentException("Invalid trytes provided");
395+
}
396+
351397
final Call<BroadcastTransactionsResponse> res = service.broadcastTransactions(IotaBroadcastTransactionRequest.createBroadcastTransactionsRequest(trytes));
352398
return wrapCheckedException(res).body();
353399
}

‎jota/src/main/java/jota/error/BroadcastAndStoreException.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎jota/src/main/java/jota/error/InvalidAddressException.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎jota/src/main/java/jota/error/InvalidApiVersionException.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎jota/src/main/java/jota/error/InvalidBundleException.java‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎jota/src/main/java/jota/error/InvalidSecurityLevelException.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎jota/src/main/java/jota/error/InvalidSignatureException.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎jota/src/main/java/jota/error/InvalidTransferException.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎jota/src/main/java/jota/error/InvalidTrytesException.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)