Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit f3200c8

Browse files
authored
Adds Codacy ruleset (#129)
* Added codacity ruleset * Added badge * Fixed iri compatibility badge * updated iota logo * first codacy errors fixed * Fixed test errors * fixed merge errors
1 parent 6c8be82 commit f3200c8

File tree

5 files changed

+146
-58
lines changed

5 files changed

+146
-58
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
![IOTA Logo][iota-logo]
22

33
# IOTA Java Library
4-
4+
[![Codacy Badge][codacy-badge]][codacy]
55
[![JitPack][jitpack-badge]][jitpack]
66
[![Build Status][travis-badge]][travis]
77
[![License][license-badge]][license]
@@ -36,7 +36,7 @@ All the boilerplate code for connecting to the node REST interface has been hidd
3636
1. [Disclaimer](#disclaimer)
3737
1. [Supporting the project](#supporting-the-project)
3838
1. [Join the Discussion](#join-the-discussion)
39-
1. [License](#license)
39+
1. [License](codacity#license)
4040

4141
## Installation
4242

@@ -279,17 +279,19 @@ See the License for the specific language governing permissions and
279279
limitations under the License.
280280
```
281281

282-
[iota-logo]: https://raw.githubusercontent.com/iotaledger/documentation/master/source/images/iota-logo.png
282+
[iota-logo]: https://avatars0.githubusercontent.com/u/20126597?s=200&v=4
283+
[codacy]: https://www.codacy.com/app/kwek20/iota-java?utm_source=github.com&utm_medium=referral&utm_content=iotaledger/iota-java&utm_campaign=Badge_Grade
284+
[codacy-badge]: https://api.codacy.com/project/badge/Grade/92feea51a15c4e589386c269475b8761
283285
[jitpack]: https://jitpack.io/#iotaledger/iota-java
284286
[jitpack-badge]: https://jitpack.io/v/iotaledger/iota-java.svg
285287
[travis]: https://travis-ci.org/iotaledger/iota-java
286288
[travis-badge]: https://travis-ci.org/iotaledger/iota-java.svg?branch=master
287289
[license]: https://github.com/iotaledger/iota-java/blob/master/LICENSE
288290
[license-badge]: https://img.shields.io/github/license/iotaledger/iota-java.svg
289-
[iota-iri]: https://github.com/iotaledger/iri/tree/v1.5.0
290-
[iota-iri-badge]: https://img.shields.io/badge/IOTA%20IRI%20compatibility-v1.5.0-blue.svg
291+
[iota-iri]: https://github.com/iotaledger/iri/tree/v1.5.5
292+
[iota-iri-badge]: https://img.shields.io/badge/IOTA%20IRI%20compatibility-v1.5.5-blue.svg
291293
[iota-api]: https://iota.readme.io/reference
292-
[iota-api-badge]: https://img.shields.io/badge/IOTA%20API%20coverage-14/15%20commands-red.svg
294+
[iota-api-badge]: https://img.shields.io/badge/IOTA%20API%20coverage-15/15%20commands-green.svg
293295
[javadoc]: https://iotaledger.github.io/iota-java/javadoc/
294296
[issues]: https://github.com/iotaledger/iota-java/issues
295297
[new-issue]: https://github.com/iotaledger/iota-java/issues/new

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

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import java.util.*;
1616
import java.util.stream.Collectors;
1717

18-
import static jota.utils.Constants.*;
19-
2018

2119
/**
2220
* IotaAPI Builder. Usage:
@@ -139,7 +137,7 @@ public GetNewAddressResponse generateNewAddresses(String seed, int security, boo
139137
*/
140138
public GetNewAddressResponse generateNewAddresses(String seed, int security, boolean checksum, int index, int amount, boolean addSpendAddresses) throws ArgumentException {
141139
if ((!InputValidator.isValidSeed(seed))) {
142-
throw new IllegalStateException(INVALID_SEED_INPUT_ERROR);
140+
throw new IllegalStateException(Constants.INVALID_SEED_INPUT_ERROR);
143141
}
144142

145143
StopWatch stopWatch = new StopWatch();
@@ -177,7 +175,7 @@ public GetNewAddressResponse generateNewAddresses(String seed, int security, boo
177175
*/
178176
public GetNewAddressResponse getAddressesUnchecked(String seed, int security, boolean checksum, int index, int amount) throws ArgumentException {
179177
if ((!InputValidator.isValidSeed(seed))) {
180-
throw new IllegalStateException(INVALID_SEED_INPUT_ERROR);
178+
throw new IllegalStateException(Constants.INVALID_SEED_INPUT_ERROR);
181179
}
182180

183181
StopWatch stopWatch = new StopWatch();
@@ -202,11 +200,11 @@ public GetTransferResponse getTransfers(String seed, int security, Integer start
202200

203201
// validate seed
204202
if ((!InputValidator.isValidSeed(seed))) {
205-
throw new IllegalStateException(INVALID_SEED_INPUT_ERROR);
203+
throw new IllegalStateException(Constants.INVALID_SEED_INPUT_ERROR);
206204
}
207205

208206
if (start > end || end > (start + 500)) {
209-
throw new ArgumentException(INVALID_INPUT_ERROR);
207+
throw new ArgumentException(Constants.INVALID_INPUT_ERROR);
210208
}
211209

212210
StopWatch stopWatch = new StopWatch();
@@ -264,7 +262,7 @@ public Bundle[] bundlesFromAddresses(String[] addresses, final Boolean inclusion
264262
if (tailTxArray.length != 0 && inclusionStates) {
265263
gisr = getLatestInclusion(tailTxArray);
266264
if (gisr == null || gisr.getStates() == null || gisr.getStates().length == 0) {
267-
throw new IllegalStateException(GET_INCLUSION_STATE_RESPONSE_ERROR);
265+
throw new IllegalStateException(Constants.GET_INCLUSION_STATE_RESPONSE_ERROR);
268266
}
269267
}
270268
final GetInclusionStateResponse finalInclusionStates = gisr;
@@ -289,7 +287,7 @@ public void perform(String param) {
289287
}
290288
// If error returned from getBundle, simply ignore it because the bundle was most likely incorrect
291289
} catch (ArgumentException e) {
292-
log.warn(GET_BUNDLE_RESPONSE_ERROR);
290+
log.warn(Constants.GET_BUNDLE_RESPONSE_ERROR);
293291
}
294292
}
295293
});
@@ -312,7 +310,7 @@ public void perform(String param) {
312310
public BroadcastTransactionsResponse storeAndBroadcast(final String... trytes) throws ArgumentException {
313311

314312
if (!InputValidator.isArrayOfAttachedTrytes(trytes)) {
315-
throw new ArgumentException(INVALID_TRYTES_INPUT_ERROR);
313+
throw new ArgumentException(Constants.INVALID_TRYTES_INPUT_ERROR);
316314
}
317315

318316
try {
@@ -363,7 +361,7 @@ public List<Transaction> sendTrytes(final String[] trytes, final int depth, fina
363361
public List<Transaction> findTransactionsObjectsByHashes(String[] hashes) throws ArgumentException {
364362

365363
if (!InputValidator.isArrayOfHashes(hashes)) {
366-
throw new IllegalStateException(INVALID_HASHES_INPUT_ERROR);
364+
throw new IllegalStateException(Constants.INVALID_HASHES_INPUT_ERROR);
367365
}
368366

369367
final GetTrytesResponse trytesResponse = getTrytes(hashes);
@@ -462,16 +460,16 @@ public List<String> prepareTransfers(String seed, int security, final List<Trans
462460

463461
// validate seed
464462
if ((!InputValidator.isValidSeed(seed))) {
465-
throw new IllegalStateException(INVALID_SEED_INPUT_ERROR);
463+
throw new IllegalStateException(Constants.INVALID_SEED_INPUT_ERROR);
466464
}
467465

468466
if (!InputValidator.isValidSecurityLevel(security)) {
469-
throw new ArgumentException(INVALID_SECURITY_LEVEL_INPUT_ERROR);
467+
throw new ArgumentException(Constants.INVALID_SECURITY_LEVEL_INPUT_ERROR);
470468
}
471469

472470
// Input validation of transfers object
473471
if (!InputValidator.isTransfersCollectionValid(transfers)) {
474-
throw new ArgumentException(INVALID_TRANSFERS_INPUT_ERROR);
472+
throw new ArgumentException(Constants.INVALID_TRANSFERS_INPUT_ERROR);
475473
}
476474

477475
// Create a new bundle
@@ -590,7 +588,7 @@ public List<String> prepareTransfers(String seed, int security, final List<Trans
590588

591589
// Return not enough balance error
592590
if (totalValue > totalBalance) {
593-
throw new IllegalStateException(NOT_ENOUGH_BALANCE_ERROR);
591+
throw new IllegalStateException(Constants.NOT_ENOUGH_BALANCE_ERROR);
594592
}
595593

596594
return addRemainder(seed, security, confirmedInputs, bundle, tag, totalValue, remainder, signatureFragments);
@@ -638,17 +636,17 @@ public GetBalancesAndFormatResponse getInputs(String seed, int security, int sta
638636

639637
// validate the seed
640638
if ((!InputValidator.isValidSeed(seed))) {
641-
throw new IllegalStateException(INVALID_SEED_INPUT_ERROR);
639+
throw new IllegalStateException(Constants.INVALID_SEED_INPUT_ERROR);
642640
}
643641

644642
if (!InputValidator.isValidSecurityLevel(security)) {
645-
throw new ArgumentException(INVALID_SECURITY_LEVEL_INPUT_ERROR);
643+
throw new ArgumentException(Constants.INVALID_SECURITY_LEVEL_INPUT_ERROR);
646644
}
647645

648646
// If start value bigger than end, return error
649647
// or if difference between end and start is bigger than 500 keys
650648
if ((start > end && end > 0) || end > (start + 500)) {
651-
throw new IllegalStateException(INVALID_INPUT_ERROR);
649+
throw new IllegalStateException(Constants.INVALID_INPUT_ERROR);
652650
}
653651

654652
StopWatch stopWatch = new StopWatch();
@@ -695,7 +693,7 @@ public GetBalancesAndFormatResponse getInputs(String seed, int security, int sta
695693
public GetBalancesAndFormatResponse getBalanceAndFormat(final List<String> addresses, final List<String> tips, long threshold, int start, StopWatch stopWatch, int security) throws ArgumentException, IllegalStateException {
696694

697695
if (!InputValidator.isValidSecurityLevel(security)) {
698-
throw new ArgumentException(INVALID_SECURITY_LEVEL_INPUT_ERROR);
696+
throw new ArgumentException(Constants.INVALID_SECURITY_LEVEL_INPUT_ERROR);
699697
}
700698

701699
GetBalancesResponse getBalancesResponse = getBalances(100, addresses, tips);
@@ -730,7 +728,7 @@ public GetBalancesAndFormatResponse getBalanceAndFormat(final List<String> addre
730728
if (thresholdReached) {
731729
return GetBalancesAndFormatResponse.create(inputs, totalBalance, stopWatch.getElapsedTimeMili());
732730
}
733-
throw new IllegalStateException(NOT_ENOUGH_BALANCE_ERROR);
731+
throw new IllegalStateException(Constants.NOT_ENOUGH_BALANCE_ERROR);
734732
}
735733

736734
/**
@@ -744,18 +742,18 @@ public GetBalancesAndFormatResponse getBalanceAndFormat(final List<String> addre
744742
public GetBundleResponse getBundle(String transaction) throws ArgumentException {
745743

746744
if (!InputValidator.isHash(transaction)) {
747-
throw new ArgumentException(INVALID_HASHES_INPUT_ERROR);
745+
throw new ArgumentException(Constants.INVALID_HASHES_INPUT_ERROR);
748746
}
749747

750748
StopWatch stopWatch = new StopWatch();
751749

752750
Bundle bundle = traverseBundle(transaction, null, new Bundle());
753751
if (bundle == null) {
754-
throw new ArgumentException(INVALID_BUNDLE_ERROR);
752+
throw new ArgumentException(Constants.INVALID_BUNDLE_ERROR);
755753
}
756754

757755
if (!BundleValidator.isBundle(bundle)){
758-
throw new ArgumentException(INVALID_BUNDLE_ERROR);
756+
throw new ArgumentException(Constants.INVALID_BUNDLE_ERROR);
759757
}
760758

761759
return GetBundleResponse.create(bundle.getTransactions(), stopWatch.getElapsedTimeMili());
@@ -778,11 +776,11 @@ public GetBundleResponse getBundle(String transaction) throws ArgumentException
778776
*/
779777
public GetAccountDataResponse getAccountData(String seed, int security, int index, boolean checksum, int total, boolean returnAll, int start, int end, boolean inclusionStates, long threshold) throws ArgumentException {
780778
if (!InputValidator.isValidSecurityLevel(security)) {
781-
throw new ArgumentException(INVALID_SECURITY_LEVEL_INPUT_ERROR);
779+
throw new ArgumentException(Constants.INVALID_SECURITY_LEVEL_INPUT_ERROR);
782780
}
783781

784782
if (start > end || end > (start + 1000)) {
785-
throw new ArgumentException(INVALID_INPUT_ERROR);
783+
throw new ArgumentException(Constants.INVALID_INPUT_ERROR);
786784
}
787785

788786
StopWatch stopWatch = new StopWatch();
@@ -849,7 +847,7 @@ public Boolean checkWereAddressSpentFrom(String address) throws ArgumentExceptio
849847
public ReplayBundleResponse replayBundle(String tailTransactionHash, int depth, int minWeightMagnitude, String reference) throws ArgumentException {
850848

851849
if (!InputValidator.isHash(tailTransactionHash)) {
852-
throw new ArgumentException(INVALID_TAIL_HASH_INPUT_ERROR);
850+
throw new ArgumentException(Constants.INVALID_TAIL_HASH_INPUT_ERROR);
853851
}
854852

855853
StopWatch stopWatch = new StopWatch();
@@ -951,16 +949,16 @@ public Bundle traverseBundle(String trunkTx, String bundleHash, Bundle bundle) t
951949
if (gtr != null) {
952950

953951
if (gtr.getTrytes().length == 0) {
954-
throw new ArgumentException(INVALID_BUNDLE_ERROR);
952+
throw new ArgumentException(Constants.INVALID_BUNDLE_ERROR);
955953
}
956954

957955
Transaction trx = new Transaction(gtr.getTrytes()[0], customCurl.clone());
958956
if (trx.getBundle() == null) {
959-
throw new ArgumentException(INVALID_TRYTES_INPUT_ERROR);
957+
throw new ArgumentException(Constants.INVALID_TRYTES_INPUT_ERROR);
960958
}
961959
// If first transaction to search is not a tail, return error
962960
if (bundleHash == null && trx.getCurrentIndex() != 0) {
963-
throw new ArgumentException(INVALID_TAIL_HASH_INPUT_ERROR);
961+
throw new ArgumentException(Constants.INVALID_TAIL_HASH_INPUT_ERROR);
964962
}
965963
// If no bundle hash, define it
966964
if (bundleHash == null) {
@@ -983,7 +981,7 @@ public Bundle traverseBundle(String trunkTx, String bundleHash, Bundle bundle) t
983981
// Continue traversing with new trunkTx
984982
return traverseBundle(trunkTx, bundleHash, bundle);
985983
} else {
986-
throw new ArgumentException(GET_TRYTES_RESPONSE_ERROR);
984+
throw new ArgumentException(Constants.GET_TRYTES_RESPONSE_ERROR);
987985
}
988986
}
989987

@@ -1058,21 +1056,21 @@ public List<Transaction> initiateTransfer(int securitySum, String inputAddress,
10581056
List<Transfer> transfers, List<Transaction> tips,
10591057
boolean testMode) throws ArgumentException {
10601058
if (securitySum < Constants.MIN_SECURITY_LEVEL) {
1061-
throw new ArgumentException(INVALID_SECURITY_LEVEL_INPUT_ERROR);
1059+
throw new ArgumentException(Constants.INVALID_SECURITY_LEVEL_INPUT_ERROR);
10621060
}
10631061

10641062
// validate input address
10651063
if (!InputValidator.isAddress(inputAddress))
1066-
throw new ArgumentException(INVALID_ADDRESSES_INPUT_ERROR);
1064+
throw new ArgumentException(Constants.INVALID_ADDRESSES_INPUT_ERROR);
10671065

10681066
// validate remainder address
10691067
if (remainderAddress != null && !InputValidator.isAddress(remainderAddress)) {
1070-
throw new ArgumentException(INVALID_ADDRESSES_INPUT_ERROR);
1068+
throw new ArgumentException(Constants.INVALID_ADDRESSES_INPUT_ERROR);
10711069
}
10721070

10731071
// Input validation of transfers object
10741072
if (!InputValidator.isTransfersCollectionValid(transfers)) {
1075-
throw new ArgumentException(INVALID_TRANSFERS_INPUT_ERROR);
1073+
throw new ArgumentException(Constants.INVALID_TRANSFERS_INPUT_ERROR);
10761074
}
10771075

10781076
// Create a new bundle
@@ -1185,7 +1183,7 @@ public List<Transaction> initiateTransfer(int securitySum, String inputAddress,
11851183
}
11861184
// Return not enough balance error
11871185
if (totalValue > totalBalance) {
1188-
throw new IllegalStateException(NOT_ENOUGH_BALANCE_ERROR);
1186+
throw new IllegalStateException(Constants.NOT_ENOUGH_BALANCE_ERROR);
11891187
}
11901188

11911189
// If there is a remainder value
@@ -1196,7 +1194,7 @@ public List<Transaction> initiateTransfer(int securitySum, String inputAddress,
11961194

11971195
// Remainder bundle entry if necessary
11981196
if (remainderAddress == null) {
1199-
throw new IllegalStateException(NO_REMAINDER_ADDRESS_ERROR);
1197+
throw new IllegalStateException(Constants.NO_REMAINDER_ADDRESS_ERROR);
12001198
}
12011199

12021200
bundle.addEntry(1, remainderAddress, remainder, tag, timestamp);
@@ -1207,7 +1205,7 @@ public List<Transaction> initiateTransfer(int securitySum, String inputAddress,
12071205

12081206
return bundle.getTransactions();
12091207
} else {
1210-
throw new RuntimeException(INVALID_VALUE_TRANSFER_ERROR);
1208+
throw new RuntimeException(Constants.INVALID_VALUE_TRANSFER_ERROR);
12111209
}
12121210

12131211
}
@@ -1322,7 +1320,7 @@ public List<String> addRemainder(final String seed,
13221320
totalTransferValue -= thisBalance;
13231321
}
13241322
}
1325-
throw new IllegalStateException(NOT_ENOUGH_BALANCE_ERROR);
1323+
throw new IllegalStateException(Constants.NOT_ENOUGH_BALANCE_ERROR);
13261324
}
13271325

13281326
/**
@@ -1348,7 +1346,7 @@ public boolean isPromotable(Transaction tail) throws ArgumentException {
13481346
public boolean isPromotable(String tail) throws ArgumentException {
13491347
GetTrytesResponse transaction = getTrytes(tail);
13501348
if (0 == transaction.getTrytes().length) {
1351-
throw new ArgumentException(TRANSACTION_NOT_FOUND);
1349+
throw new ArgumentException(Constants.TRANSACTION_NOT_FOUND);
13521350
}
13531351

13541352
return isPromotable(new Transaction(transaction.getTrytes()[0]));

jota/src/main/java/jota/utils/InputValidator.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,7 @@ public static boolean isValidTransfer(final Transfer transfer) {
220220
}
221221

222222
// Check if tag is correct trytes encoded and not longer than 27 trytes
223-
if (transfer.getTag() == null || !isTrytesOfExactLength(transfer.getTag(), transfer.getTag().length()) || transfer.getTag().length() > Constants.TAG_LENGTH) {
224-
return false;
225-
}
226-
227-
return true;
223+
return isValidTag(transfer.getTag());
228224
}
229225

230226
/**
@@ -254,7 +250,7 @@ public static boolean areValidTags(String... tags) {
254250
* @return <code>true</code> if the specified tag is valid; otherwise, <code>false</code>.
255251
**/
256252
public static boolean isValidTag(String tag) {
257-
return tag != null && !isTrytesOfExactLength(tag, Constants.TAG_LENGTH);
253+
return tag != null && tag.length() <= Constants.TAG_LENGTH && isTrytes(tag);
258254
}
259255

260256
/**
@@ -296,11 +292,7 @@ public static boolean isValidInput(Input input){
296292
return false;
297293
}
298294

299-
if (input.getSecurity() < Constants.MIN_SECURITY_LEVEL || input.getSecurity() > Constants.MAX_SECURITY_LEVEL) {
300-
return false;
301-
}
302-
303-
return true;
295+
return isValidSecurityLevel(input.getSecurity());
304296
}
305297

306298
/**
@@ -378,7 +370,7 @@ public static boolean isValidUri(String uri) {
378370
}
379371

380372
String protocol = uri.substring(0, 6);
381-
if (!protocol.equals("tcp://") && !protocol.equals("udp://")) {
373+
if (!"tcp://".equals(protocol) && !"udp://".equals(protocol)) {
382374
return false;
383375
}
384376

jota/src/main/java/jota/utils/Signing.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public class Signing {
2323

2424

2525
public Signing() {
26-
this(Optional.ofNullable(null));
26+
this(Optional.empty());
2727
}
2828

2929
public Signing(ICurl curl) {
30-
this(Optional.empty());
30+
this(curl != null ? Optional.of(curl) : Optional.empty());
3131
}
3232

3333
/**

0 commit comments

Comments
 (0)