22
33import jota .dto .request .*;
44import jota .dto .response .*;
5- import jota .error .InvalidApiVersionException ;
6- import jota .error .InvalidTrytesException ;
5+ import jota .error .ArgumentException ;
76import jota .model .Transaction ;
7+ import jota .utils .Checksum ;
88import jota .utils .InputValidator ;
99import okhttp3 .Interceptor ;
1010import okhttp3 .OkHttpClient ;
1919import java .io .BufferedReader ;
2020import java .io .FileReader ;
2121import java .io .IOException ;
22+ import java .util .ArrayList ;
2223import java .util .List ;
2324import java .util .Properties ;
2425import 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 }
0 commit comments