2
2
3
3
import jota .pow .ICurl ;
4
4
import jota .pow .SpongeFactory ;
5
+ import jota .utils .Constants ;
5
6
import jota .utils .Converter ;
7
+ import jota .utils .InputValidator ;
8
+
6
9
import org .apache .commons .lang3 .StringUtils ;
7
10
import org .apache .commons .lang3 .builder .ToStringBuilder ;
8
11
import org .apache .commons .lang3 .builder .ToStringStyle ;
14
17
/**
15
18
* This class represents an iota transaction.
16
19
*
17
- * @author pinpong
18
20
*/
19
21
public class Transaction {
20
22
@@ -39,6 +41,28 @@ public class Transaction {
39
41
private String tag ;
40
42
private long attachmentTimestampLowerBound ;
41
43
private long attachmentTimestampUpperBound ;
44
+
45
+ /**
46
+ * Converts an array of transaction trytes into an array of transaction objects.
47
+ * @param trytes the array of transactions trytes
48
+ * @return the transaction objects
49
+ */
50
+ public static Transaction [] asTransactionObjects (String ... trytes ) {
51
+ Transaction [] transactions = new Transaction [trytes .length ];
52
+ for (int i = 0 ; i < trytes .length ; i ++) {
53
+ transactions [i ] = asTransactionObject (trytes [i ]);
54
+ }
55
+ return transactions ;
56
+ }
57
+
58
+ /**
59
+ * Converts transaction trytes into a transaction object.
60
+ * @param trytes the transaction trytes
61
+ * @return the transaction object
62
+ */
63
+ public static Transaction asTransactionObject (String trytes ) {
64
+ return new Transaction (trytes );
65
+ }
42
66
43
67
/**
44
68
* Initializes a new instance of the Signature class.
@@ -73,15 +97,7 @@ public Transaction(String address, long value, String tag, long timestamp) {
73
97
this .obsoleteTag = tag ;
74
98
this .timestamp = timestamp ;
75
99
}
76
-
77
- public long getAttachmentTimestampLowerBound () {
78
- return attachmentTimestampLowerBound ;
79
- }
80
-
81
- public void setAttachmentTimestampLowerBound (long attachmentTimestampLowerBound ) {
82
- this .attachmentTimestampLowerBound = attachmentTimestampLowerBound ;
83
- }
84
-
100
+
85
101
/**
86
102
* Initializes a new instance of the Signature class.
87
103
*/
@@ -110,6 +126,14 @@ public Transaction(String trytes, ICurl customCurl) {
110
126
transactionObject (trytes );
111
127
this .customCurl = customCurl ;
112
128
}
129
+
130
+ public long getAttachmentTimestampLowerBound () {
131
+ return attachmentTimestampLowerBound ;
132
+ }
133
+
134
+ public void setAttachmentTimestampLowerBound (long attachmentTimestampLowerBound ) {
135
+ this .attachmentTimestampLowerBound = attachmentTimestampLowerBound ;
136
+ }
113
137
114
138
public long getAttachmentTimestampUpperBound () {
115
139
return attachmentTimestampUpperBound ;
@@ -452,15 +476,13 @@ public void transactionObject(final String trytes) {
452
476
}
453
477
454
478
// validity check
455
- for (int i = 2279 ; i < 2295 ; i ++) {
456
- if (trytes .charAt (i ) != '9' ) {
457
- log .warn ("Trytes {} does not seem a valid tryte" , trytes );
458
- return ;
459
- }
479
+ if (!InputValidator .isNinesTrytes (trytes .substring (2279 , 2295 ), 16 )) {
480
+ log .warn ("Trytes {} does not seem a valid tryte" , trytes );
481
+ return ;
460
482
}
461
483
462
484
int [] transactionTrits = Converter .trits (trytes );
463
- int [] hash = new int [243 ];
485
+ int [] hash = new int [Constants . HASH_LENGTH_TRITS ];
464
486
465
487
ICurl curl = SpongeFactory .create (SpongeFactory .Mode .CURLP81 );
466
488
// generate the correct transaction hash
@@ -469,20 +491,28 @@ public void transactionObject(final String trytes) {
469
491
curl .squeeze (hash , 0 , hash .length );
470
492
471
493
this .setHash (Converter .trytes (hash ));
472
- this .setSignatureFragments (trytes .substring (0 , 2187 ));
473
- this .setAddress (trytes .substring (2187 , 2268 ));
494
+ this .setSignatureFragments (trytes .substring (0 , Constants . MESSAGE_LENGTH ));
495
+ this .setAddress (trytes .substring (Constants . MESSAGE_LENGTH , Constants . MESSAGE_LENGTH + Constants . ADDRESS_LENGTH_WITHOUT_CHECKSUM ));
474
496
this .setValue (Converter .longValue (Arrays .copyOfRange (transactionTrits , 6804 , 6837 )));
475
- this .setObsoleteTag (trytes .substring (2295 , 2322 ));
497
+ this .setObsoleteTag (trytes .substring (2295 , 2295 + Constants . TAG_LENGTH ));
476
498
this .setTimestamp (Converter .longValue (Arrays .copyOfRange (transactionTrits , 6966 , 6993 )));
477
499
this .setCurrentIndex (Converter .longValue (Arrays .copyOfRange (transactionTrits , 6993 , 7020 )));
478
500
this .setLastIndex (Converter .longValue (Arrays .copyOfRange (transactionTrits , 7020 , 7047 )));
479
- this .setBundle (trytes .substring (2349 , 2430 ));
480
- this .setTrunkTransaction (trytes .substring (2430 , 2511 ));
481
- this .setBranchTransaction (trytes .substring (2511 , 2592 ));
482
- this .setTag (trytes .substring (2592 , 2619 ));
501
+ this .setBundle (trytes .substring (2349 , 2349 + Constants . ADDRESS_LENGTH_WITHOUT_CHECKSUM ));
502
+ this .setTrunkTransaction (trytes .substring (2430 , 2430 + Constants . ADDRESS_LENGTH_WITHOUT_CHECKSUM ));
503
+ this .setBranchTransaction (trytes .substring (2511 , 2511 + Constants . ADDRESS_LENGTH_WITHOUT_CHECKSUM ));
504
+ this .setTag (trytes .substring (2592 , 2592 + Constants . TAG_LENGTH ));
483
505
this .setAttachmentTimestamp (Converter .longValue (Arrays .copyOfRange (transactionTrits , 7857 , 7884 )));
484
506
this .setAttachmentTimestampLowerBound (Converter .longValue (Arrays .copyOfRange (transactionTrits , 7884 , 7911 )));
485
507
this .setAttachmentTimestampUpperBound (Converter .longValue (Arrays .copyOfRange (transactionTrits , 7911 , 7938 )));
486
508
this .setNonce (trytes .substring (2646 , 2673 ));
487
509
}
510
+
511
+ /**
512
+ * Checks if the current index is 0
513
+ * @return if this is a tail transaction
514
+ */
515
+ public boolean isTailTransaction () {
516
+ return getCurrentIndex () == 0 ;
517
+ }
488
518
}
0 commit comments