Repository navigation
Expand file tree
/
Copy pathBundle.java
More file actions
213 lines (166 loc) · 6.85 KB
/
Copy pathBundle.java
File metadata and controls
213 lines (166 loc) · 6.85 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
package jota.model;
import jota.pow.ICurl;
import jota.pow.SpongeFactory;
import jota.utils.Converter;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* This class represents a Bundle, a set of transactions.
*
* @author pinpong
**/
public class Bundle implements Comparable<Bundle> {
public static String EMPTY_HASH = "999999999999999999999999999999999999999999999999999999999999999999999999999999999";
private List<Transaction> transactions;
private int length;
/**
* Initializes a new instance of the Bundle class without transactions.
*/
public Bundle() {
this(new ArrayList<Transaction>(), 0);
}
/**
* Initializes a new instance of the Bundle class.
*/
public Bundle(List<Transaction> transactions, int length) {
this.transactions = transactions;
this.length = length;
}
/**
* Gets the transactions
*
* @return transactions The transactions.
*/
public List<Transaction> getTransactions() {
return transactions;
}
/**
* Gets the length of the bundle
*
* @return length The length.
*/
public int getLength() {
return length;
}
/**
* Sets the length of the bundle
*
* @param length The length.
*/
public void setLength(int length) {
this.length = length;
}
/**
* Adds a bundle entry.
*
* @param signatureMessageLength Length of the signature message.
* @param address The address.
* @param value The value.
* @param tag The tag.
* @param timestamp The timestamp.
*/
public void addEntry(int signatureMessageLength, String address, long value, String tag, long timestamp) {
if (getTransactions() == null) {
this.transactions = new ArrayList<>(getTransactions());
}
for (int i = 0; i < signatureMessageLength; i++) {
Transaction trx = new Transaction(address, i == 0 ? value : 0, tag, timestamp);
transactions.add(trx);
}
}
/**
* Finalizes the bundle using the specified curl implementation,
*
* @param customCurl The custom curl.
*/
public void finalize(ICurl customCurl) {
ICurl curl = customCurl == null ? SpongeFactory.create(SpongeFactory.Mode.KERL) : customCurl;
curl.reset();
for (int i = 0; i < this.getTransactions().size(); i++) {
int[] valueTrits = Converter.trits(this.getTransactions().get(i).getValue(), 81);
int[] timestampTrits = Converter.trits(this.getTransactions().get(i).getTimestamp(), 27);
this.getTransactions().get(i).setCurrentIndex(i);
int[] currentIndexTrits = Converter.trits(this.getTransactions().get(i).getCurrentIndex(), 27);
this.getTransactions().get(i).setLastIndex(this.getTransactions().size() - 1);
int[] lastIndexTrits = Converter.trits(this.getTransactions().get(i).getLastIndex(), 27);
int[] t = Converter.trits(this.getTransactions().get(i).getAddress() + Converter.trytes(valueTrits) + this.getTransactions().get(i).getObsoleteTag() + Converter.trytes(timestampTrits) + Converter.trytes(currentIndexTrits) + Converter.trytes(lastIndexTrits));
curl.absorb(t, 0, t.length);
}
int[] hash = new int[243];
curl.squeeze(hash, 0, hash.length);
String hashInTrytes = Converter.trytes(hash);
for (int i = 0; i < this.getTransactions().size(); i++) {
this.getTransactions().get(i).setBundle(hashInTrytes);
}
}
/**
* Adds the trytes.
*
* @param signatureFragments The signature fragments.
*/
public void addTrytes(List<String> signatureFragments) {
String emptySignatureFragment = "";
String emptyHash = EMPTY_HASH;
long emptyTimestamp = 999999999l;
emptySignatureFragment = StringUtils.rightPad(emptySignatureFragment, 2187, '9');
for (int i = 0; i < this.getTransactions().size(); i++) {
// Fill empty signatureMessageFragment
this.getTransactions().get(i).setSignatureFragments((signatureFragments.size() <= i || signatureFragments.get(i).isEmpty()) ? emptySignatureFragment : signatureFragments.get(i));
// Fill empty trunkTransaction
this.getTransactions().get(i).setTrunkTransaction(emptyHash);
// Fill empty branchTransaction
this.getTransactions().get(i).setBranchTransaction(emptyHash);
this.getTransactions().get(i).setAttachmentTimestamp(emptyTimestamp);
this.getTransactions().get(i).setAttachmentTimestampLowerBound(emptyTimestamp);
this.getTransactions().get(i).setAttachmentTimestampUpperBound(emptyTimestamp);
// Fill empty nonce
this.getTransactions().get(i).setNonce(StringUtils.rightPad("", 27, "9"));
}
}
/**
* Normalized the bundle.
*
* @param bundleHash The bundle hash.
* @return normalizedBundle A normalized bundle hash.
*/
public int[] normalizedBundle(String bundleHash) {
int[] normalizedBundle = new int[81];
for (int i = 0; i < 3; i++) {
long sum = 0;
for (int j = 0; j < 27; j++) {
sum += (normalizedBundle[i * 27 + j] = Converter.value(Converter.tritsString("" + bundleHash.charAt(i * 27 + j))));
}
if (sum >= 0) {
while (sum-- > 0) {
for (int j = 0; j < 27; j++) {
if (normalizedBundle[i * 27 + j] > -13) {
normalizedBundle[i * 27 + j]--;
break;
}
}
}
} else {
while (sum++ < 0) {
for (int j = 0; j < 27; j++) {
if (normalizedBundle[i * 27 + j] < 13) {
normalizedBundle[i * 27 + j]++;
break;
}
}
}
}
}
return normalizedBundle;
}
/**
* Compares the current object with another object of the same type.
*
* @param o An object to compare with this object.
* @return A value that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the <paramref name="other" /> parameter.Zero This object is equal to <paramref name="other" />. Greater than zero This object is greater than <paramref name="other" />.
*/
@Override
public int compareTo(Bundle o) {
return Long.compare(this.getTransactions().get(0).getAttachmentTimestamp(), o.getTransactions().get(0).getAttachmentTimestamp());
}
}