88import jota .pow .ICurl ;
99import jota .pow .JCurl ;
1010import jota .utils .*;
11- import jota .utils .StopWatch ;
1211import org .apache .commons .lang3 .StringUtils ;
1312import org .slf4j .Logger ;
1413import org .slf4j .LoggerFactory ;
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}
0 commit comments