Skip to content

Commit a27168d

Browse files
author
AZ
committed
cleanup
1 parent 327c3bb commit a27168d

22 files changed

Lines changed: 217 additions & 95 deletions

‎pom.xml‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@
6363
<version>4.12</version>
6464
<scope>test</scope>
6565
</dependency>
66-
<dependency>
67-
<groupId>net.java.dev.jna</groupId>
68-
<artifactId>jna-platform</artifactId>
69-
<version>4.0.0</version>
70-
</dependency>
7166
</dependencies>
7267

7368
<build>

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

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import jota.pow.ICurl;
99
import jota.pow.JCurl;
1010
import jota.utils.*;
11-
import jota.utils.StopWatch;
1211
import org.apache.commons.lang3.StringUtils;
1312
import org.slf4j.Logger;
1413
import org.slf4j.LoggerFactory;
@@ -28,26 +27,17 @@
2827
*
2928
* @author davassi
3029
*/
31-
public class IotaAPI {
30+
public class IotaAPI extends IotaAPICoreProxy {
3231

3332
private static final Logger log = LoggerFactory.getLogger(IotaAPI.class);
34-
private IotaAPICoreProxy coreProxy;
3533
private ICurl customCurl;
3634
private StopWatch stopWatch;
3735

38-
public IotaAPI() {
39-
this(null);
36+
protected IotaAPI(Builder builder) {
37+
super(builder);
38+
customCurl = builder.customCurl;
4039
}
4140

42-
public IotaAPI(ICurl customCurl) {
43-
this.customCurl = customCurl;
44-
coreProxy = new IotaAPICoreProxy.Builder().build();
45-
stopWatch = new StopWatch();
46-
}
47-
48-
49-
// end of proxied calls.
50-
5141
/**
5242
* Generates a new address from a seed and returns the remainderAddress.
5343
* This is either done deterministically, or by providing the index of the new remainderAddress
@@ -77,7 +67,7 @@ public GetNewAddressResponse getNewAddress(final String seed, final int index, f
7767
for (int i = index; ; i++) {
7868

7969
final String newAddress = IotaAPIUtils.newAddress(seed, i, checksum, customCurl);
80-
final FindTransactionResponse response = coreProxy.findTransactionsByAddresses(newAddress);
70+
final FindTransactionResponse response = findTransactionsByAddresses(newAddress);
8171

8272
allAddresses.add(newAddress);
8373
if (response.getHashes().length == 0) {
@@ -168,7 +158,7 @@ public Bundle[] bundlesFromAddresses(String[] addresses, final Boolean inclusion
168158
if (inclusionStates) {
169159
try {
170160
gisr = getLatestInclusion(tailTxArray);
171-
} catch (IllegalAccessError e) {
161+
} catch (IllegalAccessError ignored) {
172162

173163
}
174164
if (gisr == null || gisr.getStates() == null || gisr.getStates().length == 0)
@@ -213,12 +203,12 @@ public void perform(String param) {
213203
public StoreTransactionsResponse broadcastAndStore(final String... trytes) {
214204

215205
try {
216-
coreProxy.broadcastTransactions(trytes);
206+
broadcastTransactions(trytes);
217207
} catch (Exception e) {
218208
log.error("Impossible to broadcastAndStore, aborting.", e);
219209
throw new IllegalStateException("BroadcastAndStore Illegal state Exception");
220210
}
221-
return coreProxy.storeTransactions(trytes);
211+
return storeTransactions(trytes);
222212
}
223213

224214
/**
@@ -230,10 +220,10 @@ public StoreTransactionsResponse broadcastAndStore(final String... trytes) {
230220
* @return
231221
*/
232222
public List<Transaction> sendTrytes(final String[] trytes, final int depth, final int minWeightMagnitude) {
233-
final GetTransactionsToApproveResponse txs = coreProxy.getTransactionsToApprove(depth);
223+
final GetTransactionsToApproveResponse txs = getTransactionsToApprove(depth);
234224

235225
// attach to tangle - do pow
236-
final GetAttachToTangleResponse res = coreProxy.attachToTangle(txs.getTrunkTransaction(), txs.getBranchTransaction(), minWeightMagnitude, trytes);
226+
final GetAttachToTangleResponse res = attachToTangle(txs.getTrunkTransaction(), txs.getBranchTransaction(), minWeightMagnitude, trytes);
237227

238228
try {
239229
broadcastAndStore(res.getTrytes());
@@ -267,7 +257,7 @@ public List<Transaction> getTransactionsObjects(String[] hashes) {
267257
throw new IllegalStateException("Not an Array of Hashes: " + Arrays.toString(hashes));
268258
}
269259

270-
final GetTrytesResponse trytesResponse = coreProxy.getTrytes(hashes);
260+
final GetTrytesResponse trytesResponse = getTrytes(hashes);
271261

272262
final List<Transaction> trxs = new ArrayList<>();
273263

@@ -288,7 +278,7 @@ public List<Transaction> getTransactionsObjects(String[] hashes) {
288278
* @returns {object} success
289279
**/
290280
public List<Transaction> findTransactionObjects(String[] input) {
291-
FindTransactionResponse ftr = coreProxy.findTransactions(input, null, null, null);
281+
FindTransactionResponse ftr = findTransactions(input, null, null, null);
292282
if (ftr == null || ftr.getHashes() == null)
293283
return null;
294284

@@ -307,7 +297,7 @@ public List<Transaction> findTransactionObjects(String[] input) {
307297
* @returns {object} success
308298
**/
309299
public List<Transaction> findTransactionObjectsByBundle(String[] input) {
310-
FindTransactionResponse ftr = coreProxy.findTransactions(null, null, null, input);
300+
FindTransactionResponse ftr = findTransactions(null, null, null, input);
311301
if (ftr == null || ftr.getHashes() == null)
312302
return null;
313303

@@ -416,7 +406,7 @@ public List<String> prepareTransfers(String seed, final List<Transfer> transfers
416406
inputsAddresses.add(i.getAddress());
417407
}
418408

419-
GetBalancesResponse balancesResponse = coreProxy.getBalances(100, inputsAddresses);
409+
GetBalancesResponse balancesResponse = getBalances(100, inputsAddresses);
420410
String[] balances = balancesResponse.getBalances();
421411

422412
List<Input> confirmedInputs = new ArrayList<>();
@@ -448,7 +438,7 @@ public List<String> prepareTransfers(String seed, final List<Transfer> transfers
448438
// confirm that the inputs exceed the threshold
449439
else {
450440

451-
GetBalancesAndFormatResponse newinputs = getInputs(seed, Collections.EMPTY_LIST, 0, 0, totalValue);
441+
@SuppressWarnings("unchecked") GetBalancesAndFormatResponse newinputs = getInputs(seed, Collections.EMPTY_LIST, 0, 0, totalValue);
452442
// If inputs with enough balance
453443
return addRemainder(seed, newinputs.getInput(), bundle, tag, totalValue, null, signatureFragments);
454444
}
@@ -530,13 +520,13 @@ public GetBalancesAndFormatResponse getInputs(String seed, final List<String> ba
530520
public GetBalancesAndFormatResponse getBalanceAndFormat(final List<String> addresses, List<String> balances, long threshold, int start, int end, StopWatch stopWatch) {
531521

532522
if (balances == null || balances.isEmpty()) {
533-
GetBalancesResponse getBalancesResponse = coreProxy.getBalances(100, addresses);
523+
GetBalancesResponse getBalancesResponse = getBalances(100, addresses);
534524
balances = Arrays.asList(getBalancesResponse.getBalances());
535525
}
536526

537527
// If threshold defined, keep track of whether reached or not
538528
// else set default to true
539-
boolean thresholdReached = threshold != 0 ? false : true;
529+
boolean thresholdReached = threshold == 0;
540530
int i = -1;
541531

542532
List<Input> inputs = new ArrayList<>();
@@ -640,9 +630,9 @@ public GetBundleResponse getBundle(String transaction) throws ArgumentException,
640630
throw new InvalidBundleException("Invalid Bundle");
641631

642632
// Validate the signatures
643-
for (int i = 0; i < signaturesToValidate.size(); i++) {
644-
String[] signatureFragments = signaturesToValidate.get(i).getSignatureFragments().toArray(new String[signaturesToValidate.get(i).getSignatureFragments().size()]);
645-
String address = signaturesToValidate.get(i).getAddress();
633+
for (Signature aSignaturesToValidate : signaturesToValidate) {
634+
String[] signatureFragments = aSignaturesToValidate.getSignatureFragments().toArray(new String[aSignaturesToValidate.getSignatureFragments().size()]);
635+
String address = aSignaturesToValidate.getAddress();
646636
boolean isValidSignature = new Signing().validateSignatures(address, signatureFragments, bundleHash);
647637

648638
if (!isValidSignature) throw new InvalidSignatureException();
@@ -679,7 +669,7 @@ public ReplayBundleResponse replayBundle(String transaction, int depth, int minW
679669

680670
for (int i = 0; i < trxs.size(); i++) {
681671

682-
final FindTransactionResponse response = coreProxy.findTransactionsByBundles(trxs.get(i).getBundle());
672+
final FindTransactionResponse response = findTransactionsByBundles(trxs.get(i).getBundle());
683673

684674

685675
successful[i] = response.getHashes().length != 0;
@@ -697,12 +687,12 @@ public ReplayBundleResponse replayBundle(String transaction, int depth, int minW
697687
* @returns {array} state
698688
**/
699689
public GetInclusionStateResponse getLatestInclusion(String[] hashes) {
700-
GetNodeInfoResponse getNodeInfoResponse = coreProxy.getNodeInfo();
690+
GetNodeInfoResponse getNodeInfoResponse = getNodeInfo();
701691
if (getNodeInfoResponse == null) return null;
702692

703693
String[] latestMilestone = {getNodeInfoResponse.getLatestSolidSubtangleMilestone()};
704694

705-
return coreProxy.getInclusionStates(hashes, latestMilestone);
695+
return getInclusionStates(hashes, latestMilestone);
706696
}
707697

708698
public SendTransferResponse sendTransfer(String seed, int depth, int minWeightMagnitude, final List<Transfer> transfers, Input[] inputs, String address) {
@@ -715,7 +705,7 @@ public SendTransferResponse sendTransfer(String seed, int depth, int minWeightMa
715705

716706
for (int i = 0; i < trxs.size(); i++) {
717707

718-
final FindTransactionResponse response = coreProxy.findTransactionsByBundles(trxs.get(i).getBundle());
708+
final FindTransactionResponse response = findTransactionsByBundles(trxs.get(i).getBundle());
719709

720710
successful[i] = response.getHashes().length != 0;
721711
}
@@ -735,7 +725,7 @@ public SendTransferResponse sendTransfer(String seed, int depth, int minWeightMa
735725
* @returns {array} bundle Transaction objects
736726
**/
737727
public Bundle traverseBundle(String trunkTx, String bundleHash, Bundle bundle) throws ArgumentException {
738-
GetTrytesResponse gtr = coreProxy.getTrytes(trunkTx);
728+
GetTrytesResponse gtr = getTrytes(trunkTx);
739729

740730
if (gtr != null) {
741731

@@ -776,7 +766,7 @@ public Bundle traverseBundle(String trunkTx, String bundleHash, Bundle bundle) t
776766
}
777767

778768
public String findTailTransactionHash(String hash) throws ArgumentException {
779-
GetTrytesResponse gtr = coreProxy.getTrytes(hash);
769+
GetTrytesResponse gtr = getTrytes(hash);
780770

781771
if (gtr == null) throw new ArgumentException("Invalid hash");
782772

@@ -844,4 +834,18 @@ public List<String> addRemainder(final String seed,
844834
}
845835
return null;
846836
}
837+
838+
public static class Builder extends IotaAPICoreProxy.Builder<Builder> {
839+
private ICurl customCurl;
840+
841+
public Builder withCustomCurl(ICurl curl) {
842+
customCurl = curl;
843+
return this;
844+
}
845+
846+
public IotaAPI build() {
847+
super.build();
848+
return new IotaAPI(this);
849+
}
850+
}
847851
}

‎src/main/java/jota/IotaAPICoreProxy.java‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class IotaAPICoreProxy {
2828
private IotaAPIService service;
2929
private String protocol, host, port;
3030

31-
private IotaAPICoreProxy(final Builder builder) {
31+
protected IotaAPICoreProxy(final Builder builder) {
3232
protocol = builder.protocol;
3333
host = builder.host;
3434
port = builder.port;
@@ -52,7 +52,7 @@ protected static <T> Response<T> wrapCheckedException(final Call<T> call) {
5252
}
5353
}
5454

55-
private static final String env(String env, String def) {
55+
private static String env(String env, String def) {
5656
final String value = System.getenv(env);
5757
if (value == null) {
5858
log.warn("Environment variable '{}' is not defined, and actual value has not been specified. "
@@ -187,7 +187,8 @@ public BroadcastTransactionsResponse broadcastTransactions(String... trytes) {
187187
return wrapCheckedException(res).body();
188188
}
189189

190-
public static class Builder {
190+
@SuppressWarnings("unchecked")
191+
public static class Builder <T extends Builder<T>> {
191192

192193
String protocol, host, port;
193194

@@ -241,19 +242,19 @@ private void checkEnviromentVariables() {
241242
port = env("IOTA_NODE_PORT", "14265");
242243
}
243244

244-
public Builder host(String host) {
245+
public T host(String host) {
245246
this.host = host;
246-
return this;
247+
return (T) this;
247248
}
248249

249-
public Builder port(String port) {
250+
public T port(String port) {
250251
this.port = port;
251-
return this;
252+
return (T) this;
252253
}
253254

254-
public Builder protocol(String protocol) {
255+
public T protocol(String protocol) {
255256
this.protocol = protocol;
256-
return this;
257+
return (T) this;
257258
}
258259

259260
}

‎src/main/java/jota/dto/request/IotaAttachToTangleRequest.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,36 @@ private IotaAttachToTangleRequest(final String trunkTransaction, final String br
2020
public static IotaAttachToTangleRequest createAttachToTangleRequest(final String trunkTransaction, final String branchTransaction, final Integer minWeightMagnitude, final String... trytes) {
2121
return new IotaAttachToTangleRequest(trunkTransaction, branchTransaction, minWeightMagnitude, trytes);
2222
}
23+
24+
public String getTrunkTransaction() {
25+
return trunkTransaction;
26+
}
27+
28+
public void setTrunkTransaction(String trunkTransaction) {
29+
this.trunkTransaction = trunkTransaction;
30+
}
31+
32+
public String getBranchTransaction() {
33+
return branchTransaction;
34+
}
35+
36+
public void setBranchTransaction(String branchTransaction) {
37+
this.branchTransaction = branchTransaction;
38+
}
39+
40+
public Integer getMinWeightMagnitude() {
41+
return minWeightMagnitude;
42+
}
43+
44+
public void setMinWeightMagnitude(Integer minWeightMagnitude) {
45+
this.minWeightMagnitude = minWeightMagnitude;
46+
}
47+
48+
public String[] getTrytes() {
49+
return trytes;
50+
}
51+
52+
public void setTrytes(String[] trytes) {
53+
this.trytes = trytes;
54+
}
2355
}

‎src/main/java/jota/dto/request/IotaBroadcastTransactionRequest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ private IotaBroadcastTransactionRequest(final String... trytes) {
1414
public static IotaBroadcastTransactionRequest createBroadcastTransactionsRequest(final String... trytes) {
1515
return new IotaBroadcastTransactionRequest(trytes);
1616
}
17+
18+
public String[] getTrytes() {
19+
return trytes;
20+
}
21+
22+
public void setTrytes(String[] trytes) {
23+
this.trytes = trytes;
24+
}
1725
}

‎src/main/java/jota/dto/request/IotaFindTransactionsRequest.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@
44

55
public class IotaFindTransactionsRequest extends IotaCommandRequest {
66

7+
public String[] getBundles() {
8+
return bundles;
9+
}
10+
11+
public void setBundles(String[] bundles) {
12+
this.bundles = bundles;
13+
}
14+
15+
public String[] getAddresses() {
16+
return addresses;
17+
}
18+
19+
public void setAddresses(String[] addresses) {
20+
this.addresses = addresses;
21+
}
22+
23+
public String[] getTags() {
24+
return tags;
25+
}
26+
27+
public void setTags(String[] tags) {
28+
this.tags = tags;
29+
}
30+
31+
public String[] getApprovees() {
32+
return approvees;
33+
}
34+
35+
public void setApprovees(String[] approvees) {
36+
this.approvees = approvees;
37+
}
38+
739
private String[] bundles; // List of bundle hashes. The hashes need to be extended to 81chars by padding the hash with 9's.
840
private String[] addresses;
941
private String[] tags;

0 commit comments

Comments
 (0)