Repository navigation
Expand file tree
/
Copy pathIotaAPI.java
More file actions
1158 lines (946 loc) · 52.3 KB
/
Copy pathIotaAPI.java
File metadata and controls
1158 lines (946 loc) · 52.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package jota;
import com.google.gson.Gson;
import jota.dto.response.*;
import jota.error.*;
import jota.model.*;
import jota.pow.ICurl;
import jota.pow.SpongeFactory;
import jota.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
* IotaAPI Builder. Usage:
* <p>
* IotaApiProxy api = IotaApiProxy.Builder
* .protocol("http")
* .nodeAddress("localhost")
* .port(12345)
* .build();
* <p>
* GetNodeInfoResponse response = api.getNodeInfo();
*
* @author davassi
*/
public class IotaAPI extends IotaAPICore {
private static final Logger log = LoggerFactory.getLogger(IotaAPI.class);
private ICurl customCurl;
protected IotaAPI(Builder builder) {
super(builder);
customCurl = builder.customCurl;
}
/**
* Generates a new address from a seed and returns the remainderAddress.
* This is either done deterministically, or by providing the index of the new remainderAddress.
*
* @param seed Tryte-encoded seed. It should be noted that this seed is not transferred.
* @param security Security level to be used for the private key / address. Can be 1, 2 or 3.
* @param index Key index to start search from. If the index is provided, the generation of the address is not deterministic.
* @param checksum Adds 9-tryte address checksum.
* @param total Total number of addresses to generate.
* @param returnAll If <code>true</code>, it returns all addresses which were deterministically generated (until findTransactions returns null).
* @return An array of strings with the specifed number of addresses.
* @throws InvalidAddressException is thrown when the specified address is not an valid address.
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
*/
public GetNewAddressResponse getNewAddress(final String seed, int security, final int index, final boolean checksum, final int total, final boolean returnAll) throws InvalidSecurityLevelException, InvalidAddressException {
if (security < 1 || security > 3) {
throw new InvalidSecurityLevelException();
}
StopWatch stopWatch = new StopWatch();
List<String> allAddresses = new ArrayList<>();
// If total number of addresses to generate is supplied, simply generate
// and return the list of all addresses
if (total != 0) {
for (int i = index; i < index + total; i++) {
allAddresses.add(IotaAPIUtils.newAddress(seed, security, i, checksum, customCurl.clone()));
}
return GetNewAddressResponse.create(allAddresses, stopWatch.getElapsedTimeMili());
}
// No total provided: Continue calling findTransactions to see if address was
// already created if null, return list of addresses
for (int i = index; ; i++) {
final String newAddress = IotaAPIUtils.newAddress(seed, security, i, checksum, customCurl.clone());
final FindTransactionResponse response = findTransactionsByAddresses(newAddress);
allAddresses.add(newAddress);
if (response.getHashes().length == 0) {
break;
}
}
// If !returnAll return only the last address that was generated
if (!returnAll) {
//allAddresses = allAddresses.subList(allAddresses.size() - 2, allAddresses.size() - 1);
allAddresses = allAddresses.subList(allAddresses.size() - 1, allAddresses.size());
}
return GetNewAddressResponse.create(allAddresses, stopWatch.getElapsedTimeMili());
}
/**
* @param seed Tryte-encoded seed. It should be noted that this seed is not transferred.
* @param security The security level of private key / seed.
* @param start Starting key index.
* @param end Ending key index.
* @param inclusionStates If <code>true</code>, it gets the inclusion states of the transfers.
* @return Bundle of transfers.
* @throws InvalidAddressException is thrown when the specified address is not an valid address.
* @throws ArgumentException is thrown when an invalid argument is provided.
* @throws InvalidBundleException is thrown if an invalid bundle was found or provided.
* @throws InvalidSignatureException is thrown when an invalid signature is encountered.
* @throws NoNodeInfoException is thrown when its not possible to get node info.
* @throws NoInclusionStatesException when it not possible to get a inclusion state.
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
*/
public GetTransferResponse getTransfers(String seed, int security, Integer start, Integer end, Boolean inclusionStates) throws ArgumentException, InvalidBundleException, InvalidSignatureException, NoNodeInfoException, NoInclusionStatesException, InvalidSecurityLevelException, InvalidAddressException {
StopWatch stopWatch = new StopWatch();
// validate seed
if ((!InputValidator.isValidSeed(seed))) {
throw new IllegalStateException("Invalid Seed");
}
if (security < 1 || security > 3) {
throw new InvalidSecurityLevelException();
}
start = start == null ? 0 : start;
if (start > end || end > (start + 500)) {
throw new ArgumentException();
}
StopWatch sw = new StopWatch();
GetNewAddressResponse gnr = getNewAddress(seed, security, start, false, end, true);
if (gnr != null && gnr.getAddresses() != null) {
Bundle[] bundles = bundlesFromAddresses(gnr.getAddresses().toArray(new String[gnr.getAddresses().size()]), inclusionStates);
return GetTransferResponse.create(bundles, stopWatch.getElapsedTimeMili());
}
return GetTransferResponse.create(new Bundle[]{}, stopWatch.getElapsedTimeMili());
}
/**
* Internal function to get the formatted bundles of a list of addresses.
*
* @param addresses List of addresses.
* @param inclusionStates If <code>true</code>, it gets the inclusion states of the transfers.
* @return A Transaction objects.
* @throws ArgumentException is thrown when an invalid argument is provided.
* @throws InvalidBundleException is thrown if an invalid bundle was found or provided.
* @throws InvalidSignatureException is thrown when an invalid signature is encountered.
* @throws NoNodeInfoException is thrown when its not possible to get node info.
* @throws NoInclusionStatesException when it not possible to get a inclusion state.
*/
public Bundle[] bundlesFromAddresses(String[] addresses, final Boolean inclusionStates) throws ArgumentException, InvalidBundleException, InvalidSignatureException, NoNodeInfoException, NoInclusionStatesException {
List<Transaction> trxs = findTransactionObjects(addresses);
// set of tail transactions
List<String> tailTransactions = new ArrayList<>();
List<String> nonTailBundleHashes = new ArrayList<>();
for (Transaction trx : trxs) {
// Sort tail and nonTails
if (trx.getCurrentIndex() == 0) {
tailTransactions.add(trx.getHash());
} else {
if (nonTailBundleHashes.indexOf(trx.getBundle()) == -1) {
nonTailBundleHashes.add(trx.getBundle());
}
}
}
List<Transaction> bundleObjects = findTransactionObjectsByBundle(nonTailBundleHashes.toArray(new String[nonTailBundleHashes.size()]));
for (Transaction trx : bundleObjects) {
// Sort tail and nonTails
if (trx.getCurrentIndex() == 0) {
if (tailTransactions.indexOf(trx.getHash()) == -1) {
tailTransactions.add(trx.getHash());
}
}
}
final List<Bundle> finalBundles = new ArrayList<>();
final String[] tailTxArray = tailTransactions.toArray(new String[tailTransactions.size()]);
// If inclusionStates, get the confirmation status
// of the tail transactions, and thus the bundles
GetInclusionStateResponse gisr = null;
if (tailTxArray.length != 0 && inclusionStates) {
try {
gisr = getLatestInclusion(tailTxArray);
} catch (IllegalAccessError ignored) {
throw new NoInclusionStatesException();
}
if (gisr == null || gisr.getStates() == null || gisr.getStates().length == 0) {
throw new NoInclusionStatesException();
}
}
final GetInclusionStateResponse finalInclusionStates = gisr;
Parallel.For(Arrays.asList(tailTxArray),
new Parallel.Operation<String>() {
public void perform(String param) {
try {
GetBundleResponse bundleResponse = getBundle(param);
Bundle gbr = new Bundle(bundleResponse.getTransactions(), bundleResponse.getTransactions().size());
if (gbr.getTransactions() != null) {
if (inclusionStates) {
boolean thisInclusion = false;
if (finalInclusionStates != null) {
thisInclusion = finalInclusionStates.getStates()[Arrays.asList(tailTxArray).indexOf(param)];
}
for (Transaction t : gbr.getTransactions()) {
t.setPersistence(thisInclusion);
}
}
finalBundles.add(gbr);
}
// If error returned from getBundle, simply ignore it because the bundle was most likely incorrect
} catch (InvalidBundleException | ArgumentException | InvalidSignatureException e) {
log.warn("GetBundleError: ", e);
}
}
});
Collections.sort(finalBundles);
Bundle[] returnValue = new Bundle[finalBundles.size()];
for (int i = 0; i < finalBundles.size(); i++) {
returnValue[i] = new Bundle(finalBundles.get(i).getTransactions(), finalBundles.get(i).getTransactions().size());
}
return returnValue;
}
/**
* Wrapper function that broadcasts and stores the specified trytes.
*
* @param trytes The trytes.
* @return A StoreTransactionsResponse.
* @throws BroadcastAndStoreException is thrown if its not possible to broadcast and store.
*/
public StoreTransactionsResponse broadcastAndStore(final String... trytes) throws BroadcastAndStoreException {
try {
broadcastTransactions(trytes);
} catch (Exception e) {
log.error("Impossible to broadcastAndStore, aborting.", e);
throw new BroadcastAndStoreException();
}
return storeTransactions(trytes);
}
/**
* Facade method: Gets transactions to approve, attaches to Tangle, broadcasts and stores.
*
* @param trytes The trytes.
* @param depth The depth.
* @param minWeightMagnitude The minimum weight magnitude.
* @return Transactions objects.
* @throws InvalidTrytesException is thrown when invalid trytes is provided.
*/
public List<Transaction> sendTrytes(final String[] trytes, final int depth, final int minWeightMagnitude) throws InvalidTrytesException {
final GetTransactionsToApproveResponse txs = getTransactionsToApprove(depth);
// attach to tangle - do pow
System.out.println(new Gson().toJson(txs));
final GetAttachToTangleResponse res = attachToTangle(txs.getTrunkTransaction(), txs.getBranchTransaction(), minWeightMagnitude, trytes);
try {
broadcastAndStore(res.getTrytes());
} catch (BroadcastAndStoreException e) {
return new ArrayList<>();
}
final List<Transaction> trx = new ArrayList<>();
for (final String tryte : Arrays.asList(res.getTrytes())) {
trx.add(new Transaction(tryte, customCurl.clone()));
}
return trx;
}
/**
* Wrapper function for getTrytes and transactionObjects.
* Gets the trytes and transaction object from a list of transaction hashes.
*
* @param hashes The hashes
* @return Transaction objects.
**/
public List<Transaction> getTransactionsObjects(String[] hashes) {
if (!InputValidator.isArrayOfHashes(hashes)) {
throw new IllegalStateException("Not an Array of Hashes: " + Arrays.toString(hashes));
}
final GetTrytesResponse trytesResponse = getTrytes(hashes);
final List<Transaction> trxs = new ArrayList<>();
for (final String tryte : trytesResponse.getTrytes()) {
trxs.add(new Transaction(tryte, customCurl.clone()));
}
return trxs;
}
/**
* Wrapper function for findTransactions, getTrytes and transactionObjects.
* Returns the transactionObject of a transaction hash. The input can be a valid findTransactions input.
*
* @param input The inputs.
* @return Transactions.
**/
public List<Transaction> findTransactionObjects(String[] input) {
FindTransactionResponse ftr = findTransactions(input, null, null, null);
if (ftr == null || ftr.getHashes() == null)
return new ArrayList<>();
// get the transaction objects of the transactions
return getTransactionsObjects(ftr.getHashes());
}
/**
* Wrapper function for findTransactions, getTrytes and transactionObjects.
* Returns the transactionObject of a transaction hash. The input can be a valid.
* findTransactions input
*
* @param input The inputs.
* @return Transactions.
**/
public List<Transaction> findTransactionObjectsByBundle(String[] input) {
FindTransactionResponse ftr = findTransactions(null, null, null, input);
if (ftr == null || ftr.getHashes() == null)
return new ArrayList<>();
// get the transaction objects of the transactions
return getTransactionsObjects(ftr.getHashes());
}
/**
* Prepares transfer by generating bundle, finding and signing inputs.
*
* @param seed 81-tryte encoded address of recipient.
* @param security The security level of private key / seed.
* @param transfers Array of transfer objects.
* @param remainder If defined, this address will be used for sending the remainder value (of the inputs) to.
* @param inputs The inputs.
* @return Returns bundle trytes.
* @throws InvalidAddressException is thrown when the specified address is not an valid address.
* @throws NotEnoughBalanceException is thrown when a transfer fails because their is not enough balance to perform the transfer.
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
* @throws InvalidTransferException is thrown when an invalid transfer is provided.
*/
public List<String> prepareTransfers(String seed, int security, final List<Transfer> transfers, String remainder, List<Input> inputs) throws NotEnoughBalanceException, InvalidSecurityLevelException, InvalidAddressException, InvalidTransferException {
return prepareTransfers(seed, security, transfers, remainder, inputs, true);
}
/**
* Prepares transfer by generating bundle, finding and signing inputs.
*
* @param seed 81-tryte encoded address of recipient.
* @param security The security level of private key / seed.
* @param transfers Array of transfer objects.
* @param remainder If defined, this address will be used for sending the remainder value (of the inputs) to.
* @param inputs The inputs.
* @param validateInputs whether or not to validate the balances of the provided inputs
* @return Returns bundle trytes.
* @throws InvalidAddressException is thrown when the specified address is not an valid address.
* @throws NotEnoughBalanceException is thrown when a transfer fails because their is not enough balance to perform the transfer.
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
* @throws InvalidTransferException is thrown when an invalid transfer is provided.
*/
public List<String> prepareTransfers(String seed, int security, final List<Transfer> transfers, String remainder, List<Input> inputs, boolean validateInputs) throws NotEnoughBalanceException, InvalidSecurityLevelException, InvalidAddressException, InvalidTransferException {
// Input validation of transfers object
if (!InputValidator.isTransfersCollectionValid(transfers)) {
throw new InvalidTransferException();
}
// validate seed
if ((!InputValidator.isValidSeed(seed))) {
throw new IllegalStateException("Invalid Seed");
}
if (security < 1 || security > 3) {
throw new InvalidSecurityLevelException();
}
// Create a new bundle
final Bundle bundle = new Bundle();
final List<String> signatureFragments = new ArrayList<>();
long totalValue = 0;
String tag = "";
// Iterate over all transfers, get totalValue
// and prepare the signatureFragments, message and tag
for (final Transfer transfer : transfers) {
// If address with checksum then remove checksum
if (Checksum.isAddressWithChecksum(transfer.getAddress()))
transfer.setAddress(Checksum.removeChecksum(transfer.getAddress()));
int signatureMessageLength = 1;
// If message longer than 2187 trytes, increase signatureMessageLength (add 2nd transaction)
if (transfer.getMessage().length() > 2187) {
// Get total length, message / maxLength (2187 trytes)
signatureMessageLength += Math.floor(transfer.getMessage().length() / 2187);
String msgCopy = transfer.getMessage();
// While there is still a message, copy it
while (!msgCopy.isEmpty()) {
String fragment = StringUtils.substring(msgCopy, 0, 2187);
msgCopy = StringUtils.substring(msgCopy, 2187, msgCopy.length());
// Pad remainder of fragment
fragment = StringUtils.rightPad(fragment, 2187, '9');
signatureFragments.add(fragment);
}
} else {
// Else, get single fragment with 2187 of 9's trytes
String fragment = StringUtils.substring(transfer.getMessage(), 0, 2187);
fragment = StringUtils.rightPad(fragment, 2187, '9');
signatureFragments.add(fragment);
}
// get current timestamp in seconds
long timestamp = (long) Math.floor(Calendar.getInstance().getTimeInMillis() / 1000);
// If no tag defined, get 27 tryte tag.
tag = transfer.getTag().isEmpty() ? "999999999999999999999999999" : transfer.getTag();
// Pad for required 27 tryte length
tag = StringUtils.rightPad(tag, 27, '9');
// Add first entry to the bundle
bundle.addEntry(signatureMessageLength, transfer.getAddress(), transfer.getValue(), tag, timestamp);
// Sum up total value
totalValue += transfer.getValue();
}
// Get inputs if we are sending tokens
if (totalValue != 0) {
if (!validateInputs)
return addRemainder(seed, security, inputs, bundle, tag, totalValue, remainder, signatureFragments);
// Case 1: user provided inputs
// Validate the inputs by calling getBalances
if (!validateInputs)
return addRemainder(seed, security, inputs, bundle, tag, totalValue, remainder, signatureFragments);
if (inputs != null && !inputs.isEmpty()) {
// Get list if addresses of the provided inputs
List<String> inputsAddresses = new ArrayList<>();
for (final Input i : inputs) {
inputsAddresses.add(i.getAddress());
}
GetBalancesResponse balancesResponse = getBalances(100, inputsAddresses);
String[] balances = balancesResponse.getBalances();
List<Input> confirmedInputs = new ArrayList<>();
long totalBalance = 0;
int i = 0;
for (String balance : balances) {
long thisBalance = Long.parseLong(balance);
// If input has balance, add it to confirmedInputs
if (thisBalance > 0) {
totalBalance += thisBalance;
Input inputEl = inputs.get(i++);
inputEl.setBalance(thisBalance);
confirmedInputs.add(inputEl);
// if we've already reached the intended input value, break out of loop
if (totalBalance >= totalValue) {
log.info("Total balance already reached ");
break;
}
}
}
// Return not enough balance error
if (totalValue > totalBalance) {
throw new IllegalStateException("Not enough balance");
}
return addRemainder(seed, security, confirmedInputs, bundle, tag, totalValue, remainder, signatureFragments);
}
// Case 2: Get inputs deterministically
//
// If no inputs provided, derive the addresses from the seed and
// confirm that the inputs exceed the threshold
else {
@SuppressWarnings("unchecked") GetBalancesAndFormatResponse newinputs = getInputs(seed, security, 0, 0, totalValue);
// If inputs with enough balance
return addRemainder(seed, security, newinputs.getInput(), bundle, tag, totalValue, remainder, signatureFragments);
}
} else {
// If no input required, don't sign and simply finalize the bundle
bundle.finalize(customCurl.clone());
bundle.addTrytes(signatureFragments);
List<Transaction> trxb = bundle.getTransactions();
List<String> bundleTrytes = new ArrayList<>();
for (Transaction trx : trxb) {
bundleTrytes.add(trx.toTrytes());
}
Collections.reverse(bundleTrytes);
return bundleTrytes;
}
}
/**
* Gets the inputs of a seed
*
* @param seed Tryte-encoded seed. It should be noted that this seed is not transferred.
* @param security The Security level of private key / seed.
* @param start Starting key index.
* @param end Ending key index.
* @param threshold Min balance required.
* @throws InvalidAddressException is thrown when the specified address is not an valid address.
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
**/
public GetBalancesAndFormatResponse getInputs(String seed, int security, int start, int end, long threshold) throws InvalidSecurityLevelException, InvalidAddressException {
StopWatch stopWatch = new StopWatch();
// validate the seed
if ((!InputValidator.isValidSeed(seed))) {
throw new IllegalStateException("Invalid Seed");
}
if (security < 1 || security > 3) {
throw new InvalidSecurityLevelException();
}
// If start value bigger than end, return error
// or if difference between end and start is bigger than 500 keys
if (start > end || end > (start + 500)) {
throw new IllegalStateException("Invalid inputs provided");
}
// Case 1: start and end
//
// If start and end is defined by the user, simply iterate through the keys
// and call getBalances
if (end != 0) {
List<String> allAddresses = new ArrayList<>();
for (int i = start; i < end; i++) {
String address = IotaAPIUtils.newAddress(seed, security, i, false, customCurl.clone());
allAddresses.add(address);
}
return getBalanceAndFormat(allAddresses, threshold, start, end, stopWatch, security);
}
// Case 2: iterate till threshold || end
//
// Either start from index: 0 or start (if defined) until threshold is reached.
// Calls getNewAddress and deterministically generates and returns all addresses
// We then do getBalance, format the output and return it
else {
final GetNewAddressResponse res = getNewAddress(seed, security, start, false, 0, true);
return getBalanceAndFormat(res.getAddresses(), threshold, start, end, stopWatch, security);
}
}
/**
* Gets the balances and formats the output.
*
* @param addresses The addresses.
* @param threshold Min balance required.
* @param start Starting key index.
* @param end Ending key index.
* @param stopWatch the stopwatch.
* @param security The security level of private key / seed.
* @return Inputs object.
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
**/
public GetBalancesAndFormatResponse getBalanceAndFormat(final List<String> addresses, long threshold, int start, int end, StopWatch stopWatch, int security) throws InvalidSecurityLevelException {
if (security < 1 || security > 3) {
throw new InvalidSecurityLevelException();
}
GetBalancesResponse getBalancesResponse = getBalances(100, addresses);
List<String> balances = Arrays.asList(getBalancesResponse.getBalances());
// If threshold defined, keep track of whether reached or not
// else set default to true
boolean thresholdReached = threshold == 0;
int i = -1;
List<Input> inputs = new ArrayList<>();
long totalBalance = 0;
for (String address : addresses) {
long balance = Long.parseLong(balances.get(++i));
if (balance > 0) {
final Input newEntry = new Input(address, balance, start + i, security);
inputs.add(newEntry);
// Increase totalBalance of all aggregated inputs
totalBalance += balance;
if (!thresholdReached && totalBalance >= threshold) {
thresholdReached = true;
break;
}
}
}
if (thresholdReached) {
return GetBalancesAndFormatResponse.create(inputs, totalBalance, stopWatch.getElapsedTimeMili());
}
throw new IllegalStateException("Not enough balance");
}
/**
* Gets the associated bundle transactions of a single transaction.
* Does validation of signatures, total sum as well as bundle order.
*
* @param transaction The transaction encoded in trytes.
* @return an array of bundle, if there are multiple arrays it means that there are conflicting bundles.
* @throws ArgumentException is thrown when an invalid argument is provided.
* @throws InvalidBundleException is thrown if an invalid bundle was found or provided.
* @throws InvalidSignatureException is thrown when an invalid signature is encountered.
*/
public GetBundleResponse getBundle(String transaction) throws ArgumentException, InvalidBundleException, InvalidSignatureException {
StopWatch stopWatch = new StopWatch();
Bundle bundle = traverseBundle(transaction, null, new Bundle());
if (bundle == null) {
throw new ArgumentException("Unknown Bundle");
}
long totalSum = 0;
String bundleHash = bundle.getTransactions().get(0).getBundle();
ICurl curl = SpongeFactory.create(SpongeFactory.Mode.KERL);
curl.reset();
List<Signature> signaturesToValidate = new ArrayList<>();
for (int i = 0; i < bundle.getTransactions().size(); i++) {
Transaction trx = bundle.getTransactions().get(i);
Long bundleValue = trx.getValue();
totalSum += bundleValue;
if (i != bundle.getTransactions().get(i).getCurrentIndex()) {
throw new ArgumentException("Invalid Bundle");
}
String trxTrytes = trx.toTrytes().substring(2187, 2187 + 162);
//System.out.println("Bundlesize "+bundle.getTransactions().size()+" "+trxTrytes);
// Absorb bundle hash + value + timestamp + lastIndex + currentIndex trytes.
curl.absorb(Converter.trits(trxTrytes));
// Check if input transaction
if (bundleValue < 0) {
String address = trx.getAddress();
Signature sig = new Signature();
sig.setAddress(address);
sig.getSignatureFragments().add(trx.getSignatureFragments());
// Find the subsequent txs with the remaining signature fragment
for (int y = i; y < bundle.getTransactions().size() - 1; y++) {
Transaction newBundleTx = bundle.getTransactions().get(i + 1);
// Check if new tx is part of the signature fragment
if (newBundleTx.getAddress().equals(address) && newBundleTx.getValue() == 0) {
if (sig.getSignatureFragments().indexOf(newBundleTx.getSignatureFragments()) == -1)
sig.getSignatureFragments().add(newBundleTx.getSignatureFragments());
}
}
signaturesToValidate.add(sig);
}
}
// Check for total sum, if not equal 0 return error
if (totalSum != 0) throw new InvalidBundleException("Invalid Bundle Sum");
int[] bundleFromTrxs = new int[243];
curl.squeeze(bundleFromTrxs);
String bundleFromTxString = Converter.trytes(bundleFromTrxs);
// Check if bundle hash is the same as returned by tx object
if (!bundleFromTxString.equals(bundleHash)) throw new InvalidBundleException("Invalid Bundle Hash");
// Last tx in the bundle should have currentIndex === lastIndex
bundle.setLength(bundle.getTransactions().size());
if (!(bundle.getTransactions().get(bundle.getLength() - 1).getCurrentIndex() == (bundle.getTransactions().get(bundle.getLength() - 1).getLastIndex())))
throw new InvalidBundleException("Invalid Bundle");
// Validate the signatures
for (Signature aSignaturesToValidate : signaturesToValidate) {
String[] signatureFragments = aSignaturesToValidate.getSignatureFragments().toArray(new String[aSignaturesToValidate.getSignatureFragments().size()]);
String address = aSignaturesToValidate.getAddress();
boolean isValidSignature = new Signing(customCurl.clone()).validateSignatures(address, signatureFragments, bundleHash);
if (!isValidSignature) throw new InvalidSignatureException();
}
return GetBundleResponse.create(bundle.getTransactions(), stopWatch.getElapsedTimeMili());
}
/**
* Similar to getTransfers, just that it returns additional account data
*
* @param seed Tryte-encoded seed. It should be noted that this seed is not transferred.
* @param security The Security level of private key / seed.
* @param index Key index to start search from. If the index is provided, the generation of the address is not deterministic.
* @param checksum Adds 9-tryte address checksum.
* @param total Total number of addresses to generate.
* @param returnAll If <code>true</code>, it returns all addresses which were deterministically generated (until findTransactions returns null).
* @param start Starting key index.
* @param end Ending key index.
* @param inclusionStates If <code>true</code>, it gets the inclusion states of the transfers.
* @param threshold Min balance required.
* @throws InvalidBundleException is thrown if an invalid bundle was found or provided.
* @throws ArgumentException is thrown when an invalid argument is provided.
* @throws InvalidSignatureException is thrown when an invalid signature is encountered.
* @throws InvalidTrytesException is thrown when invalid trytes is provided.
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
* @throws InvalidAddressException is thrown when the specified address is not an valid address.
* @throws NoInclusionStatesException is thrown when it not possible to get a inclusion state.
* @throws NoNodeInfoException is thrown when its not possible to get node info.
*/
public GetAccountDataResponse getAccountData(String seed, int security, int index, boolean checksum, int total, boolean returnAll, int start, int end, boolean inclusionStates, long threshold)
throws InvalidBundleException, ArgumentException, InvalidSignatureException,
InvalidTrytesException, InvalidSecurityLevelException, InvalidAddressException, NoInclusionStatesException, NoNodeInfoException {
StopWatch stopWatch = new StopWatch();
GetNewAddressResponse gna = getNewAddress(seed, security, index, checksum, total, returnAll);
GetTransferResponse gtr = getTransfers(seed, security, start, end, inclusionStates);
GetBalancesAndFormatResponse gbr = getInputs(seed, security, start, end, threshold);
return GetAccountDataResponse.create(gna.getAddresses(), gtr.getTransfers(), gbr.getTotalBalance(), stopWatch.getElapsedTimeMili());
}
/**
* Replays a transfer by doing Proof of Work again.
*
* @param transaction The transaction.
* @param depth The depth.
* @param minWeightMagnitude The minimum weight magnitude.
* @return Analyzed Transaction objects.
* @throws InvalidBundleException is thrown if an invalid bundle was found or provided.
* @throws ArgumentException is thrown when an invalid argument is provided.
* @throws InvalidSignatureException is thrown when an invalid signature is encountered.
* @throws InvalidTransferException is thrown when an invalid transfer is provided.
*/
public ReplayBundleResponse replayBundle(String transaction, int depth, int minWeightMagnitude) throws InvalidBundleException, ArgumentException, InvalidSignatureException, InvalidTrytesException {
StopWatch stopWatch = new StopWatch();
List<String> bundleTrytes = new ArrayList<>();
GetBundleResponse bundleResponse = getBundle(transaction);
Bundle bundle = new Bundle(bundleResponse.getTransactions(), bundleResponse.getTransactions().size());
for (Transaction trx : bundle.getTransactions()) {
bundleTrytes.add(trx.toTrytes());
}
Collections.reverse(bundleTrytes);
List<Transaction> trxs = sendTrytes(bundleTrytes.toArray(new String[bundleTrytes.size()]), depth, minWeightMagnitude);
Boolean[] successful = new Boolean[trxs.size()];
for (int i = 0; i < trxs.size(); i++) {
final FindTransactionResponse response = findTransactionsByBundles(trxs.get(i).getBundle());
successful[i] = response.getHashes().length != 0;
}
return ReplayBundleResponse.create(successful, stopWatch.getElapsedTimeMili());
}
/**
* Wrapper function for getNodeInfo and getInclusionStates
*
* @param hashes The hashes.
* @return Inclusion state.
* @throws NoNodeInfoException is thrown when its not possible to get node info.
*/
public GetInclusionStateResponse getLatestInclusion(String[] hashes) throws NoNodeInfoException {
GetNodeInfoResponse getNodeInfoResponse = getNodeInfo();
if (getNodeInfoResponse == null) throw new NoNodeInfoException();
String[] latestMilestone = {getNodeInfoResponse.getLatestSolidSubtangleMilestone()};
return getInclusionStates(hashes, latestMilestone);
}
/**
* Wrapper function that basically does prepareTransfers, as well as attachToTangle and finally, it broadcasts and stores the transactions locally.
*
* @param seed Tryte-encoded seed
* @param security The security level of private key / seed.
* @param depth The depth.
* @param minWeightMagnitude The minimum weight magnitude.
* @param transfers Array of transfer objects.
* @param inputs List of inputs used for funding the transfer.
* @param remainderAddress If defined, this remainderAddress will be used for sending the remainder value (of the inputs) to.
* @return Array of Transaction objects.
* @throws InvalidAddressException is thrown when the specified remainderAddress is not an valid remainderAddress.
* @throws NotEnoughBalanceException is thrown when a transfer fails because their is not enough balance to perform the transfer.
* @throws InvalidSecurityLevelException is thrown when the specified security level is not valid.
* @throws InvalidTrytesException is thrown when invalid trytes is provided.
* @throws InvalidAddressException is thrown when the specified remainderAddress is not an valid remainderAddress.
* @throws InvalidTransferException is thrown when an invalid transfer is provided.
*/
public SendTransferResponse sendTransfer(String seed, int security, int depth, int minWeightMagnitude, final List<Transfer> transfers, Input[] inputs, String remainderAddress) throws NotEnoughBalanceException, InvalidSecurityLevelException, InvalidTrytesException, InvalidAddressException, InvalidTransferException {
if (security < 1 || security > 3) {
throw new InvalidSecurityLevelException();
}
StopWatch stopWatch = new StopWatch();
List<String> trytes = prepareTransfers(seed, security, transfers, remainderAddress, inputs == null ? null : Arrays.asList(inputs));
List<Transaction> trxs = sendTrytes(trytes.toArray(new String[trytes.size()]), depth, minWeightMagnitude);
Boolean[] successful = new Boolean[trxs.size()];
for (int i = 0; i < trxs.size(); i++) {
final FindTransactionResponse response = findTransactionsByBundles(trxs.get(i).getBundle());
successful[i] = response.getHashes().length != 0;
}
return SendTransferResponse.create(successful, stopWatch.getElapsedTimeMili());
}
/**
* Basically traverse the Bundle by going down the trunkTransactions until
* the bundle hash of the transaction is no longer the same. In case the input
* transaction hash is not a tail, we return an error.
*
* @param trunkTx Hash of a trunk or a tail transaction of a bundle.
* @param bundleHash The bundle hashes.
* @param bundle List of bundles to be populated.
* @return Transaction objects.
* @throws ArgumentException is thrown when an invalid argument is provided.
*/
public Bundle traverseBundle(String trunkTx, String bundleHash, Bundle bundle) throws ArgumentException {
GetTrytesResponse gtr = getTrytes(trunkTx);
if (gtr != null) {
if (gtr.getTrytes().length == 0) {
throw new ArgumentException("Bundle transactions not visible");
}
Transaction trx = new Transaction(gtr.getTrytes()[0], customCurl.clone());
if (trx.getBundle() == null) {
throw new ArgumentException("Invalid trytes, could not create object");
}
// If first transaction to search is not a tail, return error
if (bundleHash == null && trx.getCurrentIndex() != 0) {
throw new ArgumentException("Invalid tail transaction supplied.");
}
// If no bundle hash, define it
if (bundleHash == null) {
bundleHash = trx.getBundle();
}
// If different bundle hash, return with bundle
if (!bundleHash.equals(trx.getBundle())) {
return bundle;
}
// If only one bundle element, return
if (trx.getLastIndex() == 0 && trx.getCurrentIndex() == 0) {
return new Bundle(Collections.singletonList(trx), 1);
}
// Define new trunkTransaction for search
trunkTx = trx.getTrunkTransaction();
// Add transaction object to bundle
bundle.getTransactions().add(trx);
// Continue traversing with new trunkTx
return traverseBundle(trunkTx, bundleHash, bundle);
} else {
throw new ArgumentException("Get Trytes Response was null");
}
}
/**
* Prepares transfer by generating the bundle with the corresponding cosigner transactions.
* Does not contain signatures.
*
* @param securitySum The sum of security levels used by all co-signers.
* @param inputAddress Array of input addresses as well as the securitySum.
* @param remainderAddress Has to be generated by the cosigners before initiating the transfer, can be null if fully spent.
* @return Bundle of transaction objects.
* @throws InvalidBundleException is thrown if an invalid bundle was found or provided.
* @throws InvalidAddressException is thrown when the specified address is not an valid address.
*/
public List<Transaction> initiateTransfer(int securitySum, final String inputAddress, String remainderAddress,
final List<Transfer> transfers, boolean testMode) throws InvalidAddressException, InvalidBundleException, InvalidTransferException {
// If message or tag is not supplied, provide it
// Also remove the checksum of the address if it's there
for (Transfer transfer : transfers) {
transfer.setMessage(transfer.getMessage().isEmpty() ? StringUtils.rightPad(transfer.getMessage(), 2187, '9') : transfer.getMessage());
transfer.setTag(transfer.getTag().isEmpty() ? StringUtils.rightPad(transfer.getTag(), 27, '9') : transfer.getTag());
if (Checksum.isValidChecksum(transfer.getAddress())) {
transfer.setAddress(Checksum.removeChecksum(transfer.getAddress()));
}
}
// Input validation of transfers object
if (!InputValidator.isTransfersCollectionValid(transfers)) {
throw new InvalidTransferException();
}
// validate input address
if (!InputValidator.isAddress(inputAddress))
throw new InvalidAddressException();
// validate remainder address
if (remainderAddress != null && !InputValidator.isAddress(remainderAddress)) {
throw new InvalidBundleException();
}
// Create a new bundle
final Bundle bundle = new Bundle();
int totalValue = 0;
final List<String> signatureFragments = new ArrayList<>();
String tag = "";
//
// Iterate over all transfers, get totalValue
// and prepare the signatureFragments, message and tag
for (final Transfer transfer : transfers) {
int signatureMessageLength = 1;
// If message longer than 2187 trytes, increase signatureMessageLength (add 2nd transaction)
if (transfer.getMessage().length() > 2187) {
// Get total length, message / maxLength (2187 trytes)
signatureMessageLength += Math.floor(transfer.getMessage().length() / 2187);
String msgCopy = transfer.getMessage();
// While there is still a message, copy it
while (!msgCopy.isEmpty()) {
String fragment = StringUtils.substring(msgCopy, 0, 2187);
msgCopy = StringUtils.substring(msgCopy, 2187, msgCopy.length());
// Pad remainder of fragment
fragment = StringUtils.rightPad(fragment, 2187, '9');
signatureFragments.add(fragment);
}
} else {
// Else, get single fragment with 2187 of 9's trytes
String fragment = StringUtils.substring(transfer.getMessage(), 0, 2187);
fragment = StringUtils.rightPad(fragment, 2187, '9');
signatureFragments.add(fragment);
}
// get current timestamp in seconds
long timestamp = (long) Math.floor(Calendar.getInstance().getTimeInMillis() / 1000);
// If no tag defined, get 27 tryte tag.
tag = transfer.getTag().isEmpty() ? StringUtils.rightPad(transfer.getTag(), 27, '9') : transfer.getTag();
// Pad for required 27 tryte length
tag = StringUtils.rightPad(tag, 27, '9');
// Add first entry to the bundle
bundle.addEntry(signatureMessageLength, transfer.getAddress(), transfer.getValue(), tag, timestamp);
// Sum up total value
totalValue += transfer.getValue();
}
// Get inputs if we are sending tokens
if (totalValue != 0) {